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