Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | /** |
| 7 | * @implements {UI.ListWidget.Delegate} |
| 8 | * @unrestricted |
| 9 | */ |
| 10 | Network.NetworkManageCustomHeadersView = class extends UI.VBox { |
| 11 | /** |
| 12 | * @param {!Array.<!{title: string, editable: boolean}>} columnData |
| 13 | * @param {function(string) : boolean} addHeaderColumnCallback |
| 14 | * @param {function(string, string) : boolean} changeHeaderColumnCallback |
| 15 | * @param {function(string) : boolean} removeHeaderColumnCallback |
| 16 | */ |
| 17 | constructor(columnData, addHeaderColumnCallback, changeHeaderColumnCallback, removeHeaderColumnCallback) { |
| 18 | super(true); |
| 19 | this.registerRequiredCSS('network/networkManageCustomHeadersView.css'); |
| 20 | |
| 21 | this.contentElement.classList.add('custom-headers-wrapper'); |
| 22 | this.contentElement.createChild('div', 'header').textContent = Common.UIString('Manage Header Columns'); |
| 23 | |
| 24 | this._list = new UI.ListWidget(this); |
| 25 | this._list.element.classList.add('custom-headers-list'); |
| 26 | this._list.registerRequiredCSS('network/networkManageCustomHeadersView.css'); |
| 27 | |
| 28 | const placeholder = createElementWithClass('div', 'custom-headers-list-list-empty'); |
| 29 | placeholder.textContent = Common.UIString('No custom headers'); |
| 30 | this._list.setEmptyPlaceholder(placeholder); |
| 31 | this._list.show(this.contentElement); |
| 32 | this.contentElement.appendChild(UI.createTextButton( |
| 33 | Common.UIString('Add custom header\u2026'), this._addButtonClicked.bind(this), 'add-button')); |
| 34 | |
| 35 | /** @type {!Map.<string, !{title: string, editable: boolean}>} */ |
| 36 | this._columnConfigs = new Map(); |
| 37 | columnData.forEach(columnData => this._columnConfigs.set(columnData.title.toLowerCase(), columnData)); |
| 38 | |
| 39 | this._addHeaderColumnCallback = addHeaderColumnCallback; |
| 40 | this._changeHeaderColumnCallback = changeHeaderColumnCallback; |
| 41 | this._removeHeaderColumnCallback = removeHeaderColumnCallback; |
| 42 | |
| 43 | this.contentElement.tabIndex = 0; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @override |
| 48 | */ |
| 49 | wasShown() { |
| 50 | this._headersUpdated(); |
| 51 | } |
| 52 | |
| 53 | _headersUpdated() { |
| 54 | this._list.clear(); |
| 55 | this._columnConfigs.forEach(headerData => this._list.appendItem({header: headerData.title}, headerData.editable)); |
| 56 | } |
| 57 | |
| 58 | _addButtonClicked() { |
| 59 | this._list.addNewItem(this._columnConfigs.size, {header: ''}); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @override |
| 64 | * @param {*} item |
| 65 | * @param {boolean} editable |
| 66 | * @return {!Element} |
| 67 | */ |
| 68 | renderItem(item, editable) { |
| 69 | const element = createElementWithClass('div', 'custom-headers-list-item'); |
| 70 | const header = element.createChild('div', 'custom-header-name'); |
| 71 | header.textContent = item.header; |
| 72 | header.title = item.header; |
| 73 | return element; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @override |
| 78 | * @param {*} item |
| 79 | * @param {number} index |
| 80 | */ |
| 81 | removeItemRequested(item, index) { |
| 82 | this._removeHeaderColumnCallback(item.header); |
| 83 | this._columnConfigs.delete(item.header.toLowerCase()); |
| 84 | this._headersUpdated(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @override |
| 89 | * @param {*} item |
| 90 | * @param {!UI.ListWidget.Editor} editor |
| 91 | * @param {boolean} isNew |
| 92 | */ |
| 93 | commitEdit(item, editor, isNew) { |
| 94 | const headerId = editor.control('header').value.trim(); |
| 95 | let success; |
| 96 | if (isNew) |
| 97 | success = this._addHeaderColumnCallback(headerId); |
| 98 | else |
| 99 | success = this._changeHeaderColumnCallback(item.header, headerId); |
| 100 | |
| 101 | if (success && !isNew) |
| 102 | this._columnConfigs.delete(item.header.toLowerCase()); |
| 103 | if (success) |
| 104 | this._columnConfigs.set(headerId.toLowerCase(), {title: headerId, editable: true}); |
| 105 | |
| 106 | this._headersUpdated(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @override |
| 111 | * @param {*} item |
| 112 | * @return {!UI.ListWidget.Editor} |
| 113 | */ |
| 114 | beginEdit(item) { |
| 115 | const editor = this._createEditor(); |
| 116 | editor.control('header').value = item.header; |
| 117 | return editor; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @return {!UI.ListWidget.Editor} |
| 122 | */ |
| 123 | _createEditor() { |
| 124 | if (this._editor) |
| 125 | return this._editor; |
| 126 | |
| 127 | const editor = new UI.ListWidget.Editor(); |
| 128 | this._editor = editor; |
| 129 | const content = editor.contentElement(); |
| 130 | |
| 131 | const titles = content.createChild('div', 'custom-headers-edit-row'); |
| 132 | titles.createChild('div', 'custom-headers-header').textContent = Common.UIString('Header Name'); |
| 133 | |
| 134 | const fields = content.createChild('div', 'custom-headers-edit-row'); |
| 135 | fields.createChild('div', 'custom-headers-header') |
| 136 | .appendChild(editor.createInput('header', 'text', 'x-custom-header', validateHeader.bind(this))); |
| 137 | |
| 138 | return editor; |
| 139 | |
| 140 | /** |
| 141 | * @param {*} item |
| 142 | * @param {number} index |
| 143 | * @param {!HTMLInputElement|!HTMLSelectElement} input |
| 144 | * @this {Network.NetworkManageCustomHeadersView} |
| 145 | * @return {boolean} |
| 146 | */ |
| 147 | function validateHeader(item, index, input) { |
| 148 | const headerId = editor.control('header').value.trim().toLowerCase(); |
| 149 | if (this._columnConfigs.has(headerId) && item.header !== headerId) |
| 150 | return false; |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | }; |