]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/source/class/org/argeo/ria/event/Command.js
83641c416a987c123263a93a791a464bd8a82669
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / source / class / org / argeo / ria / event / Command.js
1 /**
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.
5 */
6 qx.Class.define("org.argeo.ria.event.Command",
7 {
8 extend : qx.event.Command,
9 implement : [org.argeo.ria.components.ILoadStatusable],
10
11 properties : {
12 /**
13 * Unique id of the command
14 */
15 id : {init:""},
16 /**
17 * Label of the command
18 */
19 label : {init:""},
20 /**
21 * Icon of the command
22 */
23 icon : {init:""},
24 /**
25 * Sub menu if needed
26 */
27 menu : {
28 nullable: true,
29 event : "changeMenu"
30 },
31 /**
32 * Callback associated to the submenu of the command
33 */
34 menuCallback : {nullable:true},
35 /**
36 * Context used when triggering menuCallback
37 */
38 menuContext : {nullable:true}
39 },
40
41 /**
42 * @param id {String} Id of the command
43 * @param label {String} Label of the command
44 * @param icon {String} Icon of the command
45 * @param shortcut {String} Keyboard Shortcut (like alt+o, ctrl+z, etc..)
46 */
47 construct : function(id, label, icon, shortcut){
48 this.base(arguments, shortcut);
49 this.setId(id);
50 this.setLabel(label);
51 this.setIcon(icon);
52 this.menuClones = [];
53 },
54
55 members :
56 {
57 /**
58 * Create a Button that suits a qx.ui.menu.MenuBar linked to this command
59 * @return {qx.ui.menu.Button}
60 */
61 getMenuButton : function(){
62 var button = new qx.ui.menu.Button(
63 this.getLabel(),
64 this.getIcon(),
65 this,
66 this.getMenuClone()
67 );
68 this.addTooltip(button);
69 if(this.getMenu()){
70 this.addListener("changeMenu", function(event){
71 button.setMenu(this.getMenuClone());
72 }, this);
73 }
74 return button;
75 },
76
77 /**
78 * Create a Button that suits a qx.ui.toolbar.Toolbar part linked to this command.
79 * @return {qx.ui.toolbar.MenuButton}
80 */
81 getToolbarButton : function(){
82 var button;
83 if(this.getMenu()){
84 button = new qx.ui.toolbar.MenuButton(
85 this.getLabel(),
86 this.getIcon(),
87 this.getMenuClone()
88 );
89 this.addListener("changeMenu", function(event){
90 button.setMenu(this.getMenuClone());
91 }, this);
92 this.addListener("changeEnabled", function(e){
93 this.setEnabled(e.getData());
94 }, button);
95 button.setEnabled(this.getEnabled());
96 }else{
97 button = new qx.ui.toolbar.Button(
98 this.getLabel(),
99 this.getIcon(),
100 this
101 );
102 }
103 this.addTooltip(button);
104 return button;
105 },
106
107 /**
108 * Clones the command menu
109 * @return {qx.ui.menu.Menu}
110 */
111 getMenuClone : function(){
112 var menuClone = new qx.ui.menu.Menu();
113 var submenus = this.getMenu();
114 if(!submenus) return;
115 for(var i=0;i<submenus.length;i++){
116 var button = new qx.ui.menu.Button(submenus[i].label, submenus[i].icon);
117 button.setUserData("commandId", submenus[i].commandId);
118 button.addListener("execute", this.executeSubMenuCallback, this);
119 menuClone.add(button);
120 }
121 this.menuClones.push(menuClone);
122 return menuClone;
123 },
124
125 /**
126 * Remove all existing menus and their clones.
127 */
128 clearMenus : function(){
129 if(!this.getMenu()) return;
130 for(var i=0;i<this.menuClones.length;i++){
131 this.menuClones[i].destroy();
132 }
133 this.menuClones = [];
134 },
135
136 /**
137 * Triggers the menuCallback property in the right context.
138 * @param event {qx.event.type.Event} The firing event.
139 */
140 executeSubMenuCallback : function(event){
141 var button = event.getTarget();
142 var callback = this.getMenuCallback();
143 callback = qx.lang.Function.bind(callback, this.getMenuContext() || this);
144 callback(button.getUserData("commandId"));
145 },
146 /**
147 * Adds a tooltip to a button.
148 * @param element {qx.ui.core.Widget} The element to which the command tooltip is added.
149 */
150 addTooltip : function(element){
151 if(this.getShortcut() != null){
152 element.setToolTip(new qx.ui.tooltip.ToolTip(this.getShortcut()));
153 }
154 },
155
156 /**
157 * Implementation of the ILoadStatusable interface.
158 * Sets the whole command enabled if not loading and disabled if loading.
159 * @param status {Boolean} The loading status of the button.
160 */
161 setOnLoad : function(status){
162 this.setEnabled(!status);
163 }
164
165 }
166 });