Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 MenuItem = cr.ui.MenuItem; | 7 const MenuItem = cr.ui.MenuItem; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Creates a new menu element. | 10 * Creates a new menu element. |
| 11 * @param {Object=} opt_propertyBag Optional properties. | 11 * @param {Object=} opt_propertyBag Optional properties. |
| 12 * @constructor | 12 * @constructor |
| 13 * @extends {HTMLMenuElement} | 13 * @extends {HTMLMenuElement} |
| 14 */ | 14 */ |
| 15 var Menu = cr.ui.define('menu'); | 15 var Menu = cr.ui.define('menu'); |
| 16 | 16 |
| 17 Menu.prototype = { | 17 Menu.prototype = { |
| 18 __proto__: HTMLMenuElement.prototype, | 18 __proto__: HTMLMenuElement.prototype, |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Initializes the menu element. | 21 * Initializes the menu element. |
| 22 */ | 22 */ |
| 23 decorate: function() { | 23 decorate: function() { |
| 24 this.addEventListener('mouseover', this.handleMouseOver_); | 24 this.addEventListener('mouseover', this.handleMouseOver_); |
| 25 this.addEventListener('mouseout', this.handleMouseOut_); | 25 this.addEventListener('mouseout', this.handleMouseOut_); |
| 26 | 26 |
| 27 this.selectedIndex = -1; | |
| 28 | |
| 27 // Decorate the children as menu items. | 29 // Decorate the children as menu items. |
| 28 var children = this.children; | 30 var children = this.children; |
| 29 for (var i = 0, child; child = children[i]; i++) { | 31 for (var i = 0, child; child = children[i]; i++) { |
| 30 cr.ui.decorate(child, MenuItem); | 32 cr.ui.decorate(child, MenuItem); |
| 31 } | 33 } |
| 32 }, | 34 }, |
| 33 | 35 |
| 34 /** | 36 /** |
| 35 * Walks up the ancestors until a menu item belonging to this menu is found. | 37 * Walks up the ancestors until a menu item belonging to this menu is found. |
| 36 * @param {Element} el | 38 * @param {Element} el |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 57 /** | 59 /** |
| 58 * Handles mouseout events and deselects any selected item. | 60 * Handles mouseout events and deselects any selected item. |
| 59 * @param {Event} e The mouseout event. | 61 * @param {Event} e The mouseout event. |
| 60 * @private | 62 * @private |
| 61 */ | 63 */ |
| 62 handleMouseOut_: function(e) { | 64 handleMouseOut_: function(e) { |
| 63 this.selectedItem = null; | 65 this.selectedItem = null; |
| 64 }, | 66 }, |
| 65 | 67 |
| 66 /** | 68 /** |
| 67 * The index of the selected item. | 69 * Called by cr.defineProperty when the selectedIndex property changes. |
| 68 * @type {boolean} | 70 * @type {boolean} |
| 69 */ | 71 */ |
| 70 // getter and default value is defined using cr.defineProperty. | 72 set _selectedIndexChange(selectedIndex,oldSelectedIndex) { |
|
arv (Not doing code reviews)
2011/02/10 20:18:31
Google style is to use trailing underscore
arv (Not doing code reviews)
2011/02/10 20:18:31
whitespace after ,
arv (Not doing code reviews)
2011/02/10 20:18:31
Shouldn't this be a function to work as set hook?
nduca
2011/02/10 20:47:17
Wow, major fail on my part. :)
On 2011/02/10 20:1
| |
| 71 set selectedIndex(selectedIndex) { | 73 var oldSelectedItem = this.chidren[oldSelectedIndex]; |
| 72 if (this.selectedIndex_ != selectedIndex) { | 74 if (oldSelectedItem) |
| 73 var oldSelectedItem = this.selectedItem; | 75 oldSelectedItem.selected = false; |
| 74 this.selectedIndex_ = selectedIndex; | 76 var item = this.selectedItem; |
| 75 if (oldSelectedItem) | 77 if (item) |
| 76 oldSelectedItem.selected = false; | 78 item.selected = true; |
| 77 var item = this.selectedItem; | |
| 78 if (item) | |
| 79 item.selected = true; | |
| 80 | |
| 81 cr.dispatchSimpleEvent(this, 'change'); | |
| 82 } | |
| 83 }, | 79 }, |
| 84 | 80 |
| 85 /** | 81 /** |
| 86 * The selected menu item or null if none. | 82 * The selected menu item or null if none. |
| 87 * @type {cr.ui.MenuItem} | 83 * @type {cr.ui.MenuItem} |
| 88 */ | 84 */ |
| 89 get selectedItem() { | 85 get selectedItem() { |
| 90 return this.children[this.selectedIndex]; | 86 return this.children[this.selectedIndex]; |
| 91 }, | 87 }, |
| 92 set selectedItem(item) { | 88 set selectedItem(item) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 } | 137 } |
| 142 | 138 |
| 143 return false; | 139 return false; |
| 144 } | 140 } |
| 145 }; | 141 }; |
| 146 | 142 |
| 147 /** | 143 /** |
| 148 * The selected menu item. | 144 * The selected menu item. |
| 149 * @type {number} | 145 * @type {number} |
| 150 */ | 146 */ |
| 151 cr.defineProperty(Menu, 'selectedIndex', cr.PropertyKind.JS, -1); | 147 cr.defineProperty(Menu, 'selectedIndex', cr.PropertyKind.JS, |
| 148 Menu.prototype._selectedIndexChange); | |
|
arv (Not doing code reviews)
2011/02/10 20:18:31
selectedIndexChanged
| |
| 152 | 149 |
| 153 // Export | 150 // Export |
| 154 return { | 151 return { |
| 155 Menu: Menu | 152 Menu: Menu |
| 156 }; | 153 }; |
| 157 }); | 154 }); |
| OLD | NEW |