2 * A simple Hello World applet for documentation purpose.
3 * The only associated command is the "Close" command.
5 /* *************************************************
6 #asset(resource/org.argeo.ria.sample/window-close.png)
7 ****************************************************/
8 qx
.Class
.define("org.argeo.security.ria.RolesApplet",
10 extend
: qx
.ui
.container
.Composite
,
11 implement
: [org
.argeo
.ria
.components
.IView
],
13 construct : function(){
15 this.setLayout(new qx
.ui
.layout
.VBox());
21 * The viewPane inside which this applet is added.
27 * Commands definition, see {@link org.argeo.ria.event.CommandsManager#definitions}
32 label
: "Reload Data",
33 icon
: "org.argeo.security.ria/view-refresh.png",
34 shortcut
: "Control+h",
38 callback : function(e
){
44 label
: "Create Role",
45 icon
: "org.argeo.security.ria/list-add.png",
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
){
60 modal
.attachAndShow();
65 label
: "Delete Role",
66 icon
: "org.argeo.security.ria/list-remove.png",
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
){
85 modal
.attachAndShow();
87 selectionChange : function(viewName
, data
){
88 if(viewName
!= "roles") return;
89 this.setEnabled(!(data
== null || !data
.length
));
95 icon
: "org.argeo.security.ria/document-properties.png",
96 shortcut
: "Control+r",
100 callback : function(e
){
101 // Call service to delete
102 this.setGuiMode("edit");
104 selectionChange : function(viewName
, data
){
105 if(viewName
!= "roles") return;
106 this.setEnabled(!(data
== null || !data
.length
|| data
.length
> 1));
114 check
:"org.argeo.ria.components.ViewSelection"
117 apply
: "_applyGuiMode"
121 event
: "changeRolesList"
123 chooserOriginalSelection
: {},
124 chooserSelectionModified
: {
126 event
: "chooserSelectionWasModified"
128 instanceId
: {init
:""},
129 instanceLabel
: {init
:""}
135 * Called at applet creation. Just registers viewPane.
136 * @param viewPane {org.argeo.ria.components.ViewPane} The viewPane.
138 init : function(viewPane
){
139 this.setView(viewPane
);
140 this.setViewSelection(new org
.argeo
.ria
.components
.ViewSelection(viewPane
.getViewId()));
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
);
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
)
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());
163 this.rolesUsersStub
= {"ROLE_ADMIN":["gandalf"],"ROLE_USER":["demo","frodo","gandalf"]};
165 this.toggleButton
= new qx
.ui
.form
.ToggleButton("Filter", "org.argeo.security.ria/go-next.png");
166 this.toggleButton
.set({
169 toolTip
:new qx
.ui
.tooltip
.ToolTip("Apply automatic filtering on Users list")
172 // TOGGLE THE GUI MODES
173 this.toggleButton
.addListener("changeValue", function(event
){
174 this.setGuiMode(event
.getData()?"filter":"clear");
177 this.saveButton
= new qx
.ui
.form
.Button("Save", "org.argeo.security.ria/document-save.png");
178 this.saveButton
.set({
181 toolTip
:new qx
.ui
.tooltip
.ToolTip("Save changes"),
182 visibility
: "excluded"
185 this.cancelButton
= new qx
.ui
.form
.Button("Cancel", "org.argeo.security.ria/window-close.png");
186 this.cancelButton
.set({
189 toolTip
:new qx
.ui
.tooltip
.ToolTip("Cancel changes"),
190 visibility
: "excluded"
193 this.saveButton
.addListener("execute", function(){
194 if(!this.usersAppletReference
){
195 this.setGuiMode(this.initialState
);
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
);
203 this.cancelButton
.addListener("execute", function(){
204 if(!this.getChooserSelectionModified()){
205 this.setGuiMode(this.initialState
);
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
);
213 modal
.attachAndShow();
216 this.table
.addListener("cellDblclick", function(cellEvent
){
217 this.setGuiMode("edit");
219 this.addListener("changeRolesList", function(event
){
221 event
.getData().forEach(function(el
){data
.push([el
]);});
222 this.tableModel
.setData(data
);
225 this.setGuiMode("clear");
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();
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"));
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);
246 selectionModel
.addListener("changeSelection", this.selectionToFilter
, this);
247 if(selectionModel
.getSelectedCount()){
248 var orig
= selectionModel
.getSelectedRanges()[0].minIndex
;
250 selectionModel
.setSelectionMode(qx
.ui
.table
.selection
.Model
.MULTIPLE_INTERVAL_SELECTION_TOGGLE
);
252 selectionModel
.addSelectionInterval(orig
, orig
);
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);
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);
272 this.table
.setEnabled(true);
273 selectionModel
.removeListener("changeSelection", this.selectionToFilter
, this);
274 if(selectionModel
.getSelectedCount()){
275 var orig
= selectionModel
.getSelectedRanges()[0].minIndex
;
277 selectionModel
.setSelectionMode(qx
.ui
.table
.selection
.Model
.SINGLE_SELECTION
);
279 selectionModel
.addSelectionInterval(orig
, orig
);
284 saveRoleModifications : function(deltaPlus
, deltaMinus
){
285 // LOAD CONCERNED USERS
286 var selectionModel
= this.table
.getSelectionModel();
287 if(!selectionModel
.getSelectedCount()){
290 var roleValue
= this._selectionToValues(selectionModel
)[0];
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
++){
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
);
305 var userSaveService
= org
.argeo
.security
.ria
.SecurityAPI
.getUpdateUserService(response
.getContent());
306 userSaveService
.setAsynchronous(false);
307 userSaveService
.send();
309 userDetailService
.setAsynchronous(false);
310 userDetailService
.send();
312 this.fireDataEvent("changeRolesList", this.getRolesList());
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);
327 selectionToFilter : function(){
328 if(!this.usersAppletReference
) return;
329 var selectionModel
= this.table
.getSelectionModel();
330 if(!selectionModel
.getSelectedCount()){
331 this.usersAppletReference
.resetHiddenRows();
334 this.usersAppletReference
.applyFilter(this._selectionToValues(selectionModel
), "roles", true);
337 selectionToChooser : function(){
338 if(!this.usersAppletReference
) return;
339 var selectionModel
= this.table
.getSelectionModel();
340 if(!selectionModel
.getSelectedCount()){
341 this.usersAppletReference
.resetHiddenRows();
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);
353 _selectionToValues : function(selectionModel
, viewSelection
){
355 viewSelection
.setBatchMode(true);
356 viewSelection
.clear();
358 if(!selectionModel
.getSelectedCount()) return [];
359 var ranges
= selectionModel
.getSelectedRanges();
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]);
365 viewSelection
.addNode(this.tableModel
.getData()[j
][0]);
370 viewSelection
.setBatchMode(false);
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
};
384 * Load a given row : the data passed must be a simple data array.
385 * @param data {Element} The text xml description.
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");
399 this.loadRolesList();
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());
412 addScroll : function(){