OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('cr.ui', function() { | 5 cr.define('cr.ui', function() { |
6 /** @const */ var Command = cr.ui.Command; | 6 /** @const */ var Command = cr.ui.Command; |
7 | 7 |
8 /** | 8 /** |
9 * Creates a new menu item element. | 9 * Creates a new menu item element. |
10 * @param {Object=} opt_propertyBag Optional properties. | 10 * @param {Object=} opt_propertyBag Optional properties. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 this.iconUrl = iconUrl; | 51 this.iconUrl = iconUrl; |
52 }, | 52 }, |
53 | 53 |
54 /** | 54 /** |
55 * The command associated with this menu item. If this is set to a string | 55 * The command associated with this menu item. If this is set to a string |
56 * of the form "#element-id" then the element is looked up in the document | 56 * of the form "#element-id" then the element is looked up in the document |
57 * of the command. | 57 * of the command. |
58 * @type {cr.ui.Command} | 58 * @type {cr.ui.Command} |
59 */ | 59 */ |
60 command_: null, | 60 command_: null, |
61 get command() { | 61 get command() { return this.command_; }, |
62 return this.command_; | |
63 }, | |
64 set command(command) { | 62 set command(command) { |
65 if (this.command_) { | 63 if (this.command_) { |
66 this.command_.removeEventListener('labelChange', this); | 64 this.command_.removeEventListener('labelChange', this); |
67 this.command_.removeEventListener('disabledChange', this); | 65 this.command_.removeEventListener('disabledChange', this); |
68 this.command_.removeEventListener('hiddenChange', this); | 66 this.command_.removeEventListener('hiddenChange', this); |
69 this.command_.removeEventListener('checkedChange', this); | 67 this.command_.removeEventListener('checkedChange', this); |
70 } | 68 } |
71 | 69 |
72 if (typeof command == 'string' && command[0] == '#') { | 70 if (typeof command == 'string' && command[0] == '#') { |
73 command = assert(this.ownerDocument.getElementById(command.slice(1))); | 71 command = assert(this.ownerDocument.getElementById(command.slice(1))); |
(...skipping 17 matching lines...) Expand all Loading... |
91 this.command_.addEventListener('checkedChange', this); | 89 this.command_.addEventListener('checkedChange', this); |
92 } | 90 } |
93 | 91 |
94 this.updateShortcut_(); | 92 this.updateShortcut_(); |
95 }, | 93 }, |
96 | 94 |
97 /** | 95 /** |
98 * The text label. | 96 * The text label. |
99 * @type {string} | 97 * @type {string} |
100 */ | 98 */ |
101 get label() { | 99 get label() { return this.textContent; }, |
102 return this.textContent; | 100 set label(label) { this.textContent = label; }, |
103 }, | |
104 set label(label) { | |
105 this.textContent = label; | |
106 }, | |
107 | 101 |
108 /** | 102 /** |
109 * Menu icon. | 103 * Menu icon. |
110 * @type {string} | 104 * @type {string} |
111 */ | 105 */ |
112 get iconUrl() { | 106 get iconUrl() { return this.style.backgroundImage; }, |
113 return this.style.backgroundImage; | 107 set iconUrl(url) { this.style.backgroundImage = 'url(' + url + ')'; }, |
114 }, | |
115 set iconUrl(url) { | |
116 this.style.backgroundImage = 'url(' + url + ')'; | |
117 }, | |
118 | 108 |
119 /** | 109 /** |
120 * @return {boolean} Whether the menu item is a separator. | 110 * @return {boolean} Whether the menu item is a separator. |
121 */ | 111 */ |
122 isSeparator: function() { | 112 isSeparator: function() { return this.tagName == 'HR'; }, |
123 return this.tagName == 'HR'; | |
124 }, | |
125 | 113 |
126 /** | 114 /** |
127 * Updates shortcut text according to associated command. If command has | 115 * Updates shortcut text according to associated command. If command has |
128 * multiple shortcuts, only first one is displayed. | 116 * multiple shortcuts, only first one is displayed. |
129 */ | 117 */ |
130 updateShortcut_: function() { | 118 updateShortcut_: function() { |
131 this.removeAttribute('shortcutText'); | 119 this.removeAttribute('shortcutText'); |
132 | 120 |
133 if (!this.command_ || | 121 if (!this.command_ || !this.command_.shortcut || |
134 !this.command_.shortcut || | |
135 this.command_.hideShortcutText) | 122 this.command_.hideShortcutText) |
136 return; | 123 return; |
137 | 124 |
138 var shortcuts = this.command_.shortcut.split(/\s+/); | 125 var shortcuts = this.command_.shortcut.split(/\s+/); |
139 | 126 |
140 if (shortcuts.length == 0) | 127 if (shortcuts.length == 0) |
141 return; | 128 return; |
142 | 129 |
143 var shortcut = shortcuts[0]; | 130 var shortcut = shortcuts[0]; |
144 var mods = {}; | 131 var mods = {}; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 this.setAttribute('shortcutText', shortcutText); | 165 this.setAttribute('shortcutText', shortcutText); |
179 }, | 166 }, |
180 | 167 |
181 /** | 168 /** |
182 * Handles mouseup events. This dispatches an activate event; if there is an | 169 * Handles mouseup events. This dispatches an activate event; if there is an |
183 * associated command, that command is executed. | 170 * associated command, that command is executed. |
184 * @param {!Event} e The mouseup event object. | 171 * @param {!Event} e The mouseup event object. |
185 * @private | 172 * @private |
186 */ | 173 */ |
187 handleMouseUp_: function(e) { | 174 handleMouseUp_: function(e) { |
188 e = /** @type {!MouseEvent} */(e); | 175 e = /** @type {!MouseEvent} */ (e); |
189 // Only dispatch an activate event for left or middle click. | 176 // Only dispatch an activate event for left or middle click. |
190 if (e.button > 1) | 177 if (e.button > 1) |
191 return; | 178 return; |
192 | 179 |
193 if (!this.disabled && !this.isSeparator() && this.selected) { | 180 if (!this.disabled && !this.isSeparator() && this.selected) { |
194 // Store |contextElement| since it'll be removed by {Menu} on handling | 181 // Store |contextElement| since it'll be removed by {Menu} on handling |
195 // 'activate' event. | 182 // 'activate' event. |
196 var contextElement = /** @type {{contextElement: Element}} */( | 183 var contextElement = |
197 this.parentNode).contextElement; | 184 /** @type {{contextElement: Element}} */ (this.parentNode) |
| 185 .contextElement; |
198 var activationEvent = cr.doc.createEvent('Event'); | 186 var activationEvent = cr.doc.createEvent('Event'); |
199 activationEvent.initEvent('activate', true, true); | 187 activationEvent.initEvent('activate', true, true); |
200 activationEvent.originalEvent = e; | 188 activationEvent.originalEvent = e; |
201 // Dispatch command event followed by executing the command object. | 189 // Dispatch command event followed by executing the command object. |
202 if (this.dispatchEvent(activationEvent)) { | 190 if (this.dispatchEvent(activationEvent)) { |
203 var command = this.command; | 191 var command = this.command; |
204 if (command) { | 192 if (command) { |
205 command.execute(contextElement); | 193 command.execute(contextElement); |
206 cr.ui.swallowDoubleClick(e); | 194 cr.ui.swallowDoubleClick(e); |
207 } | 195 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 * Whether the menu item is checked or not. | 248 * Whether the menu item is checked or not. |
261 */ | 249 */ |
262 cr.defineProperty(MenuItem, 'checked', cr.PropertyKind.BOOL_ATTR); | 250 cr.defineProperty(MenuItem, 'checked', cr.PropertyKind.BOOL_ATTR); |
263 | 251 |
264 /** | 252 /** |
265 * Whether the menu item is checkable or not. | 253 * Whether the menu item is checkable or not. |
266 */ | 254 */ |
267 cr.defineProperty(MenuItem, 'checkable', cr.PropertyKind.BOOL_ATTR); | 255 cr.defineProperty(MenuItem, 'checkable', cr.PropertyKind.BOOL_ATTR); |
268 | 256 |
269 // Export | 257 // Export |
270 return { | 258 return {MenuItem: MenuItem}; |
271 MenuItem: MenuItem | |
272 }; | |
273 }); | 259 }); |
OLD | NEW |