Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: devtools-panel.js

Issue 29695580: Issue 5093 - Indicate whether request in devtools panel belongs to tab (Closed)
Left Patch Set: Created Feb. 12, 2018, 4:36 p.m.
Right Patch Set: Created March 2, 2018, 3:53 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « devtools-panel.html ('k') | skin/devtools-panel.css » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 { 161 {
162 for (let element of elements) 162 for (let element of elements)
163 { 163 {
164 if (element.innerText.search(query) != -1) 164 if (element.innerText.search(query) != -1)
165 return false; 165 return false;
166 } 166 }
167 } 167 }
168 return true; 168 return true;
169 } 169 }
170 170
171 function performSearch(table, query) 171 function performSearch(rows, query)
172 { 172 {
173 for (let row of table.rows) 173 for (let row of rows)
174 { 174 {
175 if (shouldFilterRow(row, query)) 175 if (shouldFilterRow(row, query))
176 row.classList.add("filtered-by-search"); 176 row.classList.add("filtered-by-search");
177 else 177 else
178 row.classList.remove("filtered-by-search"); 178 row.classList.remove("filtered-by-search");
179 } 179 }
180 } 180 }
181 181
182 function cancelSearch(table) 182 function cancelSearch(table)
183 { 183 {
184 for (let row of table.rows) 184 for (let row of table.rows)
185 row.classList.remove("filtered-by-search"); 185 row.classList.remove("filtered-by-search");
186 } 186 }
187 187
188 /** 188 function createZebra(rows, state, type)
189 * Return a generic <tr> placeholder 189 {
190 * double linked with the <tr> itself to simplify 190 let i = 0;
191 * tbody swaps / filtering of undesired rows. 191 for (const tr of rows)
192 * @param {HTMLTableRowElement} tr 192 {
193 * @returns {Comment} 193 tr.hidden = (state ? tr.dataset.state !== state : false) ||
194 */ 194 (type ? tr.dataset.type !== type : false);
195 function getPlaceholder(tr) 195
196 { 196 if (!tr.hidden)
197 let placeholder = tr.placeholder; 197 tr.classList.toggle("odd", !!(i++ % 2));
198 if (!placeholder)
199 {
200 placeholder = document.createComment("placeholder");
201 tr.placeholder = placeholder;
202 placeholder.placeholder = tr;
203 }
204 return placeholder;
205 }
206
207 /**
208 * Put back all hidden table rows and eventually
209 * filters those that do not match the state or type.
210 * @param {HTMLTableSectionElement} tbody
211 * @param {DOMStringMap} dataset
212 */
213 function updateRows(tbody, dataset)
214 {
215 const {filterState, filterType} = dataset;
216
217 // create a list of possible CSS filter
218 // excluding #background-items from the list of rows to consider
219 const query = [];
220 if (filterState)
221 {
222 query.push(`tr:not(#background-items):not([data-state="${filterState}"])`);
223 }
224 if (filterType)
225 {
226 query.push(`tr:not(#background-items):not([data-type="${filterType}"])`);
227 }
228
229 // use filters to match nodes or retrieve them later on
230 const selector = query.join(",");
231 for (const node of tbody.childNodes)
232 {
233 // comments linked to table rows
234 // that won't match selector will be used
235 // to place their linked row back in the tbody
236 if (
237 node.nodeType === Node.COMMENT_NODE &&
238 (!selector || !node.placeholder.matches(selector))
239 )
240 {
241 tbody.replaceChild(node.placeholder, node);
242 }
243 }
244
245 // if there is a list of nodes to filter
246 if (selector)
247 {
248 // per each of them put a placeholder inside the tbody
249 for (const tr of tbody.querySelectorAll(selector))
250 {
251 tbody.replaceChild(getPlaceholder(tr), tr);
252 }
253 } 198 }
254 } 199 }
255 200
256 document.addEventListener("DOMContentLoaded", () => 201 document.addEventListener("DOMContentLoaded", () =>
257 { 202 {
258 let container = document.getElementById("items"); 203 const container = document.getElementById("items");
259 let table = container.querySelector("tbody"); 204 const foregroundTable = container.querySelector("table.foreground tbody");
260 let bgItems = container.querySelector("#background-items"); 205 const backgroundTable = container.querySelector("table.background tbody");
261 let template = document.querySelector("template").content.firstElementChild; 206 const template = document.querySelector("template")
207 .content.firstElementChild;
208
209 // We update rows by message index.
210 // Since we don't have a linear collection of rows anymore,
211 // all records, as row, will be indexed in here.
212 // Memory wise, this is just a collection that points
213 // at live DOM nodes (rows), so this does not imply
214 // higher memory consumption, leaks, or any other concern.
215 const records = [];
216
217 const filterState = document.getElementById("filter-state");
218 const filterType = document.getElementById("filter-type");
219
220 const updateRowsState = () =>
221 {
222 const state = filterState.value;
223 const type = filterType.value;
224 createZebra(foregroundTable.querySelectorAll("tr"), state, type);
225 const notFirstTR = "tr:not(:first-child)";
226 createZebra(backgroundTable.querySelectorAll(notFirstTR), state, type);
227 };
262 228
263 document.getElementById("reload").addEventListener("click", () => 229 document.getElementById("reload").addEventListener("click", () =>
264 { 230 {
265 ext.devtools.inspectedWindow.reload(); 231 ext.devtools.inspectedWindow.reload();
266 }, false); 232 }, false);
267 233
268 document.getElementById("filter-state").addEventListener("change", (event) => 234 // update rows visibility state per each change
269 { 235 filterState.addEventListener("change", updateRowsState, false);
270 container.dataset.filterState = event.target.value; 236 filterType.addEventListener("change", updateRowsState, false);
271 updateRows(table, container.dataset);
272 }, false);
273
274 document.getElementById("filter-type").addEventListener("change", (event) =>
275 {
276 container.dataset.filterType = event.target.value;
277 updateRows(table, container.dataset);
278 }, false);
279 237
280 ext.onMessage.addListener((message) => 238 ext.onMessage.addListener((message) =>
281 { 239 {
282 switch (message.type) 240 switch (message.type)
283 { 241 {
284 case "add-record": 242 case "add-record":
285 let record = createRecord(message.request, message.filter, template); 243 const record = createRecord(message.request, message.filter, template);
286 // what is the official way to know if a request is from background tab? 244 records.push(record);
287 if (/^SCRIPT|BACKGROUND$/.test(message.request.type)) 245 // waiting for 6402 to ship
288 { 246 if (message.request.docDomain == null)
289 table.appendChild(record.cloneNode(true)); 247 backgroundTable.appendChild(record);
290 }
291 else 248 else
292 { 249 foregroundTable.appendChild(record);
293 table.insertBefore(record, bgItems);
294 }
295 break; 250 break;
296 251
297 case "update-record": 252 case "update-record":
298 let oldRow = table.getElementsByTagName("tr")[message.index]; 253 let oldRow = records[message.index];
299 let newRow = createRecord(message.request, message.filter, template); 254 let newRow = createRecord(message.request, message.filter, template);
255 records[message.index] = newRow;
300 oldRow.parentNode.replaceChild(newRow, oldRow); 256 oldRow.parentNode.replaceChild(newRow, oldRow);
301 newRow.classList.add("changed"); 257 newRow.classList.add("changed");
302 container.classList.add("has-changes"); 258 container.classList.add("has-changes");
303 break; 259 break;
304 260
305 case "remove-record": 261 case "remove-record":
306 let row = table.getElementsByTagName("tr")[message.index]; 262 let row = records.splice(message.index, 1)[0];
307 row.parentNode.removeChild(row); 263 row.parentNode.removeChild(row);
308 container.classList.add("has-changes"); 264 container.classList.add("has-changes");
309 break; 265 break;
310 266
311 case "reset": 267 case "reset":
312 table.innerHTML = ""; 268 records.splice(0);
269 foregroundTable.innerHTML = "";
313 container.classList.remove("has-changes"); 270 container.classList.remove("has-changes");
314 break; 271 break;
315 } 272 }
273 updateRowsState();
316 }); 274 });
317 275
318 window.addEventListener("message", (event) => 276 window.addEventListener("message", (event) =>
319 { 277 {
320 switch (event.data.type) 278 switch (event.data.type)
321 { 279 {
322 case "performSearch": 280 case "performSearch":
323 performSearch(table, event.data.queryString); 281 performSearch(records, event.data.queryString);
324 lastFilterQuery = event.data.queryString; 282 lastFilterQuery = event.data.queryString;
325 break; 283 break;
326 case "cancelSearch": 284 case "cancelSearch":
327 cancelSearch(table); 285 cancelSearch(records);
328 lastFilterQuery = null; 286 lastFilterQuery = null;
329 break; 287 break;
330 } 288 }
331 }); 289 });
332 290
333 // Since Chrome 54 the themeName is accessible, for earlier versions we must 291 // Since Chrome 54 the themeName is accessible, for earlier versions we must
334 // assume the default theme is being used. 292 // assume the default theme is being used.
335 // https://bugs.chromium.org/p/chromium/issues/detail?id=608869 293 // https://bugs.chromium.org/p/chromium/issues/detail?id=608869
336 let theme = browser.devtools.panels.themeName || "default"; 294 let theme = browser.devtools.panels.themeName || "default";
337 document.body.classList.add(theme); 295 document.body.classList.add(theme);
338 }, false); 296 }, false);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld