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: Rebased to changeset #108 Created April 26, 2017, 5:57 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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 /* globals checkShareResource, getDocLink, i18nFormatDateTime, openSharePopup, 18 /* globals checkShareResource, getDocLink, i18nFormatDateTime, openSharePopup,
19 E */ 19 E */
20 20
21 "use strict"; 21 "use strict";
22 22
23 { 23 {
24 let subscriptionsMap = Object.create(null); 24 let subscriptionsMap = Object.create(null);
25 let filtersMap = Object.create(null); 25 let filtersMap = Object.create(null);
26 let collections = Object.create(null); 26 let collections = Object.create(null);
27 let acceptableAdsUrl = null; 27 let acceptableAdsUrl = null;
28 let isCustomFiltersLoaded = false;
28 let {getMessage} = ext.i18n; 29 let {getMessage} = ext.i18n;
29 let filterErrors = new Map([ 30 let filterErrors = new Map([
30 ["synchronize_invalid_url", 31 ["synchronize_invalid_url",
31 "options_filterList_lastDownload_invalidURL"], 32 "options_filterList_lastDownload_invalidURL"],
32 ["synchronize_connection_error", 33 ["synchronize_connection_error",
33 "options_filterList_lastDownload_connectionError"], 34 "options_filterList_lastDownload_connectionError"],
34 ["synchronize_invalid_data", 35 ["synchronize_invalid_data",
35 "options_filterList_lastDownload_invalidData"], 36 "options_filterList_lastDownload_invalidData"],
36 ["synchronize_checksum_mismatch", 37 ["synchronize_checksum_mismatch",
37 "options_filterList_lastDownload_checksumMismatch"] 38 "options_filterList_lastDownload_checksumMismatch"]
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 else 467 else
467 location.href = link; 468 location.href = link;
468 }); 469 });
469 } 470 }
470 471
471 function switchTab(id) 472 function switchTab(id)
472 { 473 {
473 location.hash = id; 474 location.hash = id;
474 } 475 }
475 476
477 function execAction(action, key, element)
478 {
479 switch (action)
480 {
481 case "add-domain-exception":
482 addWhitelistedDomain();
483 break;
484 case "add-language-subscription":
485 addEnableSubscription(findParentData(element, "access", false));
486 break;
487 case "add-predefined-subscription": {
488 let dialog = E("dialog-content-predefined");
489 let title = dialog.querySelector("h3").textContent;
490 let url = dialog.querySelector(".url").textContent;
491 addEnableSubscription(url, title);
492 closeDialog();
493 break;
494 }
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
501 .remove("mode-edit");
502 break;
503 case "close-dialog":
504 closeDialog();
505 break;
506 case "edit-custom-filters":
507 editCustomFilters();
508 break;
509 case "edit-domain-exception":
510 document.querySelector("#whitelisting .controls").classList
511 .add("mode-edit");
512 E("whitelisting-textbox").focus();
513 break;
514 case "import-subscription": {
515 let url = E("blockingList-textbox").value;
516 addEnableSubscription(url);
517 closeDialog();
518 break;
519 }
520 case "open-context-menu": {
521 let listItem = findParentData(element, "access", true);
522 if (listItem != context)
523 listItem.classList.add("show-context-menu");
524 break;
525 }
526 case "open-dialog": {
527 let dialog = findParentData(element, "dialog", false);
528 openDialog(dialog);
529 break;
530 }
531 case "open-doclink": {
532 let doclink = findParentData(element, "doclink", false);
533 openDocLink(doclink);
534 break;
535 }
536 case "remove-filter":
537 ext.backgroundPage.sendMessage({
538 type: "filters.remove",
539 text: findParentData(element, "access", false)
540 });
541 break;
542 case "remove-subscription":
543 ext.backgroundPage.sendMessage({
544 type: "subscriptions.remove",
545 url: findParentData(element, "access", false)
546 });
547 break;
548 case "save-custom-filters":
549 sendMessageHandleErrors({
550 type: "filters.importRaw",
551 text: E("custom-filters-raw").value,
552 removeExisting: true
553 },
554 () =>
555 {
556 E("custom-filters").classList.remove("mode-edit");
557 });
558 break;
559 case "switch-tab":
560 if (key == "Enter")
561 {
562 let tabId = findParentData(element, "tab", false);
563 switchTab(tabId);
564 }
565 else if (element.hasAttribute("aria-selected"))
566 {
567 if (key == "ArrowLeft" || key == "ArrowUp")
568 {
569 element = element.previousElementSibling ||
570 container.lastElementChild;
571 }
572 else if (key == "ArrowRight" || key == "ArrowDown")
573 {
574 element = element.nextElementSibling ||
575 container.firstElementChild;
576 }
577 let tabId = findParentData(element, "tab", false);
578 switchTab(tabId);
579 }
580 break;
581 case "toggle-disable-subscription":
582 ext.backgroundPage.sendMessage({
583 type: "subscriptions.toggle",
584 keepInstalled: true,
585 url: findParentData(element, "access", false)
586 });
587 break;
588 case "toggle-pref":
589 ext.backgroundPage.sendMessage({
590 type: "prefs.toggle",
591 key: findParentData(element, "pref", false)
592 });
593 break;
594 case "toggle-remove-subscription":
595 let 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 type: "subscriptions.update"
609 });
610 break;
611 case "update-subscription":
612 ext.backgroundPage.sendMessage({
613 type: "subscriptions.update",
614 url: findParentData(element, "access", false)
615 });
616 break;
617 }
618 }
619
476 function onClick(e) 620 function onClick(e)
477 { 621 {
478 let context = document.querySelector(".show-context-menu"); 622 let context = document.querySelector(".show-context-menu");
479 if (context) 623 if (context)
480 context.classList.remove("show-context-menu"); 624 context.classList.remove("show-context-menu");
481 625
482 let element = findParentData(e.target, "action", true); 626 let actions = findParentData(e.target, "action", false);
483 if (!element) 627 if (!actions)
484 return; 628 return;
485 629
486 let actions = element.getAttribute("data-action").split(","); 630 actions = actions.split(",");
487 for (let action of actions) 631 for (let action of actions)
488 { 632 {
489 switch (action) 633 execAction(action, "Enter", e.target);
490 {
491 case "add-domain-exception":
492 addWhitelistedDomain();
493 break;
494 case "add-predefined-subscription": {
495 let dialog = E("dialog-content-predefined");
496 let title = dialog.querySelector("h3").textContent;
497 let url = dialog.querySelector(".url").textContent;
498 addEnableSubscription(url, title);
499 closeDialog();
500 break;
501 }
502 case "cancel-custom-filters":
503 E("custom-filters").classList.remove("mode-edit");
504 break;
505 case "cancel-domain-exception":
506 E("whitelisting-textbox").value = "";
507 document.querySelector("#whitelisting .controls").classList
508 .remove("mode-edit");
509 break;
510 case "close-dialog":
511 closeDialog();
512 break;
513 case "edit-custom-filters":
514 E("custom-filters").classList.add("mode-edit");
515 editCustomFilters();
516 break;
517 case "edit-domain-exception":
518 document.querySelector("#whitelisting .controls").classList
519 .add("mode-edit");
520 E("whitelisting-textbox").focus();
521 break;
522 case "import-subscription": {
523 let url = E("blockingList-textbox").value;
524 addEnableSubscription(url);
525 closeDialog();
526 break;
527 }
528 case "open-dialog": {
529 let dialog = findParentData(element, "dialog", false);
530 openDialog(dialog);
531 break;
532 }
533 case "open-doclink": {
534 let doclink = findParentData(element, "doclink", false);
535 openDocLink(doclink);
536 break;
537 }
538 case "save-custom-filters":
539 sendMessageHandleErrors({
540 type: "filters.importRaw",
541 text: E("custom-filters-raw").value,
542 removeExisting: true
543 },
544 () =>
545 {
546 E("custom-filters").classList.remove("mode-edit");
547 });
548 break;
549 case "switch-tab": {
550 let tabId = findParentData(e.target, "tab", false);
551 switchTab(tabId);
552 break;
553 }
554 case "toggle-pref":
555 ext.backgroundPage.sendMessage({
556 type: "prefs.toggle",
557 key: findParentData(element, "pref", false)
558 });
559 break;
560 case "update-all-subscriptions":
561 ext.backgroundPage.sendMessage({
562 type: "subscriptions.update"
563 });
564 break;
565 case "open-context-menu": {
566 let listItem = findParentData(element, "access", true);
567 if (listItem != context)
568 listItem.classList.add("show-context-menu");
569 break;
570 }
571 case "update-subscription":
572 ext.backgroundPage.sendMessage({
573 type: "subscriptions.update",
574 url: findParentData(element, "access", false)
575 });
576 break;
577 case "remove-subscription":
578 ext.backgroundPage.sendMessage({
579 type: "subscriptions.remove",
580 url: findParentData(element, "access", false)
581 });
582 break;
583 case "toggle-remove-subscription": {
584 let subscriptionUrl = findParentData(element, "access", false);
585 if (element.getAttribute("aria-checked") == "true")
586 {
587 ext.backgroundPage.sendMessage({
588 type: "subscriptions.remove",
589 url: subscriptionUrl
590 });
591 }
592 else
593 addEnableSubscription(subscriptionUrl);
594 break;
595 }
596 case "toggle-disable-subscription":
597 ext.backgroundPage.sendMessage({
598 type: "subscriptions.toggle",
599 keepInstalled: true,
600 url: findParentData(element, "access", false)
601 });
602 break;
603 case "add-language-subscription":
604 addEnableSubscription(findParentData(element, "access", false));
605 break;
606 case "remove-filter":
607 ext.backgroundPage.sendMessage({
608 type: "filters.remove",
609 text: findParentData(element, "access", false)
610 });
611 break;
612 }
613 } 634 }
614 } 635 }
615 636
616 function getKey(e) 637 function getKey(e)
617 { 638 {
618 // e.keyCode has been deprecated so we attempt to use e.key 639 // e.keyCode has been deprecated so we attempt to use e.key
619 if ("key" in e) 640 if ("key" in e)
620 return e.key; 641 return e.key;
621 return getKey.keys[e.keyCode]; 642 return getKey.keys[e.keyCode];
622 } 643 }
(...skipping 15 matching lines...) Expand all
638 return; 659 return;
639 660
640 let container = findParentData(element, "action", true); 661 let container = findParentData(element, "action", true);
641 if (!container || !container.hasAttribute("data-keys")) 662 if (!container || !container.hasAttribute("data-keys"))
642 return; 663 return;
643 664
644 let keys = container.getAttribute("data-keys").split(" "); 665 let keys = container.getAttribute("data-keys").split(" ");
645 if (keys.indexOf(key) < 0) 666 if (keys.indexOf(key) < 0)
646 return; 667 return;
647 668
648 switch (container.getAttribute("data-action")) 669 let actions = container.getAttribute("data-action").split(",");
670 for (let action of actions)
649 { 671 {
650 case "add-domain-exception": 672 execAction(action, key, element);
651 addWhitelistedDomain();
652 break;
653 case "open-doclink":
654 let doclink = findParentData(element, "doclink", false);
655 openDocLink(doclink);
656 break;
657 case "switch-tab":
658 if (key == "Enter")
659 {
660 let tabId = findParentData(element, "tab", false);
661 switchTab(tabId);
662 }
663 else if (element.hasAttribute("aria-selected"))
664 {
665 if (key == "ArrowLeft" || key == "ArrowUp")
666 {
667 element = element.previousElementSibling ||
668 container.lastElementChild;
669 }
670 else if (key == "ArrowRight" || key == "ArrowDown")
671 {
672 element = element.nextElementSibling ||
673 container.firstElementChild;
674 }
675
676 let tabId = findParentData(element, "tab", false);
677 switchTab(tabId);
678 }
679 break;
680 } 673 }
681 } 674 }
682 675
683 function selectTabItem(tabId, container, focus) 676 function selectTabItem(tabId, container, focus)
684 { 677 {
685 // Show tab content 678 // Show tab content
686 document.body.setAttribute("data-tab", tabId); 679 document.body.setAttribute("data-tab", tabId);
687 680
688 // Select tab 681 // Select tab
689 let tabList = container.querySelector("[role='tablist']"); 682 let tabList = container.querySelector("[role='tablist']");
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 for (let subscription of subscriptions) 931 for (let subscription of subscriptions)
939 { 932 {
940 ext.backgroundPage.sendMessage({ 933 ext.backgroundPage.sendMessage({
941 type: "filters.get", 934 type: "filters.get",
942 subscriptionUrl: subscription.url 935 subscriptionUrl: subscription.url
943 }, 936 },
944 (filters) => 937 (filters) =>
945 { 938 {
946 for (let filter of filters) 939 for (let filter of filters)
947 updateFilter(filter); 940 updateFilter(filter);
941
942 isCustomFiltersLoaded = true;
948 }); 943 });
949 } 944 }
950 }); 945 });
951 loadRecommendations(); 946 loadRecommendations();
952 ext.backgroundPage.sendMessage({ 947 ext.backgroundPage.sendMessage({
953 type: "prefs.get", 948 type: "prefs.get",
954 key: "subscriptions_exceptionsurl" 949 key: "subscriptions_exceptionsurl"
955 }, 950 },
956 (url) => 951 (url) =>
957 { 952 {
(...skipping 27 matching lines...) Expand all
985 }); 980 });
986 } 981 }
987 982
988 domain.value = ""; 983 domain.value = "";
989 document.querySelector("#whitelisting .controls") 984 document.querySelector("#whitelisting .controls")
990 .classList.remove("mode-edit"); 985 .classList.remove("mode-edit");
991 } 986 }
992 987
993 function editCustomFilters() 988 function editCustomFilters()
994 { 989 {
990 if (!isCustomFiltersLoaded)
991 {
992 console.error("Custom filters are not loaded");
993 return;
994 }
995
996 E("custom-filters").classList.add("mode-edit");
995 let filterTexts = []; 997 let filterTexts = [];
996 for (let customFilterItem of collections.customFilters.items) 998 for (let customFilterItem of collections.customFilters.items)
997 filterTexts.push(customFilterItem.text); 999 filterTexts.push(customFilterItem.text);
998 E("custom-filters-raw").value = filterTexts.join("\n"); 1000 E("custom-filters-raw").value = filterTexts.join("\n");
999 } 1001 }
1000 1002
1001 function addEnableSubscription(url, title, homepage) 1003 function addEnableSubscription(url, title, homepage)
1002 { 1004 {
1003 let messageType = null; 1005 let messageType = null;
1004 let knownSubscription = subscriptionsMap[url]; 1006 let knownSubscription = subscriptionsMap[url];
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
1297 }); 1299 });
1298 ext.backgroundPage.sendMessage({ 1300 ext.backgroundPage.sendMessage({
1299 type: "subscriptions.listen", 1301 type: "subscriptions.listen",
1300 filter: ["added", "disabled", "homepage", "lastDownload", "removed", 1302 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
1301 "title", "downloadStatus", "downloading"] 1303 "title", "downloadStatus", "downloading"]
1302 }); 1304 });
1303 1305
1304 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 1306 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
1305 window.addEventListener("hashchange", onHashChange, false); 1307 window.addEventListener("hashchange", onHashChange, false);
1306 } 1308 }
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