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 | 6 |
7 /** @const */ var MenuItem = cr.ui.MenuItem; | 7 /** @const */ var MenuItem = cr.ui.MenuItem; |
8 | 8 |
9 /** | 9 /** |
10 * Creates a new menu element. Menu dispatches all commands on the element it | 10 * Creates a new menu element. Menu dispatches all commands on the element it |
(...skipping 14 matching lines...) Expand all Loading... | |
25 * Element for which menu is being shown. | 25 * Element for which menu is being shown. |
26 */ | 26 */ |
27 contextElement: null, | 27 contextElement: null, |
28 | 28 |
29 /** | 29 /** |
30 * Initializes the menu element. | 30 * Initializes the menu element. |
31 */ | 31 */ |
32 decorate: function() { | 32 decorate: function() { |
33 this.addEventListener('mouseover', this.handleMouseOver_); | 33 this.addEventListener('mouseover', this.handleMouseOver_); |
34 this.addEventListener('mouseout', this.handleMouseOut_); | 34 this.addEventListener('mouseout', this.handleMouseOut_); |
35 this.addEventListener('mouseup', this.handleMouseUp_, true); | |
35 | 36 |
36 this.classList.add('decorated'); | 37 this.classList.add('decorated'); |
37 this.setAttribute('role', 'menu'); | 38 this.setAttribute('role', 'menu'); |
38 this.hidden = true; // Hide the menu by default. | 39 this.hidden = true; // Hide the menu by default. |
39 | 40 |
40 // Decorate the children as menu items. | 41 // Decorate the children as menu items. |
41 var menuItems = this.menuItems; | 42 var menuItems = this.menuItems; |
42 for (var i = 0, menuItem; menuItem = menuItems[i]; i++) { | 43 for (var i = 0, menuItem; menuItem = menuItems[i]; i++) { |
43 cr.ui.decorate(menuItem, MenuItem); | 44 cr.ui.decorate(menuItem, MenuItem); |
44 } | 45 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 | 107 |
107 /** | 108 /** |
108 * Handles mouseout events and deselects any selected item. | 109 * Handles mouseout events and deselects any selected item. |
109 * @param {Event} e The mouseout event. | 110 * @param {Event} e The mouseout event. |
110 * @private | 111 * @private |
111 */ | 112 */ |
112 handleMouseOut_: function(e) { | 113 handleMouseOut_: function(e) { |
113 this.selectedItem = null; | 114 this.selectedItem = null; |
114 }, | 115 }, |
115 | 116 |
117 /** | |
118 * @param {Event} e | |
mustaq
2015/09/22 14:20:36
Please add a brief doc.
Dan Beam
2015/09/22 15:51:31
Done.
| |
119 */ | |
120 handleMouseUp_: function(e) { | |
121 assert(this.contains(/** @type {Element} */(e.target))); | |
122 | |
123 var mouseDownPos = this.shown_.mouseDownPos; | |
124 if (!mouseDownPos) | |
125 return; | |
126 | |
127 if (Date.now() - this.shown_.time > 200) | |
128 return; | |
129 | |
130 var xDelta = mouseDownPos.x - e.screenX; | |
131 var yDelta = mouseDownPos.y - e.screenY; | |
132 if (Math.sqrt(Math.pow(xDelta, 2) + Math.pow(yDelta, 2)) > 4) | |
mustaq
2015/09/22 14:20:36
What about a faster+simpler math? This is a heuris
Dan Beam
2015/09/22 15:51:30
Done.
| |
133 return; | |
134 | |
135 e.preventDefault(); | |
136 e.stopPropagation(); | |
137 }, | |
138 | |
116 get menuItems() { | 139 get menuItems() { |
117 return this.querySelectorAll(this.menuItemSelector || '*'); | 140 return this.querySelectorAll(this.menuItemSelector || '*'); |
118 }, | 141 }, |
119 | 142 |
120 /** | 143 /** |
121 * The selected menu item or null if none. | 144 * The selected menu item or null if none. |
122 * @type {cr.ui.MenuItem} | 145 * @type {cr.ui.MenuItem} |
123 */ | 146 */ |
124 get selectedItem() { | 147 get selectedItem() { |
125 return this.menuItems[this.selectedIndex]; | 148 return this.menuItems[this.selectedIndex]; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 if (item.command) | 254 if (item.command) |
232 item.command.execute(contextElement); | 255 item.command.execute(contextElement); |
233 } | 256 } |
234 } | 257 } |
235 return true; | 258 return true; |
236 } | 259 } |
237 | 260 |
238 return false; | 261 return false; |
239 }, | 262 }, |
240 | 263 |
264 hide: function() { | |
265 this.hidden = true; | |
266 delete this.shown_; | |
267 }, | |
268 | |
269 /** @param {{x: number, y: number}=} opt_mouseDownPos */ | |
270 show: function(opt_mouseDownPos) { | |
271 this.shown_ = {mouseDownPos: opt_mouseDownPos, time: Date.now()}; | |
272 this.hidden = false; | |
273 }, | |
274 | |
241 /** | 275 /** |
242 * Updates menu items command according to context. | 276 * Updates menu items command according to context. |
243 * @param {Node=} node Node for which to actuate commands state. | 277 * @param {Node=} node Node for which to actuate commands state. |
244 */ | 278 */ |
245 updateCommands: function(node) { | 279 updateCommands: function(node) { |
246 var menuItems = this.menuItems; | 280 var menuItems = this.menuItems; |
247 | 281 |
248 for (var i = 0, menuItem; menuItem = menuItems[i]; i++) { | 282 for (var i = 0, menuItem; menuItem = menuItems[i]; i++) { |
249 if (!menuItem.isSeparator()) | 283 if (!menuItem.isSeparator()) |
250 menuItem.updateCommand(node); | 284 menuItem.updateCommand(node); |
(...skipping 22 matching lines...) Expand all Loading... | |
273 /** | 307 /** |
274 * Selector for children which are menu items. | 308 * Selector for children which are menu items. |
275 */ | 309 */ |
276 cr.defineProperty(Menu, 'menuItemSelector', cr.PropertyKind.ATTR); | 310 cr.defineProperty(Menu, 'menuItemSelector', cr.PropertyKind.ATTR); |
277 | 311 |
278 // Export | 312 // Export |
279 return { | 313 return { |
280 Menu: Menu | 314 Menu: Menu |
281 }; | 315 }; |
282 }); | 316 }); |
OLD | NEW |