]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ria/UsersApplet.js
Prepare next development cycle
[lgpl/argeo-commons.git] / ria / UsersApplet.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.UsersApplet",
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 "load_users" : {
32 label : "Reload Users",
33 icon : "org.argeo.security.ria/view-refresh.png",
34 shortcut : "Control+h",
35 enabled : true,
36 menu : "Users",
37 toolbar : "users",
38 callback : function(e){
39 this.loadUsersList();
40 },
41 command : null
42 },
43 "new_user" : {
44 label : "New User",
45 icon : "org.argeo.security.ria/list-add.png",
46 shortcut : "Control+n",
47 enabled : true,
48 menu : "Users",
49 toolbar : null,
50 callback : function(e){
51 // Call service to delete
52 var classObj = org.argeo.security.ria.UserEditorApplet;
53 var initData = {USER:null,ROLES_LIST:this.getRolesList()};
54 var iView = org.argeo.ria.components.ViewsManager.getInstance().initIViewClass(classObj, "editor", initData);
55 iView.load();
56 iView.addListener("savedUser", function(e){
57 this.refreshUserEntry(e.getData());
58 }, this);
59 },
60 command : null
61 },
62 "delete_user" : {
63 label : "Delete User",
64 icon : "org.argeo.security.ria/list-remove.png",
65 shortcut : "Control+s",
66 enabled : true,
67 menu : "Users",
68 toolbar : null,
69 callback : function(e){
70 // Call service to delete
71 var username = this.getViewSelection().getNodes()[0];
72 var deleteService = org.argeo.security.ria.SecurityAPI.getDeleteUserService(username);
73 deleteService.addListener("completed", function(response){
74 if(response.getContent().status && response.getContent().status == "OK"){
75 this.loadUsersList();
76 }
77 }, this);
78 // Check if tab is opened before closing!
79 var editorTabPane = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("editor");
80 var iApplet = editorTabPane.contentExists(username);
81 var modal = new org.argeo.ria.components.Modal("Delete");
82 if(iApplet){
83 if(iApplet.getModified()){
84 modal.addConfirm("There are unsaved changes, are you sure you want to delete this user?");
85 modal.addListener("ok", function(e){
86 editorTabPane.closeCurrent();
87 deleteService.send();
88 }, this);
89 modal.attachAndShow();
90 }else{
91 modal.addConfirm("Are you sure you want to delete user " + username + "?");
92 modal.addListener("ok", function(e){
93 editorTabPane.closeCurrent();
94 deleteService.send();
95 }, this);
96 modal.attachAndShow();
97 }
98 }else{
99 modal.addConfirm("Are you sure you want to delete user " + username + "?");
100 modal.addListener("ok", function(e){
101 deleteService.send();
102 }, this);
103 modal.attachAndShow();
104 }
105 },
106 selectionChange : function(viewName, data){
107 if(viewName != "users") return;
108 this.setEnabled(!(data == null || !data.length || data.length>1));
109 },
110 command : null
111 },
112 "edit_user" : {
113 label : "Edit User",
114 icon : "org.argeo.security.ria/document-properties.png",
115 shortcut : "Control+u",
116 enabled : true,
117 menu : "Users",
118 toolbar : null,
119 callback : function(e){
120 // Call service to delete
121 var crtUser = this.getViewSelection().getNodes()[0];
122 var classObj = org.argeo.security.ria.UserEditorApplet;
123 var initData = {USER:crtUser,ROLES_LIST:this.getRolesList()};
124 var iView = org.argeo.ria.components.ViewsManager.getInstance().initIViewClass(classObj, "editor", initData);
125 iView.load(crtUser);
126 iView.addListener("savedUser", function(e){
127 this.refreshUserEntry(e.getData());
128 }, this);
129 },
130 selectionChange : function(viewName, data){
131 if(viewName != "users") return;
132 this.setEnabled(!(data == null || !data.length || data.length > 1));
133 },
134 command : null
135 }
136 }
137 },
138
139 guiMode : {
140 init : "filter",
141 apply : "_applyGuiMode"
142 },
143
144 viewSelection : {
145 nullable:false,
146 check:"org.argeo.ria.components.ViewSelection"
147 },
148 usersList : {
149 check : "Map",
150 apply : "_applyUsersList",
151 event : "changeUsersList"
152 },
153 rolesList : {
154 check : "Array"
155 },
156 instanceId : {init:""},
157 instanceLabel : {init:""}
158 },
159
160 members :
161 {
162 /**
163 * Called at applet creation. Just registers viewPane.
164 * @param viewPane {org.argeo.ria.components.ViewPane} The viewPane.
165 */
166 init : function(viewPane){
167 this.setView(viewPane);
168 this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
169
170 this.toolBar = new qx.ui.toolbar.ToolBar();
171 this.toolBarPart = new qx.ui.toolbar.Part();
172 this.toolBar.add(this.toolBarPart);
173 viewPane.add(this.toolBar);
174
175 this.tableModel = new qx.ui.table.model.Filtered();
176 this.tableModel.setColumns(["username", "roles"]);
177 this.table = new qx.ui.table.Table(this.tableModel, {
178 tableColumnModel: function(obj){
179 return new qx.ui.table.columnmodel.Resize(obj)
180 }
181 });
182 this.table.setStatusBarVisible(false);
183 this.table.setShowCellFocusIndicator(false);
184 this.table.setColumnVisibilityButtonVisible(false);
185 this.table.highlightFocusedRow(false);
186 viewPane.add(this.table, {height:"100%"});
187 this.table.getSelectionModel().addListener("changeSelection", function(){
188 this._selectionToValues(this.table.getSelectionModel(), this.getViewSelection());
189 }, this);
190 this.table.addListener("cellDblclick", function(cellEvent){
191 this.getCommands()["edit_user"].command.execute();
192 }, this);
193
194 this.setRolesList([]);
195 this.setUsersList({});
196 this.setGuiMode("clear");
197 },
198
199 _applyGuiMode : function(newMode, oldMode){
200 this.table.getSelectionModel().clearSelection();
201 this.resetHiddenRows();
202 if(newMode == "filter"){
203 this.table.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
204 }else if(newMode == "chooser"){
205 this.table.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.MULTIPLE_INTERVAL_SELECTION_TOGGLE);
206 }else if(newMode == "clear"){
207 this.table.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
208 }
209 },
210
211 _selectionToValues : function(selectionModel, viewSelection){
212 if(viewSelection){
213 viewSelection.setBatchMode(true);
214 viewSelection.clear();
215 }
216 if(!selectionModel.getSelectedCount()) return [];
217 var ranges = selectionModel.getSelectedRanges();
218 var values = [];
219 for(var i=0;i<ranges.length;i++){
220 for(var j=ranges[i].minIndex;j<=ranges[i].maxIndex;j++){
221 values.push(this.tableModel.getData()[j][0]);
222 if(viewSelection){
223 viewSelection.addNode(this.tableModel.getData()[j][0]);
224 }
225 }
226 }
227 if(viewSelection){
228 viewSelection.setBatchMode(false);
229 }
230 return values;
231 },
232
233 /**
234 * Load a given row : the data passed must be a simple data array.
235 * @param data {Element} The text xml description.
236 */
237 load : function(){
238 var commands = this.getCommands();
239 this.toolBarPart.add(commands["new_user"].command.getToolbarButton());
240 this.toolBarPart.add(commands["delete_user"].command.getToolbarButton());
241 this.toolBarPart.add(commands["edit_user"].command.getToolbarButton());
242 this.toolBar.setShow("icon");
243 this.loadUsersList();
244
245 var rolesApplet = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("roles").getContent();
246 rolesApplet.addListener("changeRolesList", function(e){
247 this.setRolesList(e.getData());
248 this.loadUsersList();
249 }, this);
250
251 },
252
253 loadUsersList : function(){
254 var selectionModel = this.table.getSelectionModel();
255 selectionModel.clearSelection();
256 var request = org.argeo.security.ria.SecurityAPI.getListUsersService();
257 request.addListener("completed", function(response){
258 var jSon = response.getContent();
259 var usMap = {};
260 for(var i=0;i<jSon.length;i++){
261 var user = new org.argeo.security.ria.model.User();
262 user.load(jSon[i], "json");
263 usMap[user.getName()] = user;
264 }
265 this.setUsersList(usMap);
266 }, this);
267 request.send();
268 },
269
270 /**
271 *
272 * @param {org.argeo.security.ria.model.User} userObject
273 */
274 refreshUserEntry : function(userObject){
275 var userName = userObject.getName();
276 var data = this.tableModel.getDataAsMapArray();
277 var index = 0;
278 var found = false;
279 for(index=0;index<data.length;index++){
280 if(data[index].username == userName){
281 found =true;
282 break;
283 }
284 }
285 var newRows = [{username:userName,roles:userObject.getRoles().join(",")}];
286 if(found){
287 this.tableModel.setRowsAsMapArray(newRows, index);
288 }else{
289 this.tableModel.setRowsAsMapArray(newRows, index);
290 }
291 },
292
293 _applyUsersList : function(usList){
294 var data = [];
295 qx.lang.Object.getValues(usList).forEach(function(usObject){
296 data.push([usObject.getName(), usObject.getRoles().join(",")]);
297 });
298 this.tableModel.setData(data);
299 },
300
301 applySelection : function(selectionValue, target, ignoreCase){
302 var selectionModel = this.table.getSelectionModel();
303 selectionModel.clearSelection();
304 if(!selectionValue){
305 return;
306 }
307 selectionModel.setBatchMode(true);
308 var data = this.tableModel.getData();
309 for(var i=0;i<this.tableModel.getRowCount();i++){
310 var value = this.tableModel.getRowDataAsMap(i)[target];
311 var pattern = new RegExp(selectionValue, "g"+(ignoreCase?"i":""));
312 if(pattern.test(value)){
313 selectionModel.addSelectionInterval(i, i);
314 }
315 }
316 selectionModel.setBatchMode(false);
317 },
318
319 applyFilter : function(filterValues, target, ignoreCase){
320 this.table.clearSelection();
321 this.resetHiddenRows();
322 for(var i=0;i<filterValues.length;i++){
323 this.tableModel.addRegex("^((?!"+filterValues[i]+").)*$", target, ignoreCase);
324 }
325 this.tableModel.applyFilters();
326 },
327
328 resetHiddenRows : function(){
329 this.tableModel.resetHiddenRows();
330 },
331
332 addScroll : function(){
333 return false;
334 },
335
336 close : function(){
337 return false;
338 }
339
340 }
341 });