Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Side by Side Diff: Source/devtools/front_end/network/BlockedURLsPane.js

Issue 1311693004: [DevTools] UI for blocked URLs. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: typo Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 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 /**
6 * @constructor
7 * @extends {WebInspector.VBox}
8 */
9 WebInspector.BlockedURLsPane = function()
10 {
11 WebInspector.VBox.call(this);
12 this.element.classList.add("blocked-urls");
13 this.registerRequiredCSS("network/blockedURLsPane.css");
pfeldman 2015/08/28 17:15:49 Since you have a css, could you move it into shado
dgozman 2015/08/31 21:17:39 Definitely.
14
15 this._blockedURLsSetting = WebInspector.moduleSetting("blockedURLs");
16 this._blockedURLsSetting.addChangeListener(this._update, this);
17
18 this._toolbar = new WebInspector.Toolbar(this.element);
19 this._toolbar.element.addEventListener("click", consumeEvent);
20 this._toolbar.appendToolbarItem(new WebInspector.ToolbarText(WebInspector.UI String("Requests containing following URLs will be blocked")));
21 this._toolbar.appendToolbarItem(new WebInspector.ToolbarSpace());
pfeldman 2015/08/28 17:15:49 Toolbar is a continuous set of buttons. Lets move
dgozman 2015/08/31 21:17:39 Changed to custom element.
22 var addButton = new WebInspector.ToolbarButton(WebInspector.UIString("Add UR L"), "add-toolbar-item");
23 addButton.addEventListener("click", this._addButtonClicked.bind(this));
24 this._toolbar.appendToolbarItem(addButton);
25 var clearButton = new WebInspector.ToolbarButton(WebInspector.UIString("Remo ve all"), "clear-toolbar-item");
26 clearButton.addEventListener("click", this._removeAllButtonClicked.bind(this ));
27 this._toolbar.appendToolbarItem(clearButton);
28
29 this._emptyElement = this.element.createChild("div", "no-blocked-urls");
30 this._emptyElement.textContent = WebInspector.UIString("No blocked URLs");
31 this._emptyElement.addEventListener("contextmenu", this._emptyElementContext Menu.bind(this), true);
32
33 this._listElement = this.element.createChild("div", "blocked-urls-list");
34 this._filterUI = new WebInspector.BlockedURLsPane.FilterUI();
35
36 /** @type {!Array<!WebInspector.NetworkRequest>} */
37 this._blockedRequests = [];
38 WebInspector.targetManager.addModelListener(WebInspector.NetworkManager, Web Inspector.NetworkManager.EventTypes.RequestFinished, this._onRequestFinished, th is);
39
40 this._update();
41 }
42
43 WebInspector.BlockedURLsPane.prototype = {
44 /**
45 * @return {!WebInspector.FilterUI}
46 */
47 filterUI: function()
48 {
49 return this._filterUI;
50 },
51
52 /**
53 * @param {!Event} event
54 */
55 _emptyElementContextMenu: function(event)
56 {
57 var contextMenu = new WebInspector.ContextMenu(event);
58 contextMenu.appendItem(WebInspector.UIString.capitalize("Add ^URL"), thi s._addButtonClicked.bind(this));
59 contextMenu.show();
60 },
61
62 /**
63 * @param {!WebInspector.Event=} event
64 */
65 _addButtonClicked: function(event)
66 {
67 if (event)
68 event.consume();
pfeldman 2015/08/28 17:15:49 Why consuming WebInspectorEvent?
dgozman 2015/08/31 21:17:39 Copy-pasta :)
69
70 var element = this._createElement("", this._blockedURLsSetting.get().len gth);
71 this._listElement.appendChild(element);
72 element.scrollIntoView();
73
74 /**
75 * @param {boolean} accept
76 * @param {!Element} e
77 * @param {string} text
78 * @this {WebInspector.BlockedURLsPane}
79 */
80 function finishEditing(accept, e, text)
81 {
82 this._listElement.removeChild(element);
83 if (accept && text)
84 this._addBlockedURL(text);
85 }
86
87 WebInspector.InplaceEditor.startEditing(element._label, new WebInspector .InplaceEditor.Config(finishEditing.bind(this, true), finishEditing.bind(this, f alse)));
88 },
89
90 /**
91 * @param {string} url
92 */
93 _addBlockedURL: function(url)
94 {
95 var blocked = this._blockedURLsSetting.get();
96 blocked.push(url);
97 this._blockedURLsSetting.set(blocked);
98 },
99
100 /**
101 * @param {number} index
102 */
103 _removeBlockedURL: function(index)
104 {
105 var blocked = this._blockedURLsSetting.get();
106 blocked.splice(index, 1);
107 this._blockedURLsSetting.set(blocked);
108 },
109
110 /**
111 * @param {number} index
112 * @param {string} url
113 */
114 _changeBlockedURL: function(index, url)
115 {
116 var blocked = this._blockedURLsSetting.get();
117 blocked.splice(index, 1, url);
118 this._blockedURLsSetting.set(blocked);
119 },
120
121 /**
122 * @param {!WebInspector.Event=} event
123 */
124 _removeAllButtonClicked: function(event)
125 {
126 if (event)
127 event.consume();
128 this._blockedURLsSetting.set([]);
129 },
130
131 /**
132 * @param {number} index
133 * @param {!Event} event
134 */
135 _contextMenu: function(index, event)
136 {
137 var contextMenu = new WebInspector.ContextMenu(event);
138 contextMenu.appendItem(WebInspector.UIString.capitalize("Add ^URL"), thi s._addButtonClicked.bind(this));
139 contextMenu.appendItem(WebInspector.UIString.capitalize("Remove ^URL"), this._removeBlockedURL.bind(this, index));
140 contextMenu.appendItem(WebInspector.UIString.capitalize("Remove ^all"), this._removeAllButtonClicked.bind(this));
141 contextMenu.show();
142 },
143
144 /**
145 * @param {!Element} element
146 * @param {number} index
147 */
148 _labelClicked: function(element, index)
149 {
150 /**
151 * @param {boolean} accept
152 * @param {!Element} e
153 * @param {string} text
154 * @this {WebInspector.BlockedURLsPane}
155 */
156 function finishEditing(accept, e, text)
157 {
158 if (accept)
159 this._changeBlockedURL(index, text);
160 }
161
162 WebInspector.InplaceEditor.startEditing(element, new WebInspector.Inplac eEditor.Config(finishEditing.bind(this, true), finishEditing.bind(this, false))) ;
163 },
164
165 _update: function()
166 {
167 delete this._updateTimeout;
168
169 this._listElement.removeChildren();
170 var blocked = this._blockedURLsSetting.get();
171 for (var index = 0; index < blocked.length; index++)
172 this._listElement.appendChild(this._createElement(blocked[index], in dex));
173
174 this._emptyElement.classList.toggle("hidden", !!blocked.length);
175 this._filterUI.setActive(!!blocked.length);
176 this._filterUI.dispatchEventToListeners(WebInspector.FilterUI.Events.Fil terChanged);
177 },
178
179 /**
180 * @param {string} url
181 * @param {number} index
182 * @return {!Element}
183 */
184 _createElement: function(url, index)
185 {
186 var element = createElementWithClass("div", "blocked-url");
187
188 var label = element.createChild("div", "blocked-url-text");
189 element._label = label;
190 label.textContent = url;
191
192 var count = this._blockedRequestsCount(url);
193 var countElement = element.createChild("div", "blocked-count monospace") ;
194 countElement.textContent = String.sprintf("[%d]", count);
195 countElement.title = WebInspector.UIString(count === 1 ? "%d request blo cked by this URL" : "%d requests blocked by this URL", count);
196
197 var removeButton = element.createChild("div", "remove-button");
198 removeButton.title = WebInspector.UIString("Remove URL");
199 removeButton.addEventListener("click", this._removeBlockedURL.bind(this, index), false);
200
201 element.addEventListener("contextmenu", this._contextMenu.bind(this, ind ex), true);
202 element.addEventListener("dblclick", this._labelClicked.bind(this, label , index), false);
203 return element;
204 },
205
206 /**
207 * @param {string} url
208 * @return {number}
209 */
210 _blockedRequestsCount: function(url)
211 {
212 if (!url)
213 return 0;
214
215 var result = 0;
216 for (var request of this._blockedRequests) {
pfeldman 2015/08/28 17:15:49 Looks like you could have a URL -> count map inste
dgozman 2015/08/31 21:17:39 Done.
217 if (request.url.indexOf(url) !== -1)
218 result++;
219 }
220 return result;
221 },
222
223 reset: function()
224 {
225 this._blockedRequests = [];
226 },
227
228 /**
229 * @param {!WebInspector.Event} event
230 */
231 _onRequestFinished: function(event)
232 {
233 var request = /** @type {!WebInspector.NetworkRequest} */ (event.data);
234 if (request.blocked) {
235 this._blockedRequests.push(request);
236 this._scheduleUpdate();
237 }
238 },
239
240 _scheduleUpdate: function()
241 {
242 if (!this._updateTimeout)
pfeldman 2015/08/28 17:15:49 Should you use throttler here instead?
dgozman 2015/08/31 21:17:39 Done.
243 this._updateTimeout = this.element.window().setTimeout(this._update. bind(this), 200);
244 },
245
246 __proto__: WebInspector.VBox.prototype
247 }
248
249
250 /**
251 * @constructor
252 * @extends {WebInspector.Object}
253 * @implements {WebInspector.FilterUI}
254 */
255 WebInspector.BlockedURLsPane.FilterUI = function()
256 {
257 WebInspector.Object.call(this);
258 this._active = false;
259 this._element = createElement("span");
260 }
261
262 WebInspector.BlockedURLsPane.FilterUI.prototype = {
263 /**
264 * @override
265 * @return {boolean}
266 */
267 isActive: function()
268 {
269 return this._active;
270 },
271
272 /**
273 * @param {boolean} active
274 */
275 setActive: function(active)
276 {
277 this._active = active;
278 },
279
280 /**
281 * @override
282 * @return {!Element}
283 */
284 element: function()
285 {
286 return this._element;
287 },
288
289 __proto__: WebInspector.Object.prototype
290 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698