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 DeviceTable UI, served from |
| 7 * chrome://bluetooth-internals/. |
| 8 */ |
| 9 |
| 10 cr.define('device_table', function() { |
| 11 var REMOVED_CSS = 'removed'; |
| 12 |
| 13 /** |
| 14 * A table that lists the devices and responds to changes in the given |
| 15 * DeviceCollection. |
| 16 * @constructor |
| 17 * @extends {HTMLTableElement} |
| 18 */ |
| 19 var DeviceTable = cr.ui.define(function() { |
| 20 this.devices_ = null; |
| 21 |
| 22 return document.importNode($('table-template').content.children[0], |
| 23 true /* deep */); |
| 24 }); |
| 25 DeviceTable.prototype = { |
| 26 __proto__: HTMLTableElement.prototype, |
| 27 |
| 28 /** |
| 29 * Decorates an element as a UI element class. Caches references to the |
| 30 * table body and headers. |
| 31 */ |
| 32 decorate: function() { |
| 33 this.body_ = this.tBodies[0]; |
| 34 this.headers_ = this.tHead.rows[0].cells; |
| 35 }, |
| 36 |
| 37 /** |
| 38 * Sets the tables device collection. |
| 39 * @param {!device_collection.DeviceCollection} deviceCollection |
| 40 */ |
| 41 setDevices: function(deviceCollection) { |
| 42 if (this.devices_) { |
| 43 this.devices_.removeEventListener('sorted', this.redraw_.bind(this)); |
| 44 this.devices_.removeEventListener('change', |
| 45 this.handleChange_.bind(this)); |
| 46 this.devices_.removeEventListener('splice', |
| 47 this.handleSplice_.bind(this)); |
| 48 } |
| 49 |
| 50 this.devices_ = deviceCollection; |
| 51 this.devices_.addEventListener('sorted', this.redraw_.bind(this)); |
| 52 this.devices_.addEventListener('change', this.handleChange_.bind(this)); |
| 53 this.devices_.addEventListener('splice', this.handleSplice_.bind(this)); |
| 54 |
| 55 this.redraw_(); |
| 56 }, |
| 57 |
| 58 /** |
| 59 * Updates table row on change event of the device collection. |
| 60 * @param {!Event} event |
| 61 */ |
| 62 handleChange_: function(event) { |
| 63 this.updateRow_(this.devices_.item(event.index), event.index); |
| 64 }, |
| 65 |
| 66 /** |
| 67 * Updates table row on splice event of the device collection. |
| 68 * @param {!Event} event |
| 69 */ |
| 70 handleSplice_: function(event) { |
| 71 event.removed.forEach(function() { |
| 72 this.body_.deleteRow(event.index); |
| 73 }, this); |
| 74 |
| 75 event.added.forEach(function(device, index) { |
| 76 this.newRowForDevice_(device, event.index + index); |
| 77 }, this); |
| 78 }, |
| 79 |
| 80 /** |
| 81 * Inserts a new row at |index| and updates it with info from |device|. |
| 82 * @param {!device_collection.Device} device |
| 83 * @param {?number} index |
| 84 */ |
| 85 newRowForDevice_: function(device, index) { |
| 86 var row = this.body_.insertRow(index); |
| 87 row.id = device.info.address; |
| 88 |
| 89 for (var i = 0; i < this.headers_.length; i++) { |
| 90 row.insertCell(); |
| 91 } |
| 92 |
| 93 this.updateRow_(device, row.sectionRowIndex); |
| 94 }, |
| 95 |
| 96 /** |
| 97 * Deletes and recreates the table using the cached |devices_|. |
| 98 */ |
| 99 redraw_: function() { |
| 100 this.removeChild(this.body_); |
| 101 this.appendChild(document.createElement('tbody')); |
| 102 this.body_ = this.tBodies[0]; |
| 103 this.body_.classList.add('table-body'); |
| 104 |
| 105 for (var i = 0; i < this.devices_.length; i++) { |
| 106 this.newRowForDevice_(this.devices_.item(i)); |
| 107 } |
| 108 }, |
| 109 |
| 110 /** |
| 111 * Updates the row at |index| with the info from |device|. |
| 112 * @param {!device_collection.Device} device |
| 113 * @param {!number} index |
| 114 */ |
| 115 updateRow_: function(device, index) { |
| 116 var row = this.body_.rows[index]; |
| 117 |
| 118 if (device.removed) { |
| 119 row.classList.add(REMOVED_CSS); |
| 120 } else { |
| 121 row.classList.remove(REMOVED_CSS); |
| 122 } |
| 123 |
| 124 // Update the properties based on the header field path. |
| 125 for (var i = 0; i < this.headers_.length; i++) { |
| 126 var header = this.headers_[i]; |
| 127 var propName = header.dataset.field; |
| 128 |
| 129 var parts = propName.split('.'); |
| 130 var obj = device.info; |
| 131 while (obj != null && parts.length > 0) { |
| 132 var part = parts.shift(); |
| 133 obj = obj[part]; |
| 134 } |
| 135 |
| 136 var cell = row.cells[i]; |
| 137 cell.textContent = obj || 'Unknown'; |
| 138 cell.dataset.label = header.innerText; |
| 139 } |
| 140 }, |
| 141 }; |
| 142 |
| 143 return { |
| 144 DeviceTable: DeviceTable, |
| 145 }; |
| 146 }); |
OLD | NEW |