]> 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 "edit_role" : {
32 label : "Edit Role",
33 icon : "ria/window-close.png",
34 shortcut : "Control+r",
35 enabled : true,
36 menu : "Roles",
37 toolbar : "role",
38 callback : function(e){
39 // Call service to delete
40 this.setGuiMode("edit");
41 },
42 selectionChange : function(viewName, data){
43 if(viewName != "roles") return;
44 this.setEnabled(!(data == null || !data.length || data.length > 1));
45 },
46 command : null
47 }
48 }
49 },
50 viewSelection : {
51 nullable:false,
52 check:"org.argeo.ria.components.ViewSelection"
53 },
54 guiMode : {
55 apply : "_applyGuiMode"
56 },
57 chooserOriginalSelection : {},
58 chooserSelectionModified : {
59 init:false,
60 event : "chooserSelectionWasModified"
61 },
62 instanceId : {init:""},
63 instanceLabel : {init:""}
64 },
65
66 members :
67 {
68 /**
69 * Called at applet creation. Just registers viewPane.
70 * @param viewPane {org.argeo.ria.components.ViewPane} The viewPane.
71 */
72 init : function(viewPane){
73 this.setView(viewPane);
74 this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
75 this.tableModel = new qx.ui.table.model.Simple();
76 this.tableModel.setColumns(["Role Name"]);
77 this.table = new qx.ui.table.Table(this.tableModel, {
78 tableColumnModel: function(obj){
79 return new qx.ui.table.columnmodel.Resize(obj)
80 }
81 });
82 this.table.setStatusBarVisible(false);
83 this.table.setShowCellFocusIndicator(false);
84 this.table.setColumnVisibilityButtonVisible(false);
85 this.table.highlightFocusedRow(false);
86 viewPane.add(this.table, {height:"100%"});
87 this.table.getSelectionModel().addListener("changeSelection", function(){
88 this._selectionToValues(this.table.getSelectionModel(), this.getViewSelection());
89 }, this);
90
91
92 this.rolesUsersStub = {"ROLE_ADMIN":["root","mbaudier"],"ROLE_USER":["mbaudier","cdujeu"]};
93
94 var buttonPane = new qx.ui.container.Composite(new qx.ui.layout.HBox(2, "right"));
95 this.getView().header.setPadding(0);
96 if(this.getView().headerLabel) this.getView().headerLabel.setMargin(8);
97 this.getView().header.add(buttonPane, {edge:"east"});
98
99 this.toggleButton = new qx.ui.form.ToggleButton("Filter", "ria/go-right.png");
100 this.toggleButton.set({
101 show:"icon",
102 margin:2,
103 toolTip :new qx.ui.tooltip.ToolTip("Apply automatic filtering on Users list")
104 });
105 buttonPane.add(this.toggleButton);
106
107 // TOGGLE THE GUI MODES
108 this.toggleButton.addListener("changeChecked", function(event){
109 this.setGuiMode(event.getData()?"filter":"clear");
110 }, this);
111
112 this.saveButton = new qx.ui.form.Button("Save");
113 this.saveButton.set({
114 show:"label",
115 margin:2,
116 toolTip :new qx.ui.tooltip.ToolTip("Save changes"),
117 visibility : "excluded"
118 });
119 buttonPane.add(this.saveButton);
120
121 this.cancelButton = new qx.ui.form.Button("Cancel");
122 this.cancelButton.set({
123 show:"label",
124 margin:2,
125 toolTip :new qx.ui.tooltip.ToolTip("Cancel changes"),
126 visibility : "excluded"
127 });
128 buttonPane.add(this.cancelButton);
129
130 var listener = function(){
131 this.setGuiMode(this.initialState);
132 };
133 this.saveButton.addListener("execute", listener, this);
134 this.cancelButton.addListener("execute", listener, this);
135
136 this.table.addListener("cellDblclick", function(cellEvent){
137 this.setGuiMode("edit");
138 }, this);
139
140 this.setGuiMode("clear");
141 },
142
143 _applyGuiMode : function(guiMode, previousMode){
144 var selectionModel = this.table.getSelectionModel();
145 if(!this.usersAppletReference){
146 var vManager = org.argeo.ria.components.ViewsManager.getInstance();
147 this.usersAppletReference = vManager.getViewPaneById("users").getContent();
148 }
149
150 this.saveButton.setVisibility((guiMode=="edit"?"visible":"excluded"));
151 this.cancelButton.setVisibility((guiMode=="edit"?"visible":"excluded"));
152 this.table.setEnabled((guiMode=="edit"?false:true));
153 this.toggleButton.setVisibility((guiMode=="edit"?"excluded":"visible"));
154
155 if(guiMode == "filter"){
156 if(this.usersAppletReference){
157 this.usersAppletReference.setGuiMode(("filter"));
158 var viewSel = this.usersAppletReference.getViewSelection();
159 viewSel.removeListener("changeSelection", this.monitorChooserSelectionChanges, this);
160 }
161 selectionModel.addListener("changeSelection", this.selectionToFilter, this);
162 selectionModel.setSelectionMode(qx.ui.table.selection.Model.MULTIPLE_INTERVAL_SELECTION_TOGGLE);
163 this.selectionToFilter();
164 }else if(guiMode == "edit"){
165 if(!this.usersAppletReference) return;
166 this.initialState = previousMode;
167 if(previousMode == "filter"){
168 this.usersAppletReference.setGuiMode(("clear"));
169 selectionModel.removeListener("changeSelection", this.selectionToFilter, this);
170 }
171 this.usersAppletReference.setGuiMode(("chooser"));
172 this.selectionToChooser(); // Warning, to be called before calling listener!
173 var viewSel = this.usersAppletReference.getViewSelection();
174 viewSel.addListener("changeSelection", this.monitorChooserSelectionChanges, this);
175 }else if(guiMode == "clear"){
176 if(this.usersAppletReference){
177 this.usersAppletReference.setGuiMode(("clear"));
178 var viewSel = this.usersAppletReference.getViewSelection();
179 viewSel.removeListener("changeSelection", this.monitorChooserSelectionChanges, this);
180 }
181 this.table.setEnabled(true);
182 selectionModel.removeListener("changeSelection", this.selectionToFilter, this);
183 selectionModel.setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
184 }
185 },
186
187 monitorChooserSelectionChanges : function(event){
188 if(!this.usersAppletReference || this.getChooserSelectionModified()) return;
189 var initialSelection = this.getChooserOriginalSelection();
190 var crtSelection = event.getTarget().getNodes();
191 if(!qx.lang.Array.equals(initialSelection.sort(), crtSelection.sort())){
192 this.setChooserSelectionModified(true);
193 console.log("Changed!");
194 this.saveButton.setEnabled(true);
195 }
196 },
197
198 selectionToFilter : function(){
199 if(!this.usersAppletReference) return;
200 var selectionModel = this.table.getSelectionModel();
201 if(!selectionModel.getSelectedCount()){
202 this.usersAppletReference.resetHiddenRows();
203 return;
204 }
205 this.usersAppletReference.applyFilter(this._selectionToValues(selectionModel), "roles", true);
206 },
207
208 selectionToChooser : function(){
209 if(!this.usersAppletReference) return;
210 var selectionModel = this.table.getSelectionModel();
211 if(!selectionModel.getSelectedCount()){
212 this.usersAppletReference.resetHiddenRows();
213 return;
214 }
215 var uniqueValue = this._selectionToValues(selectionModel)[0];
216 var initSelection = this.rolesUsersStub[uniqueValue];
217 this.setChooserOriginalSelection(initSelection);
218 this.setChooserSelectionModified(false);
219 this.usersAppletReference.applySelection(initSelection, "username");
220 this.saveButton.setEnabled(false);
221 },
222
223 _selectionToValues : function(selectionModel, viewSelection){
224 if(viewSelection){
225 viewSelection.setBatchMode(true);
226 viewSelection.clear();
227 }
228 if(!selectionModel.getSelectedCount()) return [];
229 var ranges = selectionModel.getSelectedRanges();
230 var values = [];
231 for(var i=0;i<ranges.length;i++){
232 for(var j=ranges[i].minIndex;j<=ranges[i].maxIndex;j++){
233 values.push(this.tableModel.getData()[j][0]);
234 if(viewSelection){
235 viewSelection.addNode(this.tableModel.getData()[j][0]);
236 }
237 }
238 }
239 if(viewSelection){
240 viewSelection.setBatchMode(false);
241 }
242 return values;
243 },
244
245 /**
246 * Load a given row : the data passed must be a simple data array.
247 * @param data {Element} The text xml description.
248 */
249 load : function(){
250 var data = [["ROLE_ADMIN"],["ROLE_USER"]];
251 this.tableModel.setData(data);
252 },
253
254 addScroll : function(){
255 return false;
256 },
257
258 close : function(){
259 return false;
260 }
261
262 }
263 });