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() { |
| 118 // TODO(kevers): Should this be pushed up to the list class? |
| 119 this.selectionModel.unselectAll(); |
| 120 }, |
| 121 |
| 122 /** |
| 123 * Updates the state of the button for adding a Bluetooth device in |
| 124 * response to a change in the selected item. |
| 125 * @private |
| 126 */ |
| 127 onChange_: function() { |
| 128 var item = this.selectedItem; |
| 129 var disabled = !item || item.paired || item.conencted; |
| 130 $('bluetooth-add-device-apply-button').disabled = disabled; |
| 131 }, |
| 132 |
| 133 /** |
| 134 * Adds a bluetooth device to the list of available devices. A check is |
| 135 * made to see if the device is already in the list, in which case the |
| 136 * existing device is updated. |
| 137 * @param {{name: string, |
| 138 * address: string, |
| 139 * icon: Constants.DEVICE_TYPE, |
| 140 * paired: boolean, |
| 141 * connected: boolean, |
| 142 * pairing: string|undefined, |
| 143 * passkey: number|undefined, |
| 144 * entered: number|undefined}} device |
| 145 * Description of the bluetooth device. |
| 146 * @return {boolean} True if the devies was successfully added or updated. |
| 147 */ |
| 148 appendDevice: function(device) { |
| 149 if (!this.isSupported_(device)) |
| 150 return false; |
| 151 var index = this.find(device.address); |
| 152 if (index == undefined) { |
| 153 this.dataModel.push(device); |
| 154 this.redraw(); |
| 155 } else { |
| 156 this.dataModel.splice(index, 1, device); |
| 157 this.redrawItem(index); |
| 158 } |
| 159 return true; |
| 160 }, |
| 161 |
| 162 /** |
| 163 * Perges all devices from the list. |
| 164 */ |
| 165 clear: function() { |
| 166 this.dataModel = new ArrayDataModel([]); |
| 167 this.redraw(); |
| 168 }, |
| 169 |
| 170 /** |
| 171 * Returns the index of the list entry with the matching address. |
| 172 * @param {string} address Unique address of the Bluetooth device. |
| 173 * @return {number|undefined} Index of the matching entry or |
| 174 * undefined if no match found. |
| 175 */ |
| 176 find: function(address) { |
| 177 var size = this.dataModel.length; |
| 178 for (var i = 0; i < size; i++) { |
| 179 var entry = this.dataModel.item(i); |
| 180 if (entry.address == address) |
| 181 return i; |
| 182 } |
| 183 }, |
| 184 |
| 185 /** @inheritDoc */ |
| 186 createItem: function(entry) { |
| 187 return new BluetoothListItem(entry); |
| 188 }, |
| 189 |
| 190 /** @inheritDoc */ |
| 191 deleteItemAtIndex: function(index) { |
| 192 var item = this.dataModel.item(index); |
| 193 if (item && (item.connected || item.paired)) { |
| 194 // Inform the bluetooth adapter that we are disconnecting the device. |
| 195 chrome.send('updateBluetoothDevice', |
| 196 [item.address, item.connected ? 'disconnect' : 'forget']); |
| 197 } |
| 198 this.dataModel.splice(index, 1); |
| 199 // Invalidate the list since it has a stale cache after a splice |
| 200 // involving a deletion. |
| 201 this.invalidate(); |
| 202 this.redraw(); |
| 203 }, |
| 204 |
| 205 /** |
| 206 * Tests if the bluetooth device is supported based on the type of device. |
| 207 * @param {Object.<string,string>} device Desription of the device. |
| 208 * @return {boolean} true if the device is supported. |
| 209 * @private |
| 210 */ |
| 211 isSupported_: function(device) { |
| 212 var target = device.icon; |
| 213 for (var key in Constants.DEVICE_TYPE) { |
| 214 if (Constants.DEVICE_TYPE[key] == target) |
| 215 return true; |
| 216 } |
| 217 return false; |
| 218 } |
| 219 }; |
| 220 |
| 221 cr.defineProperty(BluetoothListItem, 'connected', cr.PropertyKind.BOOL_ATTR); |
| 222 |
| 223 cr.defineProperty(BluetoothListItem, 'paired', cr.PropertyKind.BOOL_ATTR); |
| 224 |
| 225 cr.defineProperty(BluetoothListItem, 'connecting', cr.PropertyKind.BOOL_ATTR); |
| 226 |
| 227 return { |
| 228 BluetoothListItem: BluetoothListItem, |
| 229 BluetoothDeviceList: BluetoothDeviceList, |
| 230 Constants: Constants |
| 231 }; |
| 232 }); |
OLD | NEW |