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 /** | |
6 * @typedef {{name: string, | |
7 * address: string, | |
8 * paired: boolean, | |
9 * connected: boolean, | |
10 * connecting: boolean, | |
11 * connectable: boolean, | |
12 * pairing: (string|undefined), | |
13 * passkey: (number|undefined), | |
14 * pincode: (string|undefined), | |
15 * entered: (number|undefined)}} | |
16 */ | |
17 var BluetoothDevice; | |
18 | |
5 cr.define('options.system.bluetooth', function() { | 19 cr.define('options.system.bluetooth', function() { |
6 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; | 20 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; |
7 /** @const */ var DeletableItem = options.DeletableItem; | 21 /** @const */ var DeletableItem = options.DeletableItem; |
8 /** @const */ var DeletableItemList = options.DeletableItemList; | 22 /** @const */ var DeletableItemList = options.DeletableItemList; |
9 /** @const */ var ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; | 23 /** @const */ var ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; |
10 | 24 |
11 /** | 25 /** |
12 * Bluetooth settings constants. | 26 * Bluetooth settings constants. |
13 */ | 27 */ |
14 function Constants() {} | 28 function Constants() {} |
15 | 29 |
16 /** | 30 /** |
17 * Creates a new bluetooth list item. | 31 * Creates a new bluetooth list item. |
18 * @param {{name: string, | 32 * @param {BluetoothDevice} device Description of the Bluetooth device. |
19 * address: string, | |
20 * paired: boolean, | |
21 * connected: boolean, | |
22 * connecting: boolean, | |
23 * connectable: boolean, | |
24 * pairing: (string|undefined), | |
25 * passkey: (number|undefined), | |
26 * pincode: (string|undefined), | |
27 * entered: (number|undefined)}} device | |
28 * Description of the Bluetooth device. | |
29 * @constructor | 33 * @constructor |
30 * @extends {options.DeletableItem} | 34 * @extends {options.DeletableItem} |
31 */ | 35 */ |
32 function BluetoothListItem(device) { | 36 function BluetoothListItem(device) { |
33 var el = cr.doc.createElement('div'); | 37 var el = cr.doc.createElement('div'); |
34 el.__proto__ = BluetoothListItem.prototype; | 38 el.__proto__ = BluetoothListItem.prototype; |
35 el.data = {}; | 39 el.data = {}; |
36 for (var key in device) | 40 for (var key in device) |
37 el.data[key] = device[key]; | 41 el.data[key] = device[key]; |
38 el.decorate(); | 42 el.decorate(); |
39 // Only show the close button for paired devices, but not for connecting | 43 // Only show the close button for paired devices, but not for connecting |
40 // devices. | 44 // devices. |
41 el.deletable = device.paired && !device.connecting; | 45 el.deletable = device.paired && !device.connecting; |
42 return el; | 46 return el; |
43 } | 47 } |
44 | 48 |
45 BluetoothListItem.prototype = { | 49 BluetoothListItem.prototype = { |
46 __proto__: DeletableItem.prototype, | 50 __proto__: DeletableItem.prototype, |
47 | 51 |
48 /** | 52 /** |
49 * Description of the Bluetooth device. | 53 * Description of the Bluetooth device. |
50 * @type {{name: string, | 54 * @type {?BluetoothDevice} |
51 * address: string, | |
52 * paired: boolean, | |
53 * connected: boolean, | |
54 * connecting: boolean, | |
55 * connectable: boolean, | |
56 * pairing: string|undefined, | |
57 * passkey: number|undefined, | |
58 * pincode: string|undefined, | |
59 * entered: number|undefined}} | |
60 */ | 55 */ |
61 data: null, | 56 data: null, |
62 | 57 |
63 /** @override */ | 58 /** @override */ |
64 decorate: function() { | 59 decorate: function() { |
65 DeletableItem.prototype.decorate.call(this); | 60 DeletableItem.prototype.decorate.call(this); |
66 var label = this.ownerDocument.createElement('div'); | 61 var label = this.ownerDocument.createElement('div'); |
67 label.className = 'bluetooth-device-label'; | 62 label.className = 'bluetooth-device-label'; |
68 this.classList.add('bluetooth-device'); | 63 this.classList.add('bluetooth-device'); |
69 // There are four kinds of devices we want to distinguish: | 64 // There are four kinds of devices we want to distinguish: |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 /** | 129 /** |
135 * Adds a bluetooth device to the list of available devices. A check is | 130 * Adds a bluetooth device to the list of available devices. A check is |
136 * made to see if the device is already in the list, in which case the | 131 * made to see if the device is already in the list, in which case the |
137 * existing device is updated. | 132 * existing device is updated. |
138 * @param {{name: string, | 133 * @param {{name: string, |
139 * address: string, | 134 * address: string, |
140 * paired: boolean, | 135 * paired: boolean, |
141 * connected: boolean, | 136 * connected: boolean, |
142 * connecting: boolean, | 137 * connecting: boolean, |
143 * connectable: boolean, | 138 * connectable: boolean, |
144 * pairing: string|undefined, | 139 * pairing: (string|undefined), |
145 * passkey: number|undefined, | 140 * passkey: (number|undefined), |
146 * pincode: string|undefined, | 141 * pincode: (string|undefined), |
147 * entered: number|undefined}} device | 142 * entered: (number|undefined)}} device |
148 * Description of the bluetooth device. | 143 * Description of the bluetooth device. |
149 * @return {boolean} True if the devies was successfully added or updated. | 144 * @return {boolean} True if the devies was successfully added or updated. |
150 */ | 145 */ |
151 appendDevice: function(device) { | 146 appendDevice: function(device) { |
152 var selectedDevice = this.getSelectedDevice_(); | 147 var selectedDevice = this.getSelectedDevice_(); |
153 var index = this.find(device.address); | 148 var index = this.find(device.address); |
154 if (index == undefined) { | 149 if (index == undefined) { |
155 this.dataModel.push(device); | 150 this.dataModel.push(device); |
156 this.redraw(); | 151 this.redraw(); |
157 } else { | 152 } else { |
(...skipping 19 matching lines...) Expand all Loading... | |
177 this.getSelectedDevice_(); | 172 this.getSelectedDevice_(); |
178 this.invalidate(); | 173 this.invalidate(); |
179 this.redraw(); | 174 this.redraw(); |
180 if (selectedDevice) | 175 if (selectedDevice) |
181 this.setSelectedDevice_(selectedDevice); | 176 this.setSelectedDevice_(selectedDevice); |
182 }, | 177 }, |
183 | 178 |
184 /** | 179 /** |
185 * Retrieves the address of the selected device, or null if no device is | 180 * Retrieves the address of the selected device, or null if no device is |
186 * selected. | 181 * selected. |
187 * @return {?string} Address of selected device or null. | 182 * @return {(string|undefined)} Address of selected device or null. |
188 * @private | 183 * @private |
189 */ | 184 */ |
190 getSelectedDevice_: function() { | 185 getSelectedDevice_: function() { |
191 var selection = this.selectedItem; | 186 var selection = this.selectedItem; |
192 if (selection) | 187 if (selection) |
193 return selection.address; | 188 return selection.address; |
194 return null; | 189 return undefined; |
195 }, | 190 }, |
196 | 191 |
197 /** | 192 /** |
198 * Selects the device with the matching address. | 193 * Selects the device with the matching address. |
199 * @param {string} address The unique address of the device. | 194 * @param {string} address The unique address of the device. |
200 * @private | 195 * @private |
201 */ | 196 */ |
202 setSelectedDevice_: function(address) { | 197 setSelectedDevice_: function(address) { |
203 var index = this.find(address); | 198 var index = this.find(address); |
204 if (index != undefined) | 199 if (index != undefined) |
(...skipping 17 matching lines...) Expand all Loading... | |
222 */ | 217 */ |
223 find: function(address) { | 218 find: function(address) { |
224 var size = this.dataModel.length; | 219 var size = this.dataModel.length; |
225 for (var i = 0; i < size; i++) { | 220 for (var i = 0; i < size; i++) { |
226 var entry = this.dataModel.item(i); | 221 var entry = this.dataModel.item(i); |
227 if (entry.address == address) | 222 if (entry.address == address) |
228 return i; | 223 return i; |
229 } | 224 } |
230 }, | 225 }, |
231 | 226 |
232 /** @override */ | 227 /** |
228 * @override | |
229 * @param {BluetoothDevice} entry | |
230 */ | |
233 createItem: function(entry) { | 231 createItem: function(entry) { |
234 return new BluetoothListItem(entry); | 232 return new BluetoothListItem(entry); |
235 }, | 233 }, |
236 | 234 |
237 /** | 235 /** |
238 * Overrides the default implementation, which is used to compute the | 236 * Overrides the default implementation, which is used to compute the |
239 * size of an element in the list. The default implementation relies | 237 * size of an element in the list. The default implementation relies |
240 * on adding a placeholder item to the list and fetching its size and | 238 * on adding a placeholder item to the list and fetching its size and |
241 * position. This strategy does not work if an item is added to the list | 239 * position. This strategy does not work if an item is added to the list |
242 * while it is hidden, as the computed metrics will all be zero in that | 240 * while it is hidden, as the computed metrics will all be zero in that |
243 * case. | 241 * case. |
244 * @return {{height: number, marginTop: number, marginBottom: number, | 242 * @return {{height: number, marginTop: number, marginBottom: number, |
245 * width: number, marginLeft: number, marginRight: number}} | 243 * width: number, marginLeft: number, marginRight: number}} |
246 * The height and width of the item, taking margins into account, | 244 * The height and width of the item, taking margins into account, |
247 * and the margins themselves. | 245 * and the margins themselves. |
248 */ | 246 */ |
249 measureItem: function() { | 247 measureItem: function() { |
250 return { | 248 return { |
251 height: this.itemHeight_, | 249 height: this.itemHeight_, |
252 marginTop: 0, | 250 marginTop: 0, |
253 marginBotton: 0, | 251 marginBottom: 0, |
254 width: this.itemWidth_, | 252 width: this.itemWidth_, |
255 marginLeft: 0, | 253 marginLeft: 0, |
256 marginRight: 0 | 254 marginRight: 0 |
257 }; | 255 }; |
258 }, | 256 }, |
259 | 257 |
260 /** | 258 /** |
261 * Override the default implementation to return a predetermined size, | 259 * Override the default implementation to return a predetermined size, |
262 * which in turns allows proper layout of items even if the list is hidden. | 260 * which in turns allows proper layout of items even if the list is hidden. |
263 * @return {{height: number, width: number}} Dimensions of a single item in | 261 * @return {{height: number, width: number}} Dimensions of a single item in |
264 * the list of bluetooth device. | 262 * the list of bluetooth device. |
265 * @private | 263 * @private |
266 */ | 264 */ |
267 getDefaultItemSize_: function() { | 265 getDefaultItemSize_: function() { |
268 return { | 266 return { |
269 height: this.itemHeight_, | 267 height: this.itemHeight_, |
270 width: this.itemWidth_ | 268 width: this.itemWidth_ |
271 }; | 269 }; |
272 }, | 270 }, |
273 | 271 |
274 /** | 272 /** |
275 * Override base implementation of handleClick_, which unconditionally | 273 * Override base implementation of handleClick, which unconditionally |
276 * removes the item. In this case, removal of the element is deferred | 274 * removes the item. In this case, removal of the element is deferred |
277 * pending confirmation from the Bluetooth adapter. | 275 * pending confirmation from the Bluetooth adapter. |
278 * @param {Event} e The click event object. | 276 * @param {Event} e The click event object. |
279 * @private | 277 * @override |
278 * @protected | |
Dan Beam
2014/09/11 22:51:08
i think all of this can be /** @override */ more o
Vitaly Pavlenko
2014/09/11 23:13:09
Done.
| |
280 */ | 279 */ |
281 handleClick_: function(e) { | 280 handleClick: function(e) { |
282 if (this.disabled) | 281 if (this.disabled) |
283 return; | 282 return; |
284 | 283 |
285 var target = e.target; | 284 var target = /** @type {HTMLElement} */(e.target); |
286 if (!target.classList.contains('row-delete-button')) | 285 if (!target.classList.contains('row-delete-button')) |
287 return; | 286 return; |
288 | 287 |
289 var item = this.getListItemAncestor(target); | 288 var item = this.getListItemAncestor(target); |
290 var selected = this.selectionModel.selectedIndex; | 289 var selected = this.selectionModel.selectedIndex; |
291 var index = this.getIndexOfListItem(item); | 290 var index = this.getIndexOfListItem(item); |
292 if (item && item.deletable) { | 291 if (item && item.deletable) { |
293 if (selected != index) | 292 if (selected != index) |
294 this.setSelectedDevice_(item.data.address); | 293 this.setSelectedDevice_(item.data.address); |
295 // Device is busy until we hear back from the Bluetooth adapter. | 294 // Device is busy until we hear back from the Bluetooth adapter. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 | 337 |
339 cr.defineProperty(BluetoothListItem, 'notconnectable', | 338 cr.defineProperty(BluetoothListItem, 'notconnectable', |
340 cr.PropertyKind.BOOL_ATTR); | 339 cr.PropertyKind.BOOL_ATTR); |
341 | 340 |
342 return { | 341 return { |
343 BluetoothListItem: BluetoothListItem, | 342 BluetoothListItem: BluetoothListItem, |
344 BluetoothDeviceList: BluetoothDeviceList, | 343 BluetoothDeviceList: BluetoothDeviceList, |
345 Constants: Constants | 344 Constants: Constants |
346 }; | 345 }; |
347 }); | 346 }); |
OLD | NEW |