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