]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ria/UserEditorApplet.js
Prepare next development cycle
[lgpl/argeo-commons.git] / ria / UserEditorApplet.js
1 /**
2 * A simple Hello World applet for documentation purpose.
3 * The only associated command is the "Close" command.
4 */
5 /* *************************************************
6 #asset(resource/org.argeo.security.ria/*)
7 ****************************************************/
8 qx.Class.define("org.argeo.security.ria.UserEditorApplet",
9 {
10 extend : qx.ui.container.Composite,
11 implement : [org.argeo.ria.components.IView],
12
13 construct : function(){
14 this.base(arguments);
15 this.setLayout(new qx.ui.layout.VBox(5));
16 },
17
18 events : {
19 "savedUser" : "qx.event.type.Data"
20 },
21
22 properties :
23 {
24 /**
25 * The viewPane inside which this applet is added.
26 */
27 view : {
28 init : null
29 },
30 /**
31 * Commands definition, see {@link org.argeo.ria.event.CommandsManager#definitions}
32 */
33 commands : {
34 init : {
35 "save_user" : {
36 label : "Save",
37 icon : "org.argeo.security.ria/document-save.png",
38 shortcut : "Control+s",
39 enabled : true,
40 menu : "Users",
41 toolbar : "user",
42 callback : function(e){
43 // CALL SERVICE AND GET UP TO DATE USER
44 this.saveUser();
45 },
46 selectionChange : function(viewName, data){
47 if(viewName != "editor") return;
48 var iApplet = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("editor").getContent();
49 this.setEnabled(iApplet.getModified());
50 },
51 command : null
52 },
53 "add_nature" : {
54 label : "Add Nature ...",
55 icon : "org.argeo.security.ria/list-add.png",
56 shortcut : null,
57 enabled : true,
58 menu : "Natures",
59 toolbar : null,
60 callback : function(e){
61 },
62 submenuCallback : function(commandClass){
63 this._addNatureTab(commandClass, null, true);
64 },
65 submenu : [
66 {"label" : "Totot", "icon":"","commandId" : "toto"},
67 {"label" : "Totot", "icon":"","commandId" : "toto"}
68 ],
69 selectionChange : function(viewName, data){
70 if(viewName != "editor") return;
71 var iApplet = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("editor").getContent();
72 if(iApplet && iApplet.getCurrentNatureTabs() && iApplet.getAvailableNatures()
73 && iApplet.getCurrentNatureTabs().length < qx.lang.Object.getLength(iApplet.getAvailableNatures())){
74 this.setEnabled(true);
75 }else{
76 this.setEnabled(false);
77 }
78 },
79 command : null
80 },
81 "remove_nature" : {
82 label : "Remove Nature",
83 icon : "org.argeo.security.ria/list-remove.png",
84 shortcut : null,
85 enabled : true,
86 menu : "Natures",
87 toolbar : null,
88 callback : function(e){
89 this.removeSelectedTab();
90 },
91 selectionChange : function(viewName, data){
92 if(viewName != "editor") return;
93 var iApplet = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("editor").getContent();
94 if(iApplet && iApplet.getSelectedNatureTab() && iApplet.getSelectedNatureTab().getUserData("NATURE_CLASS")){
95 this.setEnabled(true);
96 }else{
97 this.setEnabled(false);
98 }
99 },
100 command : null
101 },
102 "close" : {
103 label : "Close",
104 icon : "org.argeo.security.ria/window-close.png",
105 shortcut : "Control+w",
106 enabled : true,
107 menu : "Users",
108 toolbar : "user",
109 callback : function(e){
110 // Call service to delete
111 var iApplet = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("editor").getContent();
112 if(!iApplet.getModified() && !iApplet.getNaturesModified()){
113 this.getView().closeCurrent();
114 return;
115 }
116 var modal = new org.argeo.ria.components.Modal("Warning");
117 modal.addConfirm("There are unsaved changes!\n Are you sure you want to close?");
118 modal.addListener("ok", function(){
119 this.getView().closeCurrent();
120 }, this);
121 modal.attachAndShow();
122 },
123 command : null
124 }
125 }
126 },
127 viewSelection : {
128 nullable:false,
129 check:"org.argeo.ria.components.ViewSelection"
130 },
131 modified : {
132 init : false,
133 apply : "_applyDetailsModified"
134 },
135 naturesModified : {
136 init : false,
137 apply : "_applyNaturesModified"
138 },
139 rolesList : {
140
141 },
142 instanceId : {init:""},
143 instanceLabel : {init:"Editor"},
144 loaded : {
145 init : false
146 },
147 currentNatureTabs : {
148 },
149 availableNatures : {
150 },
151 selectedNatureTab : {
152 nullable : true
153 },
154 currentUser : {
155
156 }
157 },
158
159 members :
160 {
161 /**
162 * Called at applet creation. Just registers viewPane.
163 * @param viewPane {org.argeo.ria.components.ViewPane} The viewPane.
164 */
165 init : function(viewPane, data){
166 if(!data.USER){
167 var now = new Date();
168 this.setInstanceId(now.getTime());
169 this.setInstanceLabel("New User");
170 }else{
171 this.setInstanceId(data.USER);
172 this.setInstanceLabel("User " + data.USER);
173 }
174 this.setView(viewPane);
175 this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
176
177 this.naturesManager = new org.argeo.security.ria.components.NaturesManager();
178 var detectedNatures = this.naturesManager.getDetectedNatures();
179 this.setAvailableNatures(detectedNatures);
180
181 // TOOLBAR
182 this.buttonGB = new qx.ui.container.Composite(new qx.ui.layout.HBox(5, "right"));
183 this.add(this.buttonGB);
184
185 this.setCurrentNatureTabs([]);
186 this.naturesTab = new qx.ui.tabview.TabView("top");
187 this.naturesTab.addListener("changeSelected", function(e){
188 this.setSelectedNatureTab(e.getData());
189 this.getViewSelection().triggerEvent();
190 }, this);
191
192 this.basicPage = new qx.ui.tabview.Page("Basic Information");
193 this.basicPage.setLayout(new qx.ui.layout.VBox(5));
194
195 // GROUPBOXES
196
197 this.basicGB = new qx.ui.groupbox.GroupBox("Base Informations");
198 var grid = new qx.ui.layout.Grid(5,5);
199 this.basicGB.setLayout(grid);
200 grid.setColumnFlex(0,1);
201 grid.setColumnAlign(0,"right", "middle");
202 grid.setColumnFlex(1,2);
203 this._initializeGroupBox(this.basicGB);
204
205 this.passGB = new qx.ui.groupbox.GroupBox("Set/Modify Password");
206 this.passGB.setLayout(new qx.ui.layout.VBox());
207 this._initializeGroupBox(this.passGB);
208
209 this.add(this.basicGB);
210 this.add(this.passGB);
211
212 // FIELDS
213 this.usernameField = new qx.ui.form.TextField();
214 this.basicGB.add(new qx.ui.basic.Label("Username"), {row:0,column:0});
215 this.basicGB.add(this.usernameField, {row:0,column:1});
216
217 this.rolesField = new org.argeo.ria.components.ui.MultipleComboBox();
218 this.rolesField.setChoiceValues(data.ROLES_LIST);
219 this.basicGB.add(new qx.ui.basic.Label("Roles"), {row:1,column:0});
220 this.basicGB.add(this.rolesField, {row:1,column:1});
221
222 this.passPane = new org.argeo.security.ria.components.PasswordCredentialImpl();
223 this.passGB.add(this.passPane.getContainer());
224
225 //this.naturesTab.add(this.basicPage);
226 this.natureButtonGB = new qx.ui.container.Composite(new qx.ui.layout.HBox(5, "right"));
227 this.natureButtonGB.setMarginTop(15);
228 this.add(this.natureButtonGB);
229
230
231 this.add(this.naturesTab, {flex:1});
232
233 this.naturesTab.setVisibility("excluded");
234 this.fakePane = new qx.ui.container.Composite(new qx.ui.layout.Canvas());
235 this.fakePane.setVisibility("visible");
236 this.fakePane.setDecorator("tabview-pane");
237 this.fakePane.setMarginTop(30);
238 this.add(this.fakePane, {flex:1});
239
240 title = new qx.ui.basic.Atom("User Details", "org.argeo.security.ria/preferences-users.png");
241 title.setFont(qx.bom.Font.fromString("16px sans-serif bold"));
242 this.buttonGB.add(title);
243 this.buttonGB.add(new qx.ui.core.Spacer(), {flex:1});
244
245 var title2 = new qx.ui.basic.Atom("User Natures", "org.argeo.security.ria/identity.png");
246 title2.setFont(qx.bom.Font.fromString("16px sans-serif bold"));
247 this.natureButtonGB.add(title2);
248 this.natureButtonGB.add(new qx.ui.core.Spacer(), {flex:1});
249
250
251 },
252
253 saveUser : function(){
254 var user = this.getCurrentUser();
255 user.setName(this.usernameField.getValue());
256 user.setRoles((this.rolesField.getValue()||"").split(","));
257 // GO TO AND RETURN FROM SERVER
258 if(user.isCreate()){
259 var userExists = false;
260 var req = org.argeo.security.ria.SecurityAPI.getUserExistsService(user.getName());
261 req.addListener("completed", function(response){
262 userExists = response.getContent().value;
263 }, this);
264 req.setAsynchronous(false);
265 req.send();
266 if(userExists){
267 this.error("User already exists, choose another name!");
268 return;
269 }
270 }
271 var userService = user.getSaveService();
272 userService.send();
273 userService.addListener("completed", function(response){
274 if(response.getContent().status && response.getContent().status == "ERROR"){
275 return;
276 }
277 this._setGuiInCreateMode(false);
278 user.load(response.getContent(), "json");
279 this.partialRefreshUser(user, ["details","natures"]);
280 this.setModified(false);
281 this.getViewSelection().triggerEvent();
282 this.fireDataEvent("savedUser", user);
283 }, this);
284 },
285
286 _addNatureTab : function(natureClass, natureData, select){
287 var crtTabs = this.getCurrentNatureTabs();
288 if(qx.lang.Array.contains(crtTabs, natureClass.NATURE_TYPE)){
289 this.error("There can only be one instance of a given nature type!");
290 return null;
291 }
292 if(!this.naturesTab.isVisible()){
293 if(this.fakePane) this.fakePane.setVisibility("excluded");
294 this.naturesTab.setVisibility("visible");
295 }
296 var page = new qx.ui.tabview.Page("Nature : " + natureClass.NATURE_LABEL);
297 page.setLayout(new qx.ui.layout.Dock());
298 page.setUserData("NATURE_CLASS", natureClass);
299 var newClass = new natureClass();
300 page.add(newClass.getContainer(), {edge:"center"});
301
302 buttons = new qx.ui.container.Composite(new qx.ui.layout.HBox(5, "center"));
303 var editB = new qx.ui.form.Button("Edit this Nature", "org.argeo.security.ria/document-properties-22.png");
304 var saveB = new qx.ui.form.Button("Save", "org.argeo.security.ria/dialog-apply.png");
305 var cancelB = new qx.ui.form.Button("Cancel", "org.argeo.security.ria/dialog-cancel.png");
306 buttons.add(editB);
307 buttons.add(saveB);
308 buttons.add(cancelB);
309 page.add(buttons, {edge:"south"});
310 editB.addListener("execute", function(){
311 newClass.setEditMode(true);
312 editB.setVisibility("excluded");
313 saveB.setVisibility("visible");
314 cancelB.setVisibility("visible");
315 });
316 cancelB.addListener("execute", function(){
317 if(newClass.getIsNew()){
318 this._removeNatureTab(natureClass);
319 }
320 newClass.setEditMode(false);
321 editB.setVisibility("visible");
322 saveB.setVisibility("excluded");
323 cancelB.setVisibility("excluded");
324 }, this);
325 saveB.addListener("execute", function(){
326 // SAVE CURRENT NATURE
327 var data = newClass.getData();
328 if(newClass.getIsNew()){
329 this.getCurrentUser().addNature(data);
330 }else{
331 this.getCurrentUser().updateNature(data);
332 }
333 this.saveUser();
334 this.setNaturesModified(false);
335 newClass.setEditMode(false);
336 editB.setVisibility("visible");
337 saveB.setVisibility("excluded");
338 cancelB.setVisibility("excluded");
339 }, this);
340 if(natureData){
341 newClass.setData(natureData);
342 cancelB.execute();
343 }else{
344 newClass.setIsNew(true);
345 editB.execute();
346 }
347 this.naturesTab.add(page);
348 crtTabs.push(natureClass.NATURE_TYPE);
349 this.getViewSelection().triggerEvent();
350 newClass.addListener("modified", function(){
351 this.setNaturesModified(true);
352 }, this);
353 if(select){
354 this.naturesTab.setSelected(page);
355 }
356 return page;
357 },
358
359 _removeNatureTab : function(natureClass){
360 this.naturesTab.getChildren().forEach(function(el){
361 if(el.getUserData("NATURE_CLASS") == natureClass){
362 this.naturesTab.remove(el);
363 qx.lang.Array.remove(this.getCurrentNatureTabs(), natureClass.NATURE_TYPE);
364 this.getViewSelection().triggerEvent();
365 }
366 }, this);
367 if(this.naturesTab.getChildren().length == 0){
368 this.naturesTab.setVisibility("excluded");
369 this.fakePane.setVisibility("visible");
370 }
371 },
372
373 removeSelectedTab : function(){
374 var selected = this.naturesTab.getSelected();
375 var tabClass = selected.getUserData("NATURE_CLASS");
376 var user = this.getCurrentUser();
377 user.removeNature(tabClass.NATURE_TYPE);
378 this.saveUser();
379 this._removeNatureTab(tabClass);
380 },
381
382 removeAllTabs : function(){
383 while(this.naturesTab.getSelected()){
384 this._removeNatureTab(this.naturesTab.getSelected().getUserData("NATURE_CLASS"));
385 }
386 },
387
388 _setGuiInCreateMode : function(bool){
389 if(bool){
390 if(!this.natureButtonGB.isVisible()) return;
391 this.natureButtonGB.hide();
392 this.fakePane.setVisibility("excluded");
393 }else{
394 if(this.natureButtonGB.isVisible()) return;
395 this.natureButtonGB.show();
396 this.fakePane.setVisibility("visible");
397 }
398 },
399
400 _attachListeners : function(){
401 this.usernameField.addListener("changeValue", function(){
402 this.setModified(true);
403 }, this);
404 this.rolesField.addListener("changeValue", function(){
405 this.setModified(true);
406 }, this);
407 this.passPane.addListener("modified", function(){
408 this.setModified(true);
409 }, this);
410 },
411
412 _initializeGroupBox: function(groupBox){
413 groupBox.setPadding(0);
414 groupBox.getChildrenContainer().setPadding(8);
415 },
416
417 _applyDetailsModified : function(value){
418 if(value) this.getViewSelection().triggerEvent();
419 },
420
421 _applyNaturesModified : function(value){
422 if(value) this.getViewSelection().triggerEvent();
423 },
424
425 loadUserData : function(userName){
426 var userDataService = org.argeo.security.ria.SecurityAPI.getUserDetailsService(userName);
427 userDataService.addListener("completed", function(response){
428 var user = new org.argeo.security.ria.model.User();
429 user.load(response.getContent(), "json");
430 this.setCurrentUser(user);
431 this.usernameField.setValue(user.getName());
432 this.usernameField.setReadOnly(true);
433 this.rolesField.setValue(user.getRoles());
434 var userNatureTabs = this.naturesManager.detectNaturesInData(user.getNatures());
435 if(userNatureTabs.length){
436 userNatureTabs.forEach(function(el){
437 this._addNatureTab(el.NATURE_CLASS, el.NATURE_DATA);
438 }, this);
439 }
440 this._attachListeners();
441 }, this);
442 userDataService.send();
443 },
444
445 partialRefreshUser : function(user, target){
446 if(!qx.lang.Array.isArray(target)) target = [target];
447
448 if(qx.lang.Array.contains(target,"natures")){
449 if(this.getSelectedNatureTab()){
450 var selectedTab = this.getSelectedNatureTab().getUserData("NATURE_CLASS");
451 }
452 this.removeAllTabs();
453 var userNatureTabs = this.naturesManager.detectNaturesInData(user.getNatures());
454 if(userNatureTabs.length){
455 userNatureTabs.forEach(function(el){
456 this._addNatureTab(el.NATURE_CLASS, el.NATURE_DATA, (selectedTab && selectedTab == el.NATURE_CLASS));
457 }, this);
458 }
459 }
460 if(qx.lang.Array.contains(target,"details")){
461 this.setInstanceLabel("User "+user.getName());
462 this.usernameField.setValue(user.getName());
463 this.rolesField.setValue(user.getRoles());
464 this.usernameField.setReadOnly(true);
465 }
466 },
467
468 /**
469 * Load a given row : the data passed must be a simple data array.
470 * @param data {Element} The text xml description.
471 */
472 load : function(userName){
473 if(this.getLoaded()){
474 return;
475 }
476 // MUST BE DONE AFTER COMMANDS ARE INITIALIZED!
477 var commands = this.getCommands();
478 var saveButton = commands["save_user"].command.getFormButton();
479 var closeButton = commands["close"].command.getFormButton();
480 var removeButton = commands["remove_nature"].command.getFormButton();
481 var natureButton = commands["add_nature"].command.getFormButton();
482
483 var detectedNatures = this.getAvailableNatures();
484 var newMenu = [];
485 for(var key in detectedNatures){
486 newMenu.push({"label" : detectedNatures[key].NATURE_LABEL, "icon":"", "commandId" : detectedNatures[key]});
487 }
488 commands["add_nature"].command.setMenu(newMenu);
489
490 natureButton.setShow("icon");
491 removeButton.setShow("icon");
492 saveButton.setShow("icon");
493 closeButton.setShow("icon");
494
495 this.buttonGB.add(saveButton);
496 this.buttonGB.add(closeButton);
497 this.natureButtonGB.add(natureButton);
498 this.natureButtonGB.add(removeButton);
499
500 if(userName){
501 this.loadUserData(userName);
502 this._setGuiInCreateMode(false);
503 }else{
504 this.setCurrentUser(new org.argeo.security.ria.model.User());
505 this._setGuiInCreateMode(true);
506 this._attachListeners();
507 this.setModified(true);
508 }
509
510
511 this.setLoaded(true);
512
513 },
514
515 addScroll : function(){
516 return false;
517 },
518
519 close : function(){
520 return false;
521 }
522
523 }
524 });