Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: chrome/browser/resources/options/chromeos/bluetooth_list_element.js

Issue 8539022: Bluetooth UI update. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Formatting tweaks. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 cr.define('options.system.bluetooth', function() { 5 cr.define('options.system.bluetooth', function() {
6 /** 6 /**
7 * Bluetooth settings constants. 7 * Bluetooth settings constants.
8 */ 8 */
9 function Constants() {} 9 function Constants() {}
10 10
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 this.appendChild(new BluetoothItem(devices[i])); 76 this.appendChild(new BluetoothItem(devices[i]));
77 } 77 }
78 }, 78 },
79 79
80 /** 80 /**
81 * Adds a bluetooth device to the list of available devices. A check is 81 * Adds a bluetooth device to the list of available devices. A check is
82 * made to see if the device is already in the list, in which case the 82 * made to see if the device is already in the list, in which case the
83 * existing device is updated. 83 * existing device is updated.
84 * @param {Object.<string,string>} device Description of the bluetooth 84 * @param {Object.<string,string>} device Description of the bluetooth
85 * device. 85 * device.
86 * @return {boolean} True if the devies was successfully added or updated.
86 */ 87 */
87 appendDevice: function(device) { 88 appendDevice: function(device) {
88 if (!this.isSupported_(device)) 89 if (!this.isSupported_(device))
89 return; 90 return false;
90 var item = new BluetoothItem(device); 91 var item = new BluetoothItem(device);
91 var existing = this.findDevice(device.address); 92 var existing = this.findDevice(device.address);
92 if (existing) { 93 if (existing) {
93 this.replaceChild(item, existing); 94 this.replaceChild(item, existing);
94 return; 95 return true;
95 } 96 }
flackr 2011/11/11 19:46:47 Style nit: The return isn't affected by the fact t
kevers 2011/11/11 20:44:23 Done.
96 this.appendChild(item); 97 this.appendChild(item);
98 return true;
97 }, 99 },
98 100
99 /** 101 /**
100 * Scans the list of elements corresponding to discovered Bluetooth 102 * Scans the list of elements corresponding to discovered Bluetooth
101 * devices for one with a matching address. 103 * devices for one with a matching address.
102 * @param {string} address The address of the device. 104 * @param {string} address The address of the device.
103 * @return {Element|undefined} Element corresponding to the device address 105 * @return {Element|undefined} Element corresponding to the device address
104 * or undefined if no corresponding element is found. 106 * or undefined if no corresponding element is found.
105 */ 107 */
106 findDevice: function(address) { 108 findDevice: function(address) {
(...skipping 28 matching lines...) Expand all
135 * icon: Constants.DEVICE_TYPE, 137 * icon: Constants.DEVICE_TYPE,
136 * paired: boolean, 138 * paired: boolean,
137 * connected: boolean, 139 * connected: boolean,
138 * pairing: string|undefined, 140 * pairing: string|undefined,
139 * passkey: number|undefined, 141 * passkey: number|undefined,
140 * entered: number|undefined}} device 142 * entered: number|undefined}} device
141 * Decription of the bluetooth device. 143 * Decription of the bluetooth device.
142 * @constructor 144 * @constructor
143 */ 145 */
144 function BluetoothItem(device) { 146 function BluetoothItem(device) {
145 var el = cr.doc.createElement('div'); 147 var el = $('bluetooth-item-template').cloneNode(true);
148 el.__proto__ = BluetoothItem.prototype;
149 el.removeAttribute('id');
150 el.hidden = false;
146 el.data = {}; 151 el.data = {};
147 for (var key in device) 152 for (var key in device)
148 el.data[key] = device[key]; 153 el.data[key] = device[key];
149 BluetoothItem.decorate(el); 154 el.decorate();
150 return el; 155 return el;
151 } 156 }
152 157
153 /**
154 * Decorates an element as a bluetooth item.
155 * @param {!HTMLElement} el The element to decorate.
156 */
157 BluetoothItem.decorate = function(el) {
158 el.__proto__ = BluetoothItem.prototype;
159 el.decorate();
160 };
161
162 BluetoothItem.prototype = { 158 BluetoothItem.prototype = {
163 __proto__: HTMLDivElement.prototype, 159 __proto__: HTMLDivElement.prototype,
164 160
165 /** @inheritDoc */ 161 /** @inheritDoc */
166 decorate: function() { 162 decorate: function() {
167 this.className = 'bluetooth-item'; 163 this.className = 'bluetooth-item';
168 this.connected = this.data.connected; 164 this.connected = this.data.connected;
169 // Though strictly speaking, a connected device will also be paired, 165 // Though strictly speaking, a connected device will also be paired,
170 // we are interested in tracking paired devices that are not connected. 166 // we are interested in tracking paired devices that are not connected.
171 this.paired = this.data.paired && !this.data.connected; 167 this.paired = this.data.paired && !this.data.connected;
172 this.connecting = !!this.data.pairing; 168 this.connecting = !!this.data.pairing;
173 this.addIcon_();
174 this.addLabels_(); 169 this.addLabels_();
175 this.addButtons_(); 170 this.addButtons_();
176 }, 171 },
177 172
178 /** 173 /**
179 * Adds an icon corresponding to the device category. 174 * Retrieves the descendent element with the matching class name.
175 * @param {string} className The class name for the target element.
176 * @return {Element|undefined} Returns the matching element if
177 * found and unique.
180 * @private 178 * @private
181 */ 179 */
182 addIcon_: function() { 180 getNodeByClass_:function(className) {
183 var iconElement = this.ownerDocument.createElement('div'); 181 var elements = this.getElementsByClassName(className);
184 iconElement.className = 'bluetooth-' + this.data.icon; 182 if (elements && elements.length == 1)
185 iconElement.classList.add('bluetooth-icon'); 183 return elements[0];
186 this.appendChild(iconElement); 184 },
185
186 /**
187 * Sets the text content for an element.
188 * @param {string} className The class name of the target element.
189 * @param {string} label Text content for the element.
190 * @private
191 */
192 setLabel_: function(className, label) {
193 var el = this.getNodeByClass_(className);
194 if (el)
flackr 2011/11/11 19:46:47 This should always exist if everything's working,
kevers 2011/11/11 20:44:23 Correct, 'el' should always exist.
195 el.textContent = label;
187 }, 196 },
188 197
189 /** 198 /**
190 * Adds an element containing the display name, status and device pairing 199 * Adds an element containing the display name, status and device pairing
191 * instructions. 200 * instructions.
192 * @private 201 * @private
193 */ 202 */
194 addLabels_: function() { 203 addLabels_: function() {
195 var textDiv = this.ownerDocument.createElement('div'); 204 this.setLabel_('network-name-label', this.data.name);
196 textDiv.className = 'bluetooth-item-text'; 205 var status;
197 var nameEl = this.ownerDocument.createElement('div'); 206 if (this.data.connected)
198 nameEl.className = 'network-name-label'; 207 status = Constants.DEVICE_STATUS.CONNECTED;
199 nameEl.textContent = this.data.name; 208 else if (this.data.pairing)
200 textDiv.appendChild(nameEl); 209 status = Constants.DEVICE_STATUS.CONNECTING;
201 this.appendChild(textDiv); 210 if (status) {
202 this.setDeviceStatus_(textDiv); 211 var statusMessage = templateData[status];
203 if (this.data.pairing) { 212 if (statusMessage)
204 this.addPairingInstructions_(textDiv); 213 this.setLabel_('network-status-label', statusMessage);
214 if (this.connecting) {
215 var spinner = this.getNodeByClass_('inline-spinner');
216 if (spinner)
217 spinner.hidden = false;
218 }
205 } 219 }
220 if (this.data.pairing)
221 this.addPairingInstructions_();
206 }, 222 },
207 223
208 /** 224 /**
209 * Adds a label showing the status of the device.
210 * @param {!Element} textDiv Target element for inserting the status label.
211 * @private
212 */
213 setDeviceStatus_: function(textDiv) {
214 var status;
215 if (this.data.connected)
216 status = Constants.DEVICE_STATUS.CONNECTED;
217 else if (this.data.paired)
218 status = Constants.DEVICE_STATUS.PAIRED;
219 else if (this.data.pairing)
220 status = Constants.DEVICE_STATUS.CONNECTING;
221 else
222 status = Constants.DEVICE_STATUS.NOT_PAIRED;
223 var statusMessage = templateData[status];
224 if (statusMessage) {
225 var statusEl = this.ownerDocument.createElement('div');
226 statusEl.className = 'network-status-label';
227 statusEl.textContent = statusMessage;
228 if (this.connecting) {
229 var throbber = this.ownerDocument.createElement('div');
230 throbber.className = 'throbber';
231 statusEl.appendChild(throbber);
232 }
233 textDiv.appendChild(statusEl);
234 }
235 },
236
237 /**
238 * Adds instructions on how to complete the pairing process. 225 * Adds instructions on how to complete the pairing process.
239 * @param {!Element} textDiv Target element for inserting the instructions. 226 * @param {!Element} textDiv Target element for inserting the instructions.
240 * @private 227 * @private
241 */ 228 */
242 addPairingInstructions_: function(textDiv) { 229 addPairingInstructions_: function() {
243 var instructionsEl = this.ownerDocument.createElement('div'); 230 var instructionsEl = this.getNodeByClass_('bluetooth-instructions');
244 instructionsEl.className = 'bluetooth-instructions';
245 var message = templateData[this.data.pairing]; 231 var message = templateData[this.data.pairing];
246 var array = this.formatInstructions_(message); 232 var array = this.formatInstructions_(message);
247 for (var i = 0; i < array.length; i++) { 233 for (var i = 0; i < array.length; i++) {
248 instructionsEl.appendChild(array[i]); 234 instructionsEl.appendChild(array[i]);
249 } 235 }
250 if (this.data.pairing == Constants.PAIRING.ENTER_PASSKEY) { 236 if (this.data.pairing == Constants.PAIRING.ENTER_PASSKEY) {
251 var input = this.ownerDocument.createElement('input'); 237 var input = this.ownerDocument.createElement('input');
252 input.type = 'text'; 238 input.type = 'text';
253 input.className = 'bluetooth-passkey-field'; 239 input.className = 'bluetooth-passkey-field';
254 instructionsEl.appendChild(input); 240 instructionsEl.appendChild(input);
255 } 241 }
256 textDiv.appendChild(instructionsEl);
257 }, 242 },
258 243
259 /** 244 /**
260 * Formats the pairing instruction, which may contain labels for 245 * Formats the pairing instruction, which may contain labels for
261 * substitution. The label '%1' is replaced with the passkey, and '%2' 246 * substitution. The label '%1' is replaced with the passkey, and '%2'
262 * is replaced with the name of the bluetooth device. Formatting of the 247 * is replaced with the name of the bluetooth device. Formatting of the
263 * passkey depends on the type of validation. 248 * passkey depends on the type of validation.
264 * @param {string} instructions The source instructions to format. 249 * @param {string} instructions The source instructions to format.
265 * @return {Array.<Element>} Array of formatted elements. 250 * @return {Array.<Element>} Array of formatted elements.
266 */ 251 */
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 if (opt_style) 314 if (opt_style)
330 el.className = opt_style; 315 el.className = opt_style;
331 return el; 316 return el;
332 }, 317 },
333 318
334 /** 319 /**
335 * Adds buttons for updating the connectivity of a device. 320 * Adds buttons for updating the connectivity of a device.
336 * @private. 321 * @private.
337 */ 322 */
338 addButtons_: function() { 323 addButtons_: function() {
339 var buttonsDiv = this.ownerDocument.createElement('div'); 324 var buttonsDiv = this.getNodeByClass_('bluetooth-button-group');
340 buttonsDiv.className = 'bluetooth-button-group';
341 var buttonLabelKey = null; 325 var buttonLabelKey = null;
342 var callbackType = null; 326 var callbackType = null;
343 if (this.connected) { 327 if (this.connected) {
344 buttonLabelKey = 'bluetoothDisconnectDevice'; 328 buttonLabelKey = 'bluetoothDisconnectDevice';
345 callbackType = 'disconnect'; 329 callbackType = 'disconnect';
346 } else if (this.paired) { 330 } else if (this.paired) {
347 buttonLabelKey = 'bluetoothForgetDevice'; 331 buttonLabelKey = 'bluetoothForgetDevice';
348 callbackType = 'disconnect'; 332 callbackType = 'disconnect';
349 } else if (this.connecting) { 333 } else if (this.connecting) {
350 buttonLabelKey = 'bluetoothCancel'; 334 buttonLabelKey = 'bluetoothCancel';
(...skipping 28 matching lines...) Expand all
379 363
380 cr.defineProperty(BluetoothItem, 'paired', cr.PropertyKind.BOOL_ATTR); 364 cr.defineProperty(BluetoothItem, 'paired', cr.PropertyKind.BOOL_ATTR);
381 365
382 cr.defineProperty(BluetoothItem, 'connecting', cr.PropertyKind.BOOL_ATTR); 366 cr.defineProperty(BluetoothItem, 'connecting', cr.PropertyKind.BOOL_ATTR);
383 367
384 return { 368 return {
385 Constants: Constants, 369 Constants: Constants,
386 BluetoothListElement: BluetoothListElement 370 BluetoothListElement: BluetoothListElement
387 }; 371 };
388 }); 372 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698