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

Side by Side Diff: popup.js

Issue 29317001: Relocated icon and redesigned icon popup (Closed)
Patch Set: Applied Sebastian's suggestions and updated strings Created Nov. 26, 2013, 4:18 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
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 var backgroundPage = ext.backgroundPage.getWindow(); 18 var backgroundPage = ext.backgroundPage.getWindow();
19 var imports = ["require", "isWhitelisted", "extractHostFromURL", "refreshIconAnd ContextMenu"]; 19 var imports = ["require", "isWhitelisted", "extractHostFromURL", "refreshIconAnd ContextMenu", "openOptions"];
20 for (var i = 0; i < imports.length; i++) 20 for (var i = 0; i < imports.length; i++)
21 window[imports[i]] = backgroundPage[imports[i]]; 21 window[imports[i]] = backgroundPage[imports[i]];
22 22
23 var Filter = require("filterClasses").Filter; 23 var Filter = require("filterClasses").Filter;
24 var FilterStorage = require("filterStorage").FilterStorage; 24 var FilterStorage = require("filterStorage").FilterStorage;
25 var Prefs = require("prefs").Prefs;
25 26
26 var tab = null; 27 var tab = null;
27 28
28 function init() 29 function init()
29 { 30 {
31 // Mark page as local to hide non-relevant elements
32 ext.windows.getLastFocused(function(win)
33 {
34 win.getActiveTab(function(tab)
35 {
36 if (!/^https?:\/\//.exec(tab.url))
37 document.body.classList.add("local");
38 });
39 });
40
30 // Attach event listeners 41 // Attach event listeners
31 $("#enabled").click(toggleEnabled); 42 document.getElementById("enabled").addEventListener("click", toggleEnabled, fa lse);
32 $("#clickHideButton").click(activateClickHide); 43 document.getElementById("clickhide").addEventListener("click", activateClickHi de, false);
33 $("#cancelButton").click(cancelClickHide); 44 document.getElementById("clickhide-cancel").addEventListener("click", cancelCl ickHide, false);
34 $("#optionsButton").click(openOptions); 45 document.getElementById("options").addEventListener("click", function()
Felix Dahlke 2013/12/02 15:45:58 Why not just |.addEventListener("click", openOptio
Thomas Greiner 2013/12/03 12:06:05 I try to avoid passing event parameters on to exte
Wladimir Palant 2013/12/03 12:55:43 Actually correct here - otherwise the event will b
Thomas Greiner 2013/12/04 10:44:50 Done.
46 {
47 openOptions();
48 }, false);
49
50 // Set up collapsing of menu items
51 var collapsers = document.getElementsByClassName("collapse");
52 for (var i = 0; i < collapsers.length; i++)
53 {
54 collapsers[i].addEventListener("click", toggleCollapse.bind(collapsers[i]), true);
Felix Dahlke 2013/12/02 15:45:58 Nit: Wouldn't hurt to assign collapsers[i] to a te
Thomas Greiner 2013/12/03 12:06:05 Done.
Wladimir Palant 2013/12/03 12:55:43 Better solution - add toggleCollapse as callback w
Thomas Greiner 2013/12/04 10:44:50 Done.
55 if (Prefs[collapsers[i].dataset.option])
56 document.getElementById(collapsers[i].dataset.collapsable).classList.add(" collapsed");
Wladimir Palant 2013/12/03 12:55:43 Isn't the logic reversed here? I think it should b
Thomas Greiner 2013/12/04 10:44:50 Done.
57 }
35 58
36 // Ask content script whether clickhide is active. If so, show cancel button. 59 // Ask content script whether clickhide is active. If so, show cancel button.
37 // If that isn't the case, ask background.html whether it has cached filters. If so, 60 // If that isn't the case, ask background.html whether it has cached filters. If so,
38 // ask the user whether she wants those filters. 61 // ask the user whether she wants those filters.
39 // Otherwise, we are in default state. 62 // Otherwise, we are in default state.
40 ext.windows.getLastFocused(function(win) { 63 ext.windows.getLastFocused(function(win)
41 win.getActiveTab(function(t) { 64 {
65 win.getActiveTab(function(t)
66 {
42 tab = t; 67 tab = t;
43 document.getElementById("enabled").checked = !isWhitelisted(tab.url); 68 document.getElementById("enabled").classList.toggle("off", isWhitelisted(t ab.url));
44 document.getElementById("enabledCheckboxAndLabel").style.display = "block" ;
45 69
46 tab.sendMessage({type: "get-clickhide-state"}, function(response) { 70 tab.sendMessage({type: "get-clickhide-state"}, function(response)
47 if(response.active) 71 {
48 clickHideActiveStuff(); 72 document.body.classList.toggle("clickhide-active", response.active);
49 else
50 clickHideInactiveStuff();
51 }); 73 });
52 }); 74 });
53 }); 75 });
54 } 76 }
55 $(init); 77 window.addEventListener("DOMContentLoaded", init, false);
56 78
57 function toggleEnabled() 79 function toggleEnabled()
58 { 80 {
59 var checked = document.getElementById("enabled").checked; 81 var enabledButton = document.getElementById("enabled")
82 var checked = enabledButton.classList.contains("off");
Wladimir Palant 2013/12/03 12:55:43 classList.toggle() returns the new value - no need
Thomas Greiner 2013/12/04 10:44:50 Done.
60 if (checked) 83 if (checked)
61 { 84 {
62 // Remove any exception rules applying to this URL 85 // Remove any exception rules applying to this URL
63 var filter = isWhitelisted(tab.url); 86 var filter = isWhitelisted(tab.url);
64 while (filter) 87 while (filter)
65 { 88 {
66 FilterStorage.removeFilter(filter); 89 FilterStorage.removeFilter(filter);
67 if (filter.subscriptions.length) 90 if (filter.subscriptions.length)
68 filter.disabled = true; 91 filter.disabled = true;
69 filter = isWhitelisted(tab.url); 92 filter = isWhitelisted(tab.url);
70 } 93 }
71 } 94 }
72 else 95 else
73 { 96 {
74 var host = extractHostFromURL(tab.url).replace(/^www\./, ""); 97 var host = extractHostFromURL(tab.url).replace(/^www\./, "");
75 var filter = Filter.fromText("@@||" + host + "^$document"); 98 var filter = Filter.fromText("@@||" + host + "^$document");
76 if (filter.subscriptions.length && filter.disabled) 99 if (filter.subscriptions.length && filter.disabled)
77 filter.disabled = false; 100 filter.disabled = false;
78 else 101 else
79 { 102 {
80 filter.disabled = false; 103 filter.disabled = false;
81 FilterStorage.addFilter(filter); 104 FilterStorage.addFilter(filter);
82 } 105 }
83 } 106 }
84 107
108 enabledButton.classList.toggle("off");
85 refreshIconAndContextMenu(tab); 109 refreshIconAndContextMenu(tab);
86 } 110 }
87 111
88 function activateClickHide() 112 function activateClickHide()
89 { 113 {
90 clickHideActiveStuff(); 114 document.body.classList.add("clickhide-active");
91 tab.sendMessage({type: "clickhide-activate"}); 115 tab.sendMessage({type: "clickhide-activate"});
92 116
93 // Close the popup after a few seconds, so user doesn't have to 117 // Close the popup after a few seconds, so user doesn't have to
94 activateClickHide.timeout = window.setTimeout(window.close, 5000); 118 activateClickHide.timeout = window.setTimeout(window.close, 5000);
95 } 119 }
96 120
97 function cancelClickHide() 121 function cancelClickHide()
98 { 122 {
99 if (activateClickHide.timeout) 123 if (activateClickHide.timeout)
100 { 124 {
101 window.clearTimeout(activateClickHide.timeout); 125 window.clearTimeout(activateClickHide.timeout);
102 activateClickHide.timeout = null; 126 activateClickHide.timeout = null;
103 } 127 }
104 clickHideInactiveStuff(); 128 document.body.classList.remove("clickhide-active");
105 tab.sendMessage({type: "clickhide-deactivate"}); 129 tab.sendMessage({type: "clickhide-deactivate"});
106 } 130 }
107 131
108 function openOptions() 132 function toggleCollapse(ev)
Felix Dahlke 2013/12/02 15:45:58 The parameter is unused, might as well get rid of
Thomas Greiner 2013/12/03 12:06:05 Done.
Wladimir Palant 2013/12/03 12:55:43 As I said, it's better to use that parameter and r
Thomas Greiner 2013/12/04 10:44:50 Done.
109 { 133 {
110 backgroundPage.openOptions(); 134 Prefs[this.dataset.option] = !Prefs[this.dataset.option];
135 this.parentNode.classList.toggle("collapsed");
111 } 136 }
112
113 function clickHideActiveStuff()
114 {
115 document.getElementById("enabledCheckboxAndLabel").style.display = "none";
116 document.getElementById("clickHideInactiveStuff").style.display = "none";
117 document.getElementById("clickHideActiveStuff").style.display = "inherit";
118 }
119
120 function clickHideInactiveStuff()
121 {
122 document.getElementById("enabledCheckboxAndLabel").style.display = "block";
123 document.getElementById("clickHideActiveStuff").style.display = "none";
124 document.getElementById("clickHideInactiveStuff").style.display = "inherit";
125 }
OLDNEW

Powered by Google App Engine
This is Rietveld