]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/event/CommandsManager.js
8f0428076b95378e9a78d809d18ad4b33d99b971
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / argeo-ria-src / class / org / argeo / ria / event / CommandsManager.js
1 /**
2 * The main controller (in a standard MVC point of view) of the application. It is a singleton
3 * thus can be called by any part of the application.
4 * This will wire all the commands that can be defined dynamically by any IView, and add their
5 * corresponding buttons to the application menubar and toolbars.
6 * See the "definitions" property documentation below for more info on how to define new commands.
7 *
8 * @author Charles du Jeu
9 */
10 qx.Class.define("org.argeo.ria.event.CommandsManager",
11 {
12 type : "singleton",
13 extend : qx.core.Object,
14
15 construct : function(){
16 this.base(arguments);
17 this.setInitialDefinitions(qx.lang.Object.copy(this.getDefinitions()));
18 this.addListener("changedCommands", this.createCommands, this);
19 },
20
21 properties :
22 {
23 /**
24 * The commands definitions is a Map described as below
25 * <pre>
26 * {
27 * <b>label : "",</b>
28 * | The label of the action
29 *
30 * <b>icon : "",</b>
31 * | The icon image
32 *
33 * <b>shortcut : "",</b>
34 * | The keyboard shortcut, as defined in qooxdoo (Control+s, Alt+k, etc.). Warning, the letter must be lowercase.
35 *
36 * <b>enabled : true,</b>
37 * | Whether it is enabled or disabled at creation
38 *
39 * <b>menu : ""|null,</b>
40 * | The menu group to which the command will be added. If null, will not appear in the menus.
41 *
42 * <b>menuPosition : "first"|"last"</b>
43 * | Optional : force the menu group to be first or last in the menubar.
44 *
45 * <b>toolbar : ""|null,</b>
46 * | The toolbar group to which the command will be added. If null, will not appear in the toolbars.
47 *
48 * <b>init : function(){},</b>
49 * | Optional function called at command creation.
50 * | Function context : the command itself
51 *
52 * <b>callback : function(e){},</b>
53 * | The main callback to be triggered when command is executed.
54 * | Function context : the current class (not the command!)
55 *
56 * <b>selectionChange : function(viewPaneId, xmlNodes){},</b>
57 * | Optional function called each time a selectionChange is detected in one of the active viewPane.
58 * | The origin viewPaneId and the new selection as a map of nodes are passed as arguments.
59 * | Function context : the command itself.
60 *
61 * <b>submenu : [{label:"", icon:"", commandId:""}, ...],</b>
62 * | If set, the command will create a submenu, being in a menu or in the toolbar.
63 * | The submenu is created with the various array entries, and the submenuCallback function
64 * | will be called with the 'commandId' parameter when a submenu entry is selected.
65 *
66 * <b>submenuCallback : function(commandId){},</b>
67 * | Callback if command is a submenu (cf. above).
68 * | Function context : the current class/
69 *
70 * <b>command : null</b>
71 * | For internal use only, caching the actual org.argeo.ria.event.Command object.
72 * }
73 * </pre>
74 * @see org.argeo.ria.event.Command for the definition Map details.
75 */
76 definitions : {
77 init : {},
78 check : "Map"
79 },
80 /**
81 * For internal use
82 */
83 initialDefinitions : {
84 init : {},
85 check : "Map"
86 },
87 /**
88 * Special command definitions that are shared between focusable parts.
89 */
90 sharedDefinitions : {
91 init: {},
92 check: "Map"
93 }
94 },
95
96 events : {
97 /**
98 * Triggered when the whole commands list is changed. Mainly used internally by the manager.
99 */
100 "changedCommands" : "qx.event.type.Event"
101 },
102
103 /*
104 *****************************************************************************
105 MEMBERS
106 *****************************************************************************
107 */
108
109 members :
110 {
111 /**
112 * Initialize the manager with basic definitions.
113 * @param initDefinitions {Map} A map of commands definitions.
114 */
115 init : function(initDefinitions){
116 this.setDefinitions(initDefinitions);
117 this.setInitialDefinitions(qx.lang.Object.copy(initDefinitions));
118 },
119
120 /**
121 * Creates all the objects (if they are not already existing) from the definitions maps.
122 */
123 createCommands : function(){
124 this.menus = {};
125 this.toolbars = {};
126 var defs = this.getDefinitions();
127 var shared = this.getSharedDefinitions();
128 for(var key in defs){
129 var definition = defs[key];
130 var command;
131 if(!definition.command){
132 command = new org.argeo.ria.event.Command(key, definition.label, definition.icon, definition.shortcut);
133 if(definition.submenu){
134 command.setMenu(definition.submenu);
135 if(definition.submenuCallback){
136 command.setMenuCallback(definition.submenuCallback);
137 command.setMenuContext((definition.callbackContext?definition.callbackContext:null));
138 }
139 }
140 command.setEnabled(definition.enabled);
141 if(definition.toggle){
142 command.setToggle(true);
143 }
144 this._attachListener(command, definition.callback, definition.callbackContext);
145 if(definition.init){
146 var binded = qx.lang.Function.bind(definition.init, command);
147 binded();
148 }
149 definition.command = command;
150 }else{
151 command = definition.command;
152 if(shared[key]){
153
154 for(var focusPartId in shared[key]){
155 var sharedCommand = shared[key][focusPartId];
156 if(sharedCommand.callback){
157 var split = sharedCommand.callbackContext.split(":");
158 var focusPart = split[0];
159 var viewId = split[1];
160 command.registerCallback(sharedCommand.callback, split[1]);
161 //this._attachListener(command, sharedCommand.callback, sharedCommand.callbackContext);
162 }
163 }
164
165 }
166 }
167 if(definition.menu){
168 if(!this.menus[definition.menu]) this.menus[definition.menu] = [];
169 this.menus[definition.menu].push(definition);
170 }
171 if(definition.toolbar){
172 if(!this.toolbars[definition.toolbar]) this.toolbars[definition.toolbar] = [];
173 this.toolbars[definition.toolbar].push(command);
174 }
175 }
176 this.setDefinitions(defs);
177 },
178
179 /**
180 * Refresh the current commands status depending on the viewSelection.
181 * @param viewSelection {org.argeo.ria.components.ViewSelection} The current ViewSelection
182 */
183 refreshCommands : function(viewSelection){
184 var defs = this.getDefinitions();
185 var shared = this.getSharedDefinitions();
186 var xmlNodes = null;
187 if(viewSelection.getCount() > 0){
188 var xmlNodes = viewSelection.getNodes();
189 }
190 for(var key in defs){
191 var definition = defs[key];
192 if(!definition.selectionChange) continue;
193 if(shared[key]){
194 var currentFocus = org.argeo.ria.components.ViewsManager.getInstance().getCurrentFocus();
195 //this.debug(currentFocus);
196 if(!currentFocus) continue;
197 var sharedComm = shared[key][currentFocus.getViewId()];
198 if(sharedComm && sharedComm.selectionChange){
199 var binded = qx.lang.Function.bind(sharedComm.selectionChange, definition.command);
200 binded(viewSelection.getViewId(), xmlNodes);
201 }
202 }
203 var binded = qx.lang.Function.bind(definition.selectionChange, definition.command);
204 binded(viewSelection.getViewId(), xmlNodes);
205 }
206 },
207
208 /**
209 * Record a menubar for the application
210 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
211 */
212 registerMenuBar : function(menuBar){
213 this.addListener("changedCommands", function(){
214 this.createMenuButtons(menuBar);
215 }, this);
216 this.createMenuButtons(menuBar);
217 },
218
219 /**
220 * Record a toolbar for the application
221 * @param toolBar {qx.ui.toolbar.ToolBar} The application toolbar
222 */
223 registerToolBar : function(toolBar){
224 this.addListener("changedCommands", function(){
225 this.createToolbarParts(toolBar);
226 }, this);
227 this.createToolbarParts(toolBar);
228 },
229
230 /**
231 * Creates the real buttons and add them to the passed menuBar.
232 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
233 */
234 createMenuButtons : function(menuBar){
235 menuBar.removeAll();
236 var anchors = {};
237 for(var key in this.menus){
238 var menu = new qx.ui.menu.Menu();
239 var button = new qx.ui.menubar.Button(key, null, menu);
240 var anchorDetected = false;
241 for(var i=0; i<this.menus[key].length;i++){
242 var def = this.menus[key][i];
243 menu.add(def.command.getMenuButton());
244 if(!anchorDetected && def.menuPosition){
245 anchorDetected = true;
246 anchors[def.menuPosition] = button;
247 }
248 }
249 if(!anchorDetected){
250 menuBar.add(button);
251 }
252 }
253 // Add specific anchored buttons
254 if(anchors.first) menuBar.addAt(anchors.first, 0);
255 else if(anchors.last){
256 menuBar.add(anchors.last);
257 }
258 },
259
260 /**
261 * Creates the real buttons and add them to the passed toolbar.
262 * @param toolbar {qx.ui.toolbar.ToolBar} The application toolbar
263 */
264 createToolbarParts : function(toolbar){
265 toolbar.removeAll();
266 for(var key in this.toolbars){
267 var tPart = new qx.ui.toolbar.Part();
268 toolbar.add(tPart);
269 this.toolbars[key].map(function(command){
270 tPart.add(command.getToolbarButton());
271 });
272 }
273 },
274 /**
275 * Creates a context menu from an array of commands ids.
276 * @param commandIdsArray {Array} An array of string
277 * @return {qx.ui.menu.Menu}
278 */
279 createMenuFromIds : function(commandIdsArray){
280 var defs = this.getDefinitions();
281 var contextMenu = new qx.ui.menu.Menu();
282 for(var i=0;i<commandIdsArray.length;i++){
283 var definition = defs[commandIdsArray[i]];
284 if(definition){
285 var command = definition.command;
286 contextMenu.add(command.getMenuButton());
287 }
288 }
289 return contextMenu;
290 },
291 /**
292 * Add a new set of commands definitions. See the definitions property of this class.
293 * @param definitions {Map} a set of commands definitions.
294 * @param callbackContext {qx.ui.core.Object} The context used inside the commands callbacks.
295 * @param focusablePartId {String} A string identifying the associated focusable part, like "view:viewId".
296 */
297 addCommands : function(definitions, callbackContext, focusablePartId){
298 var crtDefs = this.getDefinitions();
299 for(var key in definitions){
300 if(callbackContext) definitions[key]['callbackContext'] = callbackContext;
301 if(crtDefs[key] && definitions[key]['shared']){
302 if(focusablePartId) {
303 definitions[key]['focusablePartId'] = focusablePartId;
304 if(!this.getSharedDefinitions()[key]){
305 this.getSharedDefinitions()[key] = {};
306 }
307 this.getSharedDefinitions()[key][focusablePartId] = definitions[key];
308 }
309
310 }else{
311 crtDefs[key] = definitions[key];
312 }
313 }
314 this.setDefinitions(crtDefs);
315 this.fireEvent("changedCommands");
316 },
317 /**
318 * Removes a whole set of commands by their definitions maps.
319 * @param definitions {Map} a set of commands definitions
320 * @param focusablePartId {String} A string identifying the associated focusable part, like "view:viewId".
321 */
322 removeCommands : function(definitions, focusablePartId){
323 var crtDefs = this.getDefinitions();
324 var initDefs = this.getInitialDefinitions();
325 var sharedDefs = this.getSharedDefinitions();
326 for(var key in definitions){
327 if(!crtDefs[key]) continue;
328 if(initDefs[key]){
329 crtDefs[key] = initDefs[key];
330 }else{
331 if(sharedDefs[key] && sharedDefs[key][focusablePartId]){
332 crtDefs[key].command.removeCallback(focusablePartId);
333 delete sharedDefs[key][focusablePartId];
334 }else{
335 delete crtDefs[key];
336 }
337 }
338 }
339 this.setDefinitions(crtDefs);
340 this.fireEvent("changedCommands");
341 },
342 /**
343 * Executes a command by its id.
344 * @param commandId {String} The command id.
345 */
346 executeCommand : function(commandId){
347 var defs = this.getDefinitions();
348 if(defs[commandId] && defs[commandId].command.getEnabled()){
349 defs[commandId].command.execute();
350 }
351 },
352 /**
353 * Retrieves a command by its id.
354 * @param commandId {String} The command id.
355 */
356 getCommandById : function(commandId){
357 var defs = this.getDefinitions();
358 if(defs[commandId] && defs[commandId].command){
359 return defs[commandId].command;
360 }
361 },
362 /**
363 * Add a standard context menu to a toolbar for button look and feel (show icon, text, both).
364 * @param toolbar {qx.ui.toolbar.ToolBar} The toolbar
365 */
366 addToolbarContextMenu : function(toolbar){
367 var menu = new qx.ui.menu.Menu();
368 var icon = new qx.ui.menu.RadioButton("Show Icons");
369 icon.setValue("icon");
370 var text = new qx.ui.menu.RadioButton("Show Text");
371 text.setValue("label");
372 var both = new qx.ui.menu.RadioButton("Show Both");
373 both.setValue("both");
374 var mgr = new qx.ui.form.RadioGroup(icon, text, both);
375 menu.add(icon);
376 menu.add(text);
377 menu.add(both);
378 mgr.setSelected(both);
379 toolbar.setContextMenu(menu);
380 mgr.addListener("changeValue", function(e){
381 this.setShow(e.getData());
382 }, toolbar);
383
384 },
385 /**
386 * Attach a listener to a command, with a context.
387 * The context can be an object, a string like "view:viewId" or null.
388 * If a string, the viewPaneId content will be retrieved at runtime. If null, "this" will be used
389 * as default context.
390 * @param command {org.argeo.ria.event.Command} The command
391 * @param callback {Function} The function to execute
392 * @param callbackContext {Object|String} The context in which the function will be executed.
393 */
394 _attachListener:function(command, callback, callbackContext){
395 if(!callbackContext){
396 command.addListener("execute", callback, this);
397 return;
398 }
399 if(typeof(callbackContext) == "object"){
400 command.addListener("execute", callback, callbackContext);
401 return;
402 }
403 if(typeof(callbackContext) == "string"){
404
405 var split = callbackContext.split(":");
406 var focusPart = split[0];
407 var viewId = split[1];
408 if(command.getCallbacks()[viewId]) return;
409 command.registerCallback(callback, split[1]);
410 command.addListener("execute", function(event){
411 var target = event.getTarget();
412 var callbacks = target.getCallbacks();
413 if(qx.lang.Object.getLength(callbacks) == 0) return;
414 var view = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById(viewId).getContent();
415 if(qx.lang.Object.getLength(callbacks) == 1){
416 var binded = qx.lang.Function.bind(callbacks[qx.lang.Object.getKeys(callbacks)[0]], view);
417 binded(event);
418 return;
419 }
420 var currentFocus = org.argeo.ria.components.ViewsManager.getInstance().getCurrentFocus();
421 if(currentFocus && currentFocus.getViewId() && callbacks[currentFocus.getViewId()]){
422 var currentViewId = currentFocus.getViewId();
423 view = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById(currentViewId).getContent();
424 var binded = qx.lang.Function.bind(callbacks[currentFocus.getViewId()], view);
425 binded(event);
426 return;
427 }
428 });
429
430
431 /*
432 if(callbackContext.split(":")[0] == "view"){
433 var viewId = callbackContext.split(":")[1];
434 command.addListener("execute", function(event){
435 if(event.getTarget().getCheckFocusAtCallback()){
436 var currentFocus = org.argeo.ria.components.ViewsManager.getInstance().getCurrentFocus();
437 if(currentFocus.getViewId() != viewId) return;
438 }
439 var view = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById(viewId).getContent();
440 var binded = qx.lang.Function.bind(callback, view);
441 binded(event);
442 });
443 }else{
444 command.addListener("execute", callback, callbackContext);
445 }
446 */
447 }
448 }
449 }
450 });