OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 const ArrayDataModel = cr.ui.ArrayDataModel; | 6 const ArrayDataModel = cr.ui.ArrayDataModel; |
7 const DeletableItem = options.DeletableItem; | 7 const DeletableItem = options.DeletableItem; |
8 const DeletableItemList = options.DeletableItemList; | 8 const DeletableItemList = options.DeletableItemList; |
9 | 9 |
10 /** | 10 /** |
11 * Bluetooth settings constants. | 11 * Bluetooth settings constants. |
12 */ | 12 */ |
13 function Constants() {} | 13 function Constants() {} |
14 | 14 |
15 /** | 15 /** |
16 * Enumeration of supported device types. | 16 * Enumeration of supported device types. |
17 * @enum {string} | 17 * @enum {string} |
18 */ | 18 */ |
19 // TODO(kevers): Prune list based on the set of devices that will be | 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 | 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 | 21 // mouse and keyboard. Others are temporarily included for testing device |
22 // discovery. | 22 // discovery. |
23 Constants.DEVICE_TYPE = { | 23 Constants.DEVICE_TYPE = { |
24 COMPUTER: 'computer', | 24 COMPUTER: 'computer', |
25 HEADSET: 'headset', | 25 HEADSET: 'headset', |
26 KEYBOARD: 'input-keyboard', | 26 KEYBOARD: 'input-keyboard', |
27 MOUSE: 'input-mouse', | 27 MOUSE: 'input-mouse', |
28 TABLET: 'input-tablet', | |
28 PHONE: 'phone', | 29 PHONE: 'phone', |
29 }; | 30 }; |
30 | 31 |
31 /** | 32 /** |
32 * Creates a new bluetooth list item. | 33 * Creates a new bluetooth list item. |
33 * @param {{name: string, | 34 * @param {{name: string, |
34 * address: string, | 35 * address: string, |
35 * icon: Constants.DEVICE_TYPE, | 36 * icon: Constants.DEVICE_TYPE, |
36 * paired: boolean, | 37 * paired: boolean, |
37 * connected: boolean, | 38 * connected: boolean, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 * icon: Constants.DEVICE_TYPE, | 128 * icon: Constants.DEVICE_TYPE, |
128 * paired: boolean, | 129 * paired: boolean, |
129 * connected: boolean, | 130 * connected: boolean, |
130 * pairing: string|undefined, | 131 * pairing: string|undefined, |
131 * passkey: number|undefined, | 132 * passkey: number|undefined, |
132 * entered: number|undefined}} device | 133 * entered: number|undefined}} device |
133 * Description of the bluetooth device. | 134 * Description of the bluetooth device. |
134 * @return {boolean} True if the devies was successfully added or updated. | 135 * @return {boolean} True if the devies was successfully added or updated. |
135 */ | 136 */ |
136 appendDevice: function(device) { | 137 appendDevice: function(device) { |
138 if (!device.name || device.name.length == 0) { | |
139 device.name = device.address; | |
140 var type = this.getDeviceType_(device); | |
141 if (type) | |
142 device.name = device.name + ' (' + type + ')'; | |
143 } | |
keybuk
2012/03/02 15:58:02
This is dealt with in the BluetoothDevice C++ now,
kevers
2012/03/02 16:24:39
Done.
| |
137 if (!this.isSupported_(device)) | 144 if (!this.isSupported_(device)) |
138 return false; | 145 return false; |
139 var index = this.find(device.address); | 146 var index = this.find(device.address); |
140 if (index == undefined) { | 147 if (index == undefined) { |
141 this.dataModel.push(device); | 148 this.dataModel.push(device); |
142 this.redraw(); | 149 this.redraw(); |
143 } else { | 150 } else { |
144 this.dataModel.splice(index, 1, device); | 151 this.dataModel.splice(index, 1, device); |
145 this.redrawItem(index); | 152 this.redrawItem(index); |
146 } | 153 } |
147 this.updateListVisibility_(); | 154 this.updateListVisibility_(); |
148 return true; | 155 return true; |
149 }, | 156 }, |
150 | 157 |
151 /** | 158 /** |
159 * Forces a revailidation of the list content. Content added while the list | |
160 * is hidden is not properly rendered when the list becomes visible. In | |
161 * addition, deleting a single item from the list results in a stale cache | |
162 * requiring an invalidation. | |
163 */ | |
164 refresh: function() { | |
165 // TODO(kevers): Investigate if the root source of the problems can be | |
166 // fixed in cr.ui.list. | |
167 this.invalidate(); | |
168 this.redraw(); | |
169 }, | |
170 | |
171 /** | |
152 * Perges all devices from the list. | 172 * Perges all devices from the list. |
153 */ | 173 */ |
154 clear: function() { | 174 clear: function() { |
155 this.dataModel = new ArrayDataModel([]); | 175 this.dataModel = new ArrayDataModel([]); |
156 this.redraw(); | 176 this.redraw(); |
157 this.updateListVisibility_(); | 177 this.updateListVisibility_(); |
158 }, | 178 }, |
159 | 179 |
160 /** | 180 /** |
161 * Returns the index of the list entry with the matching address. | 181 * Returns the index of the list entry with the matching address. |
(...skipping 17 matching lines...) Expand all Loading... | |
179 | 199 |
180 /** @inheritDoc */ | 200 /** @inheritDoc */ |
181 deleteItemAtIndex: function(index) { | 201 deleteItemAtIndex: function(index) { |
182 var item = this.dataModel.item(index); | 202 var item = this.dataModel.item(index); |
183 if (item && (item.connected || item.paired)) { | 203 if (item && (item.connected || item.paired)) { |
184 // Inform the bluetooth adapter that we are disconnecting the device. | 204 // Inform the bluetooth adapter that we are disconnecting the device. |
185 chrome.send('updateBluetoothDevice', | 205 chrome.send('updateBluetoothDevice', |
186 [item.address, item.connected ? 'disconnect' : 'forget']); | 206 [item.address, item.connected ? 'disconnect' : 'forget']); |
187 } | 207 } |
188 this.dataModel.splice(index, 1); | 208 this.dataModel.splice(index, 1); |
189 // Invalidate the list since it has a stale cache after a splice | 209 this.refresh(); |
190 // involving a deletion. | |
191 this.invalidate(); | |
192 this.redraw(); | |
193 this.updateListVisibility_(); | 210 this.updateListVisibility_(); |
194 }, | 211 }, |
195 | 212 |
196 /** | 213 /** |
197 * Tests if the bluetooth device is supported based on the type of device. | 214 * Tests if the bluetooth device is supported based on the type of device. |
198 * @param {Object.<string,string>} device Desription of the device. | 215 * @param {Object.<string,string>} device Desription of the device. |
199 * @return {boolean} true if the device is supported. | 216 * @return {boolean} true if the device is supported. |
200 * @private | 217 * @private |
201 */ | 218 */ |
202 isSupported_: function(device) { | 219 isSupported_: function(device) { |
203 var target = device.icon; | 220 var target = device.icon; |
204 for (var key in Constants.DEVICE_TYPE) { | 221 for (var key in Constants.DEVICE_TYPE) { |
205 if (Constants.DEVICE_TYPE[key] == target) | 222 if (Constants.DEVICE_TYPE[key] == target) |
206 return true; | 223 return true; |
207 } | 224 } |
208 return false; | 225 return false; |
209 }, | 226 }, |
210 | 227 |
211 /** | 228 /** |
229 * The type of bluetooth device. | |
230 " @param {Object.<string,string>} device Description of the device. | |
231 * @return {string} The device type. | |
232 * @private | |
233 */ | |
234 getDeviceType_: function(device) { | |
235 var name = device.icon; | |
236 if (name && name.indexOf('input-') == 0) | |
237 name = name.substring(6); | |
238 return name; | |
239 }, | |
keybuk
2012/03/02 15:58:02
The 'icon' property has been dropped from the C++
kevers
2012/03/02 16:24:39
Done.
| |
240 | |
241 /** | |
212 * If the list has an associated empty list placholder then update the | 242 * If the list has an associated empty list placholder then update the |
213 * visibility of the list and placeholder. | 243 * visibility of the list and placeholder. |
214 * @private | 244 * @private |
215 */ | 245 */ |
216 updateListVisibility_: function() { | 246 updateListVisibility_: function() { |
217 var empty = this.dataModel.length == 0; | 247 var empty = this.dataModel.length == 0; |
218 var listPlaceHolderID = this.id + '-empty-placeholder'; | 248 var listPlaceHolderID = this.id + '-empty-placeholder'; |
219 if ($(listPlaceHolderID)) { | 249 if ($(listPlaceHolderID)) { |
220 this.hidden = empty; | 250 if (this.hidden != empty) { |
221 $(listPlaceHolderID).hidden = !empty; | 251 this.hidden = empty; |
252 $(listPlaceHolderID).hidden = !empty; | |
253 this.refresh(); | |
254 } | |
222 } | 255 } |
223 }, | 256 }, |
224 }; | 257 }; |
225 | 258 |
226 cr.defineProperty(BluetoothListItem, 'connected', cr.PropertyKind.BOOL_ATTR); | 259 cr.defineProperty(BluetoothListItem, 'connected', cr.PropertyKind.BOOL_ATTR); |
227 | 260 |
228 cr.defineProperty(BluetoothListItem, 'paired', cr.PropertyKind.BOOL_ATTR); | 261 cr.defineProperty(BluetoothListItem, 'paired', cr.PropertyKind.BOOL_ATTR); |
229 | 262 |
230 cr.defineProperty(BluetoothListItem, 'connecting', cr.PropertyKind.BOOL_ATTR); | 263 cr.defineProperty(BluetoothListItem, 'connecting', cr.PropertyKind.BOOL_ATTR); |
231 | 264 |
232 return { | 265 return { |
233 BluetoothListItem: BluetoothListItem, | 266 BluetoothListItem: BluetoothListItem, |
234 BluetoothDeviceList: BluetoothDeviceList, | 267 BluetoothDeviceList: BluetoothDeviceList, |
235 Constants: Constants | 268 Constants: Constants |
236 }; | 269 }; |
237 }); | 270 }); |
OLD | NEW |