]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.ria/src/argeo-ria-lib/security/class/org/argeo/security/ria/RolesApplet.js
add description field
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.ria / src / argeo-ria-lib / security / class / org / argeo / security / ria / RolesApplet.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.ria.sample/window-close.png)
7 ****************************************************/
8 qx.Class.define("org.argeo.security.ria.RolesApplet",
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());
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 "reload" : {
32 label : "Reload Data",
33 icon : "org.argeo.security.ria/view-refresh.png",
34 shortcut : "Control+h",
35 enabled : true,
36 menu : "Roles",
37 toolbar : "roles",
38 callback : function(e){
39 this.loadRolesList();
40 },
41 command : null
42 },
43 "new_role" : {
44 label : "Create Role",
45 icon : "org.argeo.security.ria/list-add.png",
46 shortcut : null,
47 enabled : true,
48 menu : "Roles",
49 toolbar : null,
50 callback : function(e){
51 // Prompt for new name
52 var modal = new org.argeo.ria.components.Modal();
53 modal.makePromptForm("Please enter a role name", function(roleName){
54 var service = org.argeo.security.ria.SecurityAPI.getCreateRoleService(roleName);
55 service.addListener("completed", function(response){
56 this.loadRolesList();
57 }, this);
58 service.send();
59 }, this);
60 modal.attachAndShow();
61 },
62 command : null
63 },
64 "delete_role" : {
65 label : "Delete Role",
66 icon : "org.argeo.security.ria/list-remove.png",
67 shortcut : null,
68 enabled : true,
69 menu : "Roles",
70 toolbar : null,
71 callback : function(e){
72 // Call service to delete
73 var roles = this.getViewSelection().getNodes();
74 var modal = new org.argeo.ria.components.Modal("Delete");
75 modal.addConfirm("Are you sure you want to delete the selected roles?");
76 modal.addListener("ok", function(e){
77 for(var i=0;i<roles.length;i++){
78 var service = org.argeo.security.ria.SecurityAPI.getDeleteRoleService(roles[i]);
79 service.addListener("completed", function(response){
80 this.loadRolesList();
81 }, this);
82 service.send();
83 }
84 }, this);
85 modal.attachAndShow();
86 },
87 selectionChange : function(viewName, data){
88 if(viewName != "roles") return;
89 this.setEnabled(!(data == null || !data.length));
90 },
91 command : null
92 },
93 "edit_role" : {
94 label : "Edit Role",
95 icon : "org.argeo.security.ria/document-properties.png",
96 shortcut : "Control+r",
97 enabled : true,
98 menu : "Roles",
99 toolbar : null,
100 callback : function(e){
101 // Call service to delete
102 this.setGuiMode("edit");
103 },
104 selectionChange : function(viewName, data){
105 if(viewName != "roles") return;
106 this.setEnabled(!(data == null || !data.length || data.length > 1));
107 },
108 command : null
109 }
110 }
111 },
112 viewSelection : {
113 nullable:false,
114 check:"org.argeo.ria.components.ViewSelection"
115 },
116 guiMode : {
117 apply : "_applyGuiMode"
118 },
119 rolesList : {
120 check : "Array",
121 event : "changeRolesList"
122 },
123 chooserOriginalSelection : {},
124 chooserSelectionModified : {
125 init:false,
126 event : "chooserSelectionWasModified"
127 },
128 instanceId : {init:""},
129 instanceLabel : {init:""}
130 },
131
132 members :
133 {
134 /**
135 * Called at applet creation. Just registers viewPane.
136 * @param viewPane {org.argeo.ria.components.ViewPane} The viewPane.
137 */
138 init : function(viewPane){
139 this.setView(viewPane);
140 this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
141
142 this.toolBar = new qx.ui.toolbar.ToolBar();
143 this.toolBarPart = new qx.ui.toolbar.Part();
144 this.toolBar.add(this.toolBarPart);
145 viewPane.add(this.toolBar);
146
147 this.tableModel = new qx.ui.table.model.Simple();
148 this.tableModel.setColumns(["Role Name"]);
149 this.table = new qx.ui.table.Table(this.tableModel, {
150 tableColumnModel: function(obj){
151 return new qx.ui.table.columnmodel.Resize(obj)
152 }
153 });
154 this.table.setStatusBarVisible(false);
155 this.table.setShowCellFocusIndicator(false);
156 this.table.setColumnVisibilityButtonVisible(false);
157 this.table.highlightFocusedRow(false);
158 viewPane.add(this.table, {height:"100%"});
159 this.table.getSelectionModel().addListener("changeSelection", function(){
160 this._selectionToValues(this.table.getSelectionModel(), this.getViewSelection());
161 }, this);
162
163 this.rolesUsersStub = {"ROLE_ADMIN":["gandalf"],"ROLE_USER":["demo","frodo","gandalf"]};
164
165 this.toggleButton = new qx.ui.form.ToggleButton("Filter", "org.argeo.security.ria/go-next.png");
166 this.toggleButton.set({
167 show:"icon",
168 margin:2,
169 toolTip :new qx.ui.tooltip.ToolTip("Apply automatic filtering on Users list")
170 });
171
172 // TOGGLE THE GUI MODES
173 this.toggleButton.addListener("changeValue", function(event){
174 this.setGuiMode(event.getData()?"filter":"clear");
175 }, this);
176
177 this.saveButton = new qx.ui.form.Button("Save", "org.argeo.security.ria/document-save.png");
178 this.saveButton.set({
179 show:"icon",
180 margin:2,
181 toolTip :new qx.ui.tooltip.ToolTip("Save changes"),
182 visibility : "excluded"
183 });
184
185 this.cancelButton = new qx.ui.form.Button("Cancel", "org.argeo.security.ria/window-close.png");
186 this.cancelButton.set({
187 show:"icon",
188 margin:2,
189 toolTip :new qx.ui.tooltip.ToolTip("Cancel changes"),
190 visibility : "excluded"
191 });
192
193 this.saveButton.addListener("execute", function(){
194 if(!this.usersAppletReference){
195 this.setGuiMode(this.initialState);
196 return;
197 }
198 var newSelection = this.usersAppletReference.getViewSelection().getNodes();
199 var diff = this._selectionDiff(this.getChooserOriginalSelection(), newSelection);
200 this.saveRoleModifications(diff.deltaPlus, diff.deltaMinus);
201 this.setGuiMode(this.initialState);
202 }, this);
203 this.cancelButton.addListener("execute", function(){
204 if(!this.getChooserSelectionModified()){
205 this.setGuiMode(this.initialState);
206 return;
207 }
208 var modal = new org.argeo.ria.components.Modal("Warning");
209 modal.addConfirm("There are unsaved changes!\n Are you sure you want to close?");
210 modal.addListener("ok", function(){
211 this.setGuiMode(this.initialState);
212 }, this);
213 modal.attachAndShow();
214 }, this);
215
216 this.table.addListener("cellDblclick", function(cellEvent){
217 this.setGuiMode("edit");
218 }, this);
219 this.addListener("changeRolesList", function(event){
220 var data = [];
221 event.getData().forEach(function(el){data.push([el]);});
222 this.tableModel.setData(data);
223 }, this);
224
225 this.setGuiMode("clear");
226 },
227
228 _applyGuiMode : function(guiMode, previousMode){
229 var selectionModel = this.table.getSelectionModel();
230 if(!this.usersAppletReference){
231 var vManager = org.argeo.ria.components.ViewsManager.getInstance();
232 this.usersAppletReference = vManager.getViewPaneById("users").getContent();
233 }
234
235 this.saveButton.setVisibility((guiMode=="edit"?"visible":"excluded"));
236 this.cancelButton.setVisibility((guiMode=="edit"?"visible":"excluded"));
237 this.table.setEnabled((guiMode=="edit"?false:true));
238 this.toggleButton.setVisibility((guiMode=="edit"?"excluded":"visible"));
239
240 if(guiMode == "filter"){
241 if(this.usersAppletReference){
242 this.usersAppletReference.setGuiMode(("filter"));
243 var viewSel = this.usersAppletReference.getViewSelection();
244 viewSel.removeListener("changeSelection", this.monitorChooserSelectionChanges, this);
245 }
246 selectionModel.addListener("changeSelection", this.selectionToFilter, this);
247 if(selectionModel.getSelectedCount()){
248 var orig = selectionModel.getSelectedRanges()[0].minIndex;
249 }
250 selectionModel.setSelectionMode(qx.ui.table.selection.Model.MULTIPLE_INTERVAL_SELECTION_TOGGLE);
251 if(orig){
252 selectionModel.addSelectionInterval(orig, orig);
253 }
254 this.selectionToFilter();
255 }else if(guiMode == "edit"){
256 if(!this.usersAppletReference) return;
257 this.initialState = previousMode;
258 if(previousMode == "filter"){
259 this.usersAppletReference.setGuiMode(("clear"));
260 selectionModel.removeListener("changeSelection", this.selectionToFilter, this);
261 }
262 this.usersAppletReference.setGuiMode(("chooser"));
263 this.selectionToChooser(); // Warning, to be called before calling listener!
264 var viewSel = this.usersAppletReference.getViewSelection();
265 viewSel.addListener("changeSelection", this.monitorChooserSelectionChanges, this);
266 }else if(guiMode == "clear"){
267 if(this.usersAppletReference){
268 this.usersAppletReference.setGuiMode(("clear"));
269 var viewSel = this.usersAppletReference.getViewSelection();
270 viewSel.removeListener("changeSelection", this.monitorChooserSelectionChanges, this);
271 }
272 this.table.setEnabled(true);
273 selectionModel.removeListener("changeSelection", this.selectionToFilter, this);
274 if(selectionModel.getSelectedCount()){
275 var orig = selectionModel.getSelectedRanges()[0].minIndex;
276 }
277 selectionModel.setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
278 if(orig){
279 selectionModel.addSelectionInterval(orig, orig);
280 }
281 }
282 },
283
284 saveRoleModifications : function(deltaPlus, deltaMinus){
285 // LOAD CONCERNED USERS
286 var selectionModel = this.table.getSelectionModel();
287 if(!selectionModel.getSelectedCount()){
288 return;
289 }
290 var roleValue = this._selectionToValues(selectionModel)[0];
291
292 var users = deltaPlus.concat(deltaMinus);
293 var modal = new org.argeo.ria.components.Modal("Batch Update", "", "Please wait, updating roles for selected users");
294 modal.attachAndShow();
295 for(var i=0;i<users.length;i++){
296 var user = users[i];
297 var userDetailService = org.argeo.security.ria.SecurityAPI.getUserDetailsService(users[i]);
298 userDetailService.addListener("completed", function(response){
299 var userRoles = response.getContent().roles;
300 if(qx.lang.Array.contains(deltaPlus, user)){
301 userRoles.push(roleValue);
302 }else if(qx.lang.Array.contains(deltaMinus, user)){
303 qx.lang.Array.remove(userRoles, roleValue);
304 }
305 var userSaveService = org.argeo.security.ria.SecurityAPI.getUpdateUserService(response.getContent());
306 userSaveService.setAsynchronous(false);
307 userSaveService.send();
308 }, this);
309 userDetailService.setAsynchronous(false);
310 userDetailService.send();
311 }
312 this.fireDataEvent("changeRolesList", this.getRolesList());
313 modal.hide();
314 modal.destroy();
315 },
316
317 monitorChooserSelectionChanges : function(event){
318 if(!this.usersAppletReference || this.getChooserSelectionModified()) return;
319 var initialSelection = this.getChooserOriginalSelection();
320 var crtSelection = event.getTarget().getNodes();
321 if(!qx.lang.Array.equals(initialSelection.sort(), crtSelection.sort())){
322 this.setChooserSelectionModified(true);
323 this.saveButton.setEnabled(true);
324 }
325 },
326
327 selectionToFilter : function(){
328 if(!this.usersAppletReference) return;
329 var selectionModel = this.table.getSelectionModel();
330 if(!selectionModel.getSelectedCount()){
331 this.usersAppletReference.resetHiddenRows();
332 return;
333 }
334 this.usersAppletReference.applyFilter(this._selectionToValues(selectionModel), "roles", true);
335 },
336
337 selectionToChooser : function(){
338 if(!this.usersAppletReference) return;
339 var selectionModel = this.table.getSelectionModel();
340 if(!selectionModel.getSelectedCount()){
341 this.usersAppletReference.resetHiddenRows();
342 return;
343 }
344 var uniqueValue = this._selectionToValues(selectionModel)[0];
345 //var initSelection = this.rolesUsersStub[uniqueValue];
346 this.usersAppletReference.applySelection(uniqueValue, "roles");
347 var initSelection = this.usersAppletReference.getViewSelection().getNodes();
348 this.setChooserOriginalSelection(initSelection);
349 this.setChooserSelectionModified(false);
350 this.saveButton.setEnabled(false);
351 },
352
353 _selectionToValues : function(selectionModel, viewSelection){
354 if(viewSelection){
355 viewSelection.setBatchMode(true);
356 viewSelection.clear();
357 }
358 if(!selectionModel.getSelectedCount()) return [];
359 var ranges = selectionModel.getSelectedRanges();
360 var values = [];
361 for(var i=0;i<ranges.length;i++){
362 for(var j=ranges[i].minIndex;j<=ranges[i].maxIndex;j++){
363 values.push(this.tableModel.getData()[j][0]);
364 if(viewSelection){
365 viewSelection.addNode(this.tableModel.getData()[j][0]);
366 }
367 }
368 }
369 if(viewSelection){
370 viewSelection.setBatchMode(false);
371 }
372 return values;
373 },
374
375 _selectionDiff : function(initialSelection, modifiedSelection){
376 var deltaMinus = qx.lang.Array.clone(initialSelection);
377 var deltaPlus = qx.lang.Array.clone(modifiedSelection);
378 qx.lang.Array.exclude(deltaPlus, initialSelection);
379 qx.lang.Array.exclude(deltaMinus, modifiedSelection);
380 return {deltaPlus : deltaPlus, deltaMinus : deltaMinus};
381 },
382
383 /**
384 * Load a given row : the data passed must be a simple data array.
385 * @param data {Element} The text xml description.
386 */
387 load : function(){
388
389 var commands = this.getCommands();
390 this.toolBarPart.add(commands["new_role"].command.getToolbarButton());
391 this.toolBarPart.add(commands["delete_role"].command.getToolbarButton());
392 this.toolBarPart.add(commands["edit_role"].command.getToolbarButton());
393 this.toolBar.addSpacer();
394 this.toolBar.add(this.toggleButton);
395 this.toolBar.add(this.saveButton);
396 this.toolBar.add(this.cancelButton);
397 this.toolBar.setShow("icon");
398
399 this.loadRolesList();
400
401 },
402
403 loadRolesList : function(){
404 this.setRolesList([]);
405 var service = org.argeo.security.ria.SecurityAPI.getListRolesService();
406 service.addListener("completed", function(response){
407 this.setRolesList(response.getContent());
408 }, this);
409 service.send();
410 },
411
412 addScroll : function(){
413 return false;
414 },
415
416 close : function(){
417 return false;
418 }
419
420 }
421 });