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 Oobe network screen implementation. | 6 * @fileoverview Oobe network screen implementation. |
7 */ | 7 */ |
8 | 8 |
9 cr.define('oobe', function() { | 9 cr.define('oobe', function() { |
10 /** | 10 /** |
| 11 * Creates a new DropDown div. |
| 12 * @constructor |
| 13 * @extends {HTMLDivElement} |
| 14 */ |
| 15 var DropDown = cr.ui.define('div'); |
| 16 |
| 17 DropDown.ITEM_DIVIDER_ID = -2; |
| 18 |
| 19 DropDown.prototype = { |
| 20 __proto__: HTMLDivElement.prototype, |
| 21 |
| 22 /** @inheritDoc */ |
| 23 decorate: function() { |
| 24 this.appendChild(this.createTitle_()); |
| 25 |
| 26 // Create menu items container. |
| 27 var container = this.ownerDocument.createElement('div') |
| 28 container.classList.add('dropdown-container'); |
| 29 this.appendChild(container); |
| 30 this.isShown = false; |
| 31 }, |
| 32 |
| 33 /** |
| 34 * Returns true if dropdown menu is shown. |
| 35 * @type {bool} Whether menu element is shown. |
| 36 */ |
| 37 get isShown() { |
| 38 return !this.lastElementChild.hidden; |
| 39 }, |
| 40 |
| 41 /** |
| 42 * Sets dropdown menu visibility. |
| 43 * @param {bool} show New visibility state for dropdown menu. |
| 44 */ |
| 45 set isShown(show) { |
| 46 this.lastElementChild.hidden = !show; |
| 47 }, |
| 48 |
| 49 /** |
| 50 * Sets title and icon. |
| 51 * @param {string} title Text on dropdown. |
| 52 * @param {string} icon Icon in dataURL format. |
| 53 */ |
| 54 setTitle: function(title, icon) { |
| 55 // TODO(nkostylev): Icon support for dropdown title. |
| 56 this.firstElementChild.textContent = title; |
| 57 }, |
| 58 |
| 59 /** |
| 60 * Sets dropdown items. |
| 61 * @param {Array} items Dropdown items array. |
| 62 */ |
| 63 setItems: function(items) { |
| 64 var container = this.lastElementChild; |
| 65 container.innerHTML = ''; |
| 66 for (var i = 0; i < items.length; ++i) { |
| 67 var item = items[i]; |
| 68 if ('sub' in item) { |
| 69 // Workaround for submenus, add items on top level. |
| 70 // TODO(altimofeev): support submenus. |
| 71 for (var j = 0; j < item.sub.length; ++j) |
| 72 this.createItem_(container, item.sub[j]); |
| 73 continue; |
| 74 } |
| 75 this.createItem_(container, item); |
| 76 } |
| 77 }, |
| 78 |
| 79 /** |
| 80 * Creates dropdown item element and adds into container. |
| 81 * @param {HTMLElement} container Container where item is added. |
| 82 * @param {!Object} item Item to be added. |
| 83 * @private |
| 84 */ |
| 85 createItem_ : function(container, item) { |
| 86 var itemContentElement; |
| 87 var className = 'dropdown-item'; |
| 88 if (item.id == DropDown.ITEM_DIVIDER_ID) { |
| 89 className = 'dropdown-divider'; |
| 90 itemContentElement = this.ownerDocument.createElement('hr'); |
| 91 } else { |
| 92 var span = this.ownerDocument.createElement('span'); |
| 93 itemContentElement = span; |
| 94 span.textContent = item.label; |
| 95 if ('bold' in item && item.bold) |
| 96 span.classList.add('bold'); |
| 97 var image = this.ownerDocument.createElement('img'); |
| 98 if (item.icon) |
| 99 image.src = item.icon; |
| 100 } |
| 101 |
| 102 var itemElement = this.ownerDocument.createElement('div'); |
| 103 itemElement.classList.add(className); |
| 104 itemElement.appendChild(itemContentElement); |
| 105 itemElement.iid = item.id; |
| 106 itemElement.controller = this; |
| 107 var enabled = 'enabled' in item && item.enabled; |
| 108 if (!enabled) |
| 109 itemElement.classList.add('disabled-item'); |
| 110 |
| 111 if (item.id > 0) { |
| 112 var wrapperDiv = this.ownerDocument.createElement('div'); |
| 113 wrapperDiv.classList.add('dropdown-item-container'); |
| 114 var imageDiv = this.ownerDocument.createElement('div'); |
| 115 imageDiv.classList.add('dropdown-image'); |
| 116 imageDiv.appendChild(image); |
| 117 wrapperDiv.appendChild(imageDiv); |
| 118 wrapperDiv.appendChild(itemElement); |
| 119 wrapperDiv.addEventListener('click', function f(e) { |
| 120 var item = this.lastElementChild; |
| 121 if (item.iid < -1 || item.classList.contains('disabled-item')) |
| 122 return; |
| 123 item.controller.isShown = !item.controller.isShown; |
| 124 if (item.iid >= 0) |
| 125 chrome.send('networkItemChosen', [item.iid]); |
| 126 }); |
| 127 itemElement = wrapperDiv; |
| 128 } |
| 129 container.appendChild(itemElement); |
| 130 }, |
| 131 |
| 132 /** |
| 133 * Creates dropdown title element. |
| 134 * @type {HTMLElement} |
| 135 * @private |
| 136 */ |
| 137 createTitle_: function() { |
| 138 var el = this.ownerDocument.createElement('button'); |
| 139 el.classList.add('dropdown-title'); |
| 140 el.iid = -1; |
| 141 el.controller = this; |
| 142 el.addEventListener('click', function f(e) { |
| 143 this.controller.isShown = !this.controller.isShown; |
| 144 }); |
| 145 return el; |
| 146 } |
| 147 }; |
| 148 |
| 149 /** |
11 * Creates a new oobe screen div. | 150 * Creates a new oobe screen div. |
12 * @constructor | 151 * @constructor |
13 * @extends {HTMLDivElement} | 152 * @extends {HTMLDivElement} |
14 */ | 153 */ |
15 var NetworkScreen = cr.ui.define('div'); | 154 var NetworkScreen = cr.ui.define('div'); |
16 | 155 |
17 /** | 156 /** |
18 * Registers with Oobe. | 157 * Registers with Oobe. |
19 */ | 158 */ |
20 NetworkScreen.register = function() { | 159 NetworkScreen.register = function() { |
21 var screen = $('connect'); | 160 var screen = $('connect'); |
22 NetworkScreen.decorate(screen); | 161 NetworkScreen.decorate(screen); |
23 Oobe.getInstance().registerScreen(screen); | 162 Oobe.getInstance().registerScreen(screen); |
24 }; | 163 }; |
25 | 164 |
26 NetworkScreen.prototype = { | 165 NetworkScreen.prototype = { |
27 __proto__: HTMLDivElement.prototype, | 166 __proto__: HTMLDivElement.prototype, |
28 | 167 |
| 168 /** |
| 169 * Dropdown element for networks selection. |
| 170 */ |
| 171 dropdown_: null, |
| 172 |
29 /** @inheritDoc */ | 173 /** @inheritDoc */ |
30 decorate: function() { | 174 decorate: function() { |
31 Oobe.setupSelect($('language-select'), | 175 Oobe.setupSelect($('language-select'), |
32 templateData.languageList, | 176 templateData.languageList, |
33 'networkOnLanguageChanged'); | 177 'networkOnLanguageChanged'); |
34 | 178 |
35 Oobe.setupSelect($('keyboard-select'), | 179 Oobe.setupSelect($('keyboard-select'), |
36 templateData.inputMethodsList, | 180 templateData.inputMethodsList, |
37 'networkOnInputMethodChanged'); | 181 'networkOnInputMethodChanged'); |
38 }, | 182 |
| 183 this.dropdown_ = $('networks-list'); |
| 184 DropDown.decorate(this.dropdown_); |
| 185 }, |
| 186 |
| 187 /** |
| 188 * Updates network list in dropdown. |
| 189 * @param {Array} items Dropdown items array. |
| 190 */ |
| 191 updateNetworks: function(data) { |
| 192 this.dropdown_.setItems(data); |
| 193 }, |
| 194 |
| 195 /** |
| 196 * Updates dropdown title and icon. |
| 197 * @param {string} title Text on dropdown. |
| 198 * @param {string} icon Icon in dataURL format. |
| 199 */ |
| 200 updateNetworkTitle: function(title, icon) { |
| 201 this.dropdown_.setTitle(title, icon); |
| 202 }, |
39 | 203 |
40 /** | 204 /** |
41 * Header text of the screen. | 205 * Header text of the screen. |
42 * @type {string} | 206 * @type {string} |
43 */ | 207 */ |
44 get header() { | 208 get header() { |
45 return localStrings.getString('networkScreenTitle'); | 209 return localStrings.getString('networkScreenTitle'); |
46 }, | 210 }, |
47 | 211 |
48 /** | 212 /** |
49 * Buttons in oobe wizard's button strip. | 213 * Buttons in oobe wizard's button strip. |
50 * @type {array} Array of Buttons. | 214 * @type {array} Array of Buttons. |
51 */ | 215 */ |
52 get buttons() { | 216 get buttons() { |
53 var buttons = []; | 217 var buttons = []; |
54 | 218 |
55 var continueButton = this.ownerDocument.createElement('button'); | 219 var continueButton = this.ownerDocument.createElement('button'); |
56 continueButton.id = 'continue-button'; | 220 continueButton.id = 'continue-button'; |
57 continueButton.textContent = localStrings.getString('continueButton'); | 221 continueButton.textContent = localStrings.getString('continueButton'); |
58 continueButton.addEventListener('click', function(e) { | 222 continueButton.addEventListener('click', function(e) { |
59 chrome.send('networkOnExit', []); | 223 chrome.send('networkOnExit', []); |
60 }); | 224 }); |
61 buttons.push(continueButton); | 225 buttons.push(continueButton); |
62 | 226 |
63 return buttons; | 227 return buttons; |
64 } | 228 } |
65 }; | 229 }; |
66 | 230 |
| 231 NetworkScreen.updateNetworks = function(data) { |
| 232 $('connect').updateNetworks(data); |
| 233 }; |
| 234 |
| 235 NetworkScreen.updateNetworkTitle = function(title, icon) { |
| 236 $('connect').updateNetworkTitle(title, icon); |
| 237 }; |
| 238 |
67 return { | 239 return { |
68 NetworkScreen: NetworkScreen | 240 NetworkScreen: NetworkScreen |
69 }; | 241 }; |
70 }); | 242 }); |
OLD | NEW |