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