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