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 16 matching lines...) Expand all Loading... |
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 | 35 |
36 this.classList.add('decorated'); | 36 this.classList.add('decorated'); |
| 37 this.setAttribute('role', 'menu'); |
37 this.hidden = true; // Hide the menu by default. | 38 this.hidden = true; // Hide the menu by default. |
38 | 39 |
39 // Decorate the children as menu items. | 40 // Decorate the children as menu items. |
40 var children = this.children; | 41 var children = this.children; |
41 for (var i = 0, child; child = children[i]; i++) { | 42 for (var i = 0, child; child = children[i]; i++) { |
42 cr.ui.decorate(child, MenuItem); | 43 cr.ui.decorate(child, MenuItem); |
43 } | 44 } |
44 }, | 45 }, |
45 | 46 |
46 /** | 47 /** |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 * @type {cr.ui.MenuItem} | 117 * @type {cr.ui.MenuItem} |
117 */ | 118 */ |
118 get selectedItem() { | 119 get selectedItem() { |
119 return this.children[this.selectedIndex]; | 120 return this.children[this.selectedIndex]; |
120 }, | 121 }, |
121 set selectedItem(item) { | 122 set selectedItem(item) { |
122 var index = Array.prototype.indexOf.call(this.children, item); | 123 var index = Array.prototype.indexOf.call(this.children, item); |
123 this.selectedIndex = index; | 124 this.selectedIndex = index; |
124 }, | 125 }, |
125 | 126 |
| 127 focusSelectedItem: function() { |
| 128 if (this.selectedItem) |
| 129 this.selectedItem.focus(); |
| 130 }, |
| 131 |
126 /** | 132 /** |
127 * Menu length | 133 * Menu length |
128 */ | 134 */ |
129 get length() { | 135 get length() { |
130 return this.children.length; | 136 return this.children.length; |
131 }, | 137 }, |
132 | 138 |
133 /** | 139 /** |
134 * This is the function that handles keyboard navigation. This is usually | 140 * This is the function that handles keyboard navigation. This is usually |
135 * called by the element responsible for managing the menu. | 141 * called by the element responsible for managing the menu. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 switch (e.keyIdentifier) { | 179 switch (e.keyIdentifier) { |
174 case 'Down': | 180 case 'Down': |
175 selectNextAvailable(1); | 181 selectNextAvailable(1); |
176 return true; | 182 return true; |
177 case 'Up': | 183 case 'Up': |
178 selectNextAvailable(-1); | 184 selectNextAvailable(-1); |
179 return true; | 185 return true; |
180 case 'Enter': | 186 case 'Enter': |
181 case 'U+0020': // Space | 187 case 'U+0020': // Space |
182 if (item) { | 188 if (item) { |
183 if (cr.dispatchSimpleEvent(item, 'activate', true, true)) { | 189 var activationEvent = cr.doc.createEvent('Event'); |
| 190 activationEvent.initEvent('activate', true, true); |
| 191 activationEvent.originalEvent = e; |
| 192 if (item.dispatchEvent(activationEvent)) { |
184 if (item.command) | 193 if (item.command) |
185 item.command.execute(); | 194 item.command.execute(); |
186 } | 195 } |
187 } | 196 } |
188 return true; | 197 return true; |
189 } | 198 } |
190 | 199 |
191 return false; | 200 return false; |
192 }, | 201 }, |
193 | 202 |
194 /** | 203 /** |
195 * Updates menu items command according to context. | 204 * Updates menu items command according to context. |
196 * @param {Node=} node Node for which to actuate commands state. | 205 * @param {Node=} node Node for which to actuate commands state. |
197 */ | 206 */ |
198 updateCommands: function(node) { | 207 updateCommands: function(node) { |
199 var children = this.children; | 208 var children = this.children; |
200 | 209 |
201 for (var i = 0, child; child = children[i]; i++) | 210 for (var i = 0, child; child = children[i]; i++) |
202 child.updateCommand(node); | 211 child.updateCommand(node); |
203 } | 212 } |
204 }; | 213 }; |
205 | 214 |
206 function selectedIndexChanged(selectedIndex, oldSelectedIndex) { | 215 function selectedIndexChanged(selectedIndex, oldSelectedIndex) { |
207 var oldSelectedItem = this.children[oldSelectedIndex]; | 216 var oldSelectedItem = this.children[oldSelectedIndex]; |
208 if (oldSelectedItem) | 217 if (oldSelectedItem) |
209 oldSelectedItem.selected = false; | 218 oldSelectedItem.selected = false; |
210 var item = this.selectedItem; | 219 var item = this.selectedItem; |
211 if (item) | 220 if (item) { |
212 item.selected = true; | 221 item.selected = true; |
| 222 this.focusSelectedItem(); |
| 223 } |
213 } | 224 } |
214 | 225 |
215 /** | 226 /** |
216 * The selected menu item. | 227 * The selected menu item. |
217 * @type {number} | 228 * @type {number} |
218 */ | 229 */ |
219 cr.defineProperty(Menu, 'selectedIndex', cr.PropertyKind.JS, | 230 cr.defineProperty(Menu, 'selectedIndex', cr.PropertyKind.JS, |
220 selectedIndexChanged); | 231 selectedIndexChanged); |
221 | 232 |
222 // Export | 233 // Export |
223 return { | 234 return { |
224 Menu: Menu | 235 Menu: Menu |
225 }; | 236 }; |
226 }); | 237 }); |
OLD | NEW |