]>
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
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.
8 * @author Charles du Jeu
10 qx
.Class
.define("org.argeo.ria.event.CommandsManager",
13 extend
: qx
.core
.Object
,
15 construct : function(){
17 this.setInitialDefinitions(qx
.lang
.Object
.copy(this.getDefinitions()));
18 this.addListener("changedCommands", this.createCommands
, this);
24 * The commands definitions is a Map described as below
28 * | The label of the action
33 * <b>shortcut : "",</b>
34 * | The keyboard shortcut, as defined in qooxdoo (Control+s, Alt+k, etc.). Warning, the letter must be lowercase.
36 * <b>enabled : true,</b>
37 * | Whether it is enabled or disabled at creation
39 * <b>menu : ""|null,</b>
40 * | The menu group to which the command will be added. If null, will not appear in the menus.
42 * <b>menuPosition : "first"|"last"</b>
43 * | Optional : force the menu group to be first or last in the menubar.
45 * <b>toolbar : ""|null,</b>
46 * | The toolbar group to which the command will be added. If null, will not appear in the toolbars.
48 * <b>init : function(){},</b>
49 * | Optional function called at command creation.
50 * | Function context : the command itself
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!)
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.
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.
66 * <b>submenuCallback : function(commandId){},</b>
67 * | Callback if command is a submenu (cf. above).
68 * | Function context : the current class/
70 * <b>command : null</b>
71 * | For internal use only, caching the actual org.argeo.ria.event.Command object.
74 * @see org.argeo.ria.event.Command for the definition Map details.
83 initialDefinitions
: {
88 * Special command definitions that are shared between focusable parts.
98 * Triggered when the whole commands list is changed. Mainly used internally by the manager.
100 "changedCommands" : "qx.event.type.Event"
104 *****************************************************************************
106 *****************************************************************************
112 * Initialize the manager with basic definitions.
113 * @param initDefinitions {Map} A map of commands definitions.
115 init : function(initDefinitions
){
116 this.setDefinitions(initDefinitions
);
117 this.setInitialDefinitions(qx
.lang
.Object
.copy(initDefinitions
));
121 * Creates all the objects (if they are not already existing) from the definitions maps.
123 createCommands : function(){
126 var defs
= this.getDefinitions();
127 var shared
= this.getSharedDefinitions();
128 for(var key
in defs
){
129 var definition
= defs
[key
];
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));
140 command
.setEnabled(definition
.enabled
);
141 if(definition
.toggle
){
142 command
.setToggle(true);
144 this._attachListener(command
, definition
.callback
, definition
.callbackContext
);
146 var binded
= qx
.lang
.Function
.bind(definition
.init
, command
);
149 definition
.command
= command
;
151 command
= definition
.command
;
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);
168 if(!this.menus
[definition
.menu
]) this.menus
[definition
.menu
] = [];
169 this.menus
[definition
.menu
].push(definition
);
171 if(definition
.toolbar
){
172 if(!this.toolbars
[definition
.toolbar
]) this.toolbars
[definition
.toolbar
] = [];
173 this.toolbars
[definition
.toolbar
].push(command
);
176 this.setDefinitions(defs
);
180 * Refresh the current commands status depending on the viewSelection.
181 * @param viewSelection {org.argeo.ria.components.ViewSelection} The current ViewSelection
183 refreshCommands : function(viewSelection
){
184 var defs
= this.getDefinitions();
185 var shared
= this.getSharedDefinitions();
187 if(viewSelection
.getCount() > 0){
188 var xmlNodes
= viewSelection
.getNodes();
190 for(var key
in defs
){
191 var definition
= defs
[key
];
192 if(!definition
.selectionChange
) continue;
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
);
203 var binded
= qx
.lang
.Function
.bind(definition
.selectionChange
, definition
.command
);
204 binded(viewSelection
.getViewId(), xmlNodes
);
209 * Record a menubar for the application
210 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
212 registerMenuBar : function(menuBar
){
213 this.addListener("changedCommands", function(){
214 this.createMenuButtons(menuBar
);
216 this.createMenuButtons(menuBar
);
220 * Record a toolbar for the application
221 * @param toolBar {qx.ui.toolbar.ToolBar} The application toolbar
223 registerToolBar : function(toolBar
){
224 this.addListener("changedCommands", function(){
225 this.createToolbarParts(toolBar
);
227 this.createToolbarParts(toolBar
);
231 * Creates the real buttons and add them to the passed menuBar.
232 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
234 createMenuButtons : function(menuBar
){
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
;
253 // Add specific anchored buttons
254 if(anchors
.first
) menuBar
.addAt(anchors
.first
, 0);
255 else if(anchors
.last
){
256 menuBar
.add(anchors
.last
);
261 * Creates the real buttons and add them to the passed toolbar.
262 * @param toolbar {qx.ui.toolbar.ToolBar} The application toolbar
264 createToolbarParts : function(toolbar
){
266 for(var key
in this.toolbars
){
267 var tPart
= new qx
.ui
.toolbar
.Part();
269 this.toolbars
[key
].map(function(command
){
270 tPart
.add(command
.getToolbarButton());
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}
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
]];
285 var command
= definition
.command
;
286 contextMenu
.add(command
.getMenuButton());
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".
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
] = {};
307 this.getSharedDefinitions()[key
][focusablePartId
] = definitions
[key
];
311 crtDefs
[key
] = definitions
[key
];
314 this.setDefinitions(crtDefs
);
315 this.fireEvent("changedCommands");
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".
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;
329 crtDefs
[key
] = initDefs
[key
];
331 if(sharedDefs
[key
] && sharedDefs
[key
][focusablePartId
]){
332 crtDefs
[key
].command
.removeCallback(focusablePartId
);
333 delete sharedDefs
[key
][focusablePartId
];
339 this.setDefinitions(crtDefs
);
340 this.fireEvent("changedCommands");
343 * Executes a command by its id.
344 * @param commandId {String} The command id.
346 executeCommand : function(commandId
){
347 var defs
= this.getDefinitions();
348 if(defs
[commandId
] && defs
[commandId
].command
.getEnabled()){
349 defs
[commandId
].command
.execute();
353 * Retrieves a command by its id.
354 * @param commandId {String} The command id.
356 getCommandById : function(commandId
){
357 var defs
= this.getDefinitions();
358 if(defs
[commandId
] && defs
[commandId
].command
){
359 return defs
[commandId
].command
;
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
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
);
378 mgr
.setSelected(both
);
379 toolbar
.setContextMenu(menu
);
380 mgr
.addListener("changeValue", function(e
){
381 this.setShow(e
.getData());
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.
394 _attachListener:function(command
, callback
, callbackContext
){
395 if(!callbackContext
){
396 command
.addListener("execute", callback
, this);
399 if(typeof(callbackContext
) == "object"){
400 command
.addListener("execute", callback
, callbackContext
);
403 if(typeof(callbackContext
) == "string"){
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
);
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
);
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;
439 var view = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById(viewId).getContent();
440 var binded = qx.lang.Function.bind(callback, view);
444 command.addListener("execute", callback, callbackContext);