Index: chrome/browser/resources/bluetooth_internals/device_table.js |
diff --git a/chrome/browser/resources/bluetooth_internals/device_table.js b/chrome/browser/resources/bluetooth_internals/device_table.js |
index b4db9b030f079ea9f7947d784a2b3442efd672e9..c57925f1cc8628e096898007d09081ca8fb9ac18 100644 |
--- a/chrome/browser/resources/bluetooth_internals/device_table.js |
+++ b/chrome/browser/resources/bluetooth_internals/device_table.js |
@@ -61,6 +61,53 @@ cr.define('device_table', function() { |
}, |
/** |
+ * Generates a function to handle click events on the connect button for the |
+ * given |row|. |
+ * @private |
+ * @param {HTMLTableRowElement} row The table row that was clicked. |
+ * @return {function(Event)} |
+ */ |
+ handleConnectBtn_: function(row) { |
ortuno
2016/11/08 04:38:02
So currently our code is split in two UI (Device T
mbrunson
2016/11/08 22:45:13
Ahh. I see your point and agree with the event-bas
|
+ return function(event) { |
+ var cellCount = row.cells.length; |
+ var connectCell = row.cells[cellCount - 2]; |
+ var connectButton = connectCell.children[0]; |
+ var connectErrorCell = row.cells[cellCount - 1]; |
+ |
+ connectErrorCell.textContent = ''; |
+ |
+ var device = this.devices_.getByAddress(row.id); |
+ if (device.proxy) { |
+ connectButton.textContent = 'Disconnecting...'; |
+ connectButton.disabled = true; |
+ device.disconnect(); |
+ } else { |
+ connectButton.textContent = 'Connecting...'; |
+ connectButton.disabled = true; |
+ |
+ this.handleConnect_(device).catch(function(error) { |
+ connectErrorCell.textContent = error.message; |
+ connectButton.textContent = 'Connect'; |
+ connectButton.disabled = false; |
+ }); |
+ } |
+ |
+ }.bind(this); |
+ }, |
+ |
+ /** |
+ * Creates a connection to the device and updates the device collection. |
+ * @private |
+ * @param {device_collection.Device} device |
+ * @return {Promise} rejects if connection failed, resolves otherwise. |
+ */ |
+ handleConnect_: function(device) { |
+ return device.connect().then(function() { |
+ this.devices_.addOrUpdate(device.info); |
+ }.bind(this)); |
+ }, |
+ |
+ /** |
* Updates table row on splice event of the device collection. |
* @private |
* @param {!CustomEvent} event |
@@ -89,6 +136,17 @@ cr.define('device_table', function() { |
row.insertCell(); |
} |
+ // Make two extra cells for the connect button and connect errors. |
+ var connectCell = row.insertCell(); |
+ var connectErrorCell = row.insertCell(); |
+ |
+ var connectButton = document.createElement('button'); |
+ connectCell.appendChild(connectButton); |
+ |
+ connectButton.textContent = 'Connect'; |
+ connectButton.addEventListener('click', |
+ this.handleConnectBtn_(row).bind(this)); |
+ |
this.updateRow_(device, row.sectionRowIndex); |
}, |
@@ -123,6 +181,26 @@ cr.define('device_table', function() { |
row.classList.remove(REMOVED_CSS); |
} |
+ var cellCount = row.cells.length; |
+ var connectCell = row.cells[cellCount - 2]; |
ortuno
2016/11/08 04:38:02
Is there a better way to retrieve this? Would it m
mbrunson
2016/11/08 22:45:13
The connection-related columns are always at the e
ortuno
2016/11/09 03:24:03
Right, but getting a cell by counting from the end
mbrunson
2016/11/09 23:39:37
Done.
|
+ var connectButton = connectCell.children[0]; |
+ var connectErrorCell = row.cells[cellCount - 1]; |
+ |
+ if (!device.connectionPending) { |
+ if (device.info.is_gatt_connected) { |
ortuno
2016/11/08 04:38:02
This is a little tricky because there are two diff
mbrunson
2016/11/08 22:45:13
I'll use inspect/forget for now. Done.
|
+ connectButton.textContent = 'Disconnect'; |
+ } else if (device.proxy) { |
+ connectButton.textContent = 'Connect'; |
+ connectErrorCell.textContent = 'Lost connection'; |
+ device.proxy = null; |
+ } else { |
+ connectButton.textContent = 'Connect'; |
+ } |
+ |
+ connectButton.disabled = false; |
+ } |
+ |
+ |
// Update the properties based on the header field path. |
for (var i = 0; i < this.headers_.length; i++) { |
var header = this.headers_[i]; |
@@ -136,7 +214,7 @@ cr.define('device_table', function() { |
} |
var cell = row.cells[i]; |
- cell.textContent = obj || 'Unknown'; |
+ cell.textContent = (obj == null || obj == undefined) ? 'Unknown' : obj; |
cell.dataset.label = header.textContent; |
} |
}, |