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
: "Create Role",
33 icon
: "org.argeo.security.ria/list-add.png",
34 shortcut
: "Control+n",
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
){
48 modal
.attachAndShow();
53 label
: "Delete Role",
54 icon
: "org.argeo.security.ria/list-remove.png",
55 shortcut
: "Control+s",
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
){
73 modal
.attachAndShow();
75 selectionChange : function(viewName
, data
){
76 if(viewName
!= "roles") return;
77 this.setEnabled(!(data
== null || !data
.length
));
83 icon
: "org.argeo.security.ria/document-properties.png",
84 shortcut
: "Control+r",
88 callback : function(e
){
89 // Call service to delete
90 this.setGuiMode("edit");
92 selectionChange : function(viewName
, data
){
93 if(viewName
!= "roles") return;
94 this.setEnabled(!(data
== null || !data
.length
|| data
.length
> 1));
102 check
:"org.argeo.ria.components.ViewSelection"
105 apply
: "_applyGuiMode"
109 event
: "changeRolesList"
111 chooserOriginalSelection
: {},
112 chooserSelectionModified
: {
114 event
: "chooserSelectionWasModified"
116 instanceId
: {init
:""},
117 instanceLabel
: {init
:""}
123 * Called at applet creation. Just registers viewPane.
124 * @param viewPane {org.argeo.ria.components.ViewPane} The viewPane.
126 init : function(viewPane
){
127 this.setView(viewPane
);
128 this.setViewSelection(new org
.argeo
.ria
.components
.ViewSelection(viewPane
.getViewId()));
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
);
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
)
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());
151 this.rolesUsersStub
= {"ROLE_ADMIN":["gandalf"],"ROLE_USER":["demo","frodo","gandalf"]};
153 this.toggleButton
= new qx
.ui
.form
.ToggleButton("Filter", "org.argeo.security.ria/go-next.png");
154 this.toggleButton
.set({
157 toolTip
:new qx
.ui
.tooltip
.ToolTip("Apply automatic filtering on Users list")
160 // TOGGLE THE GUI MODES
161 this.toggleButton
.addListener("changeChecked", function(event
){
162 this.setGuiMode(event
.getData()?"filter":"clear");
165 this.saveButton
= new qx
.ui
.form
.Button("Save", "org.argeo.security.ria/document-save.png");
166 this.saveButton
.set({
169 toolTip
:new qx
.ui
.tooltip
.ToolTip("Save changes"),
170 visibility
: "excluded"
173 this.cancelButton
= new qx
.ui
.form
.Button("Cancel", "org.argeo.security.ria/window-close.png");
174 this.cancelButton
.set({
177 toolTip
:new qx
.ui
.tooltip
.ToolTip("Cancel changes"),
178 visibility
: "excluded"
181 this.saveButton
.addListener("execute", function(){
182 if(!this.usersAppletReference
){
183 this.setGuiMode(this.initialState
);
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
);
191 this.cancelButton
.addListener("execute", function(){
192 if(!this.getChooserSelectionModified()){
193 this.setGuiMode(this.initialState
);
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
);
201 modal
.attachAndShow();
204 this.table
.addListener("cellDblclick", function(cellEvent
){
205 this.setGuiMode("edit");
207 this.addListener("changeRolesList", function(event
){
209 event
.getData().forEach(function(el
){data
.push([el
]);});
210 this.tableModel
.setData(data
);
213 this.setGuiMode("clear");
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();
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"));
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);
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);
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);
254 this.table
.setEnabled(true);
255 selectionModel
.removeListener("changeSelection", this.selectionToFilter
, this);
256 selectionModel
.setSelectionMode(qx
.ui
.table
.selection
.Model
.SINGLE_SELECTION
);
260 saveRoleModifications : function(deltaPlus
, deltaMinus
){
261 // LOAD CONCERNED USERS
262 var selectionModel
= this.table
.getSelectionModel();
263 if(!selectionModel
.getSelectedCount()){
266 var roleValue
= this._selectionToValues(selectionModel
)[0];
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
++){
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
);
281 var userSaveService
= org
.argeo
.security
.ria
.SecurityAPI
.getUpdateUserService(response
.getContent());
282 userSaveService
.setAsynchronous(false);
283 userSaveService
.send();
285 userDetailService
.setAsynchronous(false);
286 userDetailService
.send();
288 this.fireDataEvent("changeRolesList", this.getRolesList());
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);
303 selectionToFilter : function(){
304 if(!this.usersAppletReference
) return;
305 var selectionModel
= this.table
.getSelectionModel();
306 if(!selectionModel
.getSelectedCount()){
307 this.usersAppletReference
.resetHiddenRows();
310 this.usersAppletReference
.applyFilter(this._selectionToValues(selectionModel
), "roles", true);
313 selectionToChooser : function(){
314 if(!this.usersAppletReference
) return;
315 var selectionModel
= this.table
.getSelectionModel();
316 if(!selectionModel
.getSelectedCount()){
317 this.usersAppletReference
.resetHiddenRows();
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);
329 _selectionToValues : function(selectionModel
, viewSelection
){
331 viewSelection
.setBatchMode(true);
332 viewSelection
.clear();
334 if(!selectionModel
.getSelectedCount()) return [];
335 var ranges
= selectionModel
.getSelectedRanges();
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]);
341 viewSelection
.addNode(this.tableModel
.getData()[j
][0]);
346 viewSelection
.setBatchMode(false);
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
};
360 * Load a given row : the data passed must be a simple data array.
361 * @param data {Element} The text xml description.
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");
375 this.loadRolesList();
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());
388 addScroll : function(){