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

Side by Side Diff: new-options.js

Issue 29346555: Issue 4156 - Adblocking filter only being removed in advanced tab fix (Closed)
Patch Set: Changed addItems method to only accept 1 parameter and changed recommendations initial status Created Aug. 17, 2016, 2:55 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2016 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 63
64 Collection.prototype._getItemTitle = function(item, i) 64 Collection.prototype._getItemTitle = function(item, i)
65 { 65 {
66 if (item.url == acceptableAdsUrl) 66 if (item.url == acceptableAdsUrl)
67 return getMessage("options_acceptableAds_description"); 67 return getMessage("options_acceptableAds_description");
68 if (this.details[i].useOriginalTitle && item.originalTitle) 68 if (this.details[i].useOriginalTitle && item.originalTitle)
69 return item.originalTitle; 69 return item.originalTitle;
70 return item.title || item.url || item.text; 70 return item.title || item.url || item.text;
71 }; 71 };
72 72
73 Collection.prototype.addItems = function() 73 Collection.prototype.addItem = function(item)
74 { 74 {
75 var length = Array.prototype.push.apply(this.items, arguments); 75 if (this.items.indexOf(item) >= 0)
76 if (length == 0)
77 return; 76 return;
78 77
78 this.items.push(item);
79 this.items.sort(function(a, b) 79 this.items.sort(function(a, b)
80 { 80 {
81 // Make sure that Acceptable Ads is always last, since it cannot be 81 // 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 82 // 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 83 // the "Own filter list" which cannot be disabled either at the bottom
84 // of the filter lists in the Advanced tab. 84 // of the filter lists in the Advanced tab.
85 if (a.url == acceptableAdsUrl) 85 if (a.url == acceptableAdsUrl)
86 return 1; 86 return 1;
87 if (b.url == acceptableAdsUrl) 87 if (b.url == acceptableAdsUrl)
88 return -1; 88 return -1;
89 89
90 var aTitle = this._getItemTitle(a, 0).toLowerCase(); 90 var aTitle = this._getItemTitle(a, 0).toLowerCase();
91 var bTitle = this._getItemTitle(b, 0).toLowerCase(); 91 var bTitle = this._getItemTitle(b, 0).toLowerCase();
92 return aTitle.localeCompare(bTitle); 92 return aTitle.localeCompare(bTitle);
93 }.bind(this)); 93 }.bind(this));
94 94
95 for (var j = 0; j < this.details.length; j++) 95 for (var j = 0; j < this.details.length; j++)
96 { 96 {
97 var table = E(this.details[j].id); 97 var table = E(this.details[j].id);
98 var template = table.querySelector("template"); 98 var template = table.querySelector("template");
99 for (var i = 0; i < arguments.length; i++) 99 var listItem = document.createElement("li");
100 listItem.appendChild(document.importNode(template.content, true));
101 listItem.setAttribute("aria-label", this._getItemTitle(item, j));
102 listItem.setAttribute("data-access", item.url || item.text);
103 listItem.setAttribute("role", "section");
104
105 var label = listItem.querySelector(".display");
106 if (item.recommended && label.hasAttribute("data-tooltip"))
100 { 107 {
101 var item = arguments[i]; 108 var tooltipId = label.getAttribute("data-tooltip");
102 var listItem = document.createElement("li"); 109 tooltipId = tooltipId.replace("%value%", item.recommended);
103 listItem.appendChild(document.importNode(template.content, true)); 110 label.setAttribute("data-tooltip", tooltipId);
104 listItem.setAttribute("aria-label", this._getItemTitle(item, j)); 111 }
105 listItem.setAttribute("data-access", item.url || item.text);
106 listItem.setAttribute("role", "section");
107 112
108 var label = listItem.querySelector(".display"); 113 var controls = listItem.querySelectorAll(".control");
109 if (item.recommended && label.hasAttribute("data-tooltip")) 114 for (var k = 0; k < controls.length; k++)
115 {
116 if (controls[k].hasAttribute("title"))
110 { 117 {
111 var tooltipId = label.getAttribute("data-tooltip"); 118 var titleValue = getMessage(controls[k].getAttribute("title"));
112 tooltipId = tooltipId.replace("%value%", item.recommended); 119 controls[k].setAttribute("title", titleValue)
113 label.setAttribute("data-tooltip", tooltipId);
114 } 120 }
121 }
115 122
116 var controls = listItem.querySelectorAll(".control"); 123 this._setEmpty(table, null);
117 for (var k = 0; k < controls.length; k++) 124 if (table.hasChildNodes())
118 { 125 {
119 if (controls[k].hasAttribute("title")) 126 table.insertBefore(listItem,
120 { 127 table.childNodes[this.items.indexOf(item)]);
121 var titleValue = getMessage(controls[k].getAttribute("title"));
122 controls[k].setAttribute("title", titleValue)
123 }
124 }
125
126 this._setEmpty(table, null);
127 if (table.hasChildNodes())
128 {
129 table.insertBefore(listItem,
130 table.childNodes[this.items.indexOf(item)]);
131 }
132 else
133 table.appendChild(listItem);
134 this.updateItem(item);
135 } 128 }
129 else
130 table.appendChild(listItem);
131 this.updateItem(item);
136 } 132 }
137 return length; 133 return length;
138 }; 134 };
139 135
140 Collection.prototype.removeItem = function(item) 136 Collection.prototype.removeItem = function(item)
141 { 137 {
142 var index = this.items.indexOf(item); 138 var index = this.items.indexOf(item);
143 if (index == -1) 139 if (index == -1)
144 return; 140 return;
145 141
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 } 324 }
329 ]); 325 ]);
330 collections.filterLists = new Collection( 326 collections.filterLists = new Collection(
331 [ 327 [
332 { 328 {
333 id: "all-filter-lists-table", 329 id: "all-filter-lists-table",
334 useOriginalTitle: true 330 useOriginalTitle: true
335 } 331 }
336 ]); 332 ]);
337 333
338 function updateLanguageCollections(subscription) 334 function toggleShowLanguage(subscription)
339 { 335 {
340 if (subscription.recommended == "ads") 336 if (subscription.recommended == "ads")
341 { 337 {
342 if (subscription.disabled) 338 if (subscription.disabled)
343 { 339 {
344 collections.allLangs.addItems(subscription); 340 collections.allLangs.addItem(subscription);
345 collections.langs.removeItem(subscription); 341 collections.langs.removeItem(subscription);
346 } 342 }
347 else 343 else
348 { 344 {
349 collections.allLangs.removeItem(subscription); 345 collections.allLangs.removeItem(subscription);
350 collections.langs.addItems(subscription); 346 collections.langs.addItem(subscription);
351 } 347 }
352 } 348 }
353 } 349 }
354 350
355 function addSubscription(subscription) 351 function addSubscription(subscription)
356 { 352 {
357 var collection; 353 var collection;
358 if (subscription.recommended) 354 if (subscription.recommended)
359 { 355 {
360 if (subscription.recommended != "ads") 356 if (subscription.recommended != "ads")
361 collection = collections.popular; 357 collection = collections.popular;
362 else if (subscription.disabled == false) 358 else if (subscription.disabled == false)
363 collection = collections.langs; 359 collection = collections.langs;
364 else 360 else
365 collection = collections.allLangs; 361 collection = collections.allLangs;
366 } 362 }
367 else if (subscription.url == acceptableAdsUrl) 363 else if (subscription.url == acceptableAdsUrl)
368 collection = collections.acceptableAds; 364 collection = collections.acceptableAds;
369 else 365 else
370 collection = collections.custom; 366 collection = collections.custom;
371 367
372 collection.addItems(subscription); 368 collection.addItem(subscription);
373 subscriptionsMap[subscription.url] = subscription; 369 subscriptionsMap[subscription.url] = subscription;
370 toggleShowLanguage(subscription);
374 updateTooltips(); 371 updateTooltips();
375 } 372 }
376 373
377 function updateSubscription(subscription) 374 function updateSubscription(subscription)
378 { 375 {
379 var knownSubscription = subscriptionsMap[subscription.url]; 376 for (var name in collections)
380 for (var property in subscription) 377 collections[name].updateItem(subscription);
381 {
382 if (property == "title" && subscription.recommended)
383 knownSubscription.originalTitle = subscription.title;
384 else
385 knownSubscription[property] = subscription[property];
386 }
387 378
388 for (var name in collections) 379 toggleShowLanguage(subscription);
389 collections[name].updateItem(knownSubscription);
390
391 return knownSubscription;
392 } 380 }
393 381
394 function updateFilter(filter) 382 function updateFilter(filter)
395 { 383 {
396 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 384 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/);
397 if (match && !filtersMap[filter.text]) 385 if (match && !filtersMap[filter.text])
398 { 386 {
399 filter.title = match[1]; 387 filter.title = match[1];
400 collections.whitelist.addItems(filter); 388 collections.whitelist.addItem(filter);
401 } 389 }
402 else 390 else
403 collections.customFilters.addItems(filter); 391 collections.customFilters.addItem(filter);
404 392
405 filtersMap[filter.text] = filter; 393 filtersMap[filter.text] = filter;
406 } 394 }
407 395
408 function loadRecommendations() 396 function loadRecommendations()
409 { 397 {
410 fetch("subscriptions.xml") 398 fetch("subscriptions.xml")
411 .then(function(response) 399 .then(function(response)
412 { 400 {
413 return response.text(); 401 return response.text();
414 }) 402 })
415 .then(function(text) 403 .then(function(text)
416 { 404 {
417 var list = document.getElementById("subscriptionSelector"); 405 var list = document.getElementById("subscriptionSelector");
418 var doc = new DOMParser().parseFromString(text, "application/xml"); 406 var doc = new DOMParser().parseFromString(text, "application/xml");
419 var elements = doc.documentElement.getElementsByTagName("subscription"); 407 var elements = doc.documentElement.getElementsByTagName("subscription");
420 for (var i = 0; i < elements.length; i++) 408 for (var i = 0; i < elements.length; i++)
421 { 409 {
422 var element = elements[i]; 410 var element = elements[i];
423 var type = element.getAttribute("type"); 411 var type = element.getAttribute("type");
424 var subscription = { 412 var subscription = {
425 disabled: null, 413 disabled: true,
426 downloadStatus: null, 414 downloadStatus: null,
427 homepage: null, 415 homepage: null,
428 originalTitle: element.getAttribute("title"), 416 originalTitle: element.getAttribute("title"),
429 recommended: type, 417 recommended: type,
430 url: element.getAttribute("url") 418 url: element.getAttribute("url")
431 }; 419 };
432 420
433 var prefix = element.getAttribute("prefixes"); 421 var prefix = element.getAttribute("prefixes");
434 if (prefix) 422 if (prefix)
435 { 423 {
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after
1019 collections.whitelist.removeItem(knownFilter); 1007 collections.whitelist.removeItem(knownFilter);
1020 collections.customFilters.removeItem(knownFilter); 1008 collections.customFilters.removeItem(knownFilter);
1021 delete filtersMap[filter.text]; 1009 delete filtersMap[filter.text];
1022 updateShareLink(); 1010 updateShareLink();
1023 break; 1011 break;
1024 } 1012 }
1025 } 1013 }
1026 1014
1027 function onSubscriptionMessage(action, subscription) 1015 function onSubscriptionMessage(action, subscription)
1028 { 1016 {
1017 if (subscription.url in subscriptionsMap)
1018 {
1019 var knownSubscription = subscriptionsMap[subscription.url];
1020 for (var property in subscription)
1021 {
1022 if (property == "title" && knownSubscription.recommended)
1023 knownSubscription.originalTitle = subscription.title;
1024 else
1025 knownSubscription[property] = subscription[property];
1026 }
1027 subscription = knownSubscription;
1028 }
1029 switch (action) 1029 switch (action)
1030 { 1030 {
1031 case "disabled": 1031 case "disabled":
1032 subscription = updateSubscription(subscription); 1032 updateSubscription(subscription);
1033 updateLanguageCollections(subscription);
1034 break; 1033 break;
1035 case "downloading": 1034 case "downloading":
1036 case "downloadStatus": 1035 case "downloadStatus":
1037 case "homepage": 1036 case "homepage":
1038 case "lastDownload": 1037 case "lastDownload":
1039 case "title": 1038 case "title":
1040 updateSubscription(subscription); 1039 updateSubscription(subscription);
1041 break; 1040 break;
1042 case "added": 1041 case "added":
1043 if (subscription.url in subscriptionsMap) 1042 if (subscription.url in subscriptionsMap)
1044 subscription = updateSubscription(subscription); 1043 updateSubscription(subscription);
1045 else 1044 else
1046 addSubscription(subscription); 1045 addSubscription(subscription);
1047 1046
1048 collections.filterLists.addItems(subscription); 1047 collections.filterLists.addItem(subscription);
1049 updateLanguageCollections(subscription);
1050 break; 1048 break;
1051 case "removed": 1049 case "removed":
1052 var knownSubscription = subscriptionsMap[subscription.url];
1053 if (subscription.url == acceptableAdsUrl || subscription.recommended) 1050 if (subscription.url == acceptableAdsUrl || subscription.recommended)
1054 { 1051 {
1055 subscription.disabled = true; 1052 subscription.disabled = true;
1056 onSubscriptionMessage("disabled", subscription); 1053 onSubscriptionMessage("disabled", subscription);
1057 } 1054 }
1058 else 1055 else
1059 { 1056 {
1060 collections.custom.removeItem(knownSubscription); 1057 collections.custom.removeItem(subscription);
1061 delete subscriptionsMap[subscription.url]; 1058 delete subscriptionsMap[subscription.url];
1062 } 1059 }
1063 collections.filterLists.removeItem(knownSubscription); 1060 collections.filterLists.removeItem(subscription);
1064 break; 1061 break;
1065 } 1062 }
1066 1063
1067 updateShareLink(); 1064 updateShareLink();
1068 } 1065 }
1069 1066
1070 function hidePref(key, value) 1067 function hidePref(key, value)
1071 { 1068 {
1072 var element = document.querySelector("[data-pref='" + key + "']"); 1069 var element = document.querySelector("[data-pref='" + key + "']");
1073 if (element) 1070 if (element)
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1281 ext.backgroundPage.sendMessage( 1278 ext.backgroundPage.sendMessage(
1282 { 1279 {
1283 type: "subscriptions.listen", 1280 type: "subscriptions.listen",
1284 filter: ["added", "disabled", "homepage", "lastDownload", "removed", 1281 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
1285 "title", "downloadStatus", "downloading"] 1282 "title", "downloadStatus", "downloading"]
1286 }); 1283 });
1287 1284
1288 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 1285 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
1289 window.addEventListener("hashchange", onHashChange, false); 1286 window.addEventListener("hashchange", onHashChange, false);
1290 })(); 1287 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld