Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 DescriptorList and DescriptorListItem, served from | 6 * Javascript for DescriptorList and DescriptorListItem, served from |
| 7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 cr.define('descriptor_list', function() { | 10 cr.define('descriptor_list', function() { |
| 11 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; | 11 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; |
| 12 /** @const */ var ExpandableList = expandable_list.ExpandableList; | 12 /** @const */ var ExpandableList = expandable_list.ExpandableList; |
| 13 /** @const */ var ExpandableListItem = expandable_list.ExpandableListItem; | 13 /** @const */ var ExpandableListItem = expandable_list.ExpandableListItem; |
| 14 /** @const */ var Snackbar = snackbar.Snackbar; | 14 /** @const */ var Snackbar = snackbar.Snackbar; |
| 15 /** @const */ var SnackbarType = snackbar.SnackbarType; | 15 /** @const */ var SnackbarType = snackbar.SnackbarType; |
| 16 | 16 |
| 17 /** Property names for the DescriptorInfo fieldset */ | 17 /** Property names for the DescriptorInfo fieldset */ |
| 18 var INFO_PROPERTY_NAMES = { | 18 var INFO_PROPERTY_NAMES = { |
| 19 id: 'ID', | 19 id: 'ID', |
| 20 'uuid.uuid': 'UUID', | 20 'uuid.uuid': 'UUID', |
| 21 }; | 21 }; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * A list item that displays the properties of a DescriptorInfo object. | 24 * A list item that displays the properties of a DescriptorInfo object. |
| 25 * A fieldset is created within the element for the primitive | 25 * A fieldset is created within the element for the primitive |
| 26 * properties, 'id' and 'uuid' within the DescriptorInfo object. | 26 * properties, 'id' and 'uuid' within the DescriptorInfo object. |
| 27 * @constructor | 27 * @constructor |
| 28 * @param {!interfaces.BluetoothDevice.DescriptorInfo} descriptorInfo | 28 * @param {!interfaces.BluetoothDevice.DescriptorInfo} descriptorInfo |
| 29 * @param {string} deviceAddress | |
| 30 * @param {string} serviceId | |
| 31 * @param {string} characteristicId | |
| 29 */ | 32 */ |
| 30 function DescriptorListItem(descriptorInfo) { | 33 function DescriptorListItem( |
| 34 descriptorInfo, deviceAddress, serviceId, characteristicId) { | |
| 31 var listItem = new ExpandableListItem(); | 35 var listItem = new ExpandableListItem(); |
| 32 listItem.__proto__ = DescriptorListItem.prototype; | 36 listItem.__proto__ = DescriptorListItem.prototype; |
| 33 | 37 |
| 34 /** @type {!interfaces.BluetoothDevice.DescriptorInfo} */ | 38 /** @type {!interfaces.BluetoothDevice.DescriptorInfo} */ |
| 35 listItem.info = descriptorInfo; | 39 listItem.info = descriptorInfo; |
| 40 /** @private {string} */ | |
| 41 listItem.deviceAddress_ = deviceAddress; | |
| 42 /** @private {string} */ | |
| 43 listItem.serviceId_ = serviceId; | |
| 44 /** @private {string} */ | |
| 45 listItem.characteristicId_ = characteristicId; | |
| 36 | 46 |
| 37 listItem.decorate(); | 47 listItem.decorate(); |
| 38 return listItem; | 48 return listItem; |
| 39 } | 49 } |
| 40 | 50 |
| 41 DescriptorListItem.prototype = { | 51 DescriptorListItem.prototype = { |
| 42 __proto__: ExpandableListItem.prototype, | 52 __proto__: ExpandableListItem.prototype, |
| 43 | 53 |
| 44 /** | 54 /** |
| 45 * Decorates the element as a descriptor list item. Creates and caches | 55 * Decorates the element as a descriptor list item. Creates and caches |
| 46 * a fieldset for displaying property values. | 56 * a fieldset for displaying property values. |
| 47 * @override | 57 * @override |
| 48 */ | 58 */ |
| 49 decorate: function() { | 59 decorate: function() { |
| 50 this.classList.add('descriptor-list-item'); | 60 this.classList.add('descriptor-list-item'); |
| 51 | 61 |
| 52 /** @private {!object_fieldset.ObjectFieldSet} */ | 62 /** @private {!object_fieldset.ObjectFieldSet} */ |
| 53 this.descriptorFieldSet_ = object_fieldset.ObjectFieldSet(); | 63 this.descriptorFieldSet_ = object_fieldset.ObjectFieldSet(); |
| 54 this.descriptorFieldSet_.setPropertyDisplayNames(INFO_PROPERTY_NAMES); | 64 this.descriptorFieldSet_.setPropertyDisplayNames(INFO_PROPERTY_NAMES); |
| 55 this.descriptorFieldSet_.setObject({ | 65 this.descriptorFieldSet_.setObject({ |
| 56 id: this.info.id, | 66 id: this.info.id, |
| 57 'uuid.uuid': this.info.uuid.uuid, | 67 'uuid.uuid': this.info.uuid.uuid, |
| 58 }); | 68 }); |
| 59 | 69 |
| 70 /** @private {!value_control.ValueControl} */ | |
| 71 this.valueControl_ = new value_control.ValueControl(); | |
| 72 this.valueControl_.load({ | |
| 73 deviceAddress: this.deviceAddress_, | |
| 74 serviceId: this.serviceId_, | |
| 75 characteristicId: this.characteristicId_, | |
| 76 descriptorId: this.info.id, | |
| 77 }); | |
| 78 | |
| 60 // Create content for display in brief content container. | 79 // Create content for display in brief content container. |
| 61 var descriptorHeaderText = document.createElement('div'); | 80 var descriptorHeaderText = document.createElement('div'); |
| 62 descriptorHeaderText.textContent = 'Descriptor:'; | 81 descriptorHeaderText.textContent = 'Descriptor:'; |
| 63 | 82 |
| 64 var descriptorHeaderValue = document.createElement('div'); | 83 var descriptorHeaderValue = document.createElement('div'); |
| 65 descriptorHeaderValue.textContent = this.info.uuid.uuid; | 84 descriptorHeaderValue.textContent = this.info.uuid.uuid; |
| 66 | 85 |
| 67 var descriptorHeader = document.createElement('div'); | 86 var descriptorHeader = document.createElement('div'); |
| 68 descriptorHeader.appendChild(descriptorHeaderText); | 87 descriptorHeader.appendChild(descriptorHeaderText); |
| 69 descriptorHeader.appendChild(descriptorHeaderValue); | 88 descriptorHeader.appendChild(descriptorHeaderValue); |
| 70 this.briefContent_.appendChild(descriptorHeader); | 89 this.briefContent_.appendChild(descriptorHeader); |
| 71 | 90 |
| 72 // Create content for display in expanded content container. | 91 // Create content for display in expanded content container. |
| 73 var descriptorInfoHeader = document.createElement('h4'); | 92 var descriptorInfoHeader = document.createElement('h4'); |
| 74 descriptorInfoHeader.textContent = 'Descriptor Info'; | 93 descriptorInfoHeader.textContent = 'Descriptor Info'; |
| 75 | 94 |
| 76 var descriptorDiv = document.createElement('div'); | 95 var descriptorDiv = document.createElement('div'); |
| 77 descriptorDiv.classList.add('flex'); | 96 descriptorDiv.classList.add('flex'); |
| 78 descriptorDiv.appendChild(this.descriptorFieldSet_); | 97 descriptorDiv.appendChild(this.descriptorFieldSet_); |
| 79 | 98 |
| 99 var valueHeader = document.createElement('h4'); | |
| 100 valueHeader.textContent = 'Value'; | |
| 101 | |
| 80 var infoDiv = document.createElement('div'); | 102 var infoDiv = document.createElement('div'); |
| 81 infoDiv.classList.add('info-container'); | 103 infoDiv.classList.add('info-container'); |
| 82 infoDiv.appendChild(descriptorInfoHeader); | 104 infoDiv.appendChild(descriptorInfoHeader); |
| 83 infoDiv.appendChild(descriptorDiv); | 105 infoDiv.appendChild(descriptorDiv); |
| 106 infoDiv.appendChild(valueHeader); | |
| 107 infoDiv.appendChild(this.valueControl_); | |
| 84 | 108 |
| 85 this.expandedContent_.appendChild(infoDiv); | 109 this.expandedContent_.appendChild(infoDiv); |
| 86 }, | 110 }, |
| 87 }; | 111 }; |
| 88 | 112 |
| 89 /** | 113 /** |
| 90 * A list that displays DescriptorListItems. | 114 * A list that displays DescriptorListItems. |
| 91 * @constructor | 115 * @constructor |
| 92 */ | 116 */ |
| 93 var DescriptorList = cr.ui.define('list'); | 117 var DescriptorList = cr.ui.define('list'); |
| 94 | 118 |
| 95 DescriptorList.prototype = { | 119 DescriptorList.prototype = { |
| 96 __proto__: ExpandableList.prototype, | 120 __proto__: ExpandableList.prototype, |
| 97 | 121 |
| 98 /** @override */ | 122 /** @override */ |
| 99 decorate: function() { | 123 decorate: function() { |
| 100 ExpandableList.prototype.decorate.call(this); | 124 ExpandableList.prototype.decorate.call(this); |
| 101 | 125 |
| 126 /** @private {?string} */ | |
| 127 this.deviceAddress_ = null; | |
| 128 /** @private {?string} */ | |
| 129 this.serviceId_ = null; | |
| 130 /** @private {?string} */ | |
| 131 this.characteristicId_ = null; | |
| 102 /** @private {boolean} */ | 132 /** @private {boolean} */ |
| 103 this.descriptorsRequested_ = false; | 133 this.descriptorsRequested_ = false; |
| 104 | 134 |
| 105 this.classList.add('descriptor-list'); | 135 this.classList.add('descriptor-list'); |
| 106 this.setEmptyMessage('No Descriptors Found'); | 136 this.setEmptyMessage('No Descriptors Found'); |
| 137 | |
|
dpapad
2017/01/26 00:28:51
Nit: Remove unnecessary blank line.
mbrunson
2017/01/26 19:21:19
Done.
| |
| 107 }, | 138 }, |
| 108 | 139 |
| 109 /** @override */ | 140 /** @override */ |
| 110 createItem: function(data) { | 141 createItem: function(data) { |
| 111 return new DescriptorListItem(data); | 142 return new DescriptorListItem( |
| 143 data, this.deviceAddress_, this.serviceId_, this.characteristicId_); | |
| 112 }, | 144 }, |
| 113 | 145 |
| 114 /** | 146 /** |
| 115 * Loads the descriptor list with an array of DescriptorInfo from | 147 * Loads the descriptor list with an array of DescriptorInfo from |
| 116 * the device with |deviceAddress|, service with |serviceId|, and | 148 * the device with |deviceAddress|, service with |serviceId|, and |
| 117 * characteristic with |characteristicId|. If no active connection to the | 149 * characteristic with |characteristicId|. If no active connection to the |
| 118 * device exists, one is created. | 150 * device exists, one is created. |
| 119 * @param {string} deviceAddress | 151 * @param {string} deviceAddress |
| 120 * @param {string} serviceId | 152 * @param {string} serviceId |
| 121 * @param {string} characteristicId | 153 * @param {string} characteristicId |
| 122 */ | 154 */ |
| 123 load: function(deviceAddress, serviceId, characteristicId) { | 155 load: function(deviceAddress, serviceId, characteristicId) { |
| 124 if (this.descriptorsRequested_ || !this.isSpinnerShowing()) | 156 if (this.descriptorsRequested_ || !this.isSpinnerShowing()) |
| 125 return; | 157 return; |
| 126 | 158 |
| 159 this.deviceAddress_ = deviceAddress; | |
| 160 this.serviceId_ = serviceId; | |
| 161 this.characteristicId_ = characteristicId; | |
| 127 this.descriptorsRequested_ = true; | 162 this.descriptorsRequested_ = true; |
| 128 | 163 |
| 129 device_broker.connectToDevice(deviceAddress) | 164 device_broker.connectToDevice(deviceAddress) |
| 130 .then(function(device) { | 165 .then(function(device) { |
| 131 return device.getDescriptors(serviceId, characteristicId); | 166 return device.getDescriptors(serviceId, characteristicId); |
| 132 }.bind(this)) | 167 }.bind(this)) |
| 133 .then(function(response) { | 168 .then(function(response) { |
| 134 this.setData(new ArrayDataModel(response.descriptors || [])); | 169 this.setData(new ArrayDataModel(response.descriptors || [])); |
| 135 this.setSpinnerShowing(false); | 170 this.setSpinnerShowing(false); |
| 136 this.descriptorsRequested_ = false; | 171 this.descriptorsRequested_ = false; |
| 137 }.bind(this)) | 172 }.bind(this)) |
| 138 .catch(function(error) { | 173 .catch(function(error) { |
| 139 this.descriptorsRequested_ = false; | 174 this.descriptorsRequested_ = false; |
| 140 Snackbar.show( | 175 Snackbar.show( |
| 141 deviceAddress + ': ' + error.message, SnackbarType.ERROR, | 176 deviceAddress + ': ' + error.message, SnackbarType.ERROR, |
| 142 'Retry', function() { | 177 'Retry', function() { |
| 143 this.load(deviceAddress, serviceId, characteristicId); | 178 this.load(deviceAddress, serviceId, characteristicId); |
| 144 }.bind(this)); | 179 }.bind(this)); |
| 145 }.bind(this)); | 180 }.bind(this)); |
| 146 }, | 181 }, |
| 147 }; | 182 }; |
| 148 | 183 |
| 149 return { | 184 return { |
| 150 DescriptorList: DescriptorList, | 185 DescriptorList: DescriptorList, |
| 151 DescriptorListItem: DescriptorListItem, | 186 DescriptorListItem: DescriptorListItem, |
| 152 }; | 187 }; |
| 153 }); | 188 }); |
| OLD | NEW |