OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 /** | 5 /** |
6 * @fileoverview Network drop-down implementation. | 6 * @fileoverview Network drop-down implementation. |
7 */ | 7 */ |
8 | 8 |
9 cr.define('cr.ui', function() { | 9 cr.define('cr.ui', function() { |
10 /** | 10 /** |
11 * Creates a new container for the drop down menu items. | 11 * Creates a new container for the drop down menu items. |
12 * @constructor | 12 * @constructor |
13 * @extends {HTMLDivElement} | 13 * @extends {HTMLDivElement} |
14 */ | 14 */ |
15 var DropDownContainer = cr.ui.define('div'); | 15 var DropDownContainer = cr.ui.define('div'); |
16 | 16 |
17 DropDownContainer.prototype = { | 17 DropDownContainer.prototype = { |
18 __proto__: HTMLDivElement.prototype, | 18 __proto__: HTMLDivElement.prototype, |
19 | 19 |
20 /** @inheritDoc */ | 20 /** @inheritDoc */ |
21 decorate: function() { | 21 decorate: function() { |
22 this.classList.add('dropdown-container'); | 22 this.classList.add('dropdown-container'); |
23 // Selected item in the menu list. | 23 // Selected item in the menu list. |
24 this.selectedItem = null; | 24 this.selectedItem = null; |
25 // First item which could be selected. | 25 // First item which could be selected. |
26 this.firstItem = null; | 26 this.firstItem = null; |
27 | |
28 this.setAttribute('role', 'menu'); | |
27 }, | 29 }, |
28 | 30 |
29 /** | 31 /** |
30 * Selects new item. | 32 * Selects new item. |
31 * @param {!Object} selectedItem Item to be selected. | 33 * @param {!Object} selectedItem Item to be selected. |
32 */ | 34 */ |
33 selectItem: function(selectedItem) { | 35 selectItem: function(selectedItem) { |
34 if (this.selectedItem) | 36 if (this.selectedItem) |
35 this.selectedItem.classList.remove('hover'); | 37 this.selectedItem.classList.remove('hover'); |
36 selectedItem.classList.add('hover'); | 38 selectedItem.classList.add('hover'); |
37 this.selectedItem = selectedItem; | 39 this.selectedItem = selectedItem; |
40 this.previousSibling.setAttribute( | |
41 'aria-activedescendant', this.id + selectedItem.id); | |
altimofeev
2011/09/19 08:13:14
Shouldn't it be just 'selectedItem.id'?
| |
38 } | 42 } |
39 }; | 43 }; |
40 | 44 |
41 /** | 45 /** |
42 * Creates a new DropDown div. | 46 * Creates a new DropDown div. |
43 * @constructor | 47 * @constructor |
44 * @extends {HTMLDivElement} | 48 * @extends {HTMLDivElement} |
45 */ | 49 */ |
46 var DropDown = cr.ui.define('div'); | 50 var DropDown = cr.ui.define('div'); |
47 | 51 |
48 DropDown.ITEM_DIVIDER_ID = -2; | 52 DropDown.ITEM_DIVIDER_ID = -2; |
49 | 53 |
50 DropDown.prototype = { | 54 DropDown.prototype = { |
51 __proto__: HTMLDivElement.prototype, | 55 __proto__: HTMLDivElement.prototype, |
52 | 56 |
53 /** @inheritDoc */ | 57 /** @inheritDoc */ |
54 decorate: function() { | 58 decorate: function() { |
55 this.appendChild(this.createOverlay_()); | 59 this.appendChild(this.createOverlay_()); |
56 this.appendChild(this.createTitle_()); | 60 this.appendChild(this.title_ = this.createTitle_()); |
57 this.appendChild(new DropDownContainer()); | 61 this.appendChild(new DropDownContainer()); |
58 | 62 |
59 this.isShown = false; | 63 this.isShown = false; |
60 this.addEventListener('keydown', this.keyDownHandler_); | 64 this.addEventListener('keydown', this.keyDownHandler_); |
65 | |
66 this.title_.id = this.id + '-dropdown'; | |
67 this.title_.setAttribute('role', 'listbox'); | |
61 }, | 68 }, |
62 | 69 |
63 /** | 70 /** |
64 * Returns true if dropdown menu is shown. | 71 * Returns true if dropdown menu is shown. |
65 * @type {bool} Whether menu element is shown. | 72 * @type {bool} Whether menu element is shown. |
66 */ | 73 */ |
67 get isShown() { | 74 get isShown() { |
68 return !this.container.hidden; | 75 return !this.container.hidden; |
69 }, | 76 }, |
70 | 77 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 itemElement.appendChild(itemContentElement); | 166 itemElement.appendChild(itemContentElement); |
160 itemElement.iid = item.id; | 167 itemElement.iid = item.id; |
161 itemElement.controller = this; | 168 itemElement.controller = this; |
162 var enabled = 'enabled' in item && item.enabled; | 169 var enabled = 'enabled' in item && item.enabled; |
163 if (!enabled) | 170 if (!enabled) |
164 itemElement.classList.add('disabled-item'); | 171 itemElement.classList.add('disabled-item'); |
165 | 172 |
166 if (item.id > 0) { | 173 if (item.id > 0) { |
167 var wrapperDiv = this.ownerDocument.createElement('div'); | 174 var wrapperDiv = this.ownerDocument.createElement('div'); |
168 wrapperDiv.classList.add('dropdown-item-container'); | 175 wrapperDiv.classList.add('dropdown-item-container'); |
176 wrapperDiv.setAttribute('role', 'menuitem'); | |
177 wrapperDiv.id = this.id + item.id; | |
169 var imageDiv = this.ownerDocument.createElement('div'); | 178 var imageDiv = this.ownerDocument.createElement('div'); |
170 imageDiv.classList.add('dropdown-image'); | 179 imageDiv.classList.add('dropdown-image'); |
171 imageDiv.appendChild(image); | 180 imageDiv.appendChild(image); |
172 wrapperDiv.appendChild(imageDiv); | 181 wrapperDiv.appendChild(imageDiv); |
173 wrapperDiv.appendChild(itemElement); | 182 wrapperDiv.appendChild(itemElement); |
174 wrapperDiv.addEventListener('click', function f(e) { | 183 wrapperDiv.addEventListener('click', function f(e) { |
175 var item = this.lastElementChild; | 184 var item = this.lastElementChild; |
176 if (item.iid < -1 || item.classList.contains('disabled-item')) | 185 if (item.iid < -1 || item.classList.contains('disabled-item')) |
177 return; | 186 return; |
178 item.controller.isShown = false; | 187 item.controller.isShown = false; |
(...skipping 27 matching lines...) Expand all Loading... | |
206 return overlay; | 215 return overlay; |
207 }, | 216 }, |
208 | 217 |
209 /** | 218 /** |
210 * Creates dropdown title element. | 219 * Creates dropdown title element. |
211 * @type {HTMLElement} | 220 * @type {HTMLElement} |
212 * @private | 221 * @private |
213 */ | 222 */ |
214 createTitle_: function() { | 223 createTitle_: function() { |
215 var image = this.ownerDocument.createElement('img'); | 224 var image = this.ownerDocument.createElement('img'); |
225 image.alt = ""; | |
216 var text = this.ownerDocument.createElement('div'); | 226 var text = this.ownerDocument.createElement('div'); |
217 | 227 |
218 var el = this.ownerDocument.createElement('div'); | 228 var el = this.ownerDocument.createElement('div'); |
219 el.appendChild(image); | 229 el.appendChild(image); |
220 el.appendChild(text); | 230 el.appendChild(text); |
221 | 231 |
222 el.tabIndex = 0; | 232 el.tabIndex = 0; |
223 el.classList.add('dropdown-title'); | 233 el.classList.add('dropdown-title'); |
224 el.iid = -1; | 234 el.iid = -1; |
225 el.controller = this; | 235 el.controller = this; |
(...skipping 21 matching lines...) Expand all Loading... | |
247 }); | 257 }); |
248 return el; | 258 return el; |
249 }, | 259 }, |
250 | 260 |
251 /** | 261 /** |
252 * Handles keydown event from the keyboard. | 262 * Handles keydown event from the keyboard. |
253 * @private | 263 * @private |
254 * @param {!Event} e Keydown event. | 264 * @param {!Event} e Keydown event. |
255 */ | 265 */ |
256 keyDownHandler_: function(e) { | 266 keyDownHandler_: function(e) { |
257 if (!this.isShown) | |
altimofeev
2011/09/19 08:13:14
Why do you need handle keys if menu isn't shown?
| |
258 return; | |
259 var selected = this.container.selectedItem; | 267 var selected = this.container.selectedItem; |
260 switch (e.keyCode) { | 268 switch (e.keyCode) { |
261 case 38: { // Key up. | 269 case 38: { // Key up. |
262 do { | 270 do { |
263 selected = selected.previousSibling; | 271 selected = selected.previousSibling; |
264 if (!selected) | 272 if (!selected) |
265 selected = this.container.lastElementChild; | 273 selected = this.container.lastElementChild; |
266 } while (selected.iid < 0); | 274 } while (selected.iid < 0); |
267 this.container.selectItem(selected); | 275 this.container.selectItem(selected); |
268 break; | 276 break; |
269 } | 277 } |
270 case 40: { // Key down. | 278 case 40: { // Key down. |
271 do { | 279 do { |
272 selected = selected.nextSibling; | 280 selected = selected.nextSibling; |
273 if (!selected) | 281 if (!selected) |
274 selected = this.container.firstItem; | 282 selected = this.container.firstItem; |
275 } while (selected.iid < 0); | 283 } while (selected.iid < 0); |
276 this.container.selectItem(selected); | 284 this.container.selectItem(selected); |
277 break; | 285 break; |
278 } | 286 } |
279 case 27: { // Esc. | 287 case 27: { // Esc. |
280 this.isShown = false; | 288 this.isShown = false; |
281 break; | 289 break; |
282 } | 290 } |
283 case 9: { // Tab. | 291 case 9: { // Tab. |
284 this.isShown = false; | 292 this.isShown = false; |
285 break; | 293 break; |
286 } | 294 } |
287 case 13: { // Enter. | 295 case 13: { // Enter. |
296 if (!this.isShown) | |
297 return; | |
288 var button = this.titleButton; | 298 var button = this.titleButton; |
289 if (!button.opening) { | 299 if (!button.opening) { |
290 button.focus(); | 300 button.focus(); |
291 this.isShown = false; | 301 this.isShown = false; |
292 var item = | 302 var item = |
293 button.controller.container.selectedItem.lastElementChild; | 303 button.controller.container.selectedItem.lastElementChild; |
294 if (item.iid >= 0 && !item.classList.contains('disabled-item')) | 304 if (item.iid >= 0 && !item.classList.contains('disabled-item')) |
295 chrome.send('networkItemChosen', [item.iid]); | 305 chrome.send('networkItemChosen', [item.iid]); |
296 } else { | 306 } else { |
297 button.opening = false; | 307 button.opening = false; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
337 DropDown.activeElementId_ = ''; | 347 DropDown.activeElementId_ = ''; |
338 chrome.send('networkDropdownHide', []); | 348 chrome.send('networkDropdownHide', []); |
339 } | 349 } |
340 } | 350 } |
341 }; | 351 }; |
342 | 352 |
343 return { | 353 return { |
344 DropDown: DropDown | 354 DropDown: DropDown |
345 }; | 355 }; |
346 }); | 356 }); |
OLD | NEW |