Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 cr.define('options.system.bluetooth', function() { | |
| 6 const ArrayDataModel = cr.ui.ArrayDataModel; | |
| 7 const DeletableItem = options.DeletableItem; | |
| 8 const DeletableItemList = options.DeletableItemList; | |
| 9 | |
| 10 /** | |
| 11 * Bluetooth settings constants. | |
| 12 */ | |
| 13 function Constants() {} | |
| 14 | |
| 15 /** | |
| 16 * Enumeration of supported device types. | |
| 17 * @enum {string} | |
| 18 */ | |
| 19 // TODO(kevers): Prune list based on the set of devices that will be | |
| 20 // supported for V1 of the feature. The set will likely be restricted to | |
| 21 // mouse and keyboard. Others are temporarily included for testing device | |
| 22 // discovery. | |
| 23 Constants.DEVICE_TYPE = { | |
| 24 COMPUTER: 'computer', | |
| 25 HEADSET: 'headset', | |
| 26 KEYBOARD: 'input-keyboard', | |
| 27 MOUSE: 'input-mouse', | |
| 28 PHONE: 'phone', | |
| 29 }; | |
| 30 | |
| 31 /** | |
| 32 * Creates a new bluetooth list item. | |
| 33 * @param {{name: string, | |
| 34 * address: string, | |
| 35 * icon: Constants.DEVICE_TYPE, | |
| 36 * paired: boolean, | |
| 37 * connected: boolean, | |
| 38 * pairing: string|undefined, | |
| 39 * passkey: number|undefined, | |
| 40 * entered: number|undefined}} device | |
| 41 * Description of the Bluetooth device. | |
| 42 * @constructor | |
| 43 * @extends {options.DeletableItem} | |
| 44 */ | |
| 45 function BluetoothListItem(device) { | |
| 46 var el = cr.doc.createElement('div'); | |
| 47 el.__proto__ = BluetoothListItem.prototype; | |
| 48 el.data = {}; | |
| 49 for (var key in device) | |
| 50 el.data[key] = device[key]; | |
| 51 el.decorate(); | |
| 52 // Only show the close button for paired devices. | |
| 53 el.deletable = device.paired; | |
| 54 return el; | |
| 55 } | |
| 56 | |
| 57 BluetoothListItem.prototype = { | |
| 58 __proto__: DeletableItem.prototype, | |
| 59 | |
| 60 /** | |
| 61 * Description of the Bluetooth device. | |
| 62 * @type {{name: string, | |
| 63 * address: string, | |
| 64 * icon: Constants.DEVICE_TYPE, | |
| 65 * paired: boolean, | |
| 66 * connected: boolean, | |
| 67 * pairing: string|undefined, | |
| 68 * passkey: number|undefined, | |
| 69 * entered: number|undefined}} | |
| 70 */ | |
| 71 data: null, | |
| 72 | |
| 73 /** @inheritDoc */ | |
| 74 decorate: function() { | |
| 75 DeletableItem.prototype.decorate.call(this); | |
| 76 var label = this.ownerDocument.createElement('div'); | |
| 77 label.className = 'bluetooth-device-label'; | |
| 78 this.classList.add('bluetooth-device'); | |
| 79 this.connected = this.data.connected; | |
| 80 // Though strictly speaking, a connected device will also be paired, we | |
| 81 // are interested in tracking paired devices that are not connected. | |
| 82 this.paired = this.data.paired && !this.data.connected; | |
| 83 this.connecting = !!this.data.pairing; | |
| 84 var content = this.data.name; | |
| 85 // Update label for devices that are paired but not connected. | |
| 86 if (this.paired) { | |
| 87 content = content + ' (' + | |
| 88 templateData['bluetoothDeviceNotConnected'] + ')'; | |
| 89 } | |
| 90 label.textContent = content; | |
| 91 this.contentElement.appendChild(label); | |
| 92 }, | |
| 93 }; | |
| 94 | |
| 95 /** | |
| 96 * Class for displaying a list of Bluetooth devices. | |
| 97 * @constructor | |
| 98 * @extends {options.DeletableItemList} | |
| 99 */ | |
| 100 var BluetoothDeviceList = cr.ui.define('list'); | |
| 101 | |
| 102 BluetoothDeviceList.prototype = { | |
| 103 __proto__: DeletableItemList.prototype, | |
| 104 | |
| 105 /** @inheritDoc */ | |
| 106 decorate: function() { | |
| 107 DeletableItemList.prototype.decorate.call(this); | |
| 108 this.addEventListener('blur', this.onBlur_); | |
| 109 this.addEventListener('change', this.onChange_); | |
| 110 this.clear(); | |
| 111 }, | |
| 112 | |
| 113 /** | |
| 114 * When the list loses focus, unselect all items in the list. | |
| 115 * @private | |
| 116 */ | |
| 117 onBlur_: function() { | |
|
James Hawkins
2011/12/08 19:18:03
Hmm, I wonder if this should be pushed up to the l
kevers
2011/12/08 19:39:56
Added a TODO note. Certainly worth investigating.
| |
| 118 this.selectionModel.unselectAll(); | |
| 119 }, | |
| 120 | |
| 121 /** | |
| 122 * Updates the state of the button for adding a Bluetooth device in | |
| 123 * response to a change in the selected item. | |
| 124 * @private | |
| 125 */ | |
| 126 onChange_: function() { | |
| 127 var item = this.selectedItem; | |
| 128 var disabled = !item || item.paired || item.conencted; | |
| 129 $('bluetooth-add-device-apply-button').disabled = disabled; | |
| 130 }, | |
| 131 | |
| 132 /** | |
| 133 * Adds a bluetooth device to the list of available devices. A check is | |
| 134 * made to see if the device is already in the list, in which case the | |
| 135 * existing device is updated. | |
| 136 * @param {{name: string, | |
| 137 * address: string, | |
| 138 * icon: Constants.DEVICE_TYPE, | |
| 139 * paired: boolean, | |
| 140 * connected: boolean, | |
| 141 * pairing: string|undefined, | |
| 142 * passkey: number|undefined, | |
| 143 * entered: number|undefined}} device | |
| 144 * Description of the bluetooth device. | |
| 145 * @return {boolean} True if the devies was successfully added or updated. | |
| 146 */ | |
| 147 appendDevice: function(device) { | |
| 148 if (!this.isSupported_(device)) | |
| 149 return false; | |
| 150 var index = this.find(device.address); | |
| 151 if (index == undefined) { | |
| 152 this.dataModel.push(device); | |
| 153 this.redraw(); | |
| 154 } else { | |
| 155 this.dataModel.splice(index, 1, device); | |
| 156 this.redrawItem(index); | |
| 157 } | |
| 158 return true; | |
| 159 }, | |
| 160 | |
| 161 /** | |
| 162 * Perges all devices from the list. | |
| 163 */ | |
| 164 clear: function() { | |
| 165 this.dataModel = new ArrayDataModel([]); | |
| 166 this.redraw(); | |
| 167 }, | |
| 168 | |
| 169 /** | |
| 170 * Returns the index of the list entry with the matching address. | |
| 171 * @param {string} address Unique address of the Bluetooth device. | |
| 172 * @return {number|undefined} Index of the matching entry or | |
| 173 * undefined if no match found. | |
| 174 */ | |
| 175 find: function(address) { | |
| 176 var size = this.dataModel.length; | |
| 177 for (var i = 0; i < size; i++) { | |
| 178 var entry = this.dataModel.item(i); | |
| 179 if (entry.address == address) | |
| 180 return i; | |
| 181 } | |
| 182 }, | |
| 183 | |
| 184 /** @inheritDoc */ | |
| 185 createItem: function(entry) { | |
| 186 return new BluetoothListItem(entry); | |
| 187 }, | |
| 188 | |
| 189 /** @inheritDoc */ | |
| 190 deleteItemAtIndex: function(index) { | |
| 191 var item = this.dataModel.item(index); | |
| 192 if (item && (item.connected || item.paired)) { | |
| 193 // Inform the bluetooth adapter that we are disconnecting the device. | |
| 194 chrome.send('updateBluetoothDevice', | |
| 195 [item.address, item.connected ? 'disconnect' : 'forget']); | |
| 196 } | |
| 197 this.dataModel.splice(index, 1); | |
| 198 // Invalidate the list since it has a stale cache after a splice | |
| 199 // involving a deletion. | |
| 200 this.invalidate(); | |
| 201 this.redraw(); | |
| 202 }, | |
| 203 | |
| 204 /** | |
| 205 * Tests if the bluetooth device is supported based on the type of device. | |
| 206 * @param {Object.<string,string>} device Desription of the device. | |
| 207 * @return {boolean} true if the device is supported. | |
| 208 * @private | |
| 209 */ | |
| 210 isSupported_: function(device) { | |
| 211 var target = device.icon; | |
| 212 for (var key in Constants.DEVICE_TYPE) { | |
| 213 if (Constants.DEVICE_TYPE[key] == target) | |
| 214 return true; | |
| 215 } | |
| 216 return false; | |
| 217 } | |
| 218 }; | |
| 219 | |
| 220 cr.defineProperty(BluetoothListItem, 'connected', cr.PropertyKind.BOOL_ATTR); | |
| 221 | |
| 222 cr.defineProperty(BluetoothListItem, 'paired', cr.PropertyKind.BOOL_ATTR); | |
| 223 | |
| 224 cr.defineProperty(BluetoothListItem, 'connecting', cr.PropertyKind.BOOL_ATTR); | |
| 225 | |
| 226 return { | |
| 227 BluetoothListItem: BluetoothListItem, | |
| 228 BluetoothDeviceList: BluetoothDeviceList, | |
| 229 Constants: Constants | |
| 230 }; | |
| 231 }); | |
| OLD | NEW |