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: Rebased Created Oct. 9, 2017, 4:47 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') | popup.js » ('j') | 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 /* global setPref */
19
18 "use strict"; 20 "use strict";
19 21
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) 22 function getDocLinks(notification)
27 { 23 {
28 if (!notification.links) 24 if (!notification.links)
29 return []; 25 return Promise.resolve([]);
30 26
31 let docLinks = []; 27 return Promise.all(
32 notification.links.forEach(link => 28 notification.links.map(link =>
33 { 29 {
34 docLinks.push(Utils.getDocLink(link)); 30 return new Promise((resolve, reject) =>
35 }); 31 {
36 return docLinks; 32 chrome.runtime.sendMessage({
33 type: "app.get",
34 what: "doclink",
35 link
36 }, resolve);
37 });
38 })
39 );
37 } 40 }
38 41
39 function insertMessage(element, text, links) 42 function insertMessage(element, text, links)
40 { 43 {
41 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text); 44 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text);
42 if (!match) 45 if (!match)
43 { 46 {
44 element.appendChild(document.createTextNode(text)); 47 element.appendChild(document.createTextNode(text));
45 return; 48 return;
46 } 49 }
47 50
48 let before = match[1]; 51 let before = match[1];
49 let tagName = match[2]; 52 let tagName = match[2];
50 let value = match[3]; 53 let value = match[3];
51 let after = match[4]; 54 let after = match[4];
52 55
53 insertMessage(element, before, links); 56 insertMessage(element, before, links);
54 57
55 let newElement = document.createElement(tagName); 58 let newElement = document.createElement(tagName);
56 if (tagName === "a" && links && links.length) 59 if (tagName == "a" && links && links.length)
57 newElement.href = links.shift(); 60 newElement.href = links.shift();
58 insertMessage(newElement, value, links); 61 insertMessage(newElement, value, links);
59 element.appendChild(newElement); 62 element.appendChild(newElement);
60 63
61 insertMessage(element, after, links); 64 insertMessage(element, after, links);
62 } 65 }
63 66
64 window.addEventListener("load", () => 67 window.addEventListener("load", () =>
65 { 68 {
66 let notification = getActiveNotification(); 69 chrome.runtime.sendMessage({
67 if (!notification || !shouldDisplay("popup", notification.type)) 70 type: "notifications.get",
68 return; 71 displayMethod: "popup"
72 }, notification =>
73 {
74 if (!notification)
75 return;
69 76
70 let texts = Notification.getLocalizedTexts(notification); 77 let titleElement = document.getElementById("notification-title");
71 let titleElement = document.getElementById("notification-title"); 78 let messageElement = document.getElementById("notification-message");
72 titleElement.textContent = texts.title;
73 79
74 let docLinks = getDocLinks(notification); 80 titleElement.textContent = notification.texts.title;
75 let messageElement = document.getElementById("notification-message");
76 insertMessage(messageElement, texts.message, docLinks);
77 81
78 messageElement.addEventListener("click", event => 82 getDocLinks(notification).then(docLinks =>
79 { 83 {
80 let link = event.target; 84 insertMessage(messageElement, notification.texts.message, docLinks);
81 while (link && link !== messageElement && link.localName !== "a") 85
82 link = link.parentNode; 86 messageElement.addEventListener("click", event =>
83 if (!link) 87 {
84 return; 88 let link = event.target;
85 event.preventDefault(); 89 while (link && link != messageElement && link.localName != "a")
86 event.stopPropagation(); 90 link = link.parentNode;
87 chrome.tabs.create({url: link.href}); 91 if (!link)
92 return;
93 event.preventDefault();
94 event.stopPropagation();
95 chrome.tabs.create({url: link.href});
96 });
97 });
98
99 let notificationElement = document.getElementById("notification");
100 notificationElement.className = notification.type;
101 notificationElement.hidden = false;
102 notificationElement.addEventListener("click", event =>
103 {
104 if (event.target.id == "notification-close")
105 notificationElement.classList.add("closing");
106 else if (event.target.id == "notification-optout" ||
107 event.target.id == "notification-hide")
108 {
109 if (event.target.id == "notification-optout")
110 setPref("notifications_ignoredcategories", true);
111
112 notificationElement.hidden = true;
113 notification.onClicked();
114 }
115 }, true);
88 }); 116 });
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); 117 }, false);
OLDNEW
« no previous file with comments | « dependencies ('k') | popup.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld