Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2015 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 | NodeMain.NodeConnectionsPanel = class extends UI.Panel { |
| 6 | constructor() { |
| 7 | super('node-connection'); |
| 8 | this.registerRequiredCSS('node_main/nodeConnectionsPanel.css'); |
| 9 | this.contentElement.classList.add('node-panel'); |
| 10 | |
| 11 | const container = this.contentElement.createChild('div', 'node-panel-center'); |
| 12 | |
| 13 | const image = container.createChild('img', 'node-panel-logo'); |
| 14 | image.src = 'https://nodejs.org/static/images/logos/nodejs-new-pantone-black.png'; |
| 15 | |
| 16 | InspectorFrontendHost.events.addEventListener( |
| 17 | InspectorFrontendHostAPI.Events.DevicesDiscoveryConfigChanged, this._devicesDiscoveryConfigChanged, this); |
| 18 | |
| 19 | /** @type {!Adb.Config} */ |
| 20 | this._config; |
| 21 | |
| 22 | this.contentElement.tabIndex = 0; |
| 23 | this.setDefaultFocusedElement(this.contentElement); |
| 24 | |
| 25 | // Trigger notification once. |
| 26 | InspectorFrontendHost.setDevicesUpdatesEnabled(false); |
| 27 | InspectorFrontendHost.setDevicesUpdatesEnabled(true); |
| 28 | |
| 29 | this._networkDiscoveryView = new NodeMain.NodeConnectionsView(config => { |
| 30 | this._config.networkDiscoveryConfig = config; |
| 31 | InspectorFrontendHost.setDevicesDiscoveryConfig(this._config); |
| 32 | }); |
| 33 | this._networkDiscoveryView.show(container); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param {!Common.Event} event |
| 38 | */ |
| 39 | _devicesDiscoveryConfigChanged(event) { |
| 40 | this._config = /** @type {!Adb.Config} */ (event.data); |
| 41 | this._networkDiscoveryView.discoveryConfigChanged(this._config.networkDiscoveryConfig); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * @implements {UI.ListWidget.Delegate<Adb.PortForwardingRule>} |
| 47 | */ |
| 48 | NodeMain.NodeConnectionsView = class extends UI.VBox { |
| 49 | /** |
| 50 | * @param {function(!Adb.NetworkDiscoveryConfig)} callback |
| 51 | */ |
| 52 | constructor(callback) { |
| 53 | super(); |
| 54 | this._callback = callback; |
| 55 | this.element.classList.add('network-discovery-view'); |
| 56 | |
| 57 | const networkDiscoveryFooter = this.element.createChild('div', 'network-discovery-footer'); |
Mandy Chen | b07d998 | 2019-06-14 21:30:34 | [diff] [blame] | 58 | const documentationLink = UI.XLink.create('https://nodejs.org/en/docs/inspector/', ls`Node.js debugging guide`); |
| 59 | networkDiscoveryFooter.appendChild(UI.formatLocalized( |
| 60 | 'Specify network endpoint and DevTools will connect to it automatically. Read %s to learn more.', |
| 61 | [documentationLink])); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 62 | |
| 63 | /** @type {!UI.ListWidget<!Adb.PortForwardingRule>} */ |
| 64 | this._list = new UI.ListWidget(this); |
| 65 | this._list.registerRequiredCSS('node_main/nodeConnectionsPanel.css'); |
| 66 | this._list.element.classList.add('network-discovery-list'); |
| 67 | const placeholder = createElementWithClass('div', 'network-discovery-list-empty'); |
| 68 | placeholder.textContent = Common.UIString('No connections specified'); |
| 69 | this._list.setEmptyPlaceholder(placeholder); |
| 70 | this._list.show(this.element); |
| 71 | /** @type {?UI.ListWidget.Editor<!Adb.PortForwardingRule>} */ |
| 72 | this._editor = null; |
| 73 | |
| 74 | const addButton = UI.createTextButton( |
| 75 | Common.UIString('Add connection'), this._addNetworkTargetButtonClicked.bind(this), 'add-network-target-button', |
| 76 | true /* primary */); |
| 77 | this.element.appendChild(addButton); |
| 78 | |
| 79 | /** @type {!Array<{address: string}>} */ |
| 80 | this._networkDiscoveryConfig = []; |
| 81 | |
| 82 | this.element.classList.add('node-frontend'); |
| 83 | } |
| 84 | |
| 85 | _update() { |
| 86 | const config = this._networkDiscoveryConfig.map(item => item.address); |
| 87 | this._callback.call(null, config); |
| 88 | } |
| 89 | |
| 90 | _addNetworkTargetButtonClicked() { |
| 91 | this._list.addNewItem(this._networkDiscoveryConfig.length, {address: '', port: ''}); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param {!Adb.NetworkDiscoveryConfig} networkDiscoveryConfig |
| 96 | */ |
| 97 | discoveryConfigChanged(networkDiscoveryConfig) { |
| 98 | this._networkDiscoveryConfig = []; |
| 99 | this._list.clear(); |
| 100 | for (const address of networkDiscoveryConfig) { |
| 101 | const item = {address: address, port: ''}; |
| 102 | this._networkDiscoveryConfig.push(item); |
| 103 | this._list.appendItem(item, true); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @override |
| 109 | * @param {!Adb.PortForwardingRule} rule |
| 110 | * @param {boolean} editable |
| 111 | * @return {!Element} |
| 112 | */ |
| 113 | renderItem(rule, editable) { |
| 114 | const element = createElementWithClass('div', 'network-discovery-list-item'); |
| 115 | element.createChild('div', 'network-discovery-value network-discovery-address').textContent = rule.address; |
| 116 | return element; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @override |
| 121 | * @param {!Adb.PortForwardingRule} rule |
| 122 | * @param {number} index |
| 123 | */ |
| 124 | removeItemRequested(rule, index) { |
| 125 | this._networkDiscoveryConfig.splice(index, 1); |
| 126 | this._list.removeItem(index); |
| 127 | this._update(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @override |
| 132 | * @param {!Adb.PortForwardingRule} rule |
| 133 | * @param {!UI.ListWidget.Editor} editor |
| 134 | * @param {boolean} isNew |
| 135 | */ |
| 136 | commitEdit(rule, editor, isNew) { |
| 137 | rule.address = editor.control('address').value.trim(); |
| 138 | if (isNew) |
| 139 | this._networkDiscoveryConfig.push(rule); |
| 140 | this._update(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @override |
| 145 | * @param {!Adb.PortForwardingRule} rule |
| 146 | * @return {!UI.ListWidget.Editor} |
| 147 | */ |
| 148 | beginEdit(rule) { |
| 149 | const editor = this._createEditor(); |
| 150 | editor.control('address').value = rule.address; |
| 151 | return editor; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @return {!UI.ListWidget.Editor<!Adb.PortForwardingRule>} |
| 156 | */ |
| 157 | _createEditor() { |
| 158 | if (this._editor) |
| 159 | return this._editor; |
| 160 | |
| 161 | const editor = new UI.ListWidget.Editor(); |
| 162 | this._editor = editor; |
| 163 | const content = editor.contentElement(); |
| 164 | const fields = content.createChild('div', 'network-discovery-edit-row'); |
Mandy Chen | ac715f0 | 2019-05-22 19:14:01 | [diff] [blame] | 165 | const input = editor.createInput('address', 'text', ls`Network address (e.g. localhost:9229)`, addressValidator); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 166 | fields.createChild('div', 'network-discovery-value network-discovery-address').appendChild(input); |
| 167 | return editor; |
| 168 | |
| 169 | /** |
| 170 | * @param {!Adb.PortForwardingRule} rule |
| 171 | * @param {number} index |
| 172 | * @param {!HTMLInputElement|!HTMLSelectElement} input |
| 173 | * @return {boolean} |
| 174 | */ |
| 175 | function addressValidator(rule, index, input) { |
| 176 | const match = input.value.trim().match(/^([a-zA-Z0-9\.\-_]+):(\d+)$/); |
| 177 | if (!match) |
| 178 | return false; |
| 179 | const port = parseInt(match[2], 10); |
| 180 | return port <= 65535; |
| 181 | } |
| 182 | } |
| 183 | }; |