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 7bfcee8369bbce5b761992525d81d50802451720..77c23cf182039b4cb8ff049bfdaeca8b1ce23298 100644 |
--- a/chrome/browser/resources/bluetooth_internals/device_table.js |
+++ b/chrome/browser/resources/bluetooth_internals/device_table.js |
@@ -6,9 +6,7 @@ |
* Javascript for DeviceTable UI, served from |
* chrome://bluetooth-internals/. |
*/ |
- |
cr.define('device_table', function() { |
- |
var REMOVED_CSS = 'removed'; |
/** |
@@ -21,7 +19,7 @@ cr.define('device_table', function() { |
this.devices_ = null; |
return document.importNode($('table-template').content.children[0], |
- true /* deep */); |
+ true /* deep */); |
}); |
DeviceTable.prototype = { |
__proto__: HTMLTableElement.prototype, |
@@ -65,6 +63,51 @@ cr.define('device_table', function() { |
}, |
/** |
+ * Generates a function to handle click events on the connect button for the |
+ * given |row|. |
+ * @param {HTMLTableRowElement} row The table row that was clicked. |
+ * @return {function(Event)} |
+ */ |
+ handleConnectBtn_: function(row) { |
+ 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. |
+ * @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. |
* @param {!Event} event |
*/ |
@@ -91,6 +134,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); |
}, |
@@ -122,6 +176,26 @@ cr.define('device_table', function() { |
row.classList.remove(REMOVED_CSS); |
} |
+ var cellCount = row.cells.length; |
+ var connectCell = row.cells[cellCount - 2]; |
+ var connectButton = connectCell.children[0]; |
+ var connectErrorCell = row.cells[cellCount - 1]; |
+ |
+ if (!device.connectionPending) { |
+ if (device.info.is_gatt_connected) { |
+ 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]; |
@@ -135,8 +209,8 @@ cr.define('device_table', function() { |
} |
var cell = row.cells[i]; |
- cell.textContent = obj || 'Unknown'; |
- cell.dataset.label = header.innerText; |
+ cell.textContent = (obj == null || obj == undefined) ? 'Unknown' : obj; |
+ cell.dataset.label = header.textContent; |
} |
}, |
}; |