OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 /** | 5 /** |
6 * Javascript for DeviceTable UI, served from | 6 * Javascript for DeviceTable UI, served from |
7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
8 */ | 8 */ |
9 | 9 |
10 cr.define('device_table', function() { | 10 cr.define('device_table', function() { |
(...skipping 29 matching lines...) Expand all Loading... | |
40 * Sets the tables device collection. | 40 * Sets the tables device collection. |
41 * @param {!device_collection.DeviceCollection} deviceCollection | 41 * @param {!device_collection.DeviceCollection} deviceCollection |
42 */ | 42 */ |
43 setDevices: function(deviceCollection) { | 43 setDevices: function(deviceCollection) { |
44 assert(!this.devices_, 'Devices can only be set once.'); | 44 assert(!this.devices_, 'Devices can only be set once.'); |
45 | 45 |
46 this.devices_ = deviceCollection; | 46 this.devices_ = deviceCollection; |
47 this.devices_.addEventListener('sorted', this.redraw_.bind(this)); | 47 this.devices_.addEventListener('sorted', this.redraw_.bind(this)); |
48 this.devices_.addEventListener('change', this.handleChange_.bind(this)); | 48 this.devices_.addEventListener('change', this.handleChange_.bind(this)); |
49 this.devices_.addEventListener('splice', this.handleSplice_.bind(this)); | 49 this.devices_.addEventListener('splice', this.handleSplice_.bind(this)); |
50 this.devices_.addEventListener( | |
51 'connectstatus', this.handleStatus_.bind(this)); | |
50 | 52 |
51 this.redraw_(); | 53 this.redraw_(); |
52 }, | 54 }, |
53 | 55 |
54 /** | 56 /** |
55 * Updates table row on change event of the device collection. | 57 * Updates table row on change event of the device collection. |
56 * @private | 58 * @private |
57 * @param {!CustomEvent} event | 59 * @param {!CustomEvent} event |
58 */ | 60 */ |
59 handleChange_: function(event) { | 61 handleChange_: function(event) { |
60 this.updateRow_(this.devices_.item(event.index), event.index); | 62 this.updateRow_(this.devices_.item(event.index), event.index); |
61 }, | 63 }, |
62 | 64 |
63 /** | 65 /** |
66 * Fires a connect request event for the row |index|. | |
67 * @private | |
68 * @param {number} index | |
69 */ | |
70 handleConnectBtn_: function(index) { | |
71 var event = new CustomEvent('connectrequest', { | |
72 detail: { | |
73 index: index, | |
ortuno
2016/11/09 03:24:03
I would include the address rather than the index
mbrunson
2016/11/09 23:39:37
Done.
| |
74 } | |
75 }); | |
76 bluetooth_internals.publish(event); | |
77 }, | |
78 | |
79 /** | |
64 * Updates table row on splice event of the device collection. | 80 * Updates table row on splice event of the device collection. |
65 * @private | 81 * @private |
66 * @param {!CustomEvent} event | 82 * @param {!CustomEvent} event |
67 */ | 83 */ |
68 handleSplice_: function(event) { | 84 handleSplice_: function(event) { |
69 event.removed.forEach(function() { | 85 event.removed.forEach(function() { |
70 this.body_.deleteRow(event.index); | 86 this.body_.deleteRow(event.index); |
71 }, this); | 87 }, this); |
72 | 88 |
73 event.added.forEach(function(device, index) { | 89 event.added.forEach(function(device, index) { |
74 this.insertRow_(device, event.index + index); | 90 this.insertRow_(device, event.index + index); |
75 }, this); | 91 }, this); |
76 }, | 92 }, |
77 | 93 |
94 handleStatus_: function(event) { | |
95 var index = event.detail.index; | |
96 this.updateRow_(this.devices_.item(index), index); | |
97 | |
98 var row = this.body_.rows[index]; | |
99 var cellCount = row.cells.length; | |
100 var connectErrorCell = row.cells[cellCount - 1]; | |
101 connectErrorCell.textContent = event.detail.message; | |
102 }, | |
103 | |
78 /** | 104 /** |
79 * Inserts a new row at |index| and updates it with info from |device|. | 105 * Inserts a new row at |index| and updates it with info from |device|. |
80 * @private | 106 * @private |
81 * @param {!device_collection.Device} device | 107 * @param {!device_collection.Device} device |
82 * @param {?number} index | 108 * @param {?number} index |
83 */ | 109 */ |
84 insertRow_: function(device, index) { | 110 insertRow_: function(device, index) { |
85 var row = this.body_.insertRow(index); | 111 var row = this.body_.insertRow(index); |
86 row.id = device.info.address; | 112 row.id = device.info.address; |
87 | 113 |
88 for (var i = 0; i < this.headers_.length; i++) { | 114 for (var i = 0; i < this.headers_.length; i++) { |
89 row.insertCell(); | 115 row.insertCell(); |
90 } | 116 } |
91 | 117 |
118 // Make two extra cells for the connect button and connect errors. | |
119 var connectCell = row.insertCell(); | |
120 var connectErrorCell = row.insertCell(); | |
ortuno
2016/11/09 03:24:03
Would it make sense to show a snackbar[1] instead?
mbrunson
2016/11/09 23:39:37
Yeah. That would be ideal. TODO added.
| |
121 | |
122 var connectButton = document.createElement('button'); | |
123 connectCell.appendChild(connectButton); | |
124 connectButton.addEventListener('click', function() { | |
125 this.handleConnectBtn_(row.sectionRowIndex); | |
126 }.bind(this)); | |
127 | |
92 this.updateRow_(device, row.sectionRowIndex); | 128 this.updateRow_(device, row.sectionRowIndex); |
93 }, | 129 }, |
94 | 130 |
95 /** | 131 /** |
96 * Deletes and recreates the table using the cached |devices_|. | 132 * Deletes and recreates the table using the cached |devices_|. |
97 * @private | 133 * @private |
98 */ | 134 */ |
99 redraw_: function() { | 135 redraw_: function() { |
100 this.removeChild(this.body_); | 136 this.removeChild(this.body_); |
101 this.appendChild(document.createElement('tbody')); | 137 this.appendChild(document.createElement('tbody')); |
(...skipping 14 matching lines...) Expand all Loading... | |
116 updateRow_: function(device, index) { | 152 updateRow_: function(device, index) { |
117 assert(this.body_.rows[index], 'Row ' + index + ' is not in the table.'); | 153 assert(this.body_.rows[index], 'Row ' + index + ' is not in the table.'); |
118 var row = this.body_.rows[index]; | 154 var row = this.body_.rows[index]; |
119 | 155 |
120 if (device.removed) { | 156 if (device.removed) { |
121 row.classList.add(REMOVED_CSS); | 157 row.classList.add(REMOVED_CSS); |
122 } else { | 158 } else { |
123 row.classList.remove(REMOVED_CSS); | 159 row.classList.remove(REMOVED_CSS); |
124 } | 160 } |
125 | 161 |
162 var cellCount = row.cells.length; | |
163 var connectButton = row.cells[cellCount - 2].children[0]; | |
164 if (device.proxy) { | |
ortuno
2016/11/09 03:24:03
switch (device.connectionStatus) {
case 'disconn
mbrunson
2016/11/09 23:39:37
Done.
| |
165 connectButton.textContent = 'Forget'; | |
166 } else { | |
167 connectButton.textContent = 'Inspect'; | |
168 } | |
169 | |
126 // Update the properties based on the header field path. | 170 // Update the properties based on the header field path. |
127 for (var i = 0; i < this.headers_.length; i++) { | 171 for (var i = 0; i < this.headers_.length; i++) { |
128 var header = this.headers_[i]; | 172 var header = this.headers_[i]; |
129 var propName = header.dataset.field; | 173 var propName = header.dataset.field; |
130 | 174 |
131 var parts = propName.split('.'); | 175 var parts = propName.split('.'); |
132 var obj = device.info; | 176 var obj = device.info; |
133 while (obj != null && parts.length > 0) { | 177 while (obj != null && parts.length > 0) { |
134 var part = parts.shift(); | 178 var part = parts.shift(); |
135 obj = obj[part]; | 179 obj = obj[part]; |
136 } | 180 } |
137 | 181 |
182 if (propName == 'is_gatt_connected') { | |
183 obj ? obj = 'Connected' : obj = 'Not Connected'; | |
184 } | |
185 | |
138 var cell = row.cells[i]; | 186 var cell = row.cells[i]; |
139 cell.textContent = obj || 'Unknown'; | 187 cell.textContent = (obj == null || obj == undefined) ? 'Unknown' : obj; |
140 cell.dataset.label = header.textContent; | 188 cell.dataset.label = header.textContent; |
141 } | 189 } |
142 }, | 190 }, |
143 }; | 191 }; |
144 | 192 |
145 return { | 193 return { |
146 DeviceTable: DeviceTable, | 194 DeviceTable: DeviceTable, |
147 }; | 195 }; |
148 }); | 196 }); |
OLD | NEW |