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: Define and destructure backgroundPage more consistently, fix minor whitespace errors Created Jan. 18, 2017, 7:34 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 let {startIconAnimation, stopIconAnimation} = require("icon"); 20 "use strict";
21 let {Utils} = require("utils"); 21
22 let {Notification: NotificationStorage} = require("notification"); 22 const {startIconAnimation, stopIconAnimation} = require("icon");
23 let {stringifyURL} = require("url"); 23 const {Utils} = require("utils");
24 let {initAntiAdblockNotification} = require("antiadblockInit"); 24 const {Notification: NotificationStorage} = require("notification");
25 let {Prefs} = require("prefs"); 25 const {stringifyURL} = require("url");
26 const {initAntiAdblockNotification} = require("antiadblockInit");
27 const {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 function prepareNotificationIconAndPopup() 38 function prepareNotificationIconAndPopup()
37 { 39 {
38 let animateIcon = shouldDisplay("icon", activeNotification.type); 40 let animateIcon = shouldDisplay("icon", activeNotification.type);
39 activeNotification.onClicked = function() 41 activeNotification.onClicked = () =>
40 { 42 {
41 if (animateIcon) 43 if (animateIcon)
42 stopIconAnimation(); 44 stopIconAnimation();
43 notificationClosed(); 45 notificationClosed();
44 }; 46 };
45 if (animateIcon) 47 if (animateIcon)
46 startIconAnimation(activeNotification.type); 48 startIconAnimation(activeNotification.type);
47 } 49 }
48 50
49 function getNotificationButtons(notificationType, message) 51 function getNotificationButtons(notificationType, message)
50 { 52 {
51 let buttons = []; 53 let buttons = [];
52 if (notificationType == "question") 54 if (notificationType == "question")
53 { 55 {
54 buttons.push({ 56 buttons.push({
55 type: "question", 57 type: "question",
56 title: ext.i18n.getMessage("overlay_notification_button_yes") 58 title: ext.i18n.getMessage("overlay_notification_button_yes")
57 }); 59 });
58 buttons.push({ 60 buttons.push({
59 type: "question", 61 type: "question",
60 title: ext.i18n.getMessage("overlay_notification_button_no") 62 title: ext.i18n.getMessage("overlay_notification_button_no")
61 }); 63 });
62 } 64 }
63 else 65 else
64 { 66 {
65 let regex = /<a>(.*?)<\/a>/g; 67 const regex = /<a>(.*?)<\/a>/g;
Sebastian Noack 2017/01/18 11:24:26 These is one of the scenarios where I'm undecided
kzar 2017/01/18 11:46:36 Let's er on the side of caution for now.
66 let match; 68 let match;
67 while (match = regex.exec(message)) 69 while (match = regex.exec(message))
68 { 70 {
69 buttons.push({ 71 buttons.push({
70 type: "link", 72 type: "link",
71 title: match[1] 73 title: match[1]
72 }); 74 });
73 } 75 }
74 76
75 // Chrome only allows two notification buttons so we need to fall back 77 // Chrome only allows two notification buttons so we need to fall back
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 switch (activeButtons[buttonIndex].type) 115 switch (activeButtons[buttonIndex].type)
114 { 116 {
115 case "link": 117 case "link":
116 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); 118 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex]));
117 break; 119 break;
118 case "open-all": 120 case "open-all":
119 openNotificationLinks(); 121 openNotificationLinks();
120 break; 122 break;
121 case "configure": 123 case "configure":
122 Prefs.notifications_showui = true; 124 Prefs.notifications_showui = true;
123 ext.showOptions(function(page) 125 ext.showOptions(page =>
124 { 126 {
125 page.sendMessage({ 127 page.sendMessage({
126 type: "app.respond", 128 type: "app.respond",
127 action: "focusSection", 129 action: "focusSection",
128 args: ["notifications"] 130 args: ["notifications"]
129 }); 131 });
130 }); 132 });
131 break; 133 break;
132 case "question": 134 case "question":
133 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0); 135 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0);
134 NotificationStorage.markAsShown(activeNotification.id); 136 NotificationStorage.markAsShown(activeNotification.id);
135 activeNotification.onClicked(); 137 activeNotification.onClicked();
136 break; 138 break;
137 } 139 }
138 } 140 }
139 141
140 function notificationClosed() 142 function notificationClosed()
141 { 143 {
142 activeNotification = null; 144 activeNotification = null;
143 } 145 }
144 146
145 function initChromeNotifications() 147 function initChromeNotifications()
146 { 148 {
147 // Chrome hides notifications in notification center when clicked so we need t o clear them 149 // Chrome hides notifications in notification center when clicked so we need t o clear them
148 function clearActiveNotification(notificationId) 150 function clearActiveNotification(notificationId)
149 { 151 {
150 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification)) 152 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification))
151 return; 153 return;
152 154
153 chrome.notifications.clear(notificationId, function(wasCleared) 155 chrome.notifications.clear(notificationId, wasCleared =>
154 { 156 {
155 if (wasCleared) 157 if (wasCleared)
156 notificationClosed(); 158 notificationClosed();
157 }); 159 });
158 } 160 }
159 161
160 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt onIndex) 162 chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex) =>
161 { 163 {
162 notificationButtonClick(buttonIndex); 164 notificationButtonClick(buttonIndex);
163 clearActiveNotification(notificationId); 165 clearActiveNotification(notificationId);
164 }); 166 });
165 chrome.notifications.onClicked.addListener(clearActiveNotification); 167 chrome.notifications.onClicked.addListener(clearActiveNotification);
166 chrome.notifications.onClosed.addListener(notificationClosed); 168 chrome.notifications.onClosed.addListener(notificationClosed);
167 } 169 }
168 170
169 function showNotification(notification) 171 function showNotification(notification)
170 { 172 {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 else if (approved) 224 else if (approved)
223 openNotificationLinks(); 225 openNotificationLinks();
224 } 226 }
225 } 227 }
226 prepareNotificationIconAndPopup(); 228 prepareNotificationIconAndPopup();
227 }; 229 };
228 230
229 /** 231 /**
230 * Initializes the notification system. 232 * Initializes the notification system.
231 */ 233 */
232 exports.initNotifications = function() 234 exports.initNotifications = () =>
233 { 235 {
234 if ("notifications" in chrome) 236 if ("notifications" in chrome)
235 initChromeNotifications(); 237 initChromeNotifications();
236 initAntiAdblockNotification(); 238 initAntiAdblockNotification();
237 }; 239 };
238 240
239 /** 241 /**
240 * Gets the active notification to be shown if any. 242 * Gets the active notification to be shown if any.
241 * 243 *
242 * @return {?object} 244 * @return {?object}
243 */ 245 */
244 exports.getActiveNotification = function() 246 exports.getActiveNotification = () => activeNotification;
245 {
246 return activeNotification;
247 };
248 247
249 let shouldDisplay = 248 let shouldDisplay =
250 /** 249 /**
251 * Determines whether a given display method should be used for a 250 * Determines whether a given display method should be used for a
252 * specified notification type. 251 * specified notification type.
253 * 252 *
254 * @param {string} method Display method: icon, notification or popup 253 * @param {string} method Display method: icon, notification or popup
255 * @param {string} notificationType 254 * @param {string} notificationType
256 * @return {boolean} 255 * @return {boolean}
257 */ 256 */
258 exports.shouldDisplay = function(method, notificationType) 257 exports.shouldDisplay = (method, notificationType) =>
259 { 258 {
260 let methods = displayMethods[notificationType] || defaultDisplayMethods; 259 let methods = displayMethods[notificationType] || defaultDisplayMethods;
261 return methods.indexOf(method) > -1; 260 return methods.indexOf(method) > -1;
262 }; 261 };
263 262
264 ext.pages.onLoading.addListener(page => 263 ext.pages.onLoading.addListener(page =>
265 { 264 {
266 NotificationStorage.showNext(stringifyURL(page.url)); 265 NotificationStorage.showNext(stringifyURL(page.url));
267 }); 266 });
268 267
269 NotificationStorage.addShowListener(showNotification); 268 NotificationStorage.addShowListener(showNotification);
OLDNEW

Powered by Google App Engine
This is Rietveld