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

Side by Side Diff: new-options.js

Issue 29373665: issue 4264 - centralize action handling (Closed)
Patch Set: Added error log Created March 3, 2017, 1:08 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 | « new-options.html ('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-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 "use strict"; 18 "use strict";
19 19
20 (function() 20 (function()
21 { 21 {
22 var subscriptionsMap = Object.create(null); 22 var subscriptionsMap = Object.create(null);
23 var filtersMap = Object.create(null); 23 var filtersMap = Object.create(null);
24 var collections = Object.create(null); 24 var collections = Object.create(null);
25 var acceptableAdsUrl = null; 25 var acceptableAdsUrl = null;
26 var isCustomFiltersLoaded = false;
26 var getMessage = ext.i18n.getMessage; 27 var getMessage = ext.i18n.getMessage;
27 var filterErrors = 28 var filterErrors =
28 { 29 {
29 "synchronize_invalid_url": "options_filterList_lastDownload_invalidURL", 30 "synchronize_invalid_url": "options_filterList_lastDownload_invalidURL",
30 "synchronize_connection_error": "options_filterList_lastDownload_connectionE rror", 31 "synchronize_connection_error": "options_filterList_lastDownload_connectionE rror",
31 "synchronize_invalid_data": "options_filterList_lastDownload_invalidData", 32 "synchronize_invalid_data": "options_filterList_lastDownload_invalidData",
32 "synchronize_checksum_mismatch": "options_filterList_lastDownload_checksumMi smatch" 33 "synchronize_checksum_mismatch": "options_filterList_lastDownload_checksumMi smatch"
33 }; 34 };
34 35
35 function Collection(details) 36 function Collection(details)
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 else 468 else
468 location.href = link; 469 location.href = link;
469 }); 470 });
470 } 471 }
471 472
472 function switchTab(id) 473 function switchTab(id)
473 { 474 {
474 location.hash = id; 475 location.hash = id;
475 } 476 }
476 477
478 function execAction(action, key, element)
479 {
480 switch (action)
481 {
482 case "add-domain-exception":
483 addWhitelistedDomain();
484 break;
485 case "add-language-subscription":
486 addEnableSubscription(findParentData(element, "access", false));
487 break;
488 case "add-predefined-subscription":
489 var dialog = E("dialog-content-predefined");
490 var title = dialog.querySelector("h3").textContent;
491 var url = dialog.querySelector(".url").textContent;
492 addEnableSubscription(url, title);
493 closeDialog();
494 break;
495 case "cancel-custom-filters":
496 E("custom-filters").classList.remove("mode-edit");
497 break;
498 case "cancel-domain-exception":
499 E("whitelisting-textbox").value = "";
500 document.querySelector("#whitelisting .controls").classList.remove("mode -edit");
501 break;
502 case "close-dialog":
503 closeDialog();
504 break;
505 case "edit-custom-filters":
506 editCustomFilters();
507 break;
508 case "edit-domain-exception":
509 document.querySelector("#whitelisting .controls").classList.add("mode-ed it");
510 E("whitelisting-textbox").focus();
511 break;
512 case "import-subscription":
513 var url = E("blockingList-textbox").value;
514 addEnableSubscription(url);
515 closeDialog();
516 break;
517 case "open-context-menu":
518 var listItem = findParentData(element, "access", true);
519 if (listItem != context)
520 listItem.classList.add("show-context-menu");
521 break;
522 case "open-dialog":
523 var dialog = findParentData(element, "dialog", false);
524 openDialog(dialog);
525 break;
526 case "open-doclink":
527 var doclink = findParentData(element, "doclink", false);
528 openDocLink(doclink);
529 break;
530 case "remove-filter":
531 ext.backgroundPage.sendMessage(
532 {
533 type: "filters.remove",
534 text: findParentData(element, "access", false)
535 });
536 break;
537 case "remove-subscription":
538 ext.backgroundPage.sendMessage(
539 {
540 type: "subscriptions.remove",
541 url: findParentData(element, "access", false)
542 });
543 break;
544 case "save-custom-filters":
545 sendMessageHandleErrors(
546 {
547 type: "filters.importRaw",
548 text: E("custom-filters-raw").value,
549 removeExisting: true
550 },
551 function()
552 {
553 E("custom-filters").classList.remove("mode-edit");
554 });
555 break;
556 case "switch-tab":
557 if (key == "Enter")
558 {
559 var tabId = findParentData(element, "tab", false);
560 switchTab(tabId);
561 }
562 else if (element.hasAttribute("aria-selected"))
563 {
564 if (key == "ArrowLeft" || key == "ArrowUp")
565 {
566 element = element.previousElementSibling
567 || container.lastElementChild;
568 }
569 else if (key == "ArrowRight" || key == "ArrowDown")
570 {
571 element = element.nextElementSibling
572 || container.firstElementChild;
573 }
574
575 var tabId = findParentData(element, "tab", false);
576 switchTab(tabId);
577 }
578 break;
579 case "toggle-disable-subscription":
580 ext.backgroundPage.sendMessage(
581 {
582 type: "subscriptions.toggle",
583 keepInstalled: true,
584 url: findParentData(element, "access", false)
585 });
586 break;
587 case "toggle-pref":
588 ext.backgroundPage.sendMessage(
589 {
590 type: "prefs.toggle",
591 key: findParentData(element, "pref", false)
592 });
593 break;
594 case "toggle-remove-subscription":
595 var subscriptionUrl = findParentData(element, "access", false);
596 if (element.getAttribute("aria-checked") == "true")
597 {
598 ext.backgroundPage.sendMessage({
599 type: "subscriptions.remove",
600 url: subscriptionUrl
601 });
602 }
603 else
604 addEnableSubscription(subscriptionUrl);
605 break;
606 case "update-all-subscriptions":
607 ext.backgroundPage.sendMessage(
608 {
609 type: "subscriptions.update"
610 });
611 break;
612 case "update-subscription":
613 ext.backgroundPage.sendMessage(
614 {
615 type: "subscriptions.update",
616 url: findParentData(element, "access", false)
617 });
618 break;
619 }
620 }
621
477 function onClick(e) 622 function onClick(e)
478 { 623 {
479 var context = document.querySelector(".show-context-menu"); 624 var context = document.querySelector(".show-context-menu");
480 if (context) 625 if (context)
481 context.classList.remove("show-context-menu"); 626 context.classList.remove("show-context-menu");
482 627
483 var element = e.target; 628 var actions = findParentData(e.target, "action", false);
484 while (true) 629 if (!actions)
485 { 630 return;
486 if (!element)
487 return;
488 631
489 if (element.hasAttribute("data-action")) 632 actions = actions.split(",");
490 break;
491
492 element = element.parentElement;
493 }
494
495 var element = findParentData(e.target, "action", true);
496 var actions = element.getAttribute("data-action").split(",");
497 for (var i = 0; i < actions.length; i++) 633 for (var i = 0; i < actions.length; i++)
498 { 634 {
499 switch (actions[i]) 635 execAction(actions[i], "Enter", e.target);
500 {
501 case "add-domain-exception":
502 addWhitelistedDomain();
503 break;
504 case "add-predefined-subscription":
505 var dialog = E("dialog-content-predefined");
506 var title = dialog.querySelector("h3").textContent;
507 var url = dialog.querySelector(".url").textContent;
508 addEnableSubscription(url, title);
509 closeDialog();
510 break;
511 case "cancel-custom-filters":
512 E("custom-filters").classList.remove("mode-edit");
513 break;
514 case "cancel-domain-exception":
515 E("whitelisting-textbox").value = "";
516 document.querySelector("#whitelisting .controls").classList.remove("mo de-edit");
517 break;
518 case "close-dialog":
519 closeDialog();
520 break;
521 case "edit-custom-filters":
522 E("custom-filters").classList.add("mode-edit");
523 editCustomFilters();
524 break;
525 case "edit-domain-exception":
526 document.querySelector("#whitelisting .controls").classList.add("mode- edit");
527 E("whitelisting-textbox").focus();
528 break;
529 case "import-subscription":
530 var url = E("blockingList-textbox").value;
531 addEnableSubscription(url);
532 closeDialog();
533 break;
534 case "open-dialog":
535 var dialog = findParentData(element, "dialog", false);
536 openDialog(dialog);
537 break;
538 case "open-doclink":
539 var doclink = findParentData(element, "doclink", false);
540 openDocLink(doclink);
541 break;
542 case "save-custom-filters":
543 sendMessageHandleErrors(
544 {
545 type: "filters.importRaw",
546 text: E("custom-filters-raw").value,
547 removeExisting: true
548 },
549 function()
550 {
551 E("custom-filters").classList.remove("mode-edit");
552 });
553 break;
554 case "switch-tab":
555 var tabId = findParentData(e.target, "tab", false);
556 switchTab(tabId);
557 break;
558 case "toggle-pref":
559 ext.backgroundPage.sendMessage(
560 {
561 type: "prefs.toggle",
562 key: findParentData(element, "pref", false)
563 });
564 break;
565 case "update-all-subscriptions":
566 ext.backgroundPage.sendMessage(
567 {
568 type: "subscriptions.update"
569 });
570 break;
571 case "open-context-menu":
572 var listItem = findParentData(element, "access", true);
573 if (listItem != context)
574 listItem.classList.add("show-context-menu");
575 break;
576 case "update-subscription":
577 ext.backgroundPage.sendMessage(
578 {
579 type: "subscriptions.update",
580 url: findParentData(element, "access", false)
581 });
582 break;
583 case "remove-subscription":
584 ext.backgroundPage.sendMessage(
585 {
586 type: "subscriptions.remove",
587 url: findParentData(element, "access", false)
588 });
589 break;
590 case "toggle-remove-subscription":
591 var subscriptionUrl = findParentData(element, "access", false);
592 if (element.getAttribute("aria-checked") == "true")
593 {
594 ext.backgroundPage.sendMessage({
595 type: "subscriptions.remove",
596 url: subscriptionUrl
597 });
598 }
599 else
600 addEnableSubscription(subscriptionUrl);
601 break;
602 case "toggle-disable-subscription":
603 ext.backgroundPage.sendMessage(
604 {
605 type: "subscriptions.toggle",
606 keepInstalled: true,
607 url: findParentData(element, "access", false)
608 });
609 break;
610 case "add-language-subscription":
611 addEnableSubscription(findParentData(element, "access", false));
612 break;
613 case "remove-filter":
614 ext.backgroundPage.sendMessage(
615 {
616 type: "filters.remove",
617 text: findParentData(element, "access", false)
618 });
619 break;
620 }
621 } 636 }
622 } 637 }
623 638
624 function getKey(e) 639 function getKey(e)
625 { 640 {
626 // e.keyCode has been deprecated so we attempt to use e.key 641 // e.keyCode has been deprecated so we attempt to use e.key
627 if ("key" in e) 642 if ("key" in e)
628 return e.key; 643 return e.key;
629 return getKey.keys[e.keyCode]; 644 return getKey.keys[e.keyCode];
630 } 645 }
(...skipping 15 matching lines...) Expand all
646 return; 661 return;
647 662
648 var container = findParentData(element, "action", true); 663 var container = findParentData(element, "action", true);
649 if (!container || !container.hasAttribute("data-keys")) 664 if (!container || !container.hasAttribute("data-keys"))
650 return; 665 return;
651 666
652 var keys = container.getAttribute("data-keys").split(" "); 667 var keys = container.getAttribute("data-keys").split(" ");
653 if (keys.indexOf(key) < 0) 668 if (keys.indexOf(key) < 0)
654 return; 669 return;
655 670
656 switch (container.getAttribute("data-action")) 671 var actions = container.getAttribute("data-action").split(",");
672 for (var i = 0; i < actions.length; i++)
657 { 673 {
658 case "add-domain-exception": 674 execAction(actions[i], key, element);
659 addWhitelistedDomain();
660 break;
661 case "open-doclink":
662 var doclink = findParentData(element, "doclink", false);
663 openDocLink(doclink);
664 break;
665 case "switch-tab":
666 if (key == "Enter")
667 {
668 var tabId = findParentData(element, "tab", false);
669 switchTab(tabId);
670 }
671 else if (element.hasAttribute("aria-selected"))
672 {
673 if (key == "ArrowLeft" || key == "ArrowUp")
674 {
675 element = element.previousElementSibling
676 || container.lastElementChild;
677 }
678 else if (key == "ArrowRight" || key == "ArrowDown")
679 {
680 element = element.nextElementSibling
681 || container.firstElementChild;
682 }
683
684 var tabId = findParentData(element, "tab", false);
685 switchTab(tabId);
686 }
687 break;
688 } 675 }
689 } 676 }
690 677
691 function selectTabItem(tabId, container, focus) 678 function selectTabItem(tabId, container, focus)
692 { 679 {
693 // Show tab content 680 // Show tab content
694 document.body.setAttribute("data-tab", tabId); 681 document.body.setAttribute("data-tab", tabId);
695 682
696 // Select tab 683 // Select tab
697 var tabList = container.querySelector("[role='tablist']"); 684 var tabList = container.querySelector("[role='tablist']");
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 { 937 {
951 ext.backgroundPage.sendMessage( 938 ext.backgroundPage.sendMessage(
952 { 939 {
953 type: "filters.get", 940 type: "filters.get",
954 subscriptionUrl: subscriptions[i].url 941 subscriptionUrl: subscriptions[i].url
955 }, 942 },
956 function(filters) 943 function(filters)
957 { 944 {
958 for (var i = 0; i < filters.length; i++) 945 for (var i = 0; i < filters.length; i++)
959 updateFilter(filters[i]); 946 updateFilter(filters[i]);
947
948 isCustomFiltersLoaded = true;
960 }); 949 });
961 } 950 }
962 }); 951 });
963 loadRecommendations(); 952 loadRecommendations();
964 ext.backgroundPage.sendMessage( 953 ext.backgroundPage.sendMessage(
965 { 954 {
966 type: "prefs.get", 955 type: "prefs.get",
967 key: "subscriptions_exceptionsurl" 956 key: "subscriptions_exceptionsurl"
968 }, 957 },
969 function(url) 958 function(url)
(...skipping 29 matching lines...) Expand all
999 text: "@@||" + domain.value.toLowerCase() + "^$document" 988 text: "@@||" + domain.value.toLowerCase() + "^$document"
1000 }); 989 });
1001 } 990 }
1002 991
1003 domain.value = ""; 992 domain.value = "";
1004 document.querySelector("#whitelisting .controls").classList.remove("mode-edi t"); 993 document.querySelector("#whitelisting .controls").classList.remove("mode-edi t");
1005 } 994 }
1006 995
1007 function editCustomFilters() 996 function editCustomFilters()
1008 { 997 {
998 if (!isCustomFiltersLoaded)
999 {
1000 console.error("Custom filters are not loaded");
1001 return;
1002 }
1003
1004 E("custom-filters").classList.add("mode-edit");
1009 var customFilterItems = collections.customFilters.items; 1005 var customFilterItems = collections.customFilters.items;
1010 var filterTexts = []; 1006 var filterTexts = [];
1011 for (var i = 0; i < customFilterItems.length; i++) 1007 for (var i = 0; i < customFilterItems.length; i++)
1012 filterTexts.push(customFilterItems[i].text); 1008 filterTexts.push(customFilterItems[i].text);
1013 E("custom-filters-raw").value = filterTexts.join("\n"); 1009 E("custom-filters-raw").value = filterTexts.join("\n");
1014 } 1010 }
1015 1011
1016 function addEnableSubscription(url, title, homepage) 1012 function addEnableSubscription(url, title, homepage)
1017 { 1013 {
1018 var messageType = null; 1014 var messageType = null;
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
1316 ext.backgroundPage.sendMessage( 1312 ext.backgroundPage.sendMessage(
1317 { 1313 {
1318 type: "subscriptions.listen", 1314 type: "subscriptions.listen",
1319 filter: ["added", "disabled", "homepage", "lastDownload", "removed", 1315 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
1320 "title", "downloadStatus", "downloading"] 1316 "title", "downloadStatus", "downloading"]
1321 }); 1317 });
1322 1318
1323 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 1319 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
1324 window.addEventListener("hashchange", onHashChange, false); 1320 window.addEventListener("hashchange", onHashChange, false);
1325 })(); 1321 })();
OLDNEW
« no previous file with comments | « new-options.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld