blob: 262d25bf40e6a17aaf9b115549c4a5b62dcb0366 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright 2014 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 */
10Settings.FrameworkBlackboxSettingsTab = class extends UI.VBox {
11 constructor() {
12 super(true);
13 this.registerRequiredCSS('settings/frameworkBlackboxSettingsTab.css');
14
15 this.contentElement.createChild('div', 'header').textContent = Common.UIString('Framework Blackbox Patterns');
16 this.contentElement.createChild('div', 'blackbox-content-scripts')
17 .appendChild(UI.SettingsUI.createSettingCheckbox(
18 Common.UIString('Blackbox content scripts'), Common.moduleSetting('skipContentScripts'), true));
19
20 this._blackboxLabel = Common.UIString('Blackbox');
21 this._disabledLabel = Common.UIString('Disabled');
22
23 this._list = new UI.ListWidget(this);
24 this._list.element.classList.add('blackbox-list');
25 this._list.registerRequiredCSS('settings/frameworkBlackboxSettingsTab.css');
26
27 const placeholder = createElementWithClass('div', 'blackbox-list-empty');
28 placeholder.textContent = Common.UIString('No blackboxed patterns');
29 this._list.setEmptyPlaceholder(placeholder);
30 this._list.show(this.contentElement);
31 const addPatternButton =
32 UI.createTextButton(Common.UIString('Add pattern...'), this._addButtonClicked.bind(this), 'add-button');
33 this.contentElement.appendChild(addPatternButton);
34
35 this._setting = Common.moduleSetting('skipStackFramesPattern');
36 this._setting.addChangeListener(this._settingUpdated, this);
37
38 this.setDefaultFocusedElement(addPatternButton);
39 this.contentElement.tabIndex = 0;
40 }
41
42 /**
43 * @override
44 */
45 wasShown() {
46 super.wasShown();
47 this._settingUpdated();
48 }
49
50 _settingUpdated() {
51 this._list.clear();
52 const patterns = this._setting.getAsArray();
53 for (let i = 0; i < patterns.length; ++i)
54 this._list.appendItem(patterns[i], true);
55 }
56
57 _addButtonClicked() {
58 this._list.addNewItem(this._setting.getAsArray().length, {pattern: '', disabled: false});
59 }
60
61 /**
62 * @override
63 * @param {*} item
64 * @param {boolean} editable
65 * @return {!Element}
66 */
67 renderItem(item, editable) {
68 const element = createElementWithClass('div', 'blackbox-list-item');
69 const pattern = element.createChild('div', 'blackbox-pattern');
70 pattern.textContent = item.pattern;
71 pattern.title = item.pattern;
72 element.createChild('div', 'blackbox-separator');
73 element.createChild('div', 'blackbox-behavior').textContent =
74 item.disabled ? this._disabledLabel : this._blackboxLabel;
75 if (item.disabled)
76 element.classList.add('blackbox-disabled');
77 return element;
78 }
79
80 /**
81 * @override
82 * @param {*} item
83 * @param {number} index
84 */
85 removeItemRequested(item, index) {
86 const patterns = this._setting.getAsArray();
87 patterns.splice(index, 1);
88 this._setting.setAsArray(patterns);
89 }
90
91 /**
92 * @override
93 * @param {*} item
94 * @param {!UI.ListWidget.Editor} editor
95 * @param {boolean} isNew
96 */
97 commitEdit(item, editor, isNew) {
98 item.pattern = editor.control('pattern').value.trim();
99 item.disabled = editor.control('behavior').value === this._disabledLabel;
100
101 const list = this._setting.getAsArray();
102 if (isNew)
103 list.push(item);
104 this._setting.setAsArray(list);
105 }
106
107 /**
108 * @override
109 * @param {*} item
110 * @return {!UI.ListWidget.Editor}
111 */
112 beginEdit(item) {
113 const editor = this._createEditor();
114 editor.control('pattern').value = item.pattern;
115 editor.control('behavior').value = item.disabled ? this._disabledLabel : this._blackboxLabel;
116 return editor;
117 }
118
119 /**
120 * @return {!UI.ListWidget.Editor}
121 */
122 _createEditor() {
123 if (this._editor)
124 return this._editor;
125
126 const editor = new UI.ListWidget.Editor();
127 this._editor = editor;
128 const content = editor.contentElement();
129
130 const titles = content.createChild('div', 'blackbox-edit-row');
131 titles.createChild('div', 'blackbox-pattern').textContent = Common.UIString('Pattern');
132 titles.createChild('div', 'blackbox-separator blackbox-separator-invisible');
133 titles.createChild('div', 'blackbox-behavior').textContent = Common.UIString('Behavior');
134
135 const fields = content.createChild('div', 'blackbox-edit-row');
136 fields.createChild('div', 'blackbox-pattern')
137 .appendChild(editor.createInput('pattern', 'text', '/framework\\.js$', patternValidator.bind(this)));
138 fields.createChild('div', 'blackbox-separator blackbox-separator-invisible');
139 fields.createChild('div', 'blackbox-behavior')
140 .appendChild(editor.createSelect('behavior', [this._blackboxLabel, this._disabledLabel], behaviorValidator));
141
142 return editor;
143
144 /**
145 * @param {*} item
146 * @param {number} index
147 * @param {!HTMLInputElement|!HTMLSelectElement} input
148 * @this {Settings.FrameworkBlackboxSettingsTab}
149 * @return {boolean}
150 */
151 function patternValidator(item, index, input) {
152 const pattern = input.value.trim();
153 const patterns = this._setting.getAsArray();
154 for (let i = 0; i < patterns.length; ++i) {
155 if (i !== index && patterns[i].pattern === pattern)
156 return false;
157 }
158
159 let regex;
160 try {
161 regex = new RegExp(pattern);
162 } catch (e) {
163 }
164 return !!(pattern && regex);
165 }
166
167 /**
168 * @param {*} item
169 * @param {number} index
170 * @param {!HTMLInputElement|!HTMLSelectElement} input
171 * @return {boolean}
172 */
173 function behaviorValidator(item, index, input) {
174 return true;
175 }
176 }
177};