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 // Whether scroll has just happened. |
| 28 this.scrollJustHappened = false; |
| 29 }, |
| 30 |
| 31 /** |
| 32 * Gets scroll action to be done for the item. |
| 33 * @param {!Object} item Menu item. |
| 34 * @return {integer} -1 for scroll up; 0 for no action; 1 for scroll down. |
| 35 */ |
| 36 scrollAction: function(item) { |
| 37 var thisTop = this.scrollTop; |
| 38 var thisBottom = thisTop + this.offsetHeight; |
| 39 var itemTop = item.offsetTop; |
| 40 var itemBottom = itemTop + item.offsetHeight; |
| 41 if (itemTop <= thisTop) return -1; |
| 42 if (itemBottom >= thisBottom) return 1; |
| 43 return 0; |
27 }, | 44 }, |
28 | 45 |
29 /** | 46 /** |
30 * Selects new item. | 47 * Selects new item. |
31 * @param {!Object} selectedItem Item to be selected. | 48 * @param {!Object} selectedItem Item to be selected. |
| 49 * @param {boolean} mouseOver Is mouseover event triggered? |
32 */ | 50 */ |
33 selectItem: function(selectedItem) { | 51 selectItem: function(selectedItem, mouseOver) { |
| 52 if (mouseOver && this.scrollJustHappened) { |
| 53 this.scrollJustHappened = false; |
| 54 return; |
| 55 } |
34 if (this.selectedItem) | 56 if (this.selectedItem) |
35 this.selectedItem.classList.remove('hover'); | 57 this.selectedItem.classList.remove('hover'); |
36 selectedItem.classList.add('hover'); | 58 selectedItem.classList.add('hover'); |
37 this.selectedItem = selectedItem; | 59 this.selectedItem = selectedItem; |
| 60 var action = this.scrollAction(selectedItem); |
| 61 if (action != 0) { |
| 62 selectedItem.scrollIntoView(action < 0); |
| 63 this.scrollJustHappened = true; |
| 64 } |
38 } | 65 } |
39 }; | 66 }; |
40 | 67 |
41 /** | 68 /** |
42 * Creates a new DropDown div. | 69 * Creates a new DropDown div. |
43 * @constructor | 70 * @constructor |
44 * @extends {HTMLDivElement} | 71 * @extends {HTMLDivElement} |
45 */ | 72 */ |
46 var DropDown = cr.ui.define('div'); | 73 var DropDown = cr.ui.define('div'); |
47 | 74 |
(...skipping 21 matching lines...) Expand all Loading... |
69 }, | 96 }, |
70 | 97 |
71 /** | 98 /** |
72 * Sets dropdown menu visibility. | 99 * Sets dropdown menu visibility. |
73 * @param {bool} show New visibility state for dropdown menu. | 100 * @param {bool} show New visibility state for dropdown menu. |
74 */ | 101 */ |
75 set isShown(show) { | 102 set isShown(show) { |
76 this.firstElementChild.hidden = !show; | 103 this.firstElementChild.hidden = !show; |
77 this.container.hidden = !show; | 104 this.container.hidden = !show; |
78 if (show) | 105 if (show) |
79 this.container.selectItem(this.container.firstItem); | 106 this.container.selectItem(this.container.firstItem, false); |
80 }, | 107 }, |
81 | 108 |
82 /** | 109 /** |
83 * Returns title button. | 110 * Returns title button. |
84 */ | 111 */ |
85 get titleButton() { | 112 get titleButton() { |
86 return this.children[1]; | 113 return this.children[1]; |
87 }, | 114 }, |
88 | 115 |
89 /** | 116 /** |
(...skipping 25 matching lines...) Expand all Loading... |
115 var item = items[i]; | 142 var item = items[i]; |
116 if ('sub' in item) { | 143 if ('sub' in item) { |
117 // Workaround for submenus, add items on top level. | 144 // Workaround for submenus, add items on top level. |
118 // TODO(altimofeev): support submenus. | 145 // TODO(altimofeev): support submenus. |
119 for (var j = 0; j < item.sub.length; ++j) | 146 for (var j = 0; j < item.sub.length; ++j) |
120 this.createItem_(this.container, item.sub[j]); | 147 this.createItem_(this.container, item.sub[j]); |
121 continue; | 148 continue; |
122 } | 149 } |
123 this.createItem_(this.container, item); | 150 this.createItem_(this.container, item); |
124 } | 151 } |
125 this.container.selectItem(this.container.firstItem); | 152 this.container.selectItem(this.container.firstItem, false); |
126 }, | 153 }, |
127 | 154 |
128 /** | 155 /** |
129 * Id of the active drop-down element. | 156 * Id of the active drop-down element. |
130 * @private | 157 * @private |
131 */ | 158 */ |
132 activeElementId_: '', | 159 activeElementId_: '', |
133 | 160 |
134 /** | 161 /** |
135 * Creates dropdown item element and adds into container. | 162 * Creates dropdown item element and adds into container. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 wrapperDiv.addEventListener('click', function f(e) { | 201 wrapperDiv.addEventListener('click', function f(e) { |
175 var item = this.lastElementChild; | 202 var item = this.lastElementChild; |
176 if (item.iid < -1 || item.classList.contains('disabled-item')) | 203 if (item.iid < -1 || item.classList.contains('disabled-item')) |
177 return; | 204 return; |
178 item.controller.isShown = false; | 205 item.controller.isShown = false; |
179 if (item.iid >= 0) | 206 if (item.iid >= 0) |
180 chrome.send('networkItemChosen', [item.iid]); | 207 chrome.send('networkItemChosen', [item.iid]); |
181 this.parentNode.parentNode.titleButton.focus(); | 208 this.parentNode.parentNode.titleButton.focus(); |
182 }); | 209 }); |
183 wrapperDiv.addEventListener('mouseover', function f(e) { | 210 wrapperDiv.addEventListener('mouseover', function f(e) { |
184 this.parentNode.selectItem(this); | 211 this.parentNode.selectItem(this, true); |
185 }); | 212 }); |
186 itemElement = wrapperDiv; | 213 itemElement = wrapperDiv; |
187 } | 214 } |
188 container.appendChild(itemElement); | 215 container.appendChild(itemElement); |
189 if (!container.firstItem && item.id >= 0) { | 216 if (!container.firstItem && item.id >= 0) { |
190 container.firstItem = itemElement; | 217 container.firstItem = itemElement; |
191 } | 218 } |
192 }, | 219 }, |
193 | 220 |
194 /** | 221 /** |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 if (!this.isShown) | 284 if (!this.isShown) |
258 return; | 285 return; |
259 var selected = this.container.selectedItem; | 286 var selected = this.container.selectedItem; |
260 switch (e.keyCode) { | 287 switch (e.keyCode) { |
261 case 38: { // Key up. | 288 case 38: { // Key up. |
262 do { | 289 do { |
263 selected = selected.previousSibling; | 290 selected = selected.previousSibling; |
264 if (!selected) | 291 if (!selected) |
265 selected = this.container.lastElementChild; | 292 selected = this.container.lastElementChild; |
266 } while (selected.iid < 0); | 293 } while (selected.iid < 0); |
267 this.container.selectItem(selected); | 294 this.container.selectItem(selected, false); |
268 break; | 295 break; |
269 } | 296 } |
270 case 40: { // Key down. | 297 case 40: { // Key down. |
271 do { | 298 do { |
272 selected = selected.nextSibling; | 299 selected = selected.nextSibling; |
273 if (!selected) | 300 if (!selected) |
274 selected = this.container.firstItem; | 301 selected = this.container.firstItem; |
275 } while (selected.iid < 0); | 302 } while (selected.iid < 0); |
276 this.container.selectItem(selected); | 303 this.container.selectItem(selected, false); |
277 break; | 304 break; |
278 } | 305 } |
279 case 27: { // Esc. | 306 case 27: { // Esc. |
280 this.isShown = false; | 307 this.isShown = false; |
281 break; | 308 break; |
282 } | 309 } |
283 case 9: { // Tab. | 310 case 9: { // Tab. |
284 this.isShown = false; | 311 this.isShown = false; |
285 break; | 312 break; |
286 } | 313 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 DropDown.activeElementId_ = ''; | 364 DropDown.activeElementId_ = ''; |
338 chrome.send('networkDropdownHide', []); | 365 chrome.send('networkDropdownHide', []); |
339 } | 366 } |
340 } | 367 } |
341 }; | 368 }; |
342 | 369 |
343 return { | 370 return { |
344 DropDown: DropDown | 371 DropDown: DropDown |
345 }; | 372 }; |
346 }); | 373 }); |
OLD | NEW |