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

Delta Between Two Patch Sets: options.js

Issue 29321417: Issue 2357 - Added "predefined list" dialog to options page (Closed)
Left Patch Set: Created July 13, 2015, 6:17 p.m.
Right Patch Set: Post-review: Merged Created July 16, 2015, 11:36 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « options.html ('k') | skin/options.css » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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
(...skipping 20 matching lines...) Expand all
31 } 31 }
32 32
33 Collection.prototype.addItems = function() 33 Collection.prototype.addItems = function()
34 { 34 {
35 var length = Array.prototype.push.apply(this.items, arguments); 35 var length = Array.prototype.push.apply(this.items, arguments);
36 if (length == 0) 36 if (length == 0)
37 return; 37 return;
38 38
39 this.items.sort(function(a, b) 39 this.items.sort(function(a, b)
40 { 40 {
41 var aValue = (a.title || a.url || a.text).toLowerCase(); 41 var aValue = (a.title || a.text || a.url).toLowerCase();
42 var bValue = (b.title || b.url || a.text).toLowerCase(); 42 var bValue = (b.title || b.text || b.url).toLowerCase();
43 return aValue.localeCompare(bValue); 43 return aValue.localeCompare(bValue);
44 }); 44 });
45 45
46 for (var j = 0; j < this.details.length; j++) 46 for (var j = 0; j < this.details.length; j++)
47 { 47 {
48 var table = E(this.details[j].id); 48 var table = E(this.details[j].id);
49 var template = table.querySelector("template"); 49 var template = table.querySelector("template");
50 for (var i = 0; i < arguments.length; i++) 50 for (var i = 0; i < arguments.length; i++)
51 { 51 {
52 var item = arguments[i]; 52 var item = arguments[i];
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 table.appendChild(template); 100 table.appendChild(template);
101 } 101 }
102 this.items.length = 0; 102 this.items.length = 0;
103 }; 103 };
104 104
105 function onToggleSubscriptionClick(e) 105 function onToggleSubscriptionClick(e)
106 { 106 {
107 e.preventDefault(); 107 e.preventDefault();
108 var subscriptionUrl = e.target.parentNode.getAttribute("data-access"); 108 var subscriptionUrl = e.target.parentNode.getAttribute("data-access");
109 if (!e.target.checked) 109 if (!e.target.checked)
110 removeSubscription(subscriptionUrl); 110 {
111 ext.backgroundPage.sendMessage(
112 {
113 type: "subscriptions.remove",
114 url: subscriptionUrl
115 });
116 }
111 else 117 else
112 addEnableSubscription(subscriptionUrl); 118 addEnableSubscription(subscriptionUrl);
113 } 119 }
114 120
115 function onAddLanguageSubscriptionClick(e) 121 function onAddLanguageSubscriptionClick(e)
116 { 122 {
117 e.preventDefault(); 123 e.preventDefault();
118 var url = this.parentNode.getAttribute("data-access"); 124 var url = this.parentNode.getAttribute("data-access");
119 addEnableSubscription(url); 125 addEnableSubscription(url);
120 } 126 }
121 127
122 function onRemoveFilterClick() 128 function onRemoveFilterClick()
123 { 129 {
124 var filter = this.parentNode.getAttribute("data-access"); 130 var filter = this.parentNode.getAttribute("data-access");
125 removeFilter(filter); 131 ext.backgroundPage.sendMessage(
132 {
133 type: "filters.remove",
134 text: filter
135 });
126 } 136 }
127 137
128 collections.popular = new Collection( 138 collections.popular = new Collection(
129 [ 139 [
130 { 140 {
131 id: "recommend-list-table", 141 id: "recommend-list-table",
132 onClick: onToggleSubscriptionClick 142 onClick: onToggleSubscriptionClick
133 } 143 }
134 ]); 144 ]);
135 collections.langs = new Collection( 145 collections.langs = new Collection(
(...skipping 25 matching lines...) Expand all
161 { 171 {
162 id: "custom-list-table", 172 id: "custom-list-table",
163 onClick: onToggleSubscriptionClick 173 onClick: onToggleSubscriptionClick
164 } 174 }
165 ]); 175 ]);
166 collections.whitelist = new Collection( 176 collections.whitelist = new Collection(
167 [ 177 [
168 { 178 {
169 id: "whitelisting-table", 179 id: "whitelisting-table",
170 onClick: onRemoveFilterClick 180 onClick: onRemoveFilterClick
181 }
182 ]);
183 collections.customFilters = new Collection(
184 [
185 {
186 id: "custom-filters-table"
171 } 187 }
172 ]); 188 ]);
173 189
174 function updateSubscription(subscription) 190 function updateSubscription(subscription)
175 { 191 {
176 var subscriptionUrl = subscription.url; 192 var subscriptionUrl = subscription.url;
177 var knownSubscription = subscriptionsMap[subscriptionUrl]; 193 var knownSubscription = subscriptionsMap[subscriptionUrl];
178 if (knownSubscription) 194 if (knownSubscription)
179 knownSubscription.disabled = subscription.disabled; 195 knownSubscription.disabled = subscription.disabled;
180 else 196 else
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 } 275 }
260 } 276 }
261 277
262 function updateFilter(filter) 278 function updateFilter(filter)
263 { 279 {
264 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 280 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/);
265 if (match && !filtersMap[filter.text]) 281 if (match && !filtersMap[filter.text])
266 { 282 {
267 filter.title = match[1]; 283 filter.title = match[1];
268 collections.whitelist.addItems(filter); 284 collections.whitelist.addItems(filter);
269 filtersMap[filter.text] = filter
270 } 285 }
271 else 286 else
272 { 287 collections.customFilters.addItems(filter);
273 // TODO: add `filters[i].text` to list of custom filters 288
274 } 289 filtersMap[filter.text] = filter;
275 } 290 }
276 291
277 function loadRecommendations() 292 function loadRecommendations()
278 { 293 {
279 var request = new XMLHttpRequest(); 294 var request = new XMLHttpRequest();
280 request.open("GET", "subscriptions.xml", false); 295 request.open("GET", "subscriptions.xml", false);
281 request.addEventListener("load", function() 296 request.addEventListener("load", function()
282 { 297 {
283 var list = document.getElementById("subscriptionSelector"); 298 var list = document.getElementById("subscriptionSelector");
284 var docElem = request.responseXML.documentElement; 299 var docElem = request.responseXML.documentElement;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 { 338 {
324 if (!element) 339 if (!element)
325 return; 340 return;
326 341
327 if (element.hasAttribute("data-action")) 342 if (element.hasAttribute("data-action"))
328 break; 343 break;
329 344
330 element = element.parentElement; 345 element = element.parentElement;
331 } 346 }
332 347
333 switch (element.getAttribute("data-action")) 348 var actions = element.getAttribute("data-action").split(",");
334 { 349 for (var i = 0; i < actions.length; i++)
335 case "add-domain-exception": 350 {
336 addWhitelistedDomain(); 351 switch (actions[i])
337 break; 352 {
338 case "add-subscription": 353 case "add-domain-exception":
339 var dialog = E("dialog-content-predefined"); 354 addWhitelistedDomain();
340 var title = dialog.querySelector("h3").textContent; 355 break;
341 var url = dialog.querySelector(".url").textContent; 356 case "add-predefined-subscription":
342 addEnableSubscription(url, title); 357 var dialog = E("dialog-content-predefined");
343 document.body.removeAttribute("data-dialog"); 358 var title = dialog.querySelector("h3").textContent;
344 break; 359 var url = dialog.querySelector(".url").textContent;
345 case "cancel-domain-exception": 360 addEnableSubscription(url, title);
346 E("whitelisting-textbox").value = ""; 361 document.body.removeAttribute("data-dialog");
347 break; 362 break;
348 case "close-dialog": 363 case "cancel-custom-filters":
349 document.body.removeAttribute("data-dialog"); 364 E("custom-filters").classList.remove("mode-edit");
saroyanm 2015/07/16 12:32:27 Strange, classList according to MDN is supported s
Sebastian Noack 2015/07/16 12:36:50 As I already told you in a different comment, it's
saroyanm 2015/07/16 12:41:01 Ahh right your reference to caniuse went out of my
350 break; 365 break;
351 case "edit-custom-filters": 366 case "cancel-domain-exception":
352 editCustomFilters(); 367 E("whitelisting-textbox").value = "";
353 break; 368 break;
354 case "import-subscription": 369 case "close-dialog":
355 var url = E("blockingList-textbox").value; 370 document.body.removeAttribute("data-dialog");
356 addEnableSubscription(url); 371 break;
357 document.body.removeAttribute("data-dialog"); 372 case "edit-custom-filters":
358 break; 373 E("custom-filters").classList.add("mode-edit");
359 case "open-language-dialog": 374 editCustomFilters();
360 openDialog("language"); 375 break;
361 break; 376 case "import-subscription":
362 case "open-subscription-dialog": 377 var url = E("blockingList-textbox").value;
363 openDialog("custom"); 378 addEnableSubscription(url);
364 break; 379 document.body.removeAttribute("data-dialog");
365 case "switch-tab": 380 break;
366 document.body.setAttribute("data-tab", 381 case "open-dialog":
367 element.getAttribute("data-tab")); 382 openDialog(element.getAttribute("data-dialog"));
368 break; 383 break;
384 case "save-custom-filters":
385 ext.backgroundPage.sendMessage(
386 {
387 type: "filters.importRaw",
388 text: E("custom-filters-raw").value
389 });
390 E("custom-filters").classList.remove("mode-edit");
391 break;
392 case "switch-tab":
393 document.body.setAttribute("data-tab",
394 element.getAttribute("data-tab"));
395 break;
396 }
369 } 397 }
370 } 398 }
371 399
372 function onDOMLoaded() 400 function onDOMLoaded()
373 { 401 {
374 var recommendationTemplate = document.querySelector("#recommend-list-table t emplate"); 402 var recommendationTemplate = document.querySelector("#recommend-list-table t emplate");
375 var popularText = ext.i18n.getMessage("options_popular"); 403 var popularText = ext.i18n.getMessage("options_popular");
376 recommendationTemplate.content.querySelector(".popular").textContent = popul arText; 404 recommendationTemplate.content.querySelector(".popular").textContent = popul arText;
377 var languagesTemplate = document.querySelector("#all-lang-table template"); 405 var languagesTemplate = document.querySelector("#all-lang-table template");
378 var buttonText = ext.i18n.getMessage("options_button_add"); 406 var buttonText = ext.i18n.getMessage("options_button_add");
379 languagesTemplate.content.querySelector(".button-add span").textContent = bu ttonText; 407 languagesTemplate.content.querySelector(".button-add span").textContent = bu ttonText;
380 408
381 populateLists(); 409 populateLists();
382 410
383 function onFindLanguageKeyUp() 411 function onFindLanguageKeyUp()
384 { 412 {
385 var searchStyle = E("search-style"); 413 var searchStyle = E("search-style");
386 if (!this.value) 414 if (!this.value)
387 searchStyle.innerHTML = ""; 415 searchStyle.innerHTML = "";
388 else 416 else
389 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this .value.toLowerCase() + "\"]) { display: none; }"; 417 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this .value.toLowerCase() + "\"]) { display: none; }";
390 } 418 }
391 419
420 function isEnterPressed(e)
421 {
422 // e.keyCode has been deprecated so we attempt to use e.key
423 if ("key" in e)
424 return e.key == "Enter";
425 return e.keyCode == 13; // keyCode "13" corresponds to "Enter"
426 }
427
392 // Initialize navigation sidebar 428 // Initialize navigation sidebar
393 ext.backgroundPage.sendMessage( 429 ext.backgroundPage.sendMessage(
394 { 430 {
395 type: "app.get", 431 type: "app.get",
396 what: "addonVersion" 432 what: "addonVersion"
397 }, 433 },
398 function(addonVersion) 434 function(addonVersion)
399 { 435 {
400 E("abp-version").textContent = addonVersion; 436 E("abp-version").textContent = addonVersion;
401 }); 437 });
402 getDocLink("releases", function(link) 438 getDocLink("releases", function(link)
403 { 439 {
404 E("link-version").setAttribute("href", link); 440 E("link-version").setAttribute("href", link);
405 }); 441 });
406 442
407 getDocLink("contribute", function(link) 443 getDocLink("contribute", function(link)
408 { 444 {
409 document.querySelector("#tab-contribute a").setAttribute("href", link); 445 document.querySelector("#tab-contribute a").setAttribute("href", link);
410 }); 446 });
411 447
412 updateShareLink(); 448 updateShareLink();
413 449
414 // Initialize interactive UI elements 450 // Initialize interactive UI elements
415 document.body.addEventListener("click", onClick, false); 451 document.body.addEventListener("click", onClick, false);
416 var placeholderValue = ext.i18n.getMessage("options_dialog_language_find"); 452 var placeholderValue = ext.i18n.getMessage("options_dialog_language_find");
417 E("find-language").setAttribute("placeholder", placeholderValue); 453 E("find-language").setAttribute("placeholder", placeholderValue);
418 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false); 454 E("find-language").addEventListener("keyup", onFindLanguageKeyUp, false);
419 E("whitelisting-textbox").addEventListener("keypress", function(e) 455 E("whitelisting-textbox").addEventListener("keypress", function(e)
420 { 456 {
421 // e.keyCode has been deprecated so we attempt to use e.key 457 if (isEnterPressed(e))
422 // keyCode "13" corresponds to "Enter"
423 if ((e.key && e.key == "Enter") || (!e.key && e.keyCode == 13))
424 addWhitelistedDomain(); 458 addWhitelistedDomain();
425 }, false); 459 }, false);
460
461 // Advanced tab
462 var filterTextbox = document.querySelector("#custom-filters-add input");
463 placeholderValue = ext.i18n.getMessage("options_customFilters_textbox_placeh older");
464 filterTextbox.setAttribute("placeholder", placeholderValue);
465 function addCustomFilters()
466 {
467 var filterText = filterTextbox.value;
468 ext.backgroundPage.sendMessage(
469 {
470 type: "filters.add",
471 text: filterText
472 });
473 filterTextbox.value = "";
474 }
475 E("custom-filters-add").addEventListener("submit", function(e)
476 {
477 e.preventDefault();
478 addCustomFilters();
479 }, false);
480 var customFilterEditButtons = document.querySelectorAll("#custom-filters-edi t-wrapper button");
426 } 481 }
427 482
428 function openDialog(name) 483 function openDialog(name)
429 { 484 {
430 document.body.setAttribute("data-dialog", name); 485 document.body.setAttribute("data-dialog", name);
431 } 486 }
432 487
433 function populateLists() 488 function populateLists()
434 { 489 {
435 subscriptionsMap = Object.create(null); 490 subscriptionsMap = Object.create(null);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 type: "filters.add", 550 type: "filters.add",
496 text: "@@||" + domain.value.toLowerCase() + "^$document" 551 text: "@@||" + domain.value.toLowerCase() + "^$document"
497 }); 552 });
498 } 553 }
499 554
500 domain.value = ""; 555 domain.value = "";
501 } 556 }
502 557
503 function editCustomFilters() 558 function editCustomFilters()
504 { 559 {
505 //TODO: NYI 560 var customFilterItems = collections.customFilters.items;
561 var filterTexts = [];
562 for (var i = 0; i < customFilterItems.length; i++)
563 filterTexts.push(customFilterItems[i].text);
564 E("custom-filters-raw").value = filterTexts.join("\n");
506 } 565 }
507 566
508 function getAcceptableAdsURL(callback) 567 function getAcceptableAdsURL(callback)
509 { 568 {
510 ext.backgroundPage.sendMessage( 569 ext.backgroundPage.sendMessage(
511 { 570 {
512 type: "prefs.get", 571 type: "prefs.get",
513 key: "subscriptions_exceptionsurl" 572 key: "subscriptions_exceptionsurl"
514 }, 573 },
515 function(value) 574 function(value)
(...skipping 20 matching lines...) Expand all
536 url: url 595 url: url
537 }; 596 };
538 if (title) 597 if (title)
539 message.title = title; 598 message.title = title;
540 if (homepage) 599 if (homepage)
541 message.homepage = homepage; 600 message.homepage = homepage;
542 601
543 ext.backgroundPage.sendMessage(message); 602 ext.backgroundPage.sendMessage(message);
544 } 603 }
545 604
546 function removeSubscription(url)
547 {
548 ext.backgroundPage.sendMessage(
549 {
550 type: "subscriptions.remove",
551 url: url
552 });
553 }
554
555 function removeFilter(filter)
556 {
557 ext.backgroundPage.sendMessage(
558 {
559 type: "filters.remove",
560 text: filter
561 });
562 }
563
564 function onFilterMessage(action, filter) 605 function onFilterMessage(action, filter)
565 { 606 {
566 switch (action) 607 switch (action)
567 { 608 {
568 case "added": 609 case "added":
569 updateFilter(filter); 610 updateFilter(filter);
570 updateShareLink(); 611 updateShareLink();
571 break; 612 break;
572 case "loaded": 613 case "loaded":
573 populateLists(); 614 populateLists();
574 break; 615 break;
575 case "removed": 616 case "removed":
576 var knownFilter = filtersMap[filter.text]; 617 var knownFilter = filtersMap[filter.text];
577 collections.whitelist.removeItem(knownFilter); 618 collections.whitelist.removeItem(knownFilter);
619 collections.customFilters.removeItem(knownFilter);
578 delete filtersMap[filter.text]; 620 delete filtersMap[filter.text];
579 updateShareLink(); 621 updateShareLink();
580 break; 622 break;
581 } 623 }
582 } 624 }
583 625
584 function onSubscriptionMessage(action, subscription) 626 function onSubscriptionMessage(action, subscription)
585 { 627 {
586 switch (action) 628 switch (action)
587 { 629 {
(...skipping 26 matching lines...) Expand all
614 } 656 }
615 updateShareLink(); 657 updateShareLink();
616 }); 658 });
617 break; 659 break;
618 case "title": 660 case "title":
619 // TODO: NYI 661 // TODO: NYI
620 break; 662 break;
621 } 663 }
622 } 664 }
623 665
624 function showAddSubscriptionDialog(subscription)
saroyanm 2015/07/14 11:03:38 Make sense to rename the method as well, ex.: "sh
Thomas Greiner 2015/07/14 11:37:33 Done. Removed the function since it's only been us
625 {
626 var dialog = E("dialog-content-predefined");
627 dialog.querySelector("h3").textContent = subscription.title || "";
628 dialog.querySelector(".url").textContent = subscription.url;
629 openDialog("predefined");
630 }
631
632 function updateShareLink() 666 function updateShareLink()
633 { 667 {
634 ext.backgroundPage.sendMessage( 668 ext.backgroundPage.sendMessage(
635 { 669 {
636 type: "filters.blocked", 670 type: "filters.blocked",
637 url: "https://platform.twitter.com/widgets/", 671 url: "https://platform.twitter.com/widgets/",
638 requestType: "SCRIPT", 672 requestType: "SCRIPT",
639 docDomain: "adblockplus.org", 673 docDomain: "adblockplus.org",
640 thirdParty: true 674 thirdParty: true
641 }, 675 },
(...skipping 17 matching lines...) Expand all
659 link: link 693 link: link
660 }, callback); 694 }, callback);
661 } 695 }
662 696
663 ext.onMessage.addListener(function(message) 697 ext.onMessage.addListener(function(message)
664 { 698 {
665 switch (message.type) 699 switch (message.type)
666 { 700 {
667 case "app.listen": 701 case "app.listen":
668 if (message.action == "addSubscription") 702 if (message.action == "addSubscription")
669 showAddSubscriptionDialog(message.args[0]); 703 {
704 var subscription = message.args[0];
705 var dialog = E("dialog-content-predefined");
706 dialog.querySelector("h3").textContent = subscription.title || "";
707 dialog.querySelector(".url").textContent = subscription.url;
708 openDialog("predefined");
709 }
710 else if (message.action == "error")
711 {
712 alert(message.args.join("\n"));
713 }
670 break; 714 break;
671 case "filters.listen": 715 case "filters.listen":
672 onFilterMessage(message.action, message.args[0]); 716 onFilterMessage(message.action, message.args[0]);
673 break; 717 break;
674 case "subscriptions.listen": 718 case "subscriptions.listen":
675 onSubscriptionMessage(message.action, message.args[0]); 719 onSubscriptionMessage(message.action, message.args[0]);
676 break; 720 break;
677 } 721 }
678 }); 722 });
679 723
680 ext.backgroundPage.sendMessage( 724 ext.backgroundPage.sendMessage(
681 { 725 {
682 type: "app.listen", 726 type: "app.listen",
683 filter: ["addSubscription"] 727 filter: ["addSubscription", "error"]
684 }); 728 });
685 ext.backgroundPage.sendMessage( 729 ext.backgroundPage.sendMessage(
686 { 730 {
687 type: "filters.listen", 731 type: "filters.listen",
688 filter: ["added", "loaded", "removed"] 732 filter: ["added", "loaded", "removed"]
689 }); 733 });
690 ext.backgroundPage.sendMessage( 734 ext.backgroundPage.sendMessage(
691 { 735 {
692 type: "subscriptions.listen", 736 type: "subscriptions.listen",
693 filter: ["added", "disabled", "homepage", "removed", "title"] 737 filter: ["added", "disabled", "homepage", "removed", "title"]
694 }); 738 });
695 739
696 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 740 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
697 })(); 741 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld