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 Device-related features, served from | |
7 * chrome://bluetooth-internals/. | |
8 */ | |
9 | |
10 cr.define('device', function() { | |
ortuno
2016/10/26 03:26:11
Split the DeviceTable code into a separate file. W
mbrunson
2016/10/29 00:49:43
Done.
| |
11 | |
12 var REMOVED_CSS = 'removed'; | |
13 | |
14 /** | |
15 * A table that lists the devices and responds to changes in the given | |
16 * DeviceCollection. | |
17 * @constructor | |
18 * @extends {HTMLTableElement} | |
19 */ | |
20 var DeviceTable = cr.ui.define(function() { | |
21 this.devices_ = null; | |
22 | |
23 return document.importNode($('table-template').content.children[0], | |
24 true /* deep */); | |
25 }); | |
26 DeviceTable.prototype = { | |
27 __proto__: HTMLTableElement.prototype, | |
28 | |
29 /** | |
30 * Decorates an element as a UI element class. Caches references to the | |
31 * table body and headers. | |
32 */ | |
33 decorate: function() { | |
34 this.body_ = this.tBodies[0]; | |
35 this.headers_ = this.tHead.rows[0].cells; | |
36 }, | |
37 | |
38 /** | |
39 * Sets the tables device collection. | |
40 * @param {!DeviceCollection} deviceCollection | |
41 */ | |
42 setDevices: function(deviceCollection) { | |
43 if (this.devices_) { | |
44 this.devices_.removeEventListener('sorted', this.redraw_.bind(this)); | |
45 this.devices_.removeEventListener('change', | |
46 this.handleChange_.bind(this)); | |
47 this.devices_.removeEventListener('splice', | |
48 this.handleSplice_.bind(this)); | |
49 } | |
50 | |
51 this.devices_ = deviceCollection; | |
52 this.devices_.addEventListener('sorted', this.redraw_.bind(this)); | |
53 this.devices_.addEventListener('change', this.handleChange_.bind(this)); | |
54 this.devices_.addEventListener('splice', this.handleSplice_.bind(this)); | |
55 | |
56 this.redraw_(); | |
57 }, | |
58 | |
59 /** | |
60 * Updates table row on change event of the device collection. | |
61 * @param {!Event} event | |
62 */ | |
63 handleChange_: function(event) { | |
64 this.updateRow_(this.devices_.item(event.index), event.index); | |
65 }, | |
66 | |
67 /** | |
68 * Updates table row on splice event of the device collection. | |
69 * @param {!Event} event | |
70 */ | |
71 handleSplice_: function(event) { | |
72 event.removed.forEach(function() { | |
73 this.removeRow(event.index); | |
74 }, this); | |
75 | |
76 event.added.forEach(function(device, index) { | |
77 this.newRowForDevice_(device, event.index + index); | |
78 }, this); | |
79 }, | |
80 | |
81 /** | |
82 * Inserts a new row at |index| and updates it with info from |device|. | |
83 * @param {!Device} device | |
84 * @param {?number} index | |
85 */ | |
86 newRowForDevice_: function(device, index) { | |
87 var row = this.body_.insertRow(index); | |
88 row.id = device.info.address; | |
89 | |
90 for (var i = 0; i < this.headers_.length; i++) { | |
91 row.insertCell(); | |
92 } | |
93 | |
94 this.updateRow_(device, row.sectionRowIndex); | |
95 }, | |
96 | |
97 /** | |
98 * Deletes and recreates the table using the cached |devices_|. | |
99 */ | |
100 redraw_: function() { | |
101 this.removeChild(this.body_); | |
102 this.appendChild(document.createElement('tbody')); | |
103 this.body_ = this.tBodies[0]; | |
104 this.body_.classList.add('table-body'); | |
105 | |
106 for (var i = 0; i < this.devices_.length; i++) { | |
107 this.newRowForDevice_(this.devices_.item(i)); | |
108 } | |
109 }, | |
110 | |
111 /** | |
112 * Updates the row at |index| with the info from |device|. | |
113 * @param {!Device} device | |
114 * @param {!number} index | |
115 */ | |
116 updateRow_: function(device, index) { | |
117 var row = this.body_.rows[index]; | |
118 | |
119 if (device.removed) { | |
120 row.classList.add(REMOVED_CSS); | |
121 } else { | |
122 row.classList.remove(REMOVED_CSS); | |
123 } | |
124 | |
125 // Update the properties based on the header field path. | |
126 for (var i = 0; i < this.headers_.length; i++) { | |
127 var header = this.headers_[i]; | |
128 var propName = header.dataset.field; | |
129 | |
130 var parts = propName.split('.'); | |
131 var obj = device.info; | |
132 while (obj != null && parts.length > 0) { | |
133 var part = parts.shift(); | |
134 obj = obj[part]; | |
135 } | |
136 | |
137 var cell = row.cells[i]; | |
138 cell.textContent = obj || 'Unknown'; | |
139 cell.dataset.label = header.innerText; | |
140 } | |
141 }, | |
142 }; | |
143 | |
144 /* | |
145 * Collection of devices with helper functions for searching and updating by | |
146 * device address. | |
147 * @constructor | |
148 * @param {!Array} array the starting collection of devices. | |
149 * @extends {cr.ui.ArrayDataModel} | |
150 */ | |
151 var DeviceCollection = function(array) { | |
152 cr.ui.ArrayDataModel.call(this, array); | |
153 }; | |
154 DeviceCollection.prototype = { | |
155 __proto__: cr.ui.ArrayDataModel.prototype, | |
156 getByAddress: function(address) { | |
157 for (var i = 0; i < this.length; i++) { | |
158 var device = this.item(i); | |
159 if (address == device.info.address) | |
160 return device; | |
161 } | |
162 return null; | |
163 }, | |
164 update: function(device) { | |
ortuno
2016/10/26 03:26:11
Would it make sense to move all of the device info
mbrunson
2016/10/29 00:49:43
Hmm...ok. A few other places could have better sep
| |
165 var oldDevice = this.getByAddress(device.info.address); | |
166 if (oldDevice) { | |
167 this.replaceItem(oldDevice, device); | |
168 } else { | |
169 this.push(device); | |
170 } | |
171 } | |
172 }; | |
173 | |
174 /* | |
175 * Data model for a cached device. | |
176 * @constructor | |
177 * @param {!bluetoothDevice.DeviceInfo} info | |
178 */ | |
179 var Device = function(info) { | |
180 this.info = info; | |
181 this.removed = false; | |
182 }; | |
183 | |
184 return { | |
185 Device: Device, | |
186 DeviceCollection, DeviceCollection, | |
187 DeviceTable: DeviceTable, | |
188 }; | |
189 }); | |
OLD | NEW |