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

Side by Side Diff: mobile-options.js

Issue 29488575: Issue 5384 - Introduced dedicated mobile options page (Closed)
Patch Set: Created July 13, 2017, 2:54 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
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 /* globals getDocLink */
19
20 "use strict";
21
saroyanm 2017/07/18 12:46:28 I'll suggest to encapsulate this file with own blo
Thomas Greiner 2017/07/18 17:42:32 Done.
22 const {getMessage} = ext.i18n;
23
24 let whitelistFilter = null;
25 let promisedAcceptableAdsUrl = getAcceptableAdsUrl();
26
27 /* Utility functions */
28
29 function get(selector, origin)
saroyanm 2017/07/18 12:46:28 Detail: Calling function "get" is too generic, as
Thomas Greiner 2017/07/18 17:42:31 Acknowledged. Note that the inspiration for this
saroyanm 2017/07/31 11:08:54 Now I see your intention, but in that case I think
30 {
31 return (origin || document).querySelector(selector);
32 }
33
34 function getAll(selector, origin)
35 {
36 return (origin || document).querySelectorAll(selector);
37 }
38
39 function create(parent, tagName, content, attributes, onclick)
40 {
41 let element = document.createElement(tagName);
42
43 if (typeof content == "string")
44 {
45 element.textContent = content;
46 }
47
48 if (attributes)
49 {
50 for (let name in attributes)
51 {
52 element.setAttribute(name, attributes[name]);
53 }
54 }
55
56 if (onclick)
57 {
58 element.addEventListener("click", (ev) =>
59 {
60 onclick(ev);
61 ev.stopPropagation();
62 });
saroyanm 2017/07/18 12:46:28 Note: I do remember before it was required to spec
Thomas Greiner 2017/07/18 17:42:32 We dropped that requirement a while ago. From what
63 }
64
65 parent.appendChild(element);
66 return element;
67 }
68
69 /* Extension interactions */
70
71 function getInstalled()
72 {
73 return new Promise((resolve, reject) =>
74 {
75 ext.backgroundPage.sendMessage(
76 {type: "subscriptions.get", downloadable: true},
77 resolve
78 );
79 });
80 }
81
82 function getAcceptableAdsUrl()
83 {
84 return new Promise((resolve, reject) =>
85 {
86 ext.backgroundPage.sendMessage(
87 {type: "prefs.get", key: "subscriptions_exceptionsurl"},
88 resolve
89 );
90 });
91 }
92
93 function getRecommended()
saroyanm 2017/07/18 12:46:28 This function only return recommended subscription
Thomas Greiner 2017/07/18 17:42:33 Good point. Done.
94 {
95 return fetch("subscriptions.xml")
96 .then((resp) => resp.text())
saroyanm 2017/07/18 12:46:27 Detail: we usually are not using short form of the
saroyanm 2017/07/18 12:46:28 Shouldn't this be returned (return response.text()
Thomas Greiner 2017/07/18 17:42:31 No, this is one of the features of arrow functions
Thomas Greiner 2017/07/18 17:42:31 I agree that it makes sense for "response", so I'l
97 .then((text) =>
98 {
99 let doc = new DOMParser().parseFromString(text, "application/xml");
100 let elements = Array.from(doc.getElementsByTagName("subscription"));
101
102 return elements
103 .filter((element) => element.getAttribute("type") == "ads")
104 .map((element) =>
105 {
106 return {
saroyanm 2017/07/18 12:46:27 Detail: the brace should go to the next row for co
Thomas Greiner 2017/07/18 17:42:31 Moving this brace to the next row would be interpr
107 title: element.getAttribute("title"),
saroyanm 2017/07/18 12:46:28 Shouldn't this titles be translated ? I think we
Thomas Greiner 2017/07/18 17:42:31 We don't translate filter list titles (e.g. "EasyL
108 url: element.getAttribute("url")
109 };
110 });
111 });
112 }
113
114 function installSubscription(url, title)
115 {
116 ext.backgroundPage.sendMessage({type: "subscriptions.add", url, title});
117 }
118
119 function uninstallSubscription(url)
120 {
121 ext.backgroundPage.sendMessage({type: "subscriptions.remove", url});
122 }
123
124 /* Actions */
125
126 function setSubscription({disabled, title, url}, shouldAdd)
127 {
128 if (disabled)
129 return;
130
131 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
132 {
133 if (url == acceptableAdsUrl)
134 {
135 get("#acceptableAds").checked = true;
136 return;
137 }
138
139 let listInstalled = get("#subscriptions-installed");
140 let installed = get(`[data-url="${url}"]`, listInstalled);
saroyanm 2017/07/18 12:46:28 Detail: I think we are using single quotes "'", ra
Thomas Greiner 2017/07/18 17:42:32 This is a template literal, not a string literal.
141
142 if (installed)
143 {
144 let titleElement = get("span", installed);
145 titleElement.textContent = title || url;
146 }
147 else if (shouldAdd)
saroyanm 2017/07/18 12:46:28 Why are you using "shouldAdd" parameter ? If the
Thomas Greiner 2017/07/18 17:42:31 Why should we show subscriptions that are not inst
saroyanm 2017/07/31 11:08:54 I think I meant "installed", nevermind, I don't re
148 {
149 let element = create(listInstalled, "li", null, {"data-url": url});
150 create(element, "span", title || url);
151 create(element, "button", null, {class: "remove"},
152 () => uninstallSubscription(url)
153 );
154
155 let recommended = get(`#subscriptions-recommended [data-url="${url}"]`);
156 if (recommended)
157 {
158 recommended.classList.add("installed");
159 }
160 }
161 });
162 }
163
164 function removeSubscription(url)
165 {
166 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
167 {
168 if (url == acceptableAdsUrl)
169 {
170 get("#acceptable-ads").checked = false;
saroyanm 2017/07/18 12:46:27 I think you forget to change all camelCase IDs.
Thomas Greiner 2017/07/18 17:42:32 Yep, seems so. Done.
171 return;
172 }
173
174 let installed = get(`#subscriptions-installed [data-url="${url}"]`);
175 if (installed)
176 {
177 installed.parentNode.removeChild(installed);
178 }
179
180 let recommended = get(`#subscriptions-recommended [data-url="${url}"]`);
181 if (recommended)
182 {
183 recommended.classList.remove("installed");
184 }
185 });
186 }
187
188 function setDialog(id, options)
189 {
190 if (!id)
191 {
192 delete document.body.dataset.dialog;
193 return;
194 }
195
196 let fields = getAll(`#dialog-${id} input`);
197 for (let field of fields)
198 {
199 field.value = (options && field.name in options) ? options[field.name] : "";
200 }
201 setError(id, null);
202
203 document.body.dataset.dialog = id;
204 }
205
206 function setError(dialogId, message)
saroyanm 2017/07/18 12:46:28 Detail: Parameter name "message" is misleading, in
Thomas Greiner 2017/07/18 17:42:33 Looks like I forgot to adapt that. Done.
207 {
208 let dialog = get(`#dialog-${dialogId}`);
209 if (message)
210 {
211 dialog.dataset.error = message;
212 }
213 else
214 {
215 delete dialog.dataset.error;
216 }
217 }
218
219 function populateLists()
220 {
221 Promise.all([getInstalled(), getRecommended()])
222 .then(([installed, recommended]) =>
223 {
224 let listRecommended = get("#subscriptions-recommended");
225 for (let {title, url} of recommended)
226 {
227 create(listRecommended, "li", title, {"data-url": url},
228 (ev) =>
229 {
230 if (ev.target.classList.contains("installed"))
231 return;
232
233 setDialog("subscribe", {title, url});
saroyanm 2017/07/18 12:46:27 Detail: reference to "subscribe" dialog is used qu
Thomas Greiner 2017/07/18 17:42:32 Done.
234 }
235 );
236 }
237
238 for (let subscription of installed)
239 {
240 if (subscription.disabled)
241 continue;
242
243 setSubscription(subscription, true);
244 }
245 })
246 .catch((err) => console.error(err));
saroyanm 2017/07/18 12:46:28 I think you have mentioned this, so I'll ignore th
Thomas Greiner 2017/07/18 17:42:32 Acknowledged.
247 }
248
249 /* Listeners */
250
251 function onChange(ev)
252 {
253 if (ev.target.id != "acceptable-ads")
saroyanm 2017/07/18 12:46:27 Same as mentioned above, there is no element with
Thomas Greiner 2017/07/18 17:42:33 Done.
254 return;
255
256 promisedAcceptableAdsUrl.then((acceptableAdsUrl) =>
257 {
258 if (ev.target.checked)
259 {
260 installSubscription(acceptableAdsUrl, null);
261 }
262 else
263 {
264 uninstallSubscription(acceptableAdsUrl);
265 }
266 });
267 }
268 document.addEventListener("change", onChange);
269
270 function onClick(ev)
271 {
272 switch (ev.target.dataset.action)
273 {
274 case "close-dialog":
saroyanm 2017/07/18 12:46:27 Note: Why not to handle all other click events her
Thomas Greiner 2017/07/18 17:42:32 Done. I'm also using it now for the "enabled" togg
275 setDialog(null);
276 break;
277 case "open-dialog":
278 setDialog(ev.target.dataset.dialog);
279 break;
280 }
281 }
282 document.addEventListener("click", onClick);
283
284 function onSubmit(ev)
285 {
286 let fields = ev.target.elements;
287 let title = fields.title.value;
288 let url = fields.url.value;
289
290 if (!title)
291 {
292 setError("subscribe", "title");
293 }
294 else if (!url)
295 {
296 setError("subscribe", "url");
297 }
298 else
299 {
300 installSubscription(url, title);
301 setDialog(null);
302 }
303
304 ev.preventDefault();
305 }
306 document.addEventListener("submit", onSubmit);
307
308 function onToggleWhitelistFilter(ev)
309 {
310 let checkbox = ev.target;
saroyanm 2017/07/18 12:46:28 Suggestion: We can avoid using element name as a v
Thomas Greiner 2017/07/18 17:42:32 In this case the implementation depends on it bein
311 ext.backgroundPage.sendMessage(
312 {
313 type: (checkbox.checked) ? "filters.remove" : "filters.add",
314 text: whitelistFilter
315 }, (errors) =>
316 {
317 if (errors.length < 1)
318 return;
319
320 console.error(errors);
321 checkbox.checked = !checkbox.checked;
322 }
323 );
324 ev.preventDefault();
325 }
326
327 function onMessage(msg)
328 {
329 switch (msg.type)
330 {
331 case "app.respond": {
332 switch (msg.action)
333 {
334 case "addSubscription":
335 let [subscription] = msg.args;
336 setDialog("subscribe", {
337 title: subscription.title,
338 url: subscription.url
339 });
340 break;
341 case "showPageOptions":
342 let [{host, whitelisted}] = msg.args;
343 whitelistFilter = `@@||${host}^$document`;
saroyanm 2017/07/18 12:46:28 Shouldn't this be promise as well, similar to the
Thomas Greiner 2017/07/18 17:42:32 Even though it's not necessary because the UI usin
344
345 ext.i18n.setElementText(
346 get("#enabled-label"),
347 "mops_enabled_label",
348 [host]
349 );
350
351 let checkbox = get("#enabled");
352 checkbox.checked = !whitelisted;
353 checkbox.addEventListener("click", onToggleWhitelistFilter);
354
355 get("#enabled-container").hidden = false;
356 break;
357 }
358 break;
359 }
360 case "filters.respond": {
361 let [filter] = msg.args;
362 if (!whitelistFilter || filter.text != whitelistFilter)
saroyanm 2017/07/18 12:46:28 First check for "!whitelistFilter" seems to redund
Thomas Greiner 2017/07/18 17:42:31 While I don't expect `filter.text` to be `null`, i
363 break;
364
365 get("#enabled").checked = (msg.action == "removed");
366 break;
367 }
368 case "subscriptions.respond": {
369 let [subscription] = msg.args;
370 switch (msg.action)
371 {
372 case "added":
373 setSubscription(subscription, true);
374 break;
375 case "disabled":
376 if (subscription.disabled)
377 {
378 removeSubscription(subscription.url);
379 }
380 else
381 {
382 setSubscription(subscription, true);
383 }
384 break;
385 case "removed":
386 removeSubscription(subscription.url);
387 break;
388 case "title":
389 // We're also receiving these messages for subscriptions that are not
390 // installed so we shouldn't add those by accident
391 setSubscription(subscription, false);
392 break;
393 }
394 break;
395 }
396 }
397 }
398 ext.onMessage.addListener(onMessage);
399
400 ext.backgroundPage.sendMessage({
401 type: "app.listen",
402 filter: ["addSubscription", "showPageOptions"]
403 });
404
405 ext.backgroundPage.sendMessage({
406 type: "filters.listen",
407 filter: ["added", "removed"]
408 });
409
410 ext.backgroundPage.sendMessage({
411 type: "subscriptions.listen",
412 filter: ["added", "disabled", "removed", "title"]
413 });
414
415 /* Initialization */
416
417 populateLists();
418
419 getDocLink("acceptable_ads", (link) =>
420 {
421 get("#acceptableAds-more").href = link;
422 });
423
424 get("#dialog-subscribe [name='title']").setAttribute(
425 "placeholder",
426 getMessage("mops_subscribe_title")
427 );
428
429 get("#dialog-subscribe [name='url']").setAttribute(
430 "placeholder",
431 getMessage("mops_subscribe_url")
432 );
OLDNEW
« mobile-options.html ('K') | « mobile-options.html ('k') | skin/mobile-options.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld