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

Side by Side Diff: notification.js

Issue 29567749: Issue 5593 - Use messaging for the popup's notification code (Closed)
Patch Set: Created Oct. 6, 2017, 1:07 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 | « dependencies ('k') | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 const {require} = ext.backgroundPage.getWindow();
21
22 const {Utils} = require("utils");
23 const {Notification} = require("notification");
24 const {getActiveNotification, shouldDisplay} = require("notificationHelper");
25
26 function getDocLinks(notification) 20 function getDocLinks(notification)
27 { 21 {
22 let docLinks = [];
23
28 if (!notification.links) 24 if (!notification.links)
29 return []; 25 return Promise.resolve(docLinks);
30 26
31 let docLinks = []; 27 return Promise.all(
32 notification.links.forEach(link => 28 notification.links.map(link =>
29 {
30 return new Promise((resolve, reject) =>
31 {
32 ext.backgroundPage.sendMessage({
Manish Jethani 2017/10/06 14:26:04 We should use runtime.sendMessage here so we lose
kzar 2017/10/06 20:05:34 Done.
33 type: "app.get",
34 what: "doclink",
35 link
36 }, resolve);
37 });
38 })
39 );
40 }
41
42 function getActiveDisplayedNotification()
Manish Jethani 2017/10/06 14:26:04 Nit: I feel like "displayed" implies it's already
kzar 2017/10/06 20:05:34 Acknowledged.
43 {
44 return new Promise((resolve, reject) =>
33 { 45 {
34 docLinks.push(Utils.getDocLink(link)); 46 ext.backgroundPage.sendMessage({
47 type: "notifications.getActive"
48 }, notification =>
Manish Jethani 2017/10/06 14:26:05 I may be wrong about this, but "notification =>" h
kzar 2017/10/06 20:05:34 I think this looks OK like it is. If we decide to
49 {
50 if (!notification)
Manish Jethani 2017/10/06 14:26:05 We would normally add braces here around the if bl
kzar 2017/10/06 20:05:34 IIRC we considered enforcing that braces for if...
51 resolve(notification);
52 else
53 {
54 ext.backgroundPage.sendMessage({
55 type: "notifications.shouldDisplay",
56 method: "popup",
57 notificationType: notification.notificationType
Manish Jethani 2017/10/06 14:26:05 It should be notification.type, not notification.n
kzar 2017/10/06 20:05:34 Acknowledged.
58 }, shouldDisplay => { resolve(shouldDisplay && notification); });
59 }
60 });
35 }); 61 });
36 return docLinks;
37 } 62 }
38 63
39 function insertMessage(element, text, links) 64 function insertMessage(element, text, links)
40 { 65 {
41 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text); 66 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text);
42 if (!match) 67 if (!match)
43 { 68 {
44 element.appendChild(document.createTextNode(text)); 69 element.appendChild(document.createTextNode(text));
45 return; 70 return;
46 } 71 }
47 72
48 let before = match[1]; 73 let before = match[1];
49 let tagName = match[2]; 74 let tagName = match[2];
50 let value = match[3]; 75 let value = match[3];
51 let after = match[4]; 76 let after = match[4];
52 77
53 insertMessage(element, before, links); 78 insertMessage(element, before, links);
54 79
55 let newElement = document.createElement(tagName); 80 let newElement = document.createElement(tagName);
56 if (tagName === "a" && links && links.length) 81 if (tagName === "a" && links && links.length)
57 newElement.href = links.shift(); 82 newElement.href = links.shift();
58 insertMessage(newElement, value, links); 83 insertMessage(newElement, value, links);
59 element.appendChild(newElement); 84 element.appendChild(newElement);
60 85
61 insertMessage(element, after, links); 86 insertMessage(element, after, links);
62 } 87 }
63 88
64 window.addEventListener("load", () => 89 window.addEventListener("load", () =>
65 { 90 {
66 let notification = getActiveNotification(); 91 getActiveDisplayedNotification().then(notification =>
67 if (!notification || !shouldDisplay("popup", notification.type)) 92 {
68 return; 93 if (!notification)
94 return;
69 95
70 let texts = Notification.getLocalizedTexts(notification); 96 let titleElement = document.getElementById("notification-title");
71 let titleElement = document.getElementById("notification-title"); 97 let messageElement = document.getElementById("notification-message");
72 titleElement.textContent = texts.title;
73 98
74 let docLinks = getDocLinks(notification); 99 ext.backgroundPage.sendMessage({
75 let messageElement = document.getElementById("notification-message"); 100 type: "notifications.getLocalizedTexts",
76 insertMessage(messageElement, texts.message, docLinks); 101 notification
Manish Jethani 2017/10/06 14:26:04 I'm not sure how message.locale gets in there. Is
kzar 2017/10/06 20:05:34 If not specified we go with the default I think.
102 }, texts =>
103 {
104 titleElement.textContent = texts.title;
77 105
78 messageElement.addEventListener("click", event => 106 getDocLinks(notification).then(docLinks =>
79 { 107 {
80 let link = event.target; 108 insertMessage(messageElement, texts.message, docLinks);
81 while (link && link !== messageElement && link.localName !== "a") 109 });
82 link = link.parentNode; 110 });
83 if (!link) 111
84 return; 112 messageElement.addEventListener("click", event =>
Manish Jethani 2017/10/06 14:26:04 I feel like this should be added in the getDocLink
kzar 2017/10/06 20:05:34 Done.
85 event.preventDefault(); 113 {
86 event.stopPropagation(); 114 let link = event.target;
87 ext.pages.open(link.href); 115 while (link && link !== messageElement && link.localName !== "a")
116 link = link.parentNode;
117 if (!link)
118 return;
119 event.preventDefault();
120 event.stopPropagation();
121 ext.pages.open(link.href);
122 });
123
124 let notificationElement = document.getElementById("notification");
125 notificationElement.className = notification.type;
126 notificationElement.hidden = false;
127 notificationElement.addEventListener("click", event =>
128 {
129 if (event.target.id == "notification-close")
130 notificationElement.classList.add("closing");
131 else if (event.target.id == "notification-optout" ||
132 event.target.id == "notification-hide")
133 {
134 if (event.target.id == "notification-optout")
135 {
136 ext.backgroundPage.sendMessage({
Manish Jethani 2017/10/06 14:26:04 You meant to use the togglePref function here, def
kzar 2017/10/06 20:05:34 Done.
137 type: "notifications_ignoredcategories"
138 });
139 }
140
141 notificationElement.hidden = true;
142 notification.onClicked();
143 }
144 }, true);
88 }); 145 });
89
90 let notificationElement = document.getElementById("notification");
91 notificationElement.className = notification.type;
92 notificationElement.hidden = false;
93 notificationElement.addEventListener("click", event =>
94 {
95 if (event.target.id == "notification-close")
96 notificationElement.classList.add("closing");
97 else if (event.target.id == "notification-optout" ||
98 event.target.id == "notification-hide")
99 {
100 if (event.target.id == "notification-optout")
101 Notification.toggleIgnoreCategory("*", true);
102
103 notificationElement.hidden = true;
104 notification.onClicked();
105 }
106 }, true);
107 }, false); 146 }, false);
OLDNEW
« no previous file with comments | « dependencies ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld