]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/argeo-ria-src/class/org/argeo/ria/event/Command.js
77abef851b132498c7de63304894f156e2091d12
2 * The standard command for all actions. It registers keyboard shortcuts, centralizes
3 * command state, callback, etc. It is defined by command definitions that can be found
4 * in the CommandsManager.
6 qx
.Class
.define("org.argeo.ria.event.Command",
8 extend
: qx
.event
.Command
,
9 implement
: [org
.argeo
.ria
.components
.ILoadStatusable
],
13 * Unique id of the command
17 * Label of the command
25 * Weather this command is a true/false state
27 toggle
: {init
:false},
29 * It toggle button, initial state
31 toggleInitialState
: {init
: false},
40 * Callback associated to the submenu of the command
42 menuCallback
: {nullable
:true},
44 * Context used when triggering menuCallback
46 menuContext
: {nullable
:true}
50 * @param id {String} Id of the command
51 * @param label {String} Label of the command
52 * @param icon {String} Icon of the command
53 * @param shortcut {String} Keyboard Shortcut (like alt+o, ctrl+z, etc..)
55 construct : function(id
, label
, icon
, shortcut
){
56 this.base(arguments
, shortcut
);
67 * Create a Button that suits a qx.ui.menu.MenuBar linked to this command
68 * @return {qx.ui.menu.Button}
70 getMenuButton : function(){
72 button
= new qx
.ui
.menu
.CheckBox(this.getLabel());
73 this._registerToggleButtonListeners(button
);
75 var button
= new qx
.ui
.menu
.Button(
82 this.addListener("changeMenu", function(event
){
83 button
.setMenu(this.getMenuClone());
87 this.addTooltip(button
);
92 * Create a Button that suits a qx.ui.toolbar.Toolbar part linked to this command.
93 * @return {qx.ui.toolbar.MenuButton}
95 getToolbarButton : function(){
98 button
= new qx
.ui
.toolbar
.MenuButton(
103 this.addListener("changeMenu", function(event
){
104 button
.setMenu(this.getMenuClone());
106 this.addListener("changeEnabled", function(e
){
107 this.setEnabled(e
.getData());
109 button
.setEnabled(this.getEnabled());
110 }else if(this.getToggle()){
111 button
= new qx
.ui
.toolbar
.CheckBox(this.getLabel(), this.getIcon());
112 if(this.getToggleInitialState()){
113 button
.setChecked(true);
115 this._registerToggleButtonListeners(button
);
117 button
= new qx
.ui
.toolbar
.Button(
123 this.addTooltip(button
);
128 * Register a given callback to be shared by one or more focusable part.
129 * @param callback {Function} A callback function
130 * @param focusablePartId {String} A string identifiing a focusable part. At the moment, it can only be "view:viewId"
132 registerCallback : function(callback
, focusablePartId
){
133 this.callbacks
[focusablePartId
] = callback
;
136 * Return all the registered callbacks for this command.
137 * @return {Map} A map of callback, viewId => callBack.
139 getCallbacks : function(){
140 return this.callbacks
;
143 * Remove a callback for a given focusable part.
144 * @param focusablePartId {String} A id like "view:viewId".
146 removeCallback : function(focusablePartId
){
147 if(this.callbacks
[focusablePartId
]){
148 delete this.callbacks
[focusablePartId
];
153 * Special tricks using UserData to enable/disable listeners to avoid loops...
154 * @param button {qx.ui.core.Widget} toolbar Checkbox or menu Checkbox button.
156 _registerToggleButtonListeners : function(button
){
157 button
.addListener("changeChecked", function(event
){
158 if(button
.getUserData("disableListener")) return;
159 this.setUserData("slc.command.toggleState", event
.getData());
160 this.setUserData("slc.command.toggleStateSource", button
);
161 this.fireEvent("execute");
163 this.addListener("execute", function(event
){
164 if(this.getUserData("slc.command.toggleStateSource") == button
) return;
165 button
.setUserData("disableListener", true);
166 button
.setChecked(this.getUserData("slc.command.toggleState"));
167 button
.setUserData("disableListener", false);
172 * Clones the command menu
173 * @return {qx.ui.menu.Menu}
175 getMenuClone : function(){
176 var menuClone
= new qx
.ui
.menu
.Menu();
177 menuClone
.setMinWidth(100);
178 var submenus
= this.getMenu();
179 if(!submenus
) return;
180 for(var i
=0;i
<submenus
.length
;i
++){
181 if(submenus
[i
].separator
){
182 menuClone
.add(new qx
.ui
.menu
.Separator());
184 var button
= new qx
.ui
.menu
.Button(submenus
[i
].label
, submenus
[i
].icon
);
185 if(submenus
[i
].disabled
){
186 button
.setEnabled(false);
188 button
.setUserData("commandId", submenus
[i
].commandId
);
189 button
.addListener("execute", this.executeSubMenuCallback
, this);
190 menuClone
.add(button
);
193 this.menuClones
.push(menuClone
);
198 * Remove all existing menus and their clones.
200 clearMenus : function(){
201 if(!this.getMenu()) return;
202 for(var i
=0;i
<this.menuClones
.length
;i
++){
203 this.menuClones
[i
].destroy();
205 this.menuClones
= [];
209 * Triggers the menuCallback property in the right context.
210 * @param event {qx.event.type.Event} The firing event.
212 executeSubMenuCallback : function(event
){
213 var button
= event
.getTarget();
214 var callback
= this.getMenuCallback();
216 if(this.getMenuContext()){
217 if(typeof(this.getMenuContext()) == "object") context
= this.getMenuContext();
218 else if(typeof(this.getMenuContext()) == "string"){
219 if(this.getMenuContext().split(":")[0] == "view"){
220 var viewId
= this.getMenuContext().split(":")[1];
221 context
= org
.argeo
.ria
.components
.ViewsManager
.getInstance().getViewPaneById(viewId
).getContent();
225 callback
= qx
.lang
.Function
.bind(callback
, context
|| this);
226 callback(button
.getUserData("commandId"));
229 * Adds a tooltip to a button.
230 * @param element {qx.ui.core.Widget} The element to which the command tooltip is added.
232 addTooltip : function(element
){
233 if(this.getShortcut() != null){
234 element
.setToolTip(new qx
.ui
.tooltip
.ToolTip(this.getShortcut()));
239 * Implementation of the ILoadStatusable interface.
240 * Sets the whole command enabled if not loading and disabled if loading.
241 * @param status {Boolean} The loading status of the button.
243 setOnLoad : function(status
){
244 this.setEnabled(!status
);