Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/browser/resources/bluetooth_internals/device_table.js

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Move removed statement in DeviceCollection.addOrUpdate Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 chrome://bluetooth-internals/. 6 * Javascript for DeviceTable UI, served from chrome://bluetooth-internals/.
7 */ 7 */
8 8
9 cr.define('device_table', function() { 9 cr.define('device_table', function() {
10 var REMOVED_CLASS_NAME = 'removed'; 10 var REMOVED_CLASS_NAME = 'removed';
11 11
12 var COLUMNS = {
13 NAME: 0,
14 ADDRESS: 1,
15 RSSI: 2,
16 SERVICES: 3,
17 CONNECTION_STATE: 4,
18 CONNECTION_BUTTON: 5,
ortuno 2016/11/10 02:16:38 s/CONNECTION/INSPECT/
mbrunson 2016/11/10 04:18:29 Done.
19 CONNECTION_ERROR: 6,
20 };
21
12 /** 22 /**
13 * A table that lists the devices and responds to changes in the given 23 * A table that lists the devices and responds to changes in the given
14 * DeviceCollection. 24 * DeviceCollection. Fires events for connection requests from listed
ortuno 2016/11/10 02:16:38 s/connection/inspection/
mbrunson 2016/11/10 04:18:29 Done.
25 * devices.
15 * @constructor 26 * @constructor
16 * @extends {HTMLTableElement} 27 * @extends {HTMLTableElement}
17 */ 28 */
18 var DeviceTable = cr.ui.define(function() { 29 var DeviceTable = cr.ui.define(function() {
19 // @type {?Array<device_collection.Device>} 30 // @type {Array<interfaces.BluetoothDevice.DeviceInfo>}
20 // @private 31 // @private
21 this.devices_ = null; 32 this.devices_ = null;
22 33
23 return document.importNode($('table-template').content.children[0], 34 return document.importNode($('table-template').content.children[0],
24 true /* deep */); 35 true /* deep */);
25 }); 36 });
26 37
27 DeviceTable.prototype = { 38 DeviceTable.prototype = {
28 __proto__: HTMLTableElement.prototype, 39 __proto__: HTMLTableElement.prototype,
29 40
(...skipping 19 matching lines...) Expand all
49 this.devices_.addEventListener('sorted', this.redraw_.bind(this)); 60 this.devices_.addEventListener('sorted', this.redraw_.bind(this));
50 this.devices_.addEventListener('change', this.handleChange_.bind(this)); 61 this.devices_.addEventListener('change', this.handleChange_.bind(this));
51 this.devices_.addEventListener('splice', this.handleSplice_.bind(this)); 62 this.devices_.addEventListener('splice', this.handleSplice_.bind(this));
52 63
53 this.redraw_(); 64 this.redraw_();
54 }, 65 },
55 66
56 /** 67 /**
57 * Updates table row on change event of the device collection. 68 * Updates table row on change event of the device collection.
58 * @private 69 * @private
59 * @param {!CustomEvent} event 70 * @param {!Event} event
60 */ 71 */
61 handleChange_: function(event) { 72 handleChange_: function(event) {
62 this.updateRow_(this.devices_.item(event.index), event.index); 73 this.updateRow_(this.devices_.item(event.index), event.index);
63 }, 74 },
64 75
65 /** 76 /**
77 * Fires a connect pressed event for the row |index|.
ortuno 2016/11/10 02:16:38 nit: s/connect/inspect/
mbrunson 2016/11/10 04:18:29 Done.
78 * @private
79 * @param {number} index
80 */
81 handleConnectBtn_: function(index) {
ortuno 2016/11/10 02:16:39 s/Connect/Inspect/
mbrunson 2016/11/10 04:18:29 Done.
82 var event = new CustomEvent('inspectpressed', {
83 detail: {
84 address: this.devices_.item(index).address,
85 }
86 });
87 this.dispatchEvent(event);
88 },
89
90 /**
66 * Updates table row on splice event of the device collection. 91 * Updates table row on splice event of the device collection.
67 * @private 92 * @private
68 * @param {!CustomEvent} event 93 * @param {!Event} event
69 */ 94 */
70 handleSplice_: function(event) { 95 handleSplice_: function(event) {
71 event.removed.forEach(function() { 96 event.removed.forEach(function() {
72 this.body_.deleteRow(event.index); 97 this.body_.deleteRow(event.index);
73 }, this); 98 }, this);
74 99
75 event.added.forEach(function(device, index) { 100 event.added.forEach(function(device, index) {
76 this.insertRow_(device, event.index + index); 101 this.insertRow_(device, event.index + index);
77 }, this); 102 }, this);
78 }, 103 },
79 104
80 /** 105 /**
81 * Inserts a new row at |index| and updates it with info from |device|. 106 * Inserts a new row at |index| and updates it with info from |device|.
82 * @private 107 * @private
83 * @param {!device_collection.Device} device 108 * @param {!interfaces.BluetoothDevice.DeviceInfo} device
84 * @param {?number} index 109 * @param {?number} index
85 */ 110 */
86 insertRow_: function(device, index) { 111 insertRow_: function(device, index) {
87 var row = this.body_.insertRow(index); 112 var row = this.body_.insertRow(index);
88 row.id = device.info.address; 113 row.id = device.address;
89 114
90 for (var i = 0; i < this.headers_.length; i++) { 115 for (var i = 0; i < this.headers_.length; i++) {
91 row.insertCell(); 116 row.insertCell();
92 } 117 }
93 118
119 // Make two extra cells for the connect button and connect errors.
120 var connectCell = row.insertCell();
121
122 // TODO(crbug.com/663830): Replace connection error column with better
123 // notification system.
124 var connectErrorCell = row.insertCell();
125
126 var connectButton = document.createElement('button');
ortuno 2016/11/10 02:16:38 s/connect/inspect/
mbrunson 2016/11/10 04:18:29 Done.
127 connectCell.appendChild(connectButton);
128 connectButton.addEventListener('click', function() {
129 this.handleConnectBtn_(row.sectionRowIndex);
130 }.bind(this));
131
94 this.updateRow_(device, row.sectionRowIndex); 132 this.updateRow_(device, row.sectionRowIndex);
95 }, 133 },
96 134
97 /** 135 /**
98 * Deletes and recreates the table using the cached |devices_|. 136 * Deletes and recreates the table using the cached |devices_|.
99 * @private 137 * @private
100 */ 138 */
101 redraw_: function() { 139 redraw_: function() {
102 this.removeChild(this.body_); 140 this.removeChild(this.body_);
103 this.appendChild(document.createElement('tbody')); 141 this.appendChild(document.createElement('tbody'));
104 this.body_ = this.tBodies[0]; 142 this.body_ = this.tBodies[0];
105 this.body_.classList.add('table-body'); 143 this.body_.classList.add('table-body');
106 144
107 for (var i = 0; i < this.devices_.length; i++) { 145 for (var i = 0; i < this.devices_.length; i++) {
108 this.insertRow_(this.devices_.item(i)); 146 this.insertRow_(this.devices_.item(i));
109 } 147 }
110 }, 148 },
111 149
112 /** 150 /**
113 * Updates the row at |index| with the info from |device|. 151 * Updates the row at |index| with the info from |device|.
114 * @private 152 * @private
115 * @param {!device_collection.Device} device 153 * @param {!interfaces.BluetoothDevice.DeviceInfo} device
116 * @param {number} index 154 * @param {number} index
117 */ 155 */
118 updateRow_: function(device, index) { 156 updateRow_: function(device, index) {
119 assert(this.body_.rows[index], 'Row ' + index + ' is not in the table.'); 157 assert(this.body_.rows[index], 'Row ' + index + ' is not in the table.');
120 var row = this.body_.rows[index]; 158 var row = this.body_.rows[index];
121 159
122 row.classList.toggle(REMOVED_CLASS_NAME, device.removed); 160 row.classList.toggle(REMOVED_CLASS_NAME, device.removed);
123 161
162 var connectButton = row.cells[COLUMNS.CONNECTION_BUTTON].children[0];
163 switch (device.connectionStatus) {
ortuno 2016/11/10 02:16:38 Ah I missed re-enabling the button.
mbrunson 2016/11/10 04:18:29 Done.
164 case 'disconnected':
165 connectButton.textContent = 'Inspect';
166 break;
167 case 'connected':
168 connectButton.textContent = 'Forget';
169 break;
170 case 'connecting':
171 connectButton.disabled = true;
172 break;
173 default: assert('case not handled');
174 }
175
176 // TODO(crbug.com/663830): Replace connection error column with better
177 // notification system.
178 var connectErrorCell = row.cells[COLUMNS.CONNECTION_ERROR];
179 connectErrorCell.textContent = device.connectionMessage;
180
124 // Update the properties based on the header field path. 181 // Update the properties based on the header field path.
125 for (var i = 0; i < this.headers_.length; i++) { 182 for (var i = 0; i < this.headers_.length; i++) {
126 var header = this.headers_[i]; 183 var header = this.headers_[i];
127 var propName = header.dataset.field; 184 var propName = header.dataset.field;
128 185
129 var parts = propName.split('.'); 186 var parts = propName.split('.');
130 var obj = device.info; 187 var obj = device;
131 while (obj != null && parts.length > 0) { 188 while (obj != null && parts.length > 0) {
132 var part = parts.shift(); 189 var part = parts.shift();
133 obj = obj[part]; 190 obj = obj[part];
134 } 191 }
135 192
193 if (propName == 'is_gatt_connected') {
194 obj ? obj = 'Connected' : obj = 'Not Connected';
195 }
196
136 var cell = row.cells[i]; 197 var cell = row.cells[i];
137 cell.textContent = obj || 'Unknown'; 198 cell.textContent = (obj == null || obj == undefined) ? 'Unknown' : obj;
138 cell.dataset.label = header.textContent; 199 cell.dataset.label = header.textContent;
139 } 200 }
140 }, 201 },
141 }; 202 };
142 203
143 return { 204 return {
144 DeviceTable: DeviceTable, 205 DeviceTable: DeviceTable,
145 }; 206 };
146 }); 207 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698