]>
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
573c11ff642fe80da8643aba2bf25336ab8689a6
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
: {
91 * Triggered when the whole commands list is changed. Mainly used internally by the manager.
93 "changedCommands" : "qx.event.type.Event"
97 *****************************************************************************
99 *****************************************************************************
104 init : function(initDefinitions
){
105 this.setDefinitions(initDefinitions
);
106 this.setInitialDefinitions(qx
.lang
.Object
.copy(initDefinitions
));
110 * Creates all the objects (if they are not already existing) from the definitions maps.
112 createCommands : function(){
115 var defs
= this.getDefinitions();
116 for(var key
in defs
){
117 var definition
= defs
[key
];
119 if(!definition
.command
){
120 command
= new org
.argeo
.ria
.event
.Command(key
, definition
.label
, definition
.icon
, definition
.shortcut
);
121 if(definition
.submenu
){
122 command
.setMenu(definition
.submenu
);
123 if(definition
.submenuCallback
){
124 command
.setMenuCallback(definition
.submenuCallback
);
125 command
.setMenuContext((definition
.callbackContext
?definition
.callbackContext
:null));
128 command
.setEnabled(definition
.enabled
);
129 if(definition
.toggle
){
130 command
.setToggle(true);
132 this._attachListener(command
, definition
.callback
, definition
.callbackContext
);
134 var binded
= qx
.lang
.Function
.bind(definition
.init
, command
);
137 definition
.command
= command
;
139 command
= definition
.command
;
142 if(!this.menus
[definition
.menu
]) this.menus
[definition
.menu
] = [];
143 this.menus
[definition
.menu
].push(definition
);
145 if(definition
.toolbar
){
146 if(!this.toolbars
[definition
.toolbar
]) this.toolbars
[definition
.toolbar
] = [];
147 this.toolbars
[definition
.toolbar
].push(command
);
150 this.setDefinitions(defs
);
154 * Refresh the current commands status depending on the viewSelection.
155 * @param viewSelection {org.argeo.ria.components.ViewSelection} The current ViewSelection
157 refreshCommands : function(viewSelection
){
158 var defs
= this.getDefinitions();
160 if(viewSelection
.getCount() > 0){
161 var xmlNodes
= viewSelection
.getNodes();
163 for(var key
in defs
){
164 var definition
= defs
[key
];
165 if(!definition
.selectionChange
) continue;
166 var binded
= qx
.lang
.Function
.bind(definition
.selectionChange
, definition
.command
);
167 binded(viewSelection
.getViewId(), xmlNodes
);
172 * Record a menubar for the application
173 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
175 registerMenuBar : function(menuBar
){
176 this.addListener("changedCommands", function(){
177 this.createMenuButtons(menuBar
);
179 this.createMenuButtons(menuBar
);
183 * Record a toolbar for the application
184 * @param toolBar {qx.ui.toolbar.ToolBar} The application toolbar
186 registerToolBar : function(toolBar
){
187 this.addListener("changedCommands", function(){
188 this.createToolbarParts(toolBar
);
190 this.createToolbarParts(toolBar
);
194 * Creates the real buttons and add them to the passed menuBar.
195 * @param menuBar {qx.ui.menubar.MenuBar} The application menubar
197 createMenuButtons : function(menuBar
){
200 for(var key
in this.menus
){
201 var menu
= new qx
.ui
.menu
.Menu();
202 var button
= new qx
.ui
.menubar
.Button(key
, null, menu
);
203 var anchorDetected
= false;
204 for(var i
=0; i
<this.menus
[key
].length
;i
++){
205 var def
= this.menus
[key
][i
];
206 menu
.add(def
.command
.getMenuButton());
207 if(!anchorDetected
&& def
.menuPosition
){
208 anchorDetected
= true;
209 anchors
[def
.menuPosition
] = button
;
216 // Add specific anchored buttons
217 if(anchors
.first
) menuBar
.addAt(anchors
.first
, 0);
218 else if(anchors
.last
){
219 menuBar
.add(anchors
.last
);
224 * Creates the real buttons and add them to the passed toolbar.
225 * @param toolbar {qx.ui.toolbar.ToolBar} The application toolbar
227 createToolbarParts : function(toolbar
){
229 for(var key
in this.toolbars
){
230 var tPart
= new qx
.ui
.toolbar
.Part();
232 this.toolbars
[key
].map(function(command
){
233 tPart
.add(command
.getToolbarButton());
238 * Creates a context menu from an array of commands ids.
239 * @param commandIdsArray {Array} An array of string
240 * @return {qx.ui.menu.Menu}
242 createMenuFromIds : function(commandIdsArray
){
243 var defs
= this.getDefinitions();
244 var contextMenu
= new qx
.ui
.menu
.Menu();
245 for(var i
=0;i
<commandIdsArray
.length
;i
++){
246 var definition
= defs
[commandIdsArray
[i
]];
248 var command
= definition
.command
;
249 contextMenu
.add(command
.getMenuButton());
255 * Add a new set of commands definitions. See the definitions property of this class.
256 * @param definitions {Map} a set of commands definitions.
257 * @param callbackContext {qx.ui.core.Object} The context used inside the commands callbacks.
259 addCommands : function(definitions
, callbackContext
){
260 var crtDefs
= this.getDefinitions();
261 for(var key
in definitions
){
262 if(callbackContext
) definitions
[key
]['callbackContext'] = callbackContext
;
263 crtDefs
[key
] = definitions
[key
];
265 this.setDefinitions(crtDefs
);
266 this.fireEvent("changedCommands");
269 * Removes a whole set of commands by their definitions maps.
270 * @param definitions {Map} a set of commands definitions
272 removeCommands : function(definitions
){
273 var crtDefs
= this.getDefinitions();
274 var initDefs
= this.getInitialDefinitions();
275 for(var key
in definitions
){
276 if(!crtDefs
[key
]) continue;
278 crtDefs
[key
] = initDefs
[key
];
283 this.setDefinitions(crtDefs
);
284 this.fireEvent("changedCommands");
287 * Executes a command by its id.
288 * @param commandId {String} The command id.
290 executeCommand : function(commandId
){
291 var defs
= this.getDefinitions();
292 if(defs
[commandId
] && defs
[commandId
].command
.getEnabled()){
293 defs
[commandId
].command
.execute();
297 * Retrieves a command by its id.
298 * @param commandId {String} The command id.
300 getCommandById : function(commandId
){
301 var defs
= this.getDefinitions();
302 if(defs
[commandId
] && defs
[commandId
].command
){
303 return defs
[commandId
].command
;
307 * Add a standard context menu to a toolbar for button look and feel (show icon, text, both).
308 * @param toolbar {qx.ui.toolbar.ToolBar} The toolbar
310 addToolbarContextMenu : function(toolbar
){
311 var menu
= new qx
.ui
.menu
.Menu();
312 var icon
= new qx
.ui
.menu
.RadioButton("Show Icons");
313 icon
.setValue("icon");
314 var text
= new qx
.ui
.menu
.RadioButton("Show Text");
315 text
.setValue("label");
316 var both
= new qx
.ui
.menu
.RadioButton("Show Both");
317 both
.setValue("both");
318 var mgr
= new qx
.ui
.form
.RadioGroup(icon
, text
, both
);
322 mgr
.setSelected(both
);
323 toolbar
.setContextMenu(menu
);
324 mgr
.addListener("changeValue", function(e
){
325 this.setShow(e
.getData());
330 * Attach a listener to a command, with a context.
331 * The context can be an object, a string like "view:viewId" or null.
332 * If a string, the viewPaneId content will be retrieved at runtime. If null, "this" will be used
333 * as default context.
334 * @param command {org.argeo.ria.event.Command} The command
335 * @param callback {Function} The function to execute
336 * @param callbackContext {Object|String} The context in which the function will be executed.
338 _attachListener:function(command
, callback
, callbackContext
){
339 if(!callbackContext
){
340 command
.addListener("execute", callback
, this);
343 if(typeof(callbackContext
) == "object"){
344 command
.addListener("execute", callback
, callbackContext
);
347 if(typeof(callbackContext
) == "string"){
348 if(callbackContext
.split(":")[0] == "view"){
349 var viewId
= callbackContext
.split(":")[1];
350 command
.addListener("execute", function(event
){
351 var view
= org
.argeo
.ria
.components
.ViewsManager
.getInstance().getViewPaneById(viewId
).getContent();
352 var binded
= qx
.lang
.Function
.bind(callback
, view
);
356 command
.addListener("execute", callback
, callbackContext
);