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

Side by Side Diff: options.js

Issue 29339287: Issue 3885 - Disable the checkbox to toggle the Acceptable Ads list on the options page (Closed)
Patch Set: Simplified logic, got rid of getAcceptableAdsURL() Created April 6, 2016, 5:32 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
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 "use strict"; 18 "use strict";
19 19
20 (function() 20 (function()
21 { 21 {
22 var subscriptionsMap = Object.create(null); 22 var subscriptionsMap = Object.create(null);
23 var recommendationsMap = Object.create(null); 23 var recommendationsMap = Object.create(null);
24 var filtersMap = Object.create(null); 24 var filtersMap = Object.create(null);
25 var collections = Object.create(null); 25 var collections = Object.create(null);
26 var acceptableAdsUrl = null;
26 var maxLabelId = 0; 27 var maxLabelId = 0;
27 var getMessage = ext.i18n.getMessage; 28 var getMessage = ext.i18n.getMessage;
28 var filterErrors = 29 var filterErrors =
29 { 30 {
30 "synchronize_invalid_url": "options_filterList_lastDownload_invalidURL", 31 "synchronize_invalid_url": "options_filterList_lastDownload_invalidURL",
31 "synchronize_connection_error": "options_filterList_lastDownload_connectionE rror", 32 "synchronize_connection_error": "options_filterList_lastDownload_connectionE rror",
32 "synchronize_invalid_data": "options_filterList_lastDownload_invalidData", 33 "synchronize_invalid_data": "options_filterList_lastDownload_invalidData",
33 "synchronize_checksum_mismatch": "options_filterList_lastDownload_checksumMi smatch" 34 "synchronize_checksum_mismatch": "options_filterList_lastDownload_checksumMi smatch"
34 }; 35 };
35 36
(...skipping 19 matching lines...) Expand all
55 56
56 Collection.prototype._createElementQuery = function(item) 57 Collection.prototype._createElementQuery = function(item)
57 { 58 {
58 var access = (item.url || item.text).replace(/'/g, "\\'"); 59 var access = (item.url || item.text).replace(/'/g, "\\'");
59 return function(container) 60 return function(container)
60 { 61 {
61 return container.querySelector("[data-access='" + access + "']"); 62 return container.querySelector("[data-access='" + access + "']");
62 }; 63 };
63 }; 64 };
64 65
66 Collection.prototype._isControlDisabled = function(item, i)
67 {
68 return item.url == acceptableAdsUrl &&
69 this.details[i].onClick == toggleDisableSubscription;
70 };
71
72 Collection.prototype._getItemTitle = function(item, i)
73 {
74 var title = null;
75 if (this.details[i].useOriginalTitle)
76 title = item.originalTitle;
77 if (!title)
78 title = item.title || item.url || item.text;
79 return title;
80 };
81
65 Collection.prototype.addItems = function() 82 Collection.prototype.addItems = function()
66 { 83 {
67 var length = Array.prototype.push.apply(this.items, arguments); 84 var length = Array.prototype.push.apply(this.items, arguments);
68 if (length == 0) 85 if (length == 0)
69 return; 86 return;
70 87
71 this.items.sort(function(a, b) 88 this.items.sort(function(a, b)
72 { 89 {
73 var aValue = (a.title || a.text || a.url).toLowerCase(); 90 var aDisabled = this._isControlDisabled(a, 0);
74 var bValue = (b.title || b.text || b.url).toLowerCase(); 91 var bDisabled = this._isControlDisabled(b, 0);
75 return aValue.localeCompare(bValue); 92 if (aDisabled != bDisabled)
76 }); 93 return aDisabled - bDisabled;
94
95 var aTitle = this._getItemTitle(a, 0).toLowerCase();
96 var bTitle = this._getItemTitle(b, 0).toLowerCase();
97 return aTitle.localeCompare(bTitle);
98 }.bind(this));
77 99
78 for (var j = 0; j < this.details.length; j++) 100 for (var j = 0; j < this.details.length; j++)
79 { 101 {
80 var table = E(this.details[j].id); 102 var table = E(this.details[j].id);
81 var template = table.querySelector("template"); 103 var template = table.querySelector("template");
82 for (var i = 0; i < arguments.length; i++) 104 for (var i = 0; i < arguments.length; i++)
83 { 105 {
84 var item = arguments[i]; 106 var item = arguments[i];
85 var listItem = document.createElement("li"); 107 var listItem = document.createElement("li");
86 listItem.appendChild(document.importNode(template.content, true)); 108 listItem.appendChild(document.importNode(template.content, true));
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 Collection.prototype.updateItem = function(item) 183 Collection.prototype.updateItem = function(item)
162 { 184 {
163 var access = (item.url || item.text).replace(/'/g, "\\'"); 185 var access = (item.url || item.text).replace(/'/g, "\\'");
164 for (var i = 0; i < this.details.length; i++) 186 for (var i = 0; i < this.details.length; i++)
165 { 187 {
166 var table = E(this.details[i].id); 188 var table = E(this.details[i].id);
167 var element = table.querySelector("[data-access='" + access + "']"); 189 var element = table.querySelector("[data-access='" + access + "']");
168 if (!element) 190 if (!element)
169 continue; 191 continue;
170 192
171 var title = null; 193 var title = this._getItemTitle(item, i);
172 if (this.details[i].useOriginalTitle)
173 title = item.originalTitle;
174 if (!title)
175 title = item.title || item.url || item.text;
176 element.querySelector(".display").textContent = title; 194 element.querySelector(".display").textContent = title;
177 if (title) 195 if (title)
178 element.setAttribute("data-search", title.toLowerCase()); 196 element.setAttribute("data-search", title.toLowerCase());
179 var control = element.querySelector(".control[role='checkbox']"); 197 var control = element.querySelector(".control[role='checkbox']");
180 if (control) 198 if (control)
199 {
181 control.setAttribute("aria-checked", item.disabled == false); 200 control.setAttribute("aria-checked", item.disabled == false);
201 if (this._isControlDisabled(item, i))
202 control.setAttribute("disabled", true);
203 }
182 204
183 var downloadStatus = item.downloadStatus; 205 var downloadStatus = item.downloadStatus;
184 var dateElement = element.querySelector(".date"); 206 var dateElement = element.querySelector(".date");
185 var timeElement = element.querySelector(".time"); 207 var timeElement = element.querySelector(".time");
186 if(dateElement && timeElement) 208 if(dateElement && timeElement)
187 { 209 {
188 var message = element.querySelector(".message"); 210 var message = element.querySelector(".message");
189 ext.backgroundPage.sendMessage( 211 ext.backgroundPage.sendMessage(
190 { 212 {
191 type: "subscriptions.isDownloading", 213 type: "subscriptions.isDownloading",
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 var knownSubscription = subscriptionsMap[subscriptionUrl]; 454 var knownSubscription = subscriptionsMap[subscriptionUrl];
433 if (knownSubscription) 455 if (knownSubscription)
434 { 456 {
435 for (var property in subscription) 457 for (var property in subscription)
436 if (property != "title") 458 if (property != "title")
437 knownSubscription[property] = subscription[property]; 459 knownSubscription[property] = subscription[property];
438 } 460 }
439 else 461 else
440 { 462 {
441 observeSubscription(subscription); 463 observeSubscription(subscription);
442 getAcceptableAdsURL(function(acceptableAdsUrl) 464
465 var collection;
466 if (subscriptionUrl in recommendationsMap)
443 { 467 {
444 var collection = null; 468 var recommendation = recommendationsMap[subscriptionUrl];
445 if (subscriptionUrl in recommendationsMap) 469 if (recommendation.type != "ads")
446 { 470 collection = collections.popular;
447 var recommendation = recommendationsMap[subscriptionUrl]; 471 else if (subscription.disabled == false)
448 if (recommendation.type != "ads") 472 collection = collections.langs;
449 collection = collections.popular;
450 else if (subscription.disabled == false)
451 collection = collections.langs;
452 else
453 collection = collections.allLangs;
454 }
455 else if (subscriptionUrl == acceptableAdsUrl)
456 collection = collections.acceptableAds;
457 else 473 else
458 collection = collections.custom; 474 collection = collections.allLangs;
475 }
476 else if (subscriptionUrl == acceptableAdsUrl)
477 collection = collections.acceptableAds;
478 else
479 collection = collections.custom;
459 480
460 collection.addItems(subscription); 481 collection.addItems(subscription);
461 subscriptionsMap[subscriptionUrl] = subscription; 482 subscriptionsMap[subscriptionUrl] = subscription;
462 });
463 } 483 }
464 } 484 }
465 485
466 function updateFilter(filter) 486 function updateFilter(filter)
467 { 487 {
468 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 488 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/);
469 if (match && !filtersMap[filter.text]) 489 if (match && !filtersMap[filter.text])
470 { 490 {
471 filter.title = match[1]; 491 filter.title = match[1];
472 collections.whitelist.addItems(filter); 492 collections.whitelist.addItems(filter);
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 subscriptionUrl: subscriptions[i].url 861 subscriptionUrl: subscriptions[i].url
842 }, 862 },
843 function(filters) 863 function(filters)
844 { 864 {
845 for (var i = 0; i < filters.length; i++) 865 for (var i = 0; i < filters.length; i++)
846 updateFilter(filters[i]); 866 updateFilter(filters[i]);
847 }); 867 });
848 } 868 }
849 }); 869 });
850 loadRecommendations(); 870 loadRecommendations();
851 getAcceptableAdsURL(function(acceptableAdsUrl) 871 ext.backgroundPage.sendMessage(
852 { 872 {
853 var subscription = Object.create(null); 873 type: "prefs.get",
854 subscription.url = acceptableAdsUrl; 874 key: "subscriptions_exceptionsurl"
855 subscription.disabled = true; 875 },
856 subscription.title = getMessage("options_acceptableAds_description"); 876 function(url)
857 updateSubscription(subscription); 877 {
878 acceptableAdsUrl = url;
879 updateSubscription({
880 url: acceptableAdsUrl,
881 disabled: true,
882 title: getMessage("options_acceptableAds_description")
883 });
858 884
859 // Load user subscriptions 885 // Load user subscriptions
860 ext.backgroundPage.sendMessage( 886 ext.backgroundPage.sendMessage(
861 { 887 {
862 type: "subscriptions.get", 888 type: "subscriptions.get",
863 downloadable: true 889 downloadable: true
864 }, 890 },
865 function(subscriptions) 891 function(subscriptions)
866 { 892 {
867 for (var i = 0; i < subscriptions.length; i++) 893 for (var i = 0; i < subscriptions.length; i++)
(...skipping 20 matching lines...) Expand all
888 914
889 function editCustomFilters() 915 function editCustomFilters()
890 { 916 {
891 var customFilterItems = collections.customFilters.items; 917 var customFilterItems = collections.customFilters.items;
892 var filterTexts = []; 918 var filterTexts = [];
893 for (var i = 0; i < customFilterItems.length; i++) 919 for (var i = 0; i < customFilterItems.length; i++)
894 filterTexts.push(customFilterItems[i].text); 920 filterTexts.push(customFilterItems[i].text);
895 E("custom-filters-raw").value = filterTexts.join("\n"); 921 E("custom-filters-raw").value = filterTexts.join("\n");
896 } 922 }
897 923
898 function getAcceptableAdsURL(callback)
899 {
900 getPref("subscriptions_exceptionsurl", function(value)
901 {
902 getAcceptableAdsURL = function(callback)
903 {
904 callback(value);
905 };
906 getAcceptableAdsURL(callback);
907 });
908 }
909
910 function addEnableSubscription(url, title, homepage) 924 function addEnableSubscription(url, title, homepage)
911 { 925 {
912 var messageType = null; 926 var messageType = null;
913 var knownSubscription = subscriptionsMap[url]; 927 var knownSubscription = subscriptionsMap[url];
914 if (knownSubscription && knownSubscription.disabled == true) 928 if (knownSubscription && knownSubscription.disabled == true)
915 messageType = "subscriptions.toggle" 929 messageType = "subscriptions.toggle"
916 else 930 else
917 messageType = "subscriptions.add" 931 messageType = "subscriptions.add"
918 932
919 var message = { 933 var message = {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 updateShareLink(); 982 updateShareLink();
969 break; 983 break;
970 case "lastDownload": 984 case "lastDownload":
971 updateSubscription(subscription); 985 updateSubscription(subscription);
972 break; 986 break;
973 case "homepage": 987 case "homepage":
974 // TODO: NYI 988 // TODO: NYI
975 break; 989 break;
976 case "removed": 990 case "removed":
977 var knownSubscription = subscriptionsMap[subscription.url]; 991 var knownSubscription = subscriptionsMap[subscription.url];
978 getAcceptableAdsURL(function(acceptableAdsUrl) 992 if (subscription.url == acceptableAdsUrl)
979 { 993 {
980 if (subscription.url == acceptableAdsUrl) 994 subscription.disabled = true;
981 { 995 updateSubscription(subscription);
982 subscription.disabled = true; 996 }
983 updateSubscription(subscription); 997 else
984 } 998 {
999 if (subscription.url in recommendationsMap)
1000 knownSubscription.disabled = true;
985 else 1001 else
986 { 1002 {
987 if (subscription.url in recommendationsMap) 1003 collections.custom.removeItem(knownSubscription);
988 knownSubscription.disabled = true; 1004 delete subscriptionsMap[subscription.url];
989 else
990 {
991 collections.custom.removeItem(knownSubscription);
992 delete subscriptionsMap[subscription.url];
993 }
994 } 1005 }
995 updateShareLink(); 1006 }
996 collections.filterLists.removeItem(knownSubscription); 1007 updateShareLink();
997 }); 1008 collections.filterLists.removeItem(knownSubscription);
998 break; 1009 break;
999 case "title": 1010 case "title":
1000 // TODO: NYI 1011 // TODO: NYI
1001 break; 1012 break;
1002 } 1013 }
1003 } 1014 }
1004 1015
1005 function hidePref(key, value) 1016 function hidePref(key, value)
1006 { 1017 {
1007 var element = document.querySelector("[data-pref='" + key + "']"); 1018 var element = document.querySelector("[data-pref='" + key + "']");
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
1149 }); 1160 });
1150 ext.backgroundPage.sendMessage( 1161 ext.backgroundPage.sendMessage(
1151 { 1162 {
1152 type: "subscriptions.listen", 1163 type: "subscriptions.listen",
1153 filter: ["added", "disabled", "homepage", "lastDownload", "removed", 1164 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
1154 "title"] 1165 "title"]
1155 }); 1166 });
1156 1167
1157 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 1168 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
1158 })(); 1169 })();
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