Tim van der Lippe | 83f02be | 2020-01-23 11:11:40 | [diff] [blame] | 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. |
Tim van der Lippe | 119690c | 2020-01-13 12:31:30 | [diff] [blame] | 4 | |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 5 | import * as Common from '../common/common.js'; |
| 6 | import * as UI from '../ui/ui.js'; |
| 7 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 8 | /** |
| 9 | * @implements {UI.ListWidget.Delegate} |
| 10 | * @unrestricted |
| 11 | */ |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 12 | export class NetworkManageCustomHeadersView extends UI.Widget.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 13 | /** |
| 14 | * @param {!Array.<!{title: string, editable: boolean}>} columnData |
| 15 | * @param {function(string) : boolean} addHeaderColumnCallback |
| 16 | * @param {function(string, string) : boolean} changeHeaderColumnCallback |
| 17 | * @param {function(string) : boolean} removeHeaderColumnCallback |
| 18 | */ |
| 19 | constructor(columnData, addHeaderColumnCallback, changeHeaderColumnCallback, removeHeaderColumnCallback) { |
| 20 | super(true); |
| 21 | this.registerRequiredCSS('network/networkManageCustomHeadersView.css'); |
| 22 | |
| 23 | this.contentElement.classList.add('custom-headers-wrapper'); |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 24 | this.contentElement.createChild('div', 'header').textContent = Common.UIString.UIString('Manage Header Columns'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 25 | |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 26 | this._list = new UI.ListWidget.ListWidget(this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 27 | this._list.element.classList.add('custom-headers-list'); |
| 28 | this._list.registerRequiredCSS('network/networkManageCustomHeadersView.css'); |
| 29 | |
| 30 | const placeholder = createElementWithClass('div', 'custom-headers-list-list-empty'); |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 31 | placeholder.textContent = Common.UIString.UIString('No custom headers'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 32 | this._list.setEmptyPlaceholder(placeholder); |
| 33 | this._list.show(this.contentElement); |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 34 | this.contentElement.appendChild(UI.UIUtils.createTextButton( |
Mathias Bynens | 23ee1aa | 2020-03-02 12:06:38 | [diff] [blame^] | 35 | Common.UIString.UIString('Add custom header…'), this._addButtonClicked.bind(this), 'add-button')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | |
| 37 | /** @type {!Map.<string, !{title: string, editable: boolean}>} */ |
| 38 | this._columnConfigs = new Map(); |
| 39 | columnData.forEach(columnData => this._columnConfigs.set(columnData.title.toLowerCase(), columnData)); |
| 40 | |
| 41 | this._addHeaderColumnCallback = addHeaderColumnCallback; |
| 42 | this._changeHeaderColumnCallback = changeHeaderColumnCallback; |
| 43 | this._removeHeaderColumnCallback = removeHeaderColumnCallback; |
| 44 | |
| 45 | this.contentElement.tabIndex = 0; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @override |
| 50 | */ |
| 51 | wasShown() { |
| 52 | this._headersUpdated(); |
| 53 | } |
| 54 | |
| 55 | _headersUpdated() { |
| 56 | this._list.clear(); |
| 57 | this._columnConfigs.forEach(headerData => this._list.appendItem({header: headerData.title}, headerData.editable)); |
| 58 | } |
| 59 | |
| 60 | _addButtonClicked() { |
| 61 | this._list.addNewItem(this._columnConfigs.size, {header: ''}); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @override |
| 66 | * @param {*} item |
| 67 | * @param {boolean} editable |
| 68 | * @return {!Element} |
| 69 | */ |
| 70 | renderItem(item, editable) { |
| 71 | const element = createElementWithClass('div', 'custom-headers-list-item'); |
| 72 | const header = element.createChild('div', 'custom-header-name'); |
| 73 | header.textContent = item.header; |
| 74 | header.title = item.header; |
| 75 | return element; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @override |
| 80 | * @param {*} item |
| 81 | * @param {number} index |
| 82 | */ |
| 83 | removeItemRequested(item, index) { |
| 84 | this._removeHeaderColumnCallback(item.header); |
| 85 | this._columnConfigs.delete(item.header.toLowerCase()); |
| 86 | this._headersUpdated(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @override |
| 91 | * @param {*} item |
| 92 | * @param {!UI.ListWidget.Editor} editor |
| 93 | * @param {boolean} isNew |
| 94 | */ |
| 95 | commitEdit(item, editor, isNew) { |
| 96 | const headerId = editor.control('header').value.trim(); |
| 97 | let success; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 98 | if (isNew) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 99 | success = this._addHeaderColumnCallback(headerId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 100 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 101 | success = this._changeHeaderColumnCallback(item.header, headerId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 102 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 103 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 104 | if (success && !isNew) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 105 | this._columnConfigs.delete(item.header.toLowerCase()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 106 | } |
| 107 | if (success) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 108 | this._columnConfigs.set(headerId.toLowerCase(), {title: headerId, editable: true}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 109 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 110 | |
| 111 | this._headersUpdated(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @override |
| 116 | * @param {*} item |
| 117 | * @return {!UI.ListWidget.Editor} |
| 118 | */ |
| 119 | beginEdit(item) { |
| 120 | const editor = this._createEditor(); |
| 121 | editor.control('header').value = item.header; |
| 122 | return editor; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return {!UI.ListWidget.Editor} |
| 127 | */ |
| 128 | _createEditor() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 129 | if (this._editor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 130 | return this._editor; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 131 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 132 | |
| 133 | const editor = new UI.ListWidget.Editor(); |
| 134 | this._editor = editor; |
| 135 | const content = editor.contentElement(); |
| 136 | |
| 137 | const titles = content.createChild('div', 'custom-headers-edit-row'); |
Tim van der Lippe | 0ed1d2b | 2020-02-04 13:45:13 | [diff] [blame] | 138 | titles.createChild('div', 'custom-headers-header').textContent = Common.UIString.UIString('Header Name'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 139 | |
| 140 | const fields = content.createChild('div', 'custom-headers-edit-row'); |
| 141 | fields.createChild('div', 'custom-headers-header') |
| 142 | .appendChild(editor.createInput('header', 'text', 'x-custom-header', validateHeader.bind(this))); |
| 143 | |
| 144 | return editor; |
| 145 | |
| 146 | /** |
| 147 | * @param {*} item |
| 148 | * @param {number} index |
| 149 | * @param {!HTMLInputElement|!HTMLSelectElement} input |
Tim van der Lippe | 119690c | 2020-01-13 12:31:30 | [diff] [blame] | 150 | * @this {NetworkManageCustomHeadersView} |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 151 | * @return {!UI.ListWidget.ValidatorResult} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 152 | */ |
| 153 | function validateHeader(item, index, input) { |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 154 | let valid = true; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 155 | const headerId = editor.control('header').value.trim().toLowerCase(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 156 | if (this._columnConfigs.has(headerId) && item.header !== headerId) { |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 157 | valid = false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 158 | } |
Amanda Baker | ca50282 | 2019-07-02 00:01:28 | [diff] [blame] | 159 | return {valid}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 160 | } |
| 161 | } |
Paul Lewis | 5650965 | 2019-12-06 12:51:58 | [diff] [blame] | 162 | } |