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

Delta Between Two Patch Sets: new-options.js

Issue 29373665: issue 4264 - centralize action handling (Closed)
Left Patch Set: Thomas implementation Created Jan. 27, 2017, 11:39 a.m.
Right Patch Set: Moved key handling into onKeyUp Created May 16, 2017, 10:32 a.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 | « new-options.html ('k') | no next file » | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2017 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /* globals checkShareResource, getDocLink, i18nFormatDateTime, openSharePopup,
19 E */
20
18 "use strict"; 21 "use strict";
19 22
20 (function()
21 { 23 {
22 var subscriptionsMap = Object.create(null); 24 let subscriptionsMap = Object.create(null);
23 var filtersMap = Object.create(null); 25 let filtersMap = Object.create(null);
24 var collections = Object.create(null); 26 let collections = Object.create(null);
25 var acceptableAdsUrl = null; 27 let acceptableAdsUrl = null;
26 var getMessage = ext.i18n.getMessage; 28 let isCustomFiltersLoaded = false;
27 var filterErrors = 29 let {getMessage} = ext.i18n;
28 { 30 let filterErrors = new Map([
29 "synchronize_invalid_url": "options_filterList_lastDownload_invalidURL", 31 ["synchronize_invalid_url",
30 "synchronize_connection_error": "options_filterList_lastDownload_connectionE rror", 32 "options_filterList_lastDownload_invalidURL"],
31 "synchronize_invalid_data": "options_filterList_lastDownload_invalidData", 33 ["synchronize_connection_error",
32 "synchronize_checksum_mismatch": "options_filterList_lastDownload_checksumMi smatch" 34 "options_filterList_lastDownload_connectionError"],
33 }; 35 ["synchronize_invalid_data",
36 "options_filterList_lastDownload_invalidData"],
37 ["synchronize_checksum_mismatch",
38 "options_filterList_lastDownload_checksumMismatch"]
39 ]);
34 40
35 function Collection(details) 41 function Collection(details)
36 { 42 {
37 this.details = details; 43 this.details = details;
38 this.items = []; 44 this.items = [];
39 } 45 }
40 46
41 Collection.prototype._setEmpty = function(table, text) 47 Collection.prototype._setEmpty = function(table, text)
42 { 48 {
43 var placeholder = table.querySelector(".empty-placeholder"); 49 let placeholder = table.querySelector(".empty-placeholder");
44 if (text && !placeholder) 50 if (text && !placeholder)
45 { 51 {
46 placeholder = document.createElement("li"); 52 placeholder = document.createElement("li");
47 placeholder.className = "empty-placeholder"; 53 placeholder.className = "empty-placeholder";
48 placeholder.textContent = getMessage(text); 54 placeholder.textContent = getMessage(text);
49 table.appendChild(placeholder); 55 table.appendChild(placeholder);
50 } 56 }
51 else if (placeholder) 57 else if (placeholder)
52 table.removeChild(placeholder); 58 table.removeChild(placeholder);
53 }; 59 };
54 60
55 Collection.prototype._createElementQuery = function(item) 61 Collection.prototype._createElementQuery = function(item)
56 { 62 {
57 var access = (item.url || item.text).replace(/'/g, "\\'"); 63 let access = (item.url || item.text).replace(/'/g, "\\'");
58 return function(container) 64 return function(container)
59 { 65 {
60 return container.querySelector("[data-access='" + access + "']"); 66 return container.querySelector("[data-access='" + access + "']");
61 }; 67 };
62 }; 68 };
63 69
64 Collection.prototype._getItemTitle = function(item, i) 70 Collection.prototype._getItemTitle = function(item, i)
65 { 71 {
66 if (item.url == acceptableAdsUrl) 72 if (item.url == acceptableAdsUrl)
67 return getMessage("options_acceptableAds_description"); 73 return getMessage("options_acceptableAds_description");
68 if (this.details[i].useOriginalTitle && item.originalTitle) 74 if (this.details[i].useOriginalTitle && item.originalTitle)
69 return item.originalTitle; 75 return item.originalTitle;
70 return item.title || item.url || item.text; 76 return item.title || item.url || item.text;
71 }; 77 };
72 78
73 Collection.prototype.addItem = function(item) 79 Collection.prototype.addItem = function(item)
74 { 80 {
75 if (this.items.indexOf(item) >= 0) 81 if (this.items.indexOf(item) >= 0)
76 return; 82 return;
77 83
78 this.items.push(item); 84 this.items.push(item);
79 this.items.sort(function(a, b) 85 this.items.sort((a, b) =>
80 { 86 {
81 // Make sure that Acceptable Ads is always last, since it cannot be 87 // Make sure that Acceptable Ads is always last, since it cannot be
82 // disabled, but only be removed. That way it's grouped together with 88 // disabled, but only be removed. That way it's grouped together with
83 // the "Own filter list" which cannot be disabled either at the bottom 89 // the "Own filter list" which cannot be disabled either at the bottom
84 // of the filter lists in the Advanced tab. 90 // of the filter lists in the Advanced tab.
85 if (a.url == acceptableAdsUrl) 91 if (a.url == acceptableAdsUrl)
86 return 1; 92 return 1;
87 if (b.url == acceptableAdsUrl) 93 if (b.url == acceptableAdsUrl)
88 return -1; 94 return -1;
89 95
90 var aTitle = this._getItemTitle(a, 0).toLowerCase(); 96 let aTitle = this._getItemTitle(a, 0).toLowerCase();
91 var bTitle = this._getItemTitle(b, 0).toLowerCase(); 97 let bTitle = this._getItemTitle(b, 0).toLowerCase();
92 return aTitle.localeCompare(bTitle); 98 return aTitle.localeCompare(bTitle);
93 }.bind(this)); 99 });
94 100
95 for (var j = 0; j < this.details.length; j++) 101 for (let j = 0; j < this.details.length; j++)
96 { 102 {
97 var table = E(this.details[j].id); 103 let table = E(this.details[j].id);
98 var template = table.querySelector("template"); 104 let template = table.querySelector("template");
99 var listItem = document.createElement("li"); 105 let listItem = document.createElement("li");
100 listItem.appendChild(document.importNode(template.content, true)); 106 listItem.appendChild(document.importNode(template.content, true));
101 listItem.setAttribute("aria-label", this._getItemTitle(item, j)); 107 listItem.setAttribute("aria-label", this._getItemTitle(item, j));
102 listItem.setAttribute("data-access", item.url || item.text); 108 listItem.setAttribute("data-access", item.url || item.text);
103 listItem.setAttribute("role", "section"); 109 listItem.setAttribute("role", "section");
104 110
105 var label = listItem.querySelector(".display"); 111 let label = listItem.querySelector(".display");
106 if (item.recommended && label.hasAttribute("data-tooltip")) 112 if (item.recommended && label.hasAttribute("data-tooltip"))
107 { 113 {
108 var tooltipId = label.getAttribute("data-tooltip"); 114 let tooltipId = label.getAttribute("data-tooltip");
109 tooltipId = tooltipId.replace("%value%", item.recommended); 115 tooltipId = tooltipId.replace("%value%", item.recommended);
110 label.setAttribute("data-tooltip", tooltipId); 116 label.setAttribute("data-tooltip", tooltipId);
111 } 117 }
112 118
113 var controls = listItem.querySelectorAll(".control"); 119 for (let control of listItem.querySelectorAll(".control"))
114 for (var k = 0; k < controls.length; k++) 120 {
115 { 121 if (control.hasAttribute("title"))
116 if (controls[k].hasAttribute("title")) 122 {
117 { 123 let titleValue = getMessage(control.getAttribute("title"));
118 var titleValue = getMessage(controls[k].getAttribute("title")); 124 control.setAttribute("title", titleValue);
119 controls[k].setAttribute("title", titleValue)
120 } 125 }
121 } 126 }
122 127
123 this._setEmpty(table, null); 128 this._setEmpty(table, null);
124 if (table.hasChildNodes()) 129 if (table.hasChildNodes())
125 { 130 {
126 table.insertBefore(listItem, 131 table.insertBefore(listItem,
127 table.childNodes[this.items.indexOf(item)]); 132 table.childNodes[this.items.indexOf(item)]);
128 } 133 }
129 else 134 else
130 table.appendChild(listItem); 135 table.appendChild(listItem);
131 this.updateItem(item); 136 this.updateItem(item);
132 } 137 }
133 return length; 138 return length;
134 }; 139 };
135 140
136 Collection.prototype.removeItem = function(item) 141 Collection.prototype.removeItem = function(item)
137 { 142 {
138 var index = this.items.indexOf(item); 143 let index = this.items.indexOf(item);
139 if (index == -1) 144 if (index == -1)
140 return; 145 return;
141 146
142 this.items.splice(index, 1); 147 this.items.splice(index, 1);
143 var getListElement = this._createElementQuery(item); 148 let getListElement = this._createElementQuery(item);
144 for (var i = 0; i < this.details.length; i++) 149 for (let detail of this.details)
145 { 150 {
146 var table = E(this.details[i].id); 151 let table = E(detail.id);
147 var element = getListElement(table); 152 let element = getListElement(table);
148 153
149 // Element gets removed so make sure to handle focus appropriately 154 // Element gets removed so make sure to handle focus appropriately
150 var control = element.querySelector(".control"); 155 let control = element.querySelector(".control");
151 if (control && control == document.activeElement) 156 if (control && control == document.activeElement)
152 { 157 {
153 if (!focusNextElement(element.parentElement, control)) 158 if (!focusNextElement(element.parentElement, control))
154 { 159 {
155 // Fall back to next focusable element within same tab or dialog 160 // Fall back to next focusable element within same tab or dialog
156 var focusableElement = element.parentElement; 161 let focusableElement = element.parentElement;
157 while (focusableElement) 162 while (focusableElement)
158 { 163 {
159 if (focusableElement.classList.contains("tab-content") 164 if (focusableElement.classList.contains("tab-content") ||
160 || focusableElement.classList.contains("dialog-content")) 165 focusableElement.classList.contains("dialog-content"))
161 break; 166 break;
162 167
163 focusableElement = focusableElement.parentElement; 168 focusableElement = focusableElement.parentElement;
164 } 169 }
165 focusNextElement(focusableElement || document, control); 170 focusNextElement(focusableElement || document, control);
166 } 171 }
167 } 172 }
168 173
169 element.parentElement.removeChild(element); 174 element.parentElement.removeChild(element);
170 if (this.items.length == 0) 175 if (this.items.length == 0)
171 this._setEmpty(table, this.details[i].emptyText); 176 this._setEmpty(table, detail.emptyText);
172 } 177 }
173 }; 178 };
174 179
175 Collection.prototype.updateItem = function(item) 180 Collection.prototype.updateItem = function(item)
176 { 181 {
177 var access = (item.url || item.text).replace(/'/g, "\\'"); 182 let access = (item.url || item.text).replace(/'/g, "\\'");
178 for (var i = 0; i < this.details.length; i++) 183 for (let i = 0; i < this.details.length; i++)
179 { 184 {
180 var table = E(this.details[i].id); 185 let table = E(this.details[i].id);
181 var element = table.querySelector("[data-access='" + access + "']"); 186 let element = table.querySelector("[data-access='" + access + "']");
182 if (!element) 187 if (!element)
183 continue; 188 continue;
184 189
185 var title = this._getItemTitle(item, i); 190 let title = this._getItemTitle(item, i);
186 element.querySelector(".display").textContent = title; 191 element.querySelector(".display").textContent = title;
187 element.setAttribute("aria-label", title); 192 element.setAttribute("aria-label", title);
188 if (this.details[i].searchable) 193 if (this.details[i].searchable)
189 element.setAttribute("data-search", title.toLowerCase()); 194 element.setAttribute("data-search", title.toLowerCase());
190 var control = element.querySelector(".control[role='checkbox']"); 195 let control = element.querySelector(".control[role='checkbox']");
191 if (control) 196 if (control)
192 { 197 {
193 control.setAttribute("aria-checked", item.disabled == false); 198 control.setAttribute("aria-checked", item.disabled == false);
194 if (item.url == acceptableAdsUrl && this == collections.filterLists) 199 if (item.url == acceptableAdsUrl && this == collections.filterLists)
195 control.setAttribute("disabled", true); 200 control.setAttribute("disabled", true);
196 } 201 }
197 202
198 var dateElement = element.querySelector(".date"); 203 let dateElement = element.querySelector(".date");
199 var timeElement = element.querySelector(".time"); 204 let timeElement = element.querySelector(".time");
200 if (dateElement && timeElement) 205 if (dateElement && timeElement)
201 { 206 {
202 var message = element.querySelector(".message"); 207 let message = element.querySelector(".message");
203 if (item.isDownloading) 208 if (item.isDownloading)
204 { 209 {
205 var text = getMessage("options_filterList_lastDownload_inProgress"); 210 let text = getMessage("options_filterList_lastDownload_inProgress");
206 message.textContent = text; 211 message.textContent = text;
207 element.classList.add("show-message"); 212 element.classList.add("show-message");
208 } 213 }
209 else if (item.downloadStatus != "synchronize_ok") 214 else if (item.downloadStatus != "synchronize_ok")
210 { 215 {
211 var error = filterErrors[item.downloadStatus]; 216 let error = filterErrors.get(item.downloadStatus);
212 if (error) 217 if (error)
213 message.textContent = getMessage(error); 218 message.textContent = getMessage(error);
214 else 219 else
215 message.textContent = item.downloadStatus; 220 message.textContent = item.downloadStatus;
216 element.classList.add("show-message"); 221 element.classList.add("show-message");
217 } 222 }
218 else if (item.lastDownload > 0) 223 else if (item.lastDownload > 0)
219 { 224 {
220 var dateTime = i18n_formatDateTime(item.lastDownload * 1000); 225 let dateTime = i18nFormatDateTime(item.lastDownload * 1000);
221 dateElement.textContent = dateTime[0]; 226 dateElement.textContent = dateTime[0];
222 timeElement.textContent = dateTime[1]; 227 timeElement.textContent = dateTime[1];
223 element.classList.remove("show-message"); 228 element.classList.remove("show-message");
224 } 229 }
225 } 230 }
226 231
227 var websiteElement = element.querySelector(".context-menu .website"); 232 let websiteElement = element.querySelector(".context-menu .website");
228 if (websiteElement) 233 if (websiteElement)
229 { 234 {
230 if (item.homepage) 235 if (item.homepage)
231 websiteElement.setAttribute("href", item.homepage); 236 websiteElement.setAttribute("href", item.homepage);
232 else 237 else
233 websiteElement.setAttribute("aria-hidden", true); 238 websiteElement.setAttribute("aria-hidden", true);
234 } 239 }
235 240
236 var sourceElement = element.querySelector(".context-menu .source"); 241 let sourceElement = element.querySelector(".context-menu .source");
237 if (sourceElement) 242 if (sourceElement)
238 sourceElement.setAttribute("href", item.url); 243 sourceElement.setAttribute("href", item.url);
239 } 244 }
240 }; 245 };
241 246
242 Collection.prototype.clearAll = function() 247 Collection.prototype.clearAll = function()
243 { 248 {
244 this.items = []; 249 this.items = [];
245 for (var i = 0; i < this.details.length; i++) 250 for (let detail of this.details)
246 { 251 {
247 var table = E(this.details[i].id); 252 let table = E(detail.id);
248 var element = table.firstChild; 253 let element = table.firstChild;
249 while (element) 254 while (element)
250 { 255 {
251 if (element.tagName == "LI" && !element.classList.contains("static")) 256 if (element.tagName == "LI" && !element.classList.contains("static"))
252 table.removeChild(element); 257 table.removeChild(element);
253 element = element.nextElementSibling; 258 element = element.nextElementSibling;
254 } 259 }
255 260
256 this._setEmpty(table, this.details[i].emptyText); 261 this._setEmpty(table, detail.emptyText);
257 } 262 }
258 }; 263 };
259 264
260 function focusNextElement(container, currentElement) 265 function focusNextElement(container, currentElement)
261 { 266 {
262 var focusables = container.querySelectorAll("a, button, input, .control"); 267 let focusables = container.querySelectorAll("a, button, input, .control");
263 focusables = Array.prototype.slice.call(focusables); 268 focusables = Array.prototype.slice.call(focusables);
264 var index = focusables.indexOf(currentElement); 269 let index = focusables.indexOf(currentElement);
265 index += (index == focusables.length - 1) ? -1 : 1; 270 index += (index == focusables.length - 1) ? -1 : 1;
266 271
267 var nextElement = focusables[index]; 272 let nextElement = focusables[index];
268 if (!nextElement) 273 if (!nextElement)
269 return false; 274 return false;
270 275
271 nextElement.focus(); 276 nextElement.focus();
272 return true; 277 return true;
273 } 278 }
274 279
275 collections.popular = new Collection( 280 collections.popular = new Collection([
276 [
277 { 281 {
278 id: "recommend-list-table" 282 id: "recommend-list-table"
279 } 283 }
280 ]); 284 ]);
281 collections.langs = new Collection( 285 collections.langs = new Collection([
282 [
283 { 286 {
284 id: "blocking-languages-table", 287 id: "blocking-languages-table",
285 emptyText: "options_dialog_language_added_empty" 288 emptyText: "options_dialog_language_added_empty"
286 }, 289 },
287 { 290 {
288 id: "blocking-languages-dialog-table", 291 id: "blocking-languages-dialog-table",
289 emptyText: "options_dialog_language_added_empty" 292 emptyText: "options_dialog_language_added_empty"
290 } 293 }
291 ]); 294 ]);
292 collections.allLangs = new Collection( 295 collections.allLangs = new Collection([
293 [
294 { 296 {
295 id: "all-lang-table", 297 id: "all-lang-table",
296 emptyText: "options_dialog_language_other_empty", 298 emptyText: "options_dialog_language_other_empty",
297 searchable: true 299 searchable: true
298 } 300 }
299 ]); 301 ]);
300 collections.acceptableAds = new Collection( 302 collections.acceptableAds = new Collection([
301 [
302 { 303 {
303 id: "acceptableads-table" 304 id: "acceptableads-table"
304 } 305 }
305 ]); 306 ]);
306 collections.custom = new Collection( 307 collections.custom = new Collection([
307 [
308 { 308 {
309 id: "custom-list-table" 309 id: "custom-list-table"
310 } 310 }
311 ]); 311 ]);
312 collections.whitelist = new Collection( 312 collections.whitelist = new Collection([
313 [
314 { 313 {
315 id: "whitelisting-table", 314 id: "whitelisting-table",
316 emptyText: "options_whitelisted_empty" 315 emptyText: "options_whitelisted_empty"
317 } 316 }
318 ]); 317 ]);
319 collections.customFilters = new Collection( 318 collections.customFilters = new Collection([
320 [
321 { 319 {
322 id: "custom-filters-table", 320 id: "custom-filters-table",
323 emptyText: "options_customFilters_empty" 321 emptyText: "options_customFilters_empty"
324 } 322 }
325 ]); 323 ]);
326 collections.filterLists = new Collection( 324 collections.filterLists = new Collection([
327 [
328 { 325 {
329 id: "all-filter-lists-table", 326 id: "all-filter-lists-table",
330 useOriginalTitle: true 327 useOriginalTitle: true
331 } 328 }
332 ]); 329 ]);
333 330
334 function toggleShowLanguage(subscription) 331 function toggleShowLanguage(subscription)
335 { 332 {
336 if (subscription.recommended == "ads") 333 if (subscription.recommended == "ads")
337 { 334 {
338 if (subscription.disabled) 335 if (subscription.disabled)
339 { 336 {
340 collections.allLangs.addItem(subscription); 337 collections.allLangs.addItem(subscription);
341 collections.langs.removeItem(subscription); 338 collections.langs.removeItem(subscription);
342 } 339 }
343 else 340 else
344 { 341 {
345 collections.allLangs.removeItem(subscription); 342 collections.allLangs.removeItem(subscription);
346 collections.langs.addItem(subscription); 343 collections.langs.addItem(subscription);
347 } 344 }
348 } 345 }
349 } 346 }
350 347
351 function addSubscription(subscription) 348 function addSubscription(subscription)
352 { 349 {
353 var collection; 350 let collection;
354 if (subscription.recommended) 351 if (subscription.recommended)
355 { 352 {
356 if (subscription.recommended != "ads") 353 if (subscription.recommended != "ads")
357 collection = collections.popular; 354 collection = collections.popular;
358 else if (subscription.disabled == false) 355 else if (subscription.disabled == false)
359 collection = collections.langs; 356 collection = collections.langs;
360 else 357 else
361 collection = collections.allLangs; 358 collection = collections.allLangs;
362 } 359 }
363 else if (subscription.url == acceptableAdsUrl) 360 else if (subscription.url == acceptableAdsUrl)
364 collection = collections.acceptableAds; 361 collection = collections.acceptableAds;
365 else 362 else
366 collection = collections.custom; 363 collection = collections.custom;
367 364
368 collection.addItem(subscription); 365 collection.addItem(subscription);
369 subscriptionsMap[subscription.url] = subscription; 366 subscriptionsMap[subscription.url] = subscription;
370 toggleShowLanguage(subscription); 367 toggleShowLanguage(subscription);
371 updateTooltips(); 368 updateTooltips();
372 } 369 }
373 370
374 function updateSubscription(subscription) 371 function updateSubscription(subscription)
375 { 372 {
376 for (var name in collections) 373 for (let name in collections)
377 collections[name].updateItem(subscription); 374 collections[name].updateItem(subscription);
378 375
379 toggleShowLanguage(subscription); 376 toggleShowLanguage(subscription);
380 } 377 }
381 378
382 function updateFilter(filter) 379 function updateFilter(filter)
383 { 380 {
384 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 381 let match = filter.text.match(/^@@\|\|([^/:]+)\^\$document$/);
385 if (match && !filtersMap[filter.text]) 382 if (match && !filtersMap[filter.text])
386 { 383 {
387 filter.title = match[1]; 384 filter.title = match[1];
388 collections.whitelist.addItem(filter); 385 collections.whitelist.addItem(filter);
389 } 386 }
390 else 387 else
391 collections.customFilters.addItem(filter); 388 collections.customFilters.addItem(filter);
392 389
393 filtersMap[filter.text] = filter; 390 filtersMap[filter.text] = filter;
394 } 391 }
395 392
396 function loadRecommendations() 393 function loadRecommendations()
397 { 394 {
398 fetch("subscriptions.xml") 395 fetch("subscriptions.xml")
399 .then(function(response) 396 .then((response) =>
400 { 397 {
401 return response.text(); 398 return response.text();
402 }) 399 })
403 .then(function(text) 400 .then((text) =>
404 { 401 {
405 var list = document.getElementById("subscriptionSelector"); 402 let doc = new DOMParser().parseFromString(text, "application/xml");
406 var doc = new DOMParser().parseFromString(text, "application/xml"); 403 let elements = doc.documentElement.getElementsByTagName("subscription");
407 var elements = doc.documentElement.getElementsByTagName("subscription"); 404 for (let element of elements)
408 for (var i = 0; i < elements.length; i++) 405 {
409 { 406 let type = element.getAttribute("type");
410 var element = elements[i]; 407 let subscription = {
411 var type = element.getAttribute("type");
412 var subscription = {
413 disabled: true, 408 disabled: true,
414 downloadStatus: null, 409 downloadStatus: null,
415 homepage: null, 410 homepage: null,
416 originalTitle: element.getAttribute("title"), 411 originalTitle: element.getAttribute("title"),
417 recommended: type, 412 recommended: type,
418 url: element.getAttribute("url") 413 url: element.getAttribute("url")
419 }; 414 };
420 415
421 var prefix = element.getAttribute("prefixes"); 416 let prefix = element.getAttribute("prefixes");
422 if (prefix) 417 if (prefix)
423 { 418 {
424 prefix = prefix.replace(/\W/g, "_"); 419 prefix = prefix.replace(/\W/g, "_");
425 subscription.title = getMessage("options_language_" + prefix); 420 subscription.title = getMessage("options_language_" + prefix);
426 } 421 }
427 else 422 else
428 { 423 {
429 type = type.replace(/\W/g, "_"); 424 type = type.replace(/\W/g, "_");
430 subscription.title = getMessage("common_feature_" + type + "_title") ; 425 subscription.title = getMessage("common_feature_" +
426 type + "_title");
431 } 427 }
432 428
433 addSubscription(subscription); 429 addSubscription(subscription);
434 } 430 }
435 }); 431 });
436 } 432 }
437 433
438 function findParentData(element, dataName, returnElement) 434 function findParentData(element, dataName, returnElement)
439 { 435 {
440 while (element) 436 while (element)
441 { 437 {
442 if (element.hasAttribute("data-" + dataName)) 438 if (element.hasAttribute("data-" + dataName))
443 return returnElement ? element : element.getAttribute("data-" + dataName ); 439 {
440 if (returnElement)
441 return element;
442 return element.getAttribute("data-" + dataName);
443 }
444 444
445 element = element.parentElement; 445 element = element.parentElement;
446 } 446 }
447 return null; 447 return null;
448 } 448 }
449 449
450 function sendMessageHandleErrors(message, onSuccess) 450 function sendMessageHandleErrors(message, onSuccess)
451 { 451 {
452 ext.backgroundPage.sendMessage(message, function(errors) 452 ext.backgroundPage.sendMessage(message, (errors) =>
453 { 453 {
454 if (errors.length > 0) 454 if (errors.length > 0)
455 alert(errors.join("\n")); 455 alert(errors.join("\n"));
456 else if (onSuccess) 456 else if (onSuccess)
457 onSuccess(); 457 onSuccess();
458 }); 458 });
459 } 459 }
460 460
461 function openDocLink(id) 461 function openDocLink(id)
462 { 462 {
463 getDocLink(id, function(link) 463 getDocLink(id, (link) =>
464 { 464 {
465 if (id == "share-general") 465 if (id == "share-general")
466 openSharePopup(link); 466 openSharePopup(link);
467 else 467 else
468 location.href = link; 468 location.href = link;
469 }); 469 });
470 } 470 }
471 471
472 function switchTab(id) 472 function switchTab(id)
473 { 473 {
474 location.hash = id; 474 location.hash = id;
475 } 475 }
476 476
477 function execAction(action, key, element) 477 function execAction(action, element)
478 { 478 {
479 switch (action) 479 switch (action)
480 { 480 {
481 case "add-domain-exception": 481 case "add-domain-exception":
482 addWhitelistedDomain(); 482 addWhitelistedDomain();
483 break; 483 break;
484 case "add-language-subscription": 484 case "add-language-subscription":
485 addEnableSubscription(findParentData(element, "access", false)); 485 addEnableSubscription(findParentData(element, "access", false));
486 break; 486 break;
487 case "add-predefined-subscription": 487 case "add-predefined-subscription": {
488 var dialog = E("dialog-content-predefined"); 488 let dialog = E("dialog-content-predefined");
489 var title = dialog.querySelector("h3").textContent; 489 let title = dialog.querySelector("h3").textContent;
490 var url = dialog.querySelector(".url").textContent; 490 let url = dialog.querySelector(".url").textContent;
491 addEnableSubscription(url, title); 491 addEnableSubscription(url, title);
492 closeDialog(); 492 closeDialog();
493 break; 493 break;
494 }
494 case "cancel-custom-filters": 495 case "cancel-custom-filters":
495 E("custom-filters").classList.remove("mode-edit"); 496 E("custom-filters").classList.remove("mode-edit");
496 break; 497 break;
497 case "cancel-domain-exception": 498 case "cancel-domain-exception":
498 E("whitelisting-textbox").value = ""; 499 E("whitelisting-textbox").value = "";
499 document.querySelector("#whitelisting .controls").classList.remove("mode -edit"); 500 document.querySelector("#whitelisting .controls").classList
501 .remove("mode-edit");
500 break; 502 break;
501 case "close-dialog": 503 case "close-dialog":
502 closeDialog(); 504 closeDialog();
503 break; 505 break;
504 case "edit-custom-filters": 506 case "edit-custom-filters":
505 E("custom-filters").classList.add("mode-edit");
506 editCustomFilters(); 507 editCustomFilters();
507 break; 508 break;
508 case "edit-domain-exception": 509 case "edit-domain-exception":
509 document.querySelector("#whitelisting .controls").classList.add("mode-ed it"); 510 document.querySelector("#whitelisting .controls").classList
511 .add("mode-edit");
510 E("whitelisting-textbox").focus(); 512 E("whitelisting-textbox").focus();
511 break; 513 break;
512 case "import-subscription": 514 case "import-subscription": {
513 var url = E("blockingList-textbox").value; 515 let url = E("blockingList-textbox").value;
514 addEnableSubscription(url); 516 addEnableSubscription(url);
515 closeDialog(); 517 closeDialog();
516 break; 518 break;
517 case "open-context-menu": 519 }
518 var listItem = findParentData(element, "access", true); 520 case "open-context-menu": {
519 if (listItem != context) 521 let listItem = findParentData(element, "access", true);
522 if (listItem && !listItem.classList.contains("show-context-menu"))
520 listItem.classList.add("show-context-menu"); 523 listItem.classList.add("show-context-menu");
521 break; 524 break;
522 case "open-dialog": 525 }
523 var dialog = findParentData(element, "dialog", false); 526 case "open-dialog": {
527 let dialog = findParentData(element, "dialog", false);
524 openDialog(dialog); 528 openDialog(dialog);
525 break; 529 break;
526 case "open-doclink": 530 }
527 var doclink = findParentData(element, "doclink", false); 531 case "open-doclink": {
532 let doclink = findParentData(element, "doclink", false);
528 openDocLink(doclink); 533 openDocLink(doclink);
529 break; 534 break;
535 }
530 case "remove-filter": 536 case "remove-filter":
531 ext.backgroundPage.sendMessage( 537 ext.backgroundPage.sendMessage({
532 {
533 type: "filters.remove", 538 type: "filters.remove",
534 text: findParentData(element, "access", false) 539 text: findParentData(element, "access", false)
535 }); 540 });
536 break; 541 break;
537 case "remove-subscription": 542 case "remove-subscription":
538 ext.backgroundPage.sendMessage( 543 ext.backgroundPage.sendMessage({
539 {
540 type: "subscriptions.remove", 544 type: "subscriptions.remove",
541 url: findParentData(element, "access", false) 545 url: findParentData(element, "access", false)
542 }); 546 });
543 break; 547 break;
544 case "save-custom-filters": 548 case "save-custom-filters":
545 sendMessageHandleErrors( 549 sendMessageHandleErrors({
546 {
547 type: "filters.importRaw", 550 type: "filters.importRaw",
548 text: E("custom-filters-raw").value, 551 text: E("custom-filters-raw").value,
549 removeExisting: true 552 removeExisting: true
550 }, 553 },
551 function() 554 () =>
552 { 555 {
553 E("custom-filters").classList.remove("mode-edit"); 556 E("custom-filters").classList.remove("mode-edit");
554 }); 557 });
555 break; 558 break;
556 case "switch-tab": 559 case "switch-tab":
557 if (key == "Enter") 560 let tabId = findParentData(element, "tab", false);
558 { 561 switchTab(tabId);
559 var tabId = findParentData(element, "tab", false);
560 switchTab(tabId);
561 }
562 else if (element.hasAttribute("aria-selected"))
563 {
564 if (key == "ArrowLeft" || key == "ArrowUp")
565 {
566 element = element.previousElementSibling
567 || container.lastElementChild;
568 }
569 else if (key == "ArrowRight" || key == "ArrowDown")
570 {
571 element = element.nextElementSibling
572 || container.firstElementChild;
573 }
574
575 var tabId = findParentData(element, "tab", false);
576 switchTab(tabId);
577 }
578 break; 562 break;
579 case "toggle-disable-subscription": 563 case "toggle-disable-subscription":
580 ext.backgroundPage.sendMessage( 564 ext.backgroundPage.sendMessage({
581 {
582 type: "subscriptions.toggle", 565 type: "subscriptions.toggle",
583 keepInstalled: true, 566 keepInstalled: true,
584 url: findParentData(element, "access", false) 567 url: findParentData(element, "access", false)
585 }); 568 });
586 break; 569 break;
587 case "toggle-pref": 570 case "toggle-pref":
588 ext.backgroundPage.sendMessage( 571 ext.backgroundPage.sendMessage({
589 {
590 type: "prefs.toggle", 572 type: "prefs.toggle",
591 key: findParentData(element, "pref", false) 573 key: findParentData(element, "pref", false)
592 }); 574 });
593 break; 575 break;
594 case "toggle-remove-subscription": 576 case "toggle-remove-subscription":
595 var subscriptionUrl = findParentData(element, "access", false); 577 let subscriptionUrl = findParentData(element, "access", false);
596 if (element.getAttribute("aria-checked") == "true") 578 if (element.getAttribute("aria-checked") == "true")
597 { 579 {
598 ext.backgroundPage.sendMessage({ 580 ext.backgroundPage.sendMessage({
599 type: "subscriptions.remove", 581 type: "subscriptions.remove",
600 url: subscriptionUrl 582 url: subscriptionUrl
601 }); 583 });
602 } 584 }
603 else 585 else
604 addEnableSubscription(subscriptionUrl); 586 addEnableSubscription(subscriptionUrl);
605 break; 587 break;
606 case "update-all-subscriptions": 588 case "update-all-subscriptions":
607 ext.backgroundPage.sendMessage( 589 ext.backgroundPage.sendMessage({
608 {
609 type: "subscriptions.update" 590 type: "subscriptions.update"
610 }); 591 });
611 break; 592 break;
612 case "update-subscription": 593 case "update-subscription":
613 ext.backgroundPage.sendMessage( 594 ext.backgroundPage.sendMessage({
614 {
615 type: "subscriptions.update", 595 type: "subscriptions.update",
616 url: findParentData(element, "access", false) 596 url: findParentData(element, "access", false)
617 }); 597 });
618 break; 598 break;
619 } 599 }
620 } 600 }
621 601
622 function onClick(e) 602 function onClick(e)
623 { 603 {
624 var context = document.querySelector(".show-context-menu"); 604 let context = document.querySelector(".show-context-menu");
625 if (context) 605 if (context)
626 context.classList.remove("show-context-menu"); 606 context.classList.remove("show-context-menu");
627 607
628 var actions = findParentData(e.target, "action", false); 608 let actions = findParentData(e.target, "action", false);
629 if (!actions) 609 if (!actions)
630 return; 610 return;
631 611
632 actions = actions.split(","); 612 actions = actions.split(",");
633 for (var i = 0; i < actions.length; i++) 613 for (let action of actions)
634 { 614 {
635 execAction(actions[i], "Enter", e.target); 615 execAction(action, e.target);
636 } 616 }
637 } 617 }
638 618
639 function getKey(e) 619 function getKey(e)
640 { 620 {
641 // e.keyCode has been deprecated so we attempt to use e.key 621 // e.keyCode has been deprecated so we attempt to use e.key
642 if ("key" in e) 622 if ("key" in e)
643 return e.key; 623 return e.key;
644 return getKey.keys[e.keyCode]; 624 return getKey.keys[e.keyCode];
645 } 625 }
646 getKey.keys = { 626 getKey.keys = {
647 9: "Tab", 627 9: "Tab",
648 13: "Enter", 628 13: "Enter",
649 27: "Escape", 629 27: "Escape",
650 37: "ArrowLeft", 630 37: "ArrowLeft",
651 38: "ArrowUp", 631 38: "ArrowUp",
652 39: "ArrowRight", 632 39: "ArrowRight",
653 40: "ArrowDown" 633 40: "ArrowDown"
654 }; 634 };
655 635
656 function onKeyUp(e) 636 function onKeyUp(e)
657 { 637 {
658 var key = getKey(e); 638 let key = getKey(e);
659 var element = document.activeElement; 639 let element = document.activeElement;
660 if (!key || !element) 640 if (!key || !element)
661 return; 641 return;
662 642
663 var container = findParentData(element, "action", true); 643 let container = findParentData(element, "action", true);
664 if (!container || !container.hasAttribute("data-keys")) 644 if (!container || !container.hasAttribute("data-keys"))
665 return; 645 return;
666 646
667 var keys = container.getAttribute("data-keys").split(" "); 647 let keys = container.getAttribute("data-keys").split(" ");
668 if (keys.indexOf(key) < 0) 648 if (keys.indexOf(key) < 0)
669 return; 649 return;
670 650
671 var actions = container.getAttribute("data-action").split(","); 651 if (element.getAttribute("role") == "tab")
672 for (var i = 0; i < actions.length; i++) 652 {
673 { 653 if (key == "ArrowLeft" || key == "ArrowUp")
674 execAction(actions[i], key, element); 654 element = element.previousElementSibling || container.lastElementChild;
655 else if (key == "ArrowRight" || key == "ArrowDown")
656 element = element.nextElementSibling || container.firstElementChild;
657 }
658
659 let actions = container.getAttribute("data-action").split(",");
660 for (let action of actions)
661 {
662 execAction(action, element);
675 } 663 }
676 } 664 }
677 665
678 function selectTabItem(tabId, container, focus) 666 function selectTabItem(tabId, container, focus)
679 { 667 {
680 // Show tab content 668 // Show tab content
681 document.body.setAttribute("data-tab", tabId); 669 document.body.setAttribute("data-tab", tabId);
682 670
683 // Select tab 671 // Select tab
684 var tabList = container.querySelector("[role='tablist']"); 672 let tabList = container.querySelector("[role='tablist']");
685 if (!tabList) 673 if (!tabList)
686 return null; 674 return null;
687 675
688 var previousTab = tabList.querySelector("[aria-selected]"); 676 let previousTab = tabList.querySelector("[aria-selected]");
689 previousTab.removeAttribute("aria-selected"); 677 previousTab.removeAttribute("aria-selected");
690 previousTab.setAttribute("tabindex", -1); 678 previousTab.setAttribute("tabindex", -1);
691 679
692 var tab = tabList.querySelector("li[data-tab='" + tabId + "']"); 680 let tab = tabList.querySelector("li[data-tab='" + tabId + "']");
693 tab.setAttribute("aria-selected", true); 681 tab.setAttribute("aria-selected", true);
694 tab.setAttribute("tabindex", 0); 682 tab.setAttribute("tabindex", 0);
695 683
696 var tabContentId = tab.getAttribute("aria-controls"); 684 let tabContentId = tab.getAttribute("aria-controls");
697 var tabContent = document.getElementById(tabContentId); 685 let tabContent = document.getElementById(tabContentId);
698 686
699 // Select sub tabs 687 // Select sub tabs
700 if (tab.hasAttribute("data-subtab")) 688 if (tab.hasAttribute("data-subtab"))
701 selectTabItem(tab.getAttribute("data-subtab"), tabContent, false); 689 selectTabItem(tab.getAttribute("data-subtab"), tabContent, false);
702 690
703 if (tab && focus) 691 if (tab && focus)
704 tab.focus(); 692 tab.focus();
705 693
706 return tabContent; 694 return tabContent;
707 } 695 }
708 696
709 // TODO: rely on navigation functionality in here instead of data-action
710 function onHashChange() 697 function onHashChange()
711 { 698 {
712 var hash = location.hash.substr(1); 699 let hash = location.hash.substr(1);
713 if (!hash) 700 if (!hash)
714 return; 701 return;
715 702
716 // Select tab and parent tabs 703 // Select tab and parent tabs
717 var tabIds = hash.split("-"); 704 let tabIds = hash.split("-");
718 var tabContent = document.body; 705 let tabContent = document.body;
719 for (var i = 0; i < tabIds.length; i++) 706 for (let i = 0; i < tabIds.length; i++)
720 { 707 {
721 var tabId = tabIds.slice(0, i + 1).join("-"); 708 let tabId = tabIds.slice(0, i + 1).join("-");
722 tabContent = selectTabItem(tabId, tabContent, true); 709 tabContent = selectTabItem(tabId, tabContent, true);
723 if (!tabContent) 710 if (!tabContent)
724 break; 711 break;
725 } 712 }
726
727 // TODO: doesn't work yet because `customFilterItems` is not populated yet a t this point
728 switch (hash)
729 {
730 case "advanced-customFilters":
saroyanm 2017/02/08 12:14:00 I think we shouldn't implement this change, so I r
731 execAction("edit-custom-filters");
732 break;
733 }
734 } 713 }
735 714
736 function onDOMLoaded() 715 function onDOMLoaded()
737 { 716 {
738 populateLists(); 717 populateLists();
739 function onFindLanguageKeyUp() 718 function onFindLanguageKeyUp()
740 { 719 {
741 var searchStyle = E("search-style"); 720 let searchStyle = E("search-style");
742 if (!this.value) 721 if (!this.value)
743 searchStyle.innerHTML = ""; 722 searchStyle.innerHTML = "";
744 else 723 else
745 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this .value.toLowerCase() + "\"]) { display: none; }"; 724 {
725 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" +
726 this.value.toLowerCase() + "\"]) { display: none; }";
727 }
746 } 728 }
747 729
748 // Initialize navigation sidebar 730 // Initialize navigation sidebar
749 ext.backgroundPage.sendMessage( 731 ext.backgroundPage.sendMessage({
750 {
751 type: "app.get", 732 type: "app.get",
752 what: "addonVersion" 733 what: "addonVersion"
753 }, 734 },
754 function(addonVersion) 735 (addonVersion) =>
755 { 736 {
756 E("abp-version").textContent = addonVersion; 737 E("abp-version").textContent = addonVersion;
757 }); 738 });
758 getDocLink("releases", function(link) 739 getDocLink("releases", (link) =>
759 { 740 {
760 E("link-version").setAttribute("href", link); 741 E("link-version").setAttribute("href", link);
761 }); 742 });
762 743
763 updateShareLink(); 744 updateShareLink();
764 updateTooltips(); 745 updateTooltips();
765 746
766 // Initialize interactive UI elements 747 // Initialize interactive UI elements
767 document.body.addEventListener("click", onClick, false); 748 document.body.addEventListener("click", onClick, false);
768 document.body.addEventListener("keyup", onKeyUp, false); 749 document.body.addEventListener("keyup", onKeyUp, false);
769 var placeholderValue = getMessage("options_dialog_language_find"); 750 let placeholderValue = getMessage("options_dialog_language_find");
770 E("find-language").setAttribute("placeholder", placeholderValue); 751 E("find-language").setAttribute("placeholder", placeholderValue);
771 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false); 752 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false);
772 E("whitelisting-textbox").addEventListener("keypress", function(e) 753 E("whitelisting-textbox").addEventListener("keypress", (e) =>
773 { 754 {
774 if (getKey(e) == "Enter") 755 if (getKey(e) == "Enter")
775 addWhitelistedDomain(); 756 addWhitelistedDomain();
776 }, false); 757 }, false);
777 758
778 // Advanced tab 759 // Advanced tab
779 var tweaks = document.querySelectorAll("#tweaks li[data-pref]"); 760 let tweaks = document.querySelectorAll("#tweaks li[data-pref]");
780 tweaks = Array.prototype.map.call(tweaks, function(checkbox) 761 tweaks = Array.prototype.map.call(tweaks, (checkbox) =>
781 { 762 {
782 return checkbox.getAttribute("data-pref"); 763 return checkbox.getAttribute("data-pref");
783 }); 764 });
784 tweaks.forEach(function(key) 765 for (let key of tweaks)
785 { 766 {
786 getPref(key, function(value) 767 getPref(key, (value) =>
787 { 768 {
788 onPrefMessage(key, value, true); 769 onPrefMessage(key, value, true);
789 }); 770 });
790 }); 771 }
791 ext.backgroundPage.sendMessage( 772 ext.backgroundPage.sendMessage({
792 {
793 type: "app.get", 773 type: "app.get",
794 what: "features" 774 what: "features"
795 }, 775 },
796 function(features) 776 (features) =>
797 { 777 {
798 hidePref("show_devtools_panel", !features.devToolsPanel); 778 hidePref("show_devtools_panel", !features.devToolsPanel);
799 }); 779 });
800 780
801 var filterTextbox = document.querySelector("#custom-filters-add input"); 781 let filterTextbox = document.querySelector("#custom-filters-add input");
802 placeholderValue = getMessage("options_customFilters_textbox_placeholder"); 782 placeholderValue = getMessage("options_customFilters_textbox_placeholder");
803 filterTextbox.setAttribute("placeholder", placeholderValue); 783 filterTextbox.setAttribute("placeholder", placeholderValue);
804 function addCustomFilters() 784 function addCustomFilters()
805 { 785 {
806 var filterText = filterTextbox.value; 786 let filterText = filterTextbox.value;
807 sendMessageHandleErrors( 787 sendMessageHandleErrors({
808 {
809 type: "filters.add", 788 type: "filters.add",
810 text: filterText 789 text: filterText
811 }, 790 },
812 function() 791 () =>
813 { 792 {
814 filterTextbox.value = ""; 793 filterTextbox.value = "";
815 }); 794 });
816 } 795 }
817 E("custom-filters-add").addEventListener("submit", function(e) 796 E("custom-filters-add").addEventListener("submit", (e) =>
818 { 797 {
819 e.preventDefault(); 798 e.preventDefault();
820 addCustomFilters(); 799 addCustomFilters();
821 }, false); 800 }, false);
822 var customFilterEditButtons = document.querySelectorAll("#custom-filters-edi t-wrapper button"); 801
802 // Help tab
803 getDocLink("faq", (link) =>
804 {
805 E("link-faq").setAttribute("href", link);
806 });
807 getDocLink("social_twitter", (link) =>
808 {
809 E("link-twitter").setAttribute("href", link);
810 });
811 getDocLink("social_facebook", (link) =>
812 {
813 E("link-facebook").setAttribute("href", link);
814 });
815 getDocLink("social_gplus", (link) =>
816 {
817 E("link-gplus").setAttribute("href", link);
818 });
819 getDocLink("social_renren", (link) =>
820 {
821 E("link-renren").setAttribute("href", link);
822 });
823 getDocLink("social_weibo", (link) =>
824 {
825 E("link-weibo").setAttribute("href", link);
826 });
827
828 // Set forum link
829 ext.backgroundPage.sendMessage({
830 type: "app.get",
831 what: "platform"
832 },
833 (platform) =>
834 {
835 ext.backgroundPage.sendMessage({
836 type: "app.get",
837 what: "application"
838 },
839 (application) =>
840 {
841 if (platform == "chromium" && application != "opera")
842 application = "chrome";
843
844 getDocLink(application + "_support", (link) =>
845 {
846 E("link-forum").setAttribute("href", link);
847 });
848 });
849 });
823 850
824 E("dialog").addEventListener("keydown", function(e) 851 E("dialog").addEventListener("keydown", function(e)
825 { 852 {
826 switch (getKey(e)) 853 switch (getKey(e))
827 { 854 {
828 case "Escape": 855 case "Escape":
829 closeDialog(); 856 closeDialog();
830 break; 857 break;
831 case "Tab": 858 case "Tab":
832 if (e.shiftKey) 859 if (e.shiftKey)
833 { 860 {
834 if (e.target.classList.contains("focus-first")) 861 if (e.target.classList.contains("focus-first"))
835 { 862 {
836 e.preventDefault(); 863 e.preventDefault();
837 this.querySelector(".focus-last").focus(); 864 this.querySelector(".focus-last").focus();
838 } 865 }
839 } 866 }
840 else if (e.target.classList.contains("focus-last")) 867 else if (e.target.classList.contains("focus-last"))
841 { 868 {
842 e.preventDefault(); 869 e.preventDefault();
843 this.querySelector(".focus-first").focus(); 870 this.querySelector(".focus-first").focus();
844 } 871 }
845 break; 872 break;
846 } 873 }
847 }, false); 874 }, false);
848 875
849 onHashChange(); 876 onHashChange();
850 } 877 }
851 878
852 var focusedBeforeDialog = null; 879 let focusedBeforeDialog = null;
853 function openDialog(name) 880 function openDialog(name)
854 { 881 {
855 var dialog = E("dialog"); 882 let dialog = E("dialog");
856 dialog.setAttribute("aria-hidden", false); 883 dialog.setAttribute("aria-hidden", false);
857 dialog.setAttribute("aria-labelledby", "dialog-title-" + name); 884 dialog.setAttribute("aria-labelledby", "dialog-title-" + name);
858 document.body.setAttribute("data-dialog", name); 885 document.body.setAttribute("data-dialog", name);
859 886
860 var defaultFocus = document.querySelector("#dialog-content-" + name 887 let defaultFocus = document.querySelector(
861 + " .default-focus"); 888 "#dialog-content-" + name + " .default-focus"
889 );
862 if (!defaultFocus) 890 if (!defaultFocus)
863 defaultFocus = dialog.querySelector(".focus-first"); 891 defaultFocus = dialog.querySelector(".focus-first");
864 focusedBeforeDialog = document.activeElement; 892 focusedBeforeDialog = document.activeElement;
865 defaultFocus.focus(); 893 defaultFocus.focus();
866 } 894 }
867 895
868 function closeDialog() 896 function closeDialog()
869 { 897 {
870 var dialog = E("dialog"); 898 let dialog = E("dialog");
871 dialog.setAttribute("aria-hidden", true); 899 dialog.setAttribute("aria-hidden", true);
872 dialog.removeAttribute("aria-labelledby"); 900 dialog.removeAttribute("aria-labelledby");
873 document.body.removeAttribute("data-dialog"); 901 document.body.removeAttribute("data-dialog");
874 focusedBeforeDialog.focus(); 902 focusedBeforeDialog.focus();
875 } 903 }
876 904
877 function populateLists() 905 function populateLists()
878 { 906 {
879 subscriptionsMap = Object.create(null); 907 subscriptionsMap = Object.create(null);
880 filtersMap = Object.create(null); 908 filtersMap = Object.create(null);
881 909
882 // Empty collections and lists 910 // Empty collections and lists
883 for (var property in collections) 911 for (let property in collections)
884 collections[property].clearAll(); 912 collections[property].clearAll();
885 913
886 ext.backgroundPage.sendMessage( 914 ext.backgroundPage.sendMessage({
887 {
888 type: "subscriptions.get", 915 type: "subscriptions.get",
889 special: true 916 special: true
890 }, 917 },
891 function(subscriptions) 918 (subscriptions) =>
892 { 919 {
893 // Load filters 920 // Load filters
894 for (var i = 0; i < subscriptions.length; i++) 921 for (let subscription of subscriptions)
895 { 922 {
896 ext.backgroundPage.sendMessage( 923 ext.backgroundPage.sendMessage({
897 {
898 type: "filters.get", 924 type: "filters.get",
899 subscriptionUrl: subscriptions[i].url 925 subscriptionUrl: subscription.url
900 }, 926 },
901 function(filters) 927 (filters) =>
902 { 928 {
903 for (var i = 0; i < filters.length; i++) 929 for (let filter of filters)
904 updateFilter(filters[i]); 930 updateFilter(filter);
931
932 isCustomFiltersLoaded = true;
905 }); 933 });
906 } 934 }
907 }); 935 });
908 loadRecommendations(); 936 loadRecommendations();
909 ext.backgroundPage.sendMessage( 937 ext.backgroundPage.sendMessage({
910 {
911 type: "prefs.get", 938 type: "prefs.get",
912 key: "subscriptions_exceptionsurl" 939 key: "subscriptions_exceptionsurl"
913 }, 940 },
914 function(url) 941 (url) =>
915 { 942 {
916 acceptableAdsUrl = url; 943 acceptableAdsUrl = url;
917 addSubscription({ 944 addSubscription({
918 url: acceptableAdsUrl, 945 url: acceptableAdsUrl,
919 disabled: true 946 disabled: true
920 }); 947 });
921 948
922 // Load user subscriptions 949 // Load user subscriptions
923 ext.backgroundPage.sendMessage( 950 ext.backgroundPage.sendMessage({
924 {
925 type: "subscriptions.get", 951 type: "subscriptions.get",
926 downloadable: true 952 downloadable: true
927 }, 953 },
928 function(subscriptions) 954 (subscriptions) =>
929 { 955 {
930 for (var i = 0; i < subscriptions.length; i++) 956 for (let subscription of subscriptions)
931 onSubscriptionMessage("added", subscriptions[i]); 957 onSubscriptionMessage("added", subscription);
932 }); 958 });
933 }); 959 });
934 } 960 }
935 961
936 function addWhitelistedDomain() 962 function addWhitelistedDomain()
937 { 963 {
938 var domain = E("whitelisting-textbox"); 964 let domain = E("whitelisting-textbox");
939 if (domain.value) 965 if (domain.value)
940 { 966 {
941 sendMessageHandleErrors( 967 sendMessageHandleErrors({
942 {
943 type: "filters.add", 968 type: "filters.add",
944 text: "@@||" + domain.value.toLowerCase() + "^$document" 969 text: "@@||" + domain.value.toLowerCase() + "^$document"
945 }); 970 });
946 } 971 }
947 972
948 domain.value = ""; 973 domain.value = "";
949 document.querySelector("#whitelisting .controls").classList.remove("mode-edi t"); 974 document.querySelector("#whitelisting .controls")
975 .classList.remove("mode-edit");
950 } 976 }
951 977
952 function editCustomFilters() 978 function editCustomFilters()
953 { 979 {
954 var customFilterItems = collections.customFilters.items; 980 if (!isCustomFiltersLoaded)
955 var filterTexts = []; 981 {
956 for (var i = 0; i < customFilterItems.length; i++) 982 console.error("Custom filters are not loaded");
957 filterTexts.push(customFilterItems[i].text); 983 return;
984 }
985
986 E("custom-filters").classList.add("mode-edit");
987 let filterTexts = [];
988 for (let customFilterItem of collections.customFilters.items)
989 filterTexts.push(customFilterItem.text);
958 E("custom-filters-raw").value = filterTexts.join("\n"); 990 E("custom-filters-raw").value = filterTexts.join("\n");
959 } 991 }
960 992
961 function addEnableSubscription(url, title, homepage) 993 function addEnableSubscription(url, title, homepage)
962 { 994 {
963 var messageType = null; 995 let messageType = null;
964 var knownSubscription = subscriptionsMap[url]; 996 let knownSubscription = subscriptionsMap[url];
965 if (knownSubscription && knownSubscription.disabled == true) 997 if (knownSubscription && knownSubscription.disabled == true)
966 messageType = "subscriptions.toggle"; 998 messageType = "subscriptions.toggle";
967 else 999 else
968 messageType = "subscriptions.add"; 1000 messageType = "subscriptions.add";
969 1001
970 var message = { 1002 let message = {
971 type: messageType, 1003 type: messageType,
972 url: url 1004 url
973 }; 1005 };
974 if (title) 1006 if (title)
975 message.title = title; 1007 message.title = title;
976 if (homepage) 1008 if (homepage)
977 message.homepage = homepage; 1009 message.homepage = homepage;
978 1010
979 ext.backgroundPage.sendMessage(message); 1011 ext.backgroundPage.sendMessage(message);
980 } 1012 }
981 1013
982 function onFilterMessage(action, filter) 1014 function onFilterMessage(action, filter)
983 { 1015 {
984 switch (action) 1016 switch (action)
985 { 1017 {
986 case "added": 1018 case "added":
987 updateFilter(filter); 1019 updateFilter(filter);
988 updateShareLink(); 1020 updateShareLink();
989 break; 1021 break;
990 case "loaded": 1022 case "loaded":
991 populateLists(); 1023 populateLists();
992 break; 1024 break;
993 case "removed": 1025 case "removed":
994 var knownFilter = filtersMap[filter.text]; 1026 let knownFilter = filtersMap[filter.text];
995 collections.whitelist.removeItem(knownFilter); 1027 collections.whitelist.removeItem(knownFilter);
996 collections.customFilters.removeItem(knownFilter); 1028 collections.customFilters.removeItem(knownFilter);
997 delete filtersMap[filter.text]; 1029 delete filtersMap[filter.text];
998 updateShareLink(); 1030 updateShareLink();
999 break; 1031 break;
1000 } 1032 }
1001 } 1033 }
1002 1034
1003 function onSubscriptionMessage(action, subscription) 1035 function onSubscriptionMessage(action, subscription)
1004 { 1036 {
1005 if (subscription.url in subscriptionsMap) 1037 if (subscription.url in subscriptionsMap)
1006 { 1038 {
1007 var knownSubscription = subscriptionsMap[subscription.url]; 1039 let knownSubscription = subscriptionsMap[subscription.url];
1008 for (var property in subscription) 1040 for (let property in subscription)
1009 { 1041 {
1010 if (property == "title" && knownSubscription.recommended) 1042 if (property == "title" && knownSubscription.recommended)
1011 knownSubscription.originalTitle = subscription.title; 1043 knownSubscription.originalTitle = subscription.title;
1012 else 1044 else
1013 knownSubscription[property] = subscription[property]; 1045 knownSubscription[property] = subscription[property];
1014 } 1046 }
1015 subscription = knownSubscription; 1047 subscription = knownSubscription;
1016 } 1048 }
1017 switch (action) 1049 switch (action)
1018 { 1050 {
(...skipping 28 matching lines...) Expand all
1047 } 1079 }
1048 collections.filterLists.removeItem(subscription); 1080 collections.filterLists.removeItem(subscription);
1049 break; 1081 break;
1050 } 1082 }
1051 1083
1052 updateShareLink(); 1084 updateShareLink();
1053 } 1085 }
1054 1086
1055 function hidePref(key, value) 1087 function hidePref(key, value)
1056 { 1088 {
1057 var element = document.querySelector("[data-pref='" + key + "']"); 1089 let element = document.querySelector("[data-pref='" + key + "']");
1058 if (element) 1090 if (element)
1059 element.setAttribute("aria-hidden", value); 1091 element.setAttribute("aria-hidden", value);
1060 } 1092 }
1061 1093
1062 function getPref(key, callback) 1094 function getPref(key, callback)
1063 { 1095 {
1064 var checkPref = getPref.checks[key] || getPref.checkNone; 1096 let checkPref = getPref.checks[key] || getPref.checkNone;
1065 checkPref(function(isActive) 1097 checkPref((isActive) =>
1066 { 1098 {
1067 if (!isActive) 1099 if (!isActive)
1068 { 1100 {
1069 hidePref(key, !isActive); 1101 hidePref(key, !isActive);
1070 return; 1102 return;
1071 } 1103 }
1072 1104
1073 ext.backgroundPage.sendMessage( 1105 ext.backgroundPage.sendMessage({
1074 {
1075 type: "prefs.get", 1106 type: "prefs.get",
1076 key: key 1107 key
1077 }, callback); 1108 }, callback);
1078 }); 1109 });
1079 } 1110 }
1080 1111
1081 getPref.checkNone = function(callback) 1112 getPref.checkNone = function(callback)
1082 { 1113 {
1083 callback(true); 1114 callback(true);
1084 }; 1115 };
1085 1116
1086 getPref.checks = 1117 getPref.checks =
1087 { 1118 {
1088 notifications_ignoredcategories: function(callback) 1119 notifications_ignoredcategories(callback)
1089 { 1120 {
1090 getPref("notifications_showui", callback); 1121 getPref("notifications_showui", callback);
1091 } 1122 }
1092 }; 1123 };
1093 1124
1094 function onPrefMessage(key, value, initial) 1125 function onPrefMessage(key, value, initial)
1095 { 1126 {
1096 switch (key) 1127 switch (key)
1097 { 1128 {
1098 case "notifications_ignoredcategories": 1129 case "notifications_ignoredcategories":
1099 value = value.indexOf("*") == -1; 1130 value = value.indexOf("*") == -1;
1100 break; 1131 break;
1101 1132
1102 case "notifications_showui": 1133 case "notifications_showui":
1103 hidePref("notifications_ignoredcategories", !value); 1134 hidePref("notifications_ignoredcategories", !value);
1104 break; 1135 break;
1105 } 1136 }
1106 1137
1107 var checkbox = document.querySelector("[data-pref='" + key + "'] button[role ='checkbox']"); 1138 let checkbox = document.querySelector(
1139 "[data-pref='" + key + "'] button[role='checkbox']"
1140 );
1108 if (checkbox) 1141 if (checkbox)
1109 checkbox.setAttribute("aria-checked", value); 1142 checkbox.setAttribute("aria-checked", value);
1110 } 1143 }
1111 1144
1112 function updateShareLink() 1145 function updateShareLink()
1113 { 1146 {
1114 var shareResources = [ 1147 let shareResources = [
1115 "https://facebook.com/plugins/like.php?", 1148 "https://facebook.com/plugins/like.php?",
1116 "https://platform.twitter.com/widgets/", 1149 "https://platform.twitter.com/widgets/",
1117 "https://apis.google.com/se/0/_/+1/fastbutton?" 1150 "https://apis.google.com/se/0/_/+1/fastbutton?"
1118 ]; 1151 ];
1119 var isAnyBlocked = false; 1152 let isAnyBlocked = false;
1120 var checksRemaining = shareResources.length; 1153 let checksRemaining = shareResources.length;
1121 1154
1122 function onResult(isBlocked) 1155 function onResult(isBlocked)
1123 { 1156 {
1124 isAnyBlocked |= isBlocked; 1157 isAnyBlocked |= isBlocked;
1125 if (!--checksRemaining) 1158 if (!--checksRemaining)
1126 { 1159 {
1127 // Hide the share tab if a script on the share page would be blocked 1160 // Hide the share tab if a script on the share page would be blocked
1128 E("tab-share").hidden = isAnyBlocked; 1161 E("tab-share").hidden = isAnyBlocked;
1129 } 1162 }
1130 } 1163 }
1131 1164
1132 for (var i = 0; i < shareResources.length; i++) 1165 for (let sharedResource of shareResources)
1133 checkShareResource(shareResources[i], onResult); 1166 checkShareResource(sharedResource, onResult);
1134 } 1167 }
1135 1168
1136 function getMessages(id) 1169 function getMessages(id)
1137 { 1170 {
1138 var messages = []; 1171 let messages = [];
1139 for (var i = 1; true; i++) 1172 for (let i = 1; true; i++)
1140 { 1173 {
1141 var message = ext.i18n.getMessage(id + "_" + i); 1174 let message = ext.i18n.getMessage(id + "_" + i);
1142 if (!message) 1175 if (!message)
1143 break; 1176 break;
1144 1177
1145 messages.push(message); 1178 messages.push(message);
1146 } 1179 }
1147 return messages; 1180 return messages;
1148 } 1181 }
1149 1182
1150 function updateTooltips() 1183 function updateTooltips()
1151 { 1184 {
1152 var anchors = document.querySelectorAll(":not(.tooltip) > [data-tooltip]"); 1185 let anchors = document.querySelectorAll(":not(.tooltip) > [data-tooltip]");
1153 for (var i = 0; i < anchors.length; i++) 1186 for (let anchor of anchors)
1154 { 1187 {
1155 var anchor = anchors[i]; 1188 let id = anchor.getAttribute("data-tooltip");
1156 var id = anchor.getAttribute("data-tooltip"); 1189
1157 1190 let wrapper = document.createElement("div");
1158 var wrapper = document.createElement("div");
1159 wrapper.className = "tooltip"; 1191 wrapper.className = "tooltip";
1160 anchor.parentNode.replaceChild(wrapper, anchor); 1192 anchor.parentNode.replaceChild(wrapper, anchor);
1161 wrapper.appendChild(anchor); 1193 wrapper.appendChild(anchor);
1162 1194
1163 var topTexts = getMessages(id); 1195 let topTexts = getMessages(id);
1164 var bottomTexts = getMessages(id + "_notes"); 1196 let bottomTexts = getMessages(id + "_notes");
1165 1197
1166 // We have to use native tooltips to avoid issues when attaching a tooltip 1198 // We have to use native tooltips to avoid issues when attaching a tooltip
1167 // to an element in a scrollable list or otherwise it might get cut off 1199 // to an element in a scrollable list or otherwise it might get cut off
1168 if (anchor.hasAttribute("data-tooltip-native")) 1200 if (anchor.hasAttribute("data-tooltip-native"))
1169 { 1201 {
1170 var title = topTexts.concat(bottomTexts).join("\n\n"); 1202 let title = topTexts.concat(bottomTexts).join("\n\n");
1171 anchor.setAttribute("title", title); 1203 anchor.setAttribute("title", title);
1172 continue; 1204 continue;
1173 } 1205 }
1174 1206
1175 var tooltip = document.createElement("div"); 1207 let tooltip = document.createElement("div");
1176 tooltip.setAttribute("role", "tooltip"); 1208 tooltip.setAttribute("role", "tooltip");
1177 1209
1178 var flip = anchor.getAttribute("data-tooltip-flip"); 1210 let flip = anchor.getAttribute("data-tooltip-flip");
1179 if (flip) 1211 if (flip)
1180 tooltip.className = "flip-" + flip; 1212 tooltip.className = "flip-" + flip;
1181 1213
1182 var imageSource = anchor.getAttribute("data-tooltip-image"); 1214 let imageSource = anchor.getAttribute("data-tooltip-image");
1183 if (imageSource) 1215 if (imageSource)
1184 { 1216 {
1185 var image = document.createElement("img"); 1217 let image = document.createElement("img");
1186 image.src = imageSource; 1218 image.src = imageSource;
1187 image.alt = ""; 1219 image.alt = "";
1188 tooltip.appendChild(image); 1220 tooltip.appendChild(image);
1189 } 1221 }
1190 1222
1191 for (var j = 0; j < topTexts.length; j++) 1223 for (let topText of topTexts)
1192 { 1224 {
1193 var paragraph = document.createElement("p"); 1225 let paragraph = document.createElement("p");
1194 paragraph.innerHTML = topTexts[j]; 1226 paragraph.innerHTML = topText;
1195 tooltip.appendChild(paragraph); 1227 tooltip.appendChild(paragraph);
1196 } 1228 }
1197 if (bottomTexts.length > 0) 1229 if (bottomTexts.length > 0)
1198 { 1230 {
1199 var notes = document.createElement("div"); 1231 let notes = document.createElement("div");
1200 notes.className = "notes"; 1232 notes.className = "notes";
1201 for (var j = 0; j < bottomTexts.length; j++) 1233 for (let bottomText of bottomTexts)
1202 { 1234 {
1203 var paragraph = document.createElement("p"); 1235 let paragraph = document.createElement("p");
1204 paragraph.innerHTML = bottomTexts[j]; 1236 paragraph.innerHTML = bottomText;
1205 notes.appendChild(paragraph); 1237 notes.appendChild(paragraph);
1206 } 1238 }
1207 tooltip.appendChild(notes); 1239 tooltip.appendChild(notes);
1208 } 1240 }
1209 1241
1210 wrapper.appendChild(tooltip); 1242 wrapper.appendChild(tooltip);
1211 } 1243 }
1212 } 1244 }
1213 1245
1214 ext.onMessage.addListener(function(message) 1246 ext.onMessage.addListener((message) =>
1215 { 1247 {
1216 switch (message.type) 1248 switch (message.type)
1217 { 1249 {
1218 case "app.respond": 1250 case "app.respond":
1219 switch (message.action) 1251 switch (message.action)
1220 { 1252 {
1221 case "addSubscription": 1253 case "addSubscription":
1222 var subscription = message.args[0]; 1254 let subscription = message.args[0];
1223 var dialog = E("dialog-content-predefined"); 1255 let dialog = E("dialog-content-predefined");
1224 dialog.querySelector("h3").textContent = subscription.title || ""; 1256 dialog.querySelector("h3").textContent = subscription.title || "";
1225 dialog.querySelector(".url").textContent = subscription.url; 1257 dialog.querySelector(".url").textContent = subscription.url;
1226 openDialog("predefined"); 1258 openDialog("predefined");
1227 break; 1259 break;
1228 case "focusSection": 1260 case "focusSection":
1229 document.body.setAttribute("data-tab", message.args[0]); 1261 document.body.setAttribute("data-tab", message.args[0]);
1230 break; 1262 break;
1231 } 1263 }
1232 break; 1264 break;
1233 case "filters.respond": 1265 case "filters.respond":
1234 onFilterMessage(message.action, message.args[0]); 1266 onFilterMessage(message.action, message.args[0]);
1235 break; 1267 break;
1236 case "prefs.respond": 1268 case "prefs.respond":
1237 onPrefMessage(message.action, message.args[0], false); 1269 onPrefMessage(message.action, message.args[0], false);
1238 break; 1270 break;
1239 case "subscriptions.respond": 1271 case "subscriptions.respond":
1240 onSubscriptionMessage(message.action, message.args[0]); 1272 onSubscriptionMessage(message.action, message.args[0]);
1241 break; 1273 break;
1242 } 1274 }
1243 }); 1275 });
1244 1276
1245 ext.backgroundPage.sendMessage( 1277 ext.backgroundPage.sendMessage({
1246 {
1247 type: "app.listen", 1278 type: "app.listen",
1248 filter: ["addSubscription", "focusSection"] 1279 filter: ["addSubscription", "focusSection"]
1249 }); 1280 });
1250 ext.backgroundPage.sendMessage( 1281 ext.backgroundPage.sendMessage({
1251 {
1252 type: "filters.listen", 1282 type: "filters.listen",
1253 filter: ["added", "loaded", "removed"] 1283 filter: ["added", "loaded", "removed"]
1254 }); 1284 });
1255 ext.backgroundPage.sendMessage( 1285 ext.backgroundPage.sendMessage({
1256 {
1257 type: "prefs.listen", 1286 type: "prefs.listen",
1258 filter: ["notifications_ignoredcategories", "notifications_showui", 1287 filter: ["notifications_ignoredcategories", "notifications_showui",
1259 "show_devtools_panel", "shouldShowBlockElementMenu"] 1288 "show_devtools_panel", "shouldShowBlockElementMenu"]
1260 }); 1289 });
1261 ext.backgroundPage.sendMessage( 1290 ext.backgroundPage.sendMessage({
1262 {
1263 type: "subscriptions.listen", 1291 type: "subscriptions.listen",
1264 filter: ["added", "disabled", "homepage", "lastDownload", "removed", 1292 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
1265 "title", "downloadStatus", "downloading"] 1293 "title", "downloadStatus", "downloading"]
1266 }); 1294 });
1267 1295
1268 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 1296 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
1269 window.addEventListener("hashchange", onHashChange, false); 1297 window.addEventListener("hashchange", onHashChange, false);
1270 })(); 1298 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld