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