| 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 |
| 6 // Network status constants. |
| 7 const StatusConnected = 'connected'; |
| 8 const StatusDisconnected = 'disconnected'; |
| 9 const StatusConnecting = 'connecting'; |
| 10 const StatusError = 'error'; |
| 11 |
| 12 const NetworkOther = 'other'; |
| 13 |
| 5 /** | 14 /** |
| 6 * Sends "connect" using the 'action' DOMUI message. | 15 * Sends "connect" using the 'action' DOMUI message. |
| 7 */ | 16 */ |
| 8 function sendConnect(index, passphrase, identity) { | 17 function sendConnect(index, passphrase, identity) { |
| 9 chrome.send('action', [ 'connect', String(index), passphrase, identity ]); | 18 chrome.send('action', [ 'connect', String(index), passphrase, identity ]); |
| 10 } | 19 } |
| 11 | 20 |
| 12 var NetworkMenuItem = cr.ui.define('div'); | 21 var networkMenuItemProto = (function() { |
| 22 var networkMenuItem = cr.doc.createElement('div'); |
| 23 networkMenuItem.innerHTML = '<div class="network-menu-item">' + |
| 24 '<div class="network-label-icon">' + |
| 25 '<div class="network-label"></div>' + |
| 26 '<img class="network-icon hidden">' + |
| 27 '</div>' + |
| 28 '<div class="network-status hidden"></div>' + |
| 29 '<div class="hidden"></div>' + |
| 30 '</div>'; |
| 31 return networkMenuItem; |
| 32 })(); |
| 33 |
| 34 var NetworkMenuItem = cr.ui.define(function() { |
| 35 return networkMenuItemProto.cloneNode(true); |
| 36 }); |
| 13 | 37 |
| 14 NetworkMenuItem.prototype = { | 38 NetworkMenuItem.prototype = { |
| 15 __proto__: MenuItem.prototype, | 39 __proto__: MenuItem.prototype, |
| 16 | 40 |
| 41 ssidEdit: null, |
| 42 passwordEdit: null, |
| 43 rememberCheckbox: null, |
| 44 |
| 45 /** |
| 46 * The label element. |
| 47 * @private |
| 48 */ |
| 49 get label_() { |
| 50 return this.firstElementChild.firstElementChild.firstElementChild; |
| 51 }, |
| 52 |
| 53 /** |
| 54 * The icon element. |
| 55 * @private |
| 56 */ |
| 57 get icon_() { |
| 58 return this.label_.nextElementSibling; |
| 59 }, |
| 60 |
| 61 /** |
| 62 * The status area element. |
| 63 * @private |
| 64 */ |
| 65 get status_() { |
| 66 return this.firstElementChild.firstElementChild.nextElementSibling; |
| 67 }, |
| 68 |
| 69 /** |
| 70 * The action area container element. |
| 71 * @private |
| 72 */ |
| 73 get action_() { |
| 74 return this.status_.nextElementSibling; |
| 75 }, |
| 76 |
| 77 /** |
| 78 * Set status message. |
| 79 * @param {string} message The message to display in status area. |
| 80 * @private |
| 81 */ |
| 82 setStatus_: function(message) { |
| 83 if (message) { |
| 84 this.status_.textContent = message; |
| 85 this.status_.classList.remove('hidden'); |
| 86 } else { |
| 87 this.status_.classList.add('hidden'); |
| 88 } |
| 89 }, |
| 90 |
| 91 /** |
| 92 * Set status icon. |
| 93 * @param {string} icon Source url for the icon image. |
| 94 * @private |
| 95 */ |
| 96 setIcon_: function(icon) { |
| 97 if (icon) { |
| 98 this.icon_.src = icon; |
| 99 this.icon_.classList.remove('hidden'); |
| 100 } else { |
| 101 this.icon_.classList.add('hidden'); |
| 102 } |
| 103 }, |
| 104 |
| 105 /** |
| 106 * Handle reconnect. |
| 107 * @private |
| 108 */ |
| 109 handleConnect_ : function(e) { |
| 110 // TODO: Handle "Remember" checkbox |
| 111 |
| 112 if (this.ssidEdit && this.passwordEdit) { |
| 113 sendConnect(this.menu_.getMenuItemIndexOf(this), |
| 114 this.passwordEdit.value, this.ssidEdit.value); |
| 115 } else if (this.passwordEdit) { |
| 116 sendConnect(this.menu_.getMenuItemIndexOf(this), |
| 117 this.passwordEdit.value, ''); |
| 118 } else { |
| 119 sendConnect(this.menu_.getMenuItemIndexOf(this), '', ''); |
| 120 } |
| 121 }, |
| 122 |
| 123 /** |
| 124 * Handle keydown event in ssid edit. |
| 125 * @private |
| 126 */ |
| 127 handleSsidEditKeydown_: function(e) { |
| 128 if (e.target == this.ssidEdit && |
| 129 e.keyIdentifier == 'Enter') { |
| 130 this.passwordEdit.focus(); |
| 131 } |
| 132 }, |
| 133 |
| 134 /** |
| 135 * Handle keydown event in password edit. |
| 136 * @private |
| 137 */ |
| 138 handlePassEditKeydown_: function(e) { |
| 139 if (e.target == this.passwordEdit && |
| 140 e.keyIdentifier == 'Enter') { |
| 141 this.handleConnect_(); |
| 142 } |
| 143 }, |
| 144 |
| 145 /** |
| 146 * Returns whether action area is visible. |
| 147 * @private |
| 148 */ |
| 149 isActionVisible_: function() { |
| 150 return !this.action_.classList.contains('hidden'); |
| 151 }, |
| 152 |
| 153 /** |
| 154 * Show/hide action area. |
| 155 * @private |
| 156 */ |
| 157 showAction_: function(show) { |
| 158 var visible = this.isActionVisible_(); |
| 159 if (show && !visible) { |
| 160 this.action_.classList.remove('hidden'); |
| 161 } else if (!show && visible) { |
| 162 this.action_.classList.add('hidden'); |
| 163 } |
| 164 }, |
| 165 |
| 166 /** |
| 167 * Add network name edit to action area. |
| 168 * @private |
| 169 */ |
| 170 addSsidEdit_: function() { |
| 171 this.ssidEdit = this.ownerDocument.createElement('input'); |
| 172 this.ssidEdit.type = 'text'; |
| 173 this.ssidEdit.placeholder = localStrings.getString('ssid_prompt'); |
| 174 this.ssidEdit.pattern = '^\\S+$'; |
| 175 this.ssidEdit.addEventListener('keydown', |
| 176 this.handleSsidEditKeydown_.bind(this)); |
| 177 |
| 178 var box = this.ownerDocument.createElement('div'); |
| 179 box.appendChild(this.ssidEdit); |
| 180 this.action_.appendChild(box); |
| 181 }, |
| 182 |
| 183 /** |
| 184 * Add password edit to action area. |
| 185 * @private |
| 186 */ |
| 187 addPasswordEdit_: function() { |
| 188 this.passwordEdit = this.ownerDocument.createElement('input'); |
| 189 this.passwordEdit.type = 'password'; |
| 190 this.passwordEdit.placeholder = localStrings.getString('pass_prompt'); |
| 191 this.passwordEdit.pattern = '^\\S+$'; |
| 192 this.passwordEdit.addEventListener('keydown', |
| 193 this.handlePassEditKeydown_.bind(this)); |
| 194 |
| 195 var box = this.ownerDocument.createElement('div'); |
| 196 box.appendChild(this.passwordEdit); |
| 197 this.action_.appendChild(box); |
| 198 }, |
| 199 |
| 200 /** |
| 201 * Add remember this network check box to action area. |
| 202 * @private |
| 203 */ |
| 204 addRememberCheckbox_: function() { |
| 205 this.rememberCheckbox = this.ownerDocument.createElement('input'); |
| 206 this.rememberCheckbox.type = 'checkbox'; |
| 207 this.rememberCheckbox.checked = this.attrs.remembered; |
| 208 |
| 209 var rememberSpan = this.ownerDocument.createElement('span'); |
| 210 rememberSpan.textContent = localStrings.getString('remeber_this_network'); |
| 211 |
| 212 var rememberLabel = this.ownerDocument.createElement('label'); |
| 213 rememberLabel.appendChild(this.rememberCheckbox); |
| 214 rememberLabel.appendChild(rememberSpan); |
| 215 |
| 216 this.action_.appendChild(rememberLabel); |
| 217 }, |
| 218 |
| 17 /** | 219 /** |
| 18 * Internal method to initiailze the MenuItem. | 220 * Internal method to initiailze the MenuItem. |
| 19 * @private | 221 * @private |
| 20 */ | 222 */ |
| 21 initMenuItem_: function() { | 223 initMenuItem_: function() { |
| 22 // *TODO: eliminate code duplication with menu.js | 224 // *TODO: eliminate code duplication with menu.js |
| 23 // MenuItem.prototype.initMenuItem_(); | 225 // MenuItem.prototype.initMenuItem_(); |
| 24 var attrs = this.attrs; | 226 var attrs = this.attrs; |
| 25 this.className = 'menu-item ' + attrs.type; | 227 this.classList.add(attrs.type); |
| 26 this.menu_.addHandlers(this, this); | 228 this.menu_.addHandlers(this, this); |
| 27 var label = document.createElement('div'); | |
| 28 | |
| 29 label.className = 'menu-label'; | |
| 30 | 229 |
| 31 //////// NetworkMenuItem specific code: | 230 //////// NetworkMenuItem specific code: |
| 32 // TODO: Handle specific types of network, connecting icon. | 231 // TODO: Handle specific types of network, connecting icon. |
| 33 if (attrs.status && attrs.status != 'unknown') { | 232 this.label_.textContent = attrs.label; |
| 34 label.appendChild(document.createTextNode(attrs.label)); | 233 |
| 35 if (attrs.status == 'connected') { | 234 if (attrs.network_type == NetworkOther) { |
| 36 // label.appendChild(document.createElement('p')). | 235 this.addSsidEdit_(); |
| 37 // appendChild(document.createTextNode(attrs.message)); | 236 this.addPasswordEdit_(); |
| 38 label.appendChild(document.createElement('p')). | 237 this.addRememberCheckbox_(); |
| 39 appendChild(document.createTextNode(attrs.ip_address)); | 238 } else if (attrs.status && attrs.status != 'unknown') { |
| 40 } else if (attrs.status == 'connecting') { | 239 if (attrs.status == StatusConnected) { |
| 41 label.appendChild(document.createElement('p')). | 240 this.setStatus_(attrs.ip_address); |
| 42 appendChild(document.createTextNode(attrs.message)); | 241 } else if (attrs.status == StatusConnecting) { |
| 43 } else { | 242 this.setStatus_(attrs.message); |
| 44 // error | 243 } else if (attrs.status == StatusError) { |
| 45 label.appendChild(document.createElement('p')). | 244 this.setStatus_(attrs.message); |
| 46 appendChild(document.createTextNode(attrs.message)); | 245 this.setIcon_('chrome://theme/IDR_WARNING'); |
| 47 // TODO: error icon, reconnect button | 246 |
| 247 var button = this.ownerDocument.createElement('button'); |
| 248 button.textContent = localStrings.getString('reconnect'); |
| 249 button.addEventListener('click', this.handleConnect_.bind(this)); |
| 250 this.action_.appendChild(button); |
| 251 this.showAction_(true); |
| 48 } | 252 } |
| 49 // TODO: need_passphrase | 253 |
| 50 // TODO: remembered | 254 if (attrs.need_passphrase) { |
| 51 } else { | 255 this.addPasswordEdit_(); |
| 52 label.textContent = attrs.label; | 256 } |
| 257 |
| 258 this.addRememberCheckbox_(); |
| 53 } | 259 } |
| 54 //////// End NetworkMenuItem specifi code | 260 //////// End NetworkMenuItem specifi code |
| 55 | 261 |
| 56 if (attrs.font) { | 262 if (attrs.font) { |
| 57 label.style.font = attrs.font; | 263 this.label_.style.font = attrs.font; |
| 264 |
| 265 var base_font = attrs.font.replace(/bold/, '').replace(/italic/, ''); |
| 266 this.status_.style.font = base_font; |
| 267 this.action_.style.font = base_font; |
| 58 } | 268 } |
| 59 this.appendChild(label); | 269 }, |
| 60 | 270 |
| 61 }, | 271 /** @inheritDoc */ |
| 272 activate: function() { |
| 273 // Show action area for encrypted network and 'other' network. |
| 274 if ((this.attrs.network_type == NetworkOther || |
| 275 this.attrs.status == StatusDisconnected) && |
| 276 this.attrs.need_passphrase && |
| 277 !this.isActionVisible_()) { |
| 278 this.showAction_(true); |
| 279 } |
| 280 |
| 281 // No default activate if action area is visible. |
| 282 if (this.isActionVisible_()) |
| 283 return; |
| 284 |
| 285 MenuItem.prototype.activate.call(this); |
| 286 } |
| 62 }; | 287 }; |
| 63 | 288 |
| 64 | 289 |
| 65 var NetworkMenu = cr.ui.define('div'); | 290 var NetworkMenu = cr.ui.define('div'); |
| 66 | 291 |
| 67 NetworkMenu.prototype = { | 292 NetworkMenu.prototype = { |
| 68 __proto__: Menu.prototype, | 293 __proto__: Menu.prototype, |
| 69 | 294 |
| 295 /** @inheritDoc */ |
| 70 createMenuItem: function(attrs) { | 296 createMenuItem: function(attrs) { |
| 71 if (attrs.type == 'command') { | 297 if (attrs.type == 'command') { |
| 72 return new NetworkMenuItem(); | 298 return new NetworkMenuItem(); |
| 73 } else { | 299 } else { |
| 74 return new MenuItem(); | 300 return new MenuItem(); |
| 75 } | 301 } |
| 76 }, | |
| 77 | |
| 78 onKeydown_: function(event) { | |
| 79 switch (event.keyIdentifier) { | |
| 80 case 'Enter': | |
| 81 case 'U+0020': // space | |
| 82 // Temporary, for testing sendConnect() | |
| 83 sendConnect(this.getMenuItemIndexOf(this.current_), | |
| 84 "passphrase", "identity"); | |
| 85 break; | |
| 86 default: | |
| 87 Menu.prototype.onKeydown_.call(this, event); | |
| 88 break; | |
| 89 } | |
| 90 } | 302 } |
| 91 }; | 303 }; |
| OLD | NEW |