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

Side by Side Diff: lib/notificationHelper.js

Issue 29371763: Issue 4795 - Use modern JavaScript syntax (Closed)
Patch Set: Fix scoping regression Created Jan. 16, 2017, 9:12 a.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 <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 /** @module notificationHelper */ 18 /** @module notificationHelper */
19 19
20 "use strict";
21
20 let {startIconAnimation, stopIconAnimation} = require("icon"); 22 let {startIconAnimation, stopIconAnimation} = require("icon");
21 let {Utils} = require("utils"); 23 let {Utils} = require("utils");
22 let {Notification: NotificationStorage} = require("notification"); 24 let {Notification: NotificationStorage} = require("notification");
23 let {stringifyURL} = require("url"); 25 let {stringifyURL} = require("url");
24 let {initAntiAdblockNotification} = require("antiadblockInit"); 26 let {initAntiAdblockNotification} = require("antiadblockInit");
25 let {Prefs} = require("prefs"); 27 let {Prefs} = require("prefs");
26 28
27 let activeNotification = null; 29 let activeNotification = null;
28 let activeButtons = null; 30 let activeButtons = null;
29 let defaultDisplayMethods = ["popup"]; 31 let defaultDisplayMethods = ["popup"];
30 let displayMethods = Object.create(null); 32 let displayMethods = Object.create(null);
31 displayMethods.critical = ["icon", "notification", "popup"]; 33 displayMethods.critical = ["icon", "notification", "popup"];
32 displayMethods.question = ["notification"]; 34 displayMethods.question = ["notification"];
33 displayMethods.normal = ["notification"]; 35 displayMethods.normal = ["notification"];
34 displayMethods.information = ["icon", "popup"]; 36 displayMethods.information = ["icon", "popup"];
35 37
36 let canUseChromeNotifications = typeof chrome != "undefined" && "notifications" in chrome; 38 let canUseChromeNotifications = typeof chrome != "undefined" && "notifications" in chrome;
kzar 2017/01/16 15:00:47 (Due to rebase again.)
37 39
38 function prepareNotificationIconAndPopup() 40 function prepareNotificationIconAndPopup()
39 { 41 {
40 let animateIcon = shouldDisplay("icon", activeNotification.type); 42 let animateIcon = shouldDisplay("icon", activeNotification.type);
41 activeNotification.onClicked = function() 43 activeNotification.onClicked = () =>
42 { 44 {
43 if (animateIcon) 45 if (animateIcon)
44 stopIconAnimation(); 46 stopIconAnimation();
45 notificationClosed(); 47 notificationClosed();
46 }; 48 };
47 if (animateIcon) 49 if (animateIcon)
48 startIconAnimation(activeNotification.type); 50 startIconAnimation(activeNotification.type);
49 } 51 }
50 52
51 function getNotificationButtons(notificationType, message) 53 function getNotificationButtons(notificationType, message)
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 switch (activeButtons[buttonIndex].type) 117 switch (activeButtons[buttonIndex].type)
116 { 118 {
117 case "link": 119 case "link":
118 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); 120 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex]));
119 break; 121 break;
120 case "open-all": 122 case "open-all":
121 openNotificationLinks(); 123 openNotificationLinks();
122 break; 124 break;
123 case "configure": 125 case "configure":
124 Prefs.notifications_showui = true; 126 Prefs.notifications_showui = true;
125 ext.showOptions(function(page) 127 ext.showOptions(page =>
126 { 128 {
127 page.sendMessage({ 129 page.sendMessage({
128 type: "app.respond", 130 type: "app.respond",
129 action: "focusSection", 131 action: "focusSection",
130 args: ["notifications"] 132 args: ["notifications"]
131 }); 133 });
132 }); 134 });
133 break; 135 break;
134 case "question": 136 case "question":
135 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0); 137 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0);
136 NotificationStorage.markAsShown(activeNotification.id); 138 NotificationStorage.markAsShown(activeNotification.id);
137 activeNotification.onClicked(); 139 activeNotification.onClicked();
138 break; 140 break;
139 } 141 }
140 } 142 }
141 143
142 function notificationClosed() 144 function notificationClosed()
143 { 145 {
144 activeNotification = null; 146 activeNotification = null;
145 } 147 }
146 148
147 function initChromeNotifications() 149 function initChromeNotifications()
148 { 150 {
149 // Chrome hides notifications in notification center when clicked so we need t o clear them 151 // Chrome hides notifications in notification center when clicked so we need t o clear them
150 function clearActiveNotification(notificationId) 152 function clearActiveNotification(notificationId)
151 { 153 {
152 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification)) 154 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification))
153 return; 155 return;
154 156
155 chrome.notifications.clear(notificationId, function(wasCleared) 157 chrome.notifications.clear(notificationId, wasCleared =>
156 { 158 {
157 if (wasCleared) 159 if (wasCleared)
158 notificationClosed(); 160 notificationClosed();
159 }); 161 });
160 } 162 }
161 163
162 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt onIndex) 164 chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex) =>
163 { 165 {
164 notificationButtonClick(buttonIndex); 166 notificationButtonClick(buttonIndex);
165 clearActiveNotification(notificationId); 167 clearActiveNotification(notificationId);
166 }); 168 });
167 chrome.notifications.onClicked.addListener(clearActiveNotification); 169 chrome.notifications.onClicked.addListener(clearActiveNotification);
168 chrome.notifications.onClosed.addListener(notificationClosed); 170 chrome.notifications.onClosed.addListener(notificationClosed);
169 } 171 }
170 172
171 function showNotification(notification) 173 function showNotification(notification)
172 { 174 {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 else if (approved) 226 else if (approved)
225 openNotificationLinks(); 227 openNotificationLinks();
226 } 228 }
227 } 229 }
228 prepareNotificationIconAndPopup(); 230 prepareNotificationIconAndPopup();
229 }; 231 };
230 232
231 /** 233 /**
232 * Initializes the notification system. 234 * Initializes the notification system.
233 */ 235 */
234 exports.initNotifications = function() 236 exports.initNotifications = () =>
235 { 237 {
236 if (canUseChromeNotifications) 238 if (canUseChromeNotifications)
237 initChromeNotifications(); 239 initChromeNotifications();
238 initAntiAdblockNotification(); 240 initAntiAdblockNotification();
239 }; 241 };
240 242
241 /** 243 /**
242 * Gets the active notification to be shown if any. 244 * Gets the active notification to be shown if any.
243 * 245 *
244 * @return {?object} 246 * @return {?object}
245 */ 247 */
246 exports.getActiveNotification = function() 248 exports.getActiveNotification = () => activeNotification;
247 {
248 return activeNotification;
249 };
250 249
251 let shouldDisplay = 250 let shouldDisplay =
252 /** 251 /**
253 * Determines whether a given display method should be used for a 252 * Determines whether a given display method should be used for a
254 * specified notification type. 253 * specified notification type.
255 * 254 *
256 * @param {string} method Display method: icon, notification or popup 255 * @param {string} method Display method: icon, notification or popup
257 * @param {string} notificationType 256 * @param {string} notificationType
258 * @return {boolean} 257 * @return {boolean}
259 */ 258 */
260 exports.shouldDisplay = function(method, notificationType) 259 exports.shouldDisplay = (method, notificationType) =>
261 { 260 {
262 let methods = displayMethods[notificationType] || defaultDisplayMethods; 261 let methods = displayMethods[notificationType] || defaultDisplayMethods;
263 return methods.indexOf(method) > -1; 262 return methods.indexOf(method) > -1;
264 }; 263 };
265 264
266 ext.pages.onLoading.addListener(page => 265 ext.pages.onLoading.addListener(page =>
267 { 266 {
268 NotificationStorage.showNext(stringifyURL(page.url)); 267 NotificationStorage.showNext(stringifyURL(page.url));
269 }); 268 });
270 269
271 NotificationStorage.addShowListener(showNotification); 270 NotificationStorage.addShowListener(showNotification);
OLDNEW
« include.preload.js ('K') | « lib/messaging.js ('k') | lib/prefs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld