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