X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.webapp%2Fsrc%2Fmain%2Fwebapp%2Fargeo-ria-src%2Fclass%2Forg%2Fargeo%2Fria%2Fevent%2FCommandsManager.js;h=6452a3a87f313761fed6a9bf8670ef52ff2b5f6e;hb=81d7470df58e4d921fea676145b6fd7a477f7248;hp=0f6f1182a9817b3df28e04edc9b632222be533f6;hpb=a10c083ba5bde2d7ebb466153c99858247ddb0aa;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/event/CommandsManager.js b/org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/event/CommandsManager.js index 0f6f1182a..6452a3a87 100644 --- a/org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/event/CommandsManager.js +++ b/org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/event/CommandsManager.js @@ -3,6 +3,7 @@ * thus can be called by any part of the application. * This will wire all the commands that can be defined dynamically by any IView, and add their * corresponding buttons to the application menubar and toolbars. + * See the "definitions" property documentation below for more info on how to define new commands. * * @author Charles du Jeu */ @@ -20,72 +21,78 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", properties : { /** - * Commands definitions + * The commands definitions is a Map described as below + *
+	 * {
+	 * 	label : "", 
+	 * 	 | The label of the action
+	 * 
+	 * 	icon	: "", 
+	 * 	 | The icon image
+	 * 
+	 * 	shortcut : "",
+	 * 	 | The keyboard shortcut, as defined in qooxdoo (Control+s, Alt+k, etc.). Warning, the letter must be lowercase.
+	 * 
+	 * 	enabled : true,
+	 * 	 | Whether it is enabled or disabled at creation
+	 * 
+	 * 	menu : ""|null,
+	 * 	 | The menu group to which the command will be added. If null, will not appear in the menus.
+	 * 
+	 * 	menuPosition : "first"|"last"
+	 *	 | Optional : force the menu group to be first or last in the menubar.
+	 *   
+	 * 	toolbar : ""|null,
+	 * 	 | The toolbar group to which the command will be added. If null, will not appear in the toolbars.
+	 * 
+	 * 	init : function(){},
+	 * 	 | Optional function called at command creation.
+	 * 	 | Function context : the command itself
+	 * 
+	 * 	callback : function(e){},
+	 * 	 | The main callback to be triggered when command is executed.
+	 * 	 | Function context : the current class (not the command!)
+	 *  
+	 * 	selectionChange : function(viewPaneId, xmlNodes){},
+	 * 	 | Optional function called each time a selectionChange is detected in one of the active viewPane.
+	 * 	 | The origin viewPaneId and the new selection as a map of nodes are passed as arguments.
+	 * 	 | Function context : the command itself.
+	 * 
+	 * 	submenu : [{label:"", icon:"", commandId:""}, ...],
+	 * 	 | If set, the command will create a submenu, being in a menu or in the toolbar.
+	 * 	 | The submenu is created with the various array entries, and the submenuCallback function
+	 * 	 | will be called with the 'commandId' parameter when a submenu entry is selected.
+	 * 
+	 * 	submenuCallback : function(commandId){},
+	 * 	 | Callback if command is a submenu (cf. above).
+	 * 	 | Function context : the current class/
+	 * 
+	 * 	command : null
+	 * 	 | For internal use only, caching the actual org.argeo.ria.event.Command object.
+	 * }
+	 * 
* @see org.argeo.ria.event.Command for the definition Map details. */ definitions : { - init : { - "stop" : { - label : "Stop", - icon : "resource/slc/process-stop.png", - shortcut : "Control+s", - enabled : false, - menu : null, - toolbar : "list", - callback : function(e){}, - command : null - }, - /* - "quit" : { - label : "Quit", - icon : "resource/slc/system-shutdown.png", - shortcut : "Control+q", - enabled : true, - menu : "File", - toolbar : false, - callback : function(e){}, - command : null - }, - */ - "log" : { - label : "Show Console", - icon : "resource/slc/help-contents.png", - shortcut : "", - enabled : true, - menu : "Help", - menuPosition: "last", - toolbar : false, - callback : function(e){ - org.argeo.ria.components.Logger.getInstance().toggle(); - }, - command : null - }, - "help" : { - label : "About...", - icon : "resource/slc/help-about.png", - shortcut : "Control+h", - enabled : true, - menu : "Help", - toolbar : false, - callback : function(e){ - var win = new org.argeo.ria.components.Modal("About SLC", null, "SLC is a product from Argeo."); - win.attachAndShow(); - }, - command : null - } - } + init : {}, + check : "Map" }, /** * For internal use */ initialDefinitions : { - init : {} + init : {}, + check : "Map" + }, + sharedDefinitions : { + init: {}, + check: "Map" } }, events : { /** - * Triggered when the whole commands list is changed. + * Triggered when the whole commands list is changed. Mainly used internally by the manager. */ "changedCommands" : "qx.event.type.Event" }, @@ -98,6 +105,11 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", members : { + init : function(initDefinitions){ + this.setDefinitions(initDefinitions); + this.setInitialDefinitions(qx.lang.Object.copy(initDefinitions)); + }, + /** * Creates all the objects (if they are not already existing) from the definitions maps. */ @@ -105,6 +117,7 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", this.menus = {}; this.toolbars = {}; var defs = this.getDefinitions(); + var shared = this.getSharedDefinitions(); for(var key in defs){ var definition = defs[key]; var command; @@ -121,7 +134,7 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", if(definition.toggle){ command.setToggle(true); } - command.addListener("execute", definition.callback, (definition.callbackContext?definition.callbackContext:this)); + this._attachListener(command, definition.callback, definition.callbackContext); if(definition.init){ var binded = qx.lang.Function.bind(definition.init, command); binded(); @@ -129,6 +142,20 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", definition.command = command; }else{ command = definition.command; + if(shared[key]){ + + for(var focusPartId in shared[key]){ + var sharedCommand = shared[key][focusPartId]; + if(sharedCommand.callback){ + var split = sharedCommand.callbackContext.split(":"); + var focusPart = split[0]; + var viewId = split[1]; + command.registerCallback(sharedCommand.callback, split[1]); + //this._attachListener(command, sharedCommand.callback, sharedCommand.callbackContext); + } + } + + } } if(definition.menu){ if(!this.menus[definition.menu]) this.menus[definition.menu] = []; @@ -148,6 +175,7 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", */ refreshCommands : function(viewSelection){ var defs = this.getDefinitions(); + var shared = this.getSharedDefinitions(); var xmlNodes = null; if(viewSelection.getCount() > 0){ var xmlNodes = viewSelection.getNodes(); @@ -155,6 +183,15 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", for(var key in defs){ var definition = defs[key]; if(!definition.selectionChange) continue; + if(shared[key]){ + var currentFocus = org.argeo.ria.components.ViewsManager.getInstance().getCurrentFocus(); + if(!currentFocus) continue; + var sharedComm = shared[key][currentFocus.getViewId()]; + if(sharedComm && sharedComm.selectionChange){ + var binded = qx.lang.Function.bind(sharedComm.selectionChange, definition.command); + binded(viewSelection.getViewId(), xmlNodes); + } + } var binded = qx.lang.Function.bind(definition.selectionChange, definition.command); binded(viewSelection.getViewId(), xmlNodes); } @@ -244,15 +281,26 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", return contextMenu; }, /** - * Add a new set of commands definitions + * Add a new set of commands definitions. See the definitions property of this class. * @param definitions {Map} a set of commands definitions. - * @param callbackContext {qx.ui.core.Object} The context used inside the commands callbacks. + * @param callbackContext {qx.ui.core.Object} The context used inside the commands callbacks. */ - addCommands : function(definitions, callbackContext){ + addCommands : function(definitions, callbackContext, focusablePartId){ var crtDefs = this.getDefinitions(); for(var key in definitions){ if(callbackContext) definitions[key]['callbackContext'] = callbackContext; - crtDefs[key] = definitions[key]; + if(crtDefs[key] && definitions[key]['shared']){ + if(focusablePartId) { + definitions[key]['focusablePartId'] = focusablePartId; + if(!this.getSharedDefinitions()[key]){ + this.getSharedDefinitions()[key] = {}; + } + this.getSharedDefinitions()[key][focusablePartId] = definitions[key]; + } + + }else{ + crtDefs[key] = definitions[key]; + } } this.setDefinitions(crtDefs); this.fireEvent("changedCommands"); @@ -261,15 +309,21 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", * Removes a whole set of commands by their definitions maps. * @param definitions {Map} a set of commands definitions */ - removeCommands : function(definitions){ + removeCommands : function(definitions, focusablePartId){ var crtDefs = this.getDefinitions(); var initDefs = this.getInitialDefinitions(); + var sharedDefs = this.getSharedDefinitions(); for(var key in definitions){ if(!crtDefs[key]) continue; if(initDefs[key]){ crtDefs[key] = initDefs[key]; }else{ - delete crtDefs[key]; + if(sharedDefs[key] && sharedDefs[key][focusablePartId]){ + crtDefs[key].command.removeCallback(focusablePartId); + delete sharedDefs[key][focusablePartId]; + }else{ + delete crtDefs[key]; + } } } this.setDefinitions(crtDefs); @@ -317,6 +371,68 @@ qx.Class.define("org.argeo.ria.event.CommandsManager", this.setShow(e.getData()); }, toolbar); + }, + /** + * Attach a listener to a command, with a context. + * The context can be an object, a string like "view:viewId" or null. + * If a string, the viewPaneId content will be retrieved at runtime. If null, "this" will be used + * as default context. + * @param command {org.argeo.ria.event.Command} The command + * @param callback {Function} The function to execute + * @param callbackContext {Object|String} The context in which the function will be executed. + */ + _attachListener:function(command, callback, callbackContext){ + if(!callbackContext){ + command.addListener("execute", callback, this); + return; + } + if(typeof(callbackContext) == "object"){ + command.addListener("execute", callback, callbackContext); + return; + } + if(typeof(callbackContext) == "string"){ + + var split = callbackContext.split(":"); + var focusPart = split[0]; + var viewId = split[1]; + if(command.getCallbacks()[viewId]) return; + command.registerCallback(callback, split[1]); + command.addListener("execute", function(event){ + var target = event.getTarget(); + var callbacks = target.getCallbacks(); + if(qx.lang.Object.getLength(callbacks) == 0) return; + var view = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById(viewId).getContent(); + if(qx.lang.Object.getLength(callbacks) == 1){ + var binded = qx.lang.Function.bind(callbacks[qx.lang.Object.getKeys(callbacks)[0]], view); + binded(event); + return; + } + var currentFocus = org.argeo.ria.components.ViewsManager.getInstance().getCurrentFocus(); + if(currentFocus && currentFocus.getViewId() && callbacks[currentFocus.getViewId()]){ + var binded = qx.lang.Function.bind(callbacks[currentFocus.getViewId()], view); + binded(event); + return; + } + }); + + + /* + if(callbackContext.split(":")[0] == "view"){ + var viewId = callbackContext.split(":")[1]; + command.addListener("execute", function(event){ + if(event.getTarget().getCheckFocusAtCallback()){ + var currentFocus = org.argeo.ria.components.ViewsManager.getInstance().getCurrentFocus(); + if(currentFocus.getViewId() != viewId) return; + } + var view = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById(viewId).getContent(); + var binded = qx.lang.Function.bind(callback, view); + binded(event); + }); + }else{ + command.addListener("execute", callback, callbackContext); + } + */ + } } } -}); \ No newline at end of file +});