OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 /** |
| 6 * Javascript for DeviceCollection, served from |
| 7 * chrome://bluetooth-internals/. |
| 8 */ |
| 9 |
| 10 cr.define('device_collection', function() { |
| 11 /* |
| 12 * Collection of devices. Extends ArrayDataModel which provides a set of |
| 13 * functions and events that notifies observers when the collection changes. |
| 14 * @constructor |
| 15 * @param {!Array} array The starting collection of devices. |
| 16 * @extends {cr.ui.ArrayDataModel} |
| 17 */ |
| 18 var DeviceCollection = function(array) { |
| 19 cr.ui.ArrayDataModel.call(this, array); |
| 20 }; |
| 21 DeviceCollection.prototype = { |
| 22 __proto__: cr.ui.ArrayDataModel.prototype, |
| 23 |
| 24 /** |
| 25 * Finds the Device in the collection with the matching address. |
| 26 * @param {!string} address |
| 27 */ |
| 28 getByAddress: function(address) { |
| 29 for (var i = 0; i < this.length; i++) { |
| 30 var device = this.item(i); |
| 31 if (address == device.info.address) |
| 32 return device; |
| 33 } |
| 34 return null; |
| 35 }, |
| 36 |
| 37 /** |
| 38 * Adds or updates a Device with new DeviceInfo. |
| 39 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
| 40 */ |
| 41 addOrUpdate: function(deviceInfo) { |
| 42 var oldDevice = this.getByAddress(deviceInfo.address); |
| 43 if (oldDevice) { |
| 44 // Update rssi if it's valid |
| 45 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || |
| 46 (oldDevice.info.rssi && oldDevice.info.rssi.value); |
| 47 |
| 48 oldDevice.info = deviceInfo; |
| 49 oldDevice.info.rssi = { value: rssi }; |
| 50 oldDevice.removed = false; |
| 51 |
| 52 this.updateIndex(this.indexOf(oldDevice)); |
| 53 } else { |
| 54 this.push(new Device(deviceInfo)); |
| 55 } |
| 56 }, |
| 57 |
| 58 /** |
| 59 * Marks the Device as removed. |
| 60 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo |
| 61 */ |
| 62 remove: function(deviceInfo) { |
| 63 var device = this.getByAddress(deviceInfo.address); |
| 64 assert(device, 'Device does not exist.'); |
| 65 device.removed = true; |
| 66 this.updateIndex(this.indexOf(device)); |
| 67 } |
| 68 }; |
| 69 |
| 70 /* |
| 71 * Data model for a cached device. |
| 72 * @constructor |
| 73 * @param {!interfaces.BluetoothDevice.DeviceInfo} info |
| 74 */ |
| 75 var Device = function(info) { |
| 76 this.info = info; |
| 77 this.removed = false; |
| 78 }; |
| 79 |
| 80 return { |
| 81 DeviceCollection: DeviceCollection, |
| 82 }; |
| 83 }); |
OLD | NEW |