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: Moved key handling into onKeyUp Created May 16, 2017, 10:32 a.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, 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 && !listItem.classList.contains("show-context-menu"))
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 let tabId = findParentData(element, "tab", false);
561 switchTab(tabId);
562 break;
563 case "toggle-disable-subscription":
564 ext.backgroundPage.sendMessage({
565 type: "subscriptions.toggle",
566 keepInstalled: true,
567 url: findParentData(element, "access", false)
568 });
569 break;
570 case "toggle-pref":
571 ext.backgroundPage.sendMessage({
572 type: "prefs.toggle",
573 key: findParentData(element, "pref", false)
574 });
575 break;
576 case "toggle-remove-subscription":
577 let subscriptionUrl = findParentData(element, "access", false);
578 if (element.getAttribute("aria-checked") == "true")
579 {
580 ext.backgroundPage.sendMessage({
581 type: "subscriptions.remove",
582 url: subscriptionUrl
583 });
584 }
585 else
586 addEnableSubscription(subscriptionUrl);
587 break;
588 case "update-all-subscriptions":
589 ext.backgroundPage.sendMessage({
590 type: "subscriptions.update"
591 });
592 break;
593 case "update-subscription":
594 ext.backgroundPage.sendMessage({
595 type: "subscriptions.update",
596 url: findParentData(element, "access", false)
597 });
598 break;
599 }
600 }
601
476 function onClick(e) 602 function onClick(e)
477 { 603 {
478 let context = document.querySelector(".show-context-menu"); 604 let context = document.querySelector(".show-context-menu");
479 if (context) 605 if (context)
480 context.classList.remove("show-context-menu"); 606 context.classList.remove("show-context-menu");
481 607
482 let element = findParentData(e.target, "action", true); 608 let actions = findParentData(e.target, "action", false);
483 if (!element) 609 if (!actions)
484 return; 610 return;
485 611
486 let actions = element.getAttribute("data-action").split(","); 612 actions = actions.split(",");
487 for (let action of actions) 613 for (let action of actions)
488 { 614 {
489 switch (action) 615 execAction(action, 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 } 616 }
614 } 617 }
615 618
616 function getKey(e) 619 function getKey(e)
617 { 620 {
618 // e.keyCode has been deprecated so we attempt to use e.key 621 // e.keyCode has been deprecated so we attempt to use e.key
619 if ("key" in e) 622 if ("key" in e)
620 return e.key; 623 return e.key;
621 return getKey.keys[e.keyCode]; 624 return getKey.keys[e.keyCode];
622 } 625 }
(...skipping 15 matching lines...) Expand all
638 return; 641 return;
639 642
640 let container = findParentData(element, "action", true); 643 let container = findParentData(element, "action", true);
641 if (!container || !container.hasAttribute("data-keys")) 644 if (!container || !container.hasAttribute("data-keys"))
642 return; 645 return;
643 646
644 let keys = container.getAttribute("data-keys").split(" "); 647 let keys = container.getAttribute("data-keys").split(" ");
645 if (keys.indexOf(key) < 0) 648 if (keys.indexOf(key) < 0)
646 return; 649 return;
647 650
648 switch (container.getAttribute("data-action")) 651 if (element.getAttribute("role") == "tab")
649 { 652 {
650 case "add-domain-exception": 653 if (key == "ArrowLeft" || key == "ArrowUp")
651 addWhitelistedDomain(); 654 element = element.previousElementSibling || container.lastElementChild;
652 break; 655 else if (key == "ArrowRight" || key == "ArrowDown")
653 case "open-doclink": 656 element = element.nextElementSibling || container.firstElementChild;
654 let doclink = findParentData(element, "doclink", false); 657 }
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 658
676 let tabId = findParentData(element, "tab", false); 659 let actions = container.getAttribute("data-action").split(",");
677 switchTab(tabId); 660 for (let action of actions)
678 } 661 {
679 break; 662 execAction(action, element);
680 } 663 }
681 } 664 }
682 665
683 function selectTabItem(tabId, container, focus) 666 function selectTabItem(tabId, container, focus)
684 { 667 {
685 // Show tab content 668 // Show tab content
686 document.body.setAttribute("data-tab", tabId); 669 document.body.setAttribute("data-tab", tabId);
687 670
688 // Select tab 671 // Select tab
689 let tabList = container.querySelector("[role='tablist']"); 672 let tabList = container.querySelector("[role='tablist']");
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 for (let subscription of subscriptions) 921 for (let subscription of subscriptions)
939 { 922 {
940 ext.backgroundPage.sendMessage({ 923 ext.backgroundPage.sendMessage({
941 type: "filters.get", 924 type: "filters.get",
942 subscriptionUrl: subscription.url 925 subscriptionUrl: subscription.url
943 }, 926 },
944 (filters) => 927 (filters) =>
945 { 928 {
946 for (let filter of filters) 929 for (let filter of filters)
947 updateFilter(filter); 930 updateFilter(filter);
931
932 isCustomFiltersLoaded = true;
948 }); 933 });
949 } 934 }
950 }); 935 });
951 loadRecommendations(); 936 loadRecommendations();
952 ext.backgroundPage.sendMessage({ 937 ext.backgroundPage.sendMessage({
953 type: "prefs.get", 938 type: "prefs.get",
954 key: "subscriptions_exceptionsurl" 939 key: "subscriptions_exceptionsurl"
955 }, 940 },
956 (url) => 941 (url) =>
957 { 942 {
(...skipping 27 matching lines...) Expand all
985 }); 970 });
986 } 971 }
987 972
988 domain.value = ""; 973 domain.value = "";
989 document.querySelector("#whitelisting .controls") 974 document.querySelector("#whitelisting .controls")
990 .classList.remove("mode-edit"); 975 .classList.remove("mode-edit");
991 } 976 }
992 977
993 function editCustomFilters() 978 function editCustomFilters()
994 { 979 {
980 if (!isCustomFiltersLoaded)
981 {
982 console.error("Custom filters are not loaded");
983 return;
984 }
985
986 E("custom-filters").classList.add("mode-edit");
995 let filterTexts = []; 987 let filterTexts = [];
996 for (let customFilterItem of collections.customFilters.items) 988 for (let customFilterItem of collections.customFilters.items)
997 filterTexts.push(customFilterItem.text); 989 filterTexts.push(customFilterItem.text);
998 E("custom-filters-raw").value = filterTexts.join("\n"); 990 E("custom-filters-raw").value = filterTexts.join("\n");
999 } 991 }
1000 992
1001 function addEnableSubscription(url, title, homepage) 993 function addEnableSubscription(url, title, homepage)
1002 { 994 {
1003 let messageType = null; 995 let messageType = null;
1004 let knownSubscription = subscriptionsMap[url]; 996 let knownSubscription = subscriptionsMap[url];
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
1297 }); 1289 });
1298 ext.backgroundPage.sendMessage({ 1290 ext.backgroundPage.sendMessage({
1299 type: "subscriptions.listen", 1291 type: "subscriptions.listen",
1300 filter: ["added", "disabled", "homepage", "lastDownload", "removed", 1292 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
1301 "title", "downloadStatus", "downloading"] 1293 "title", "downloadStatus", "downloading"]
1302 }); 1294 });
1303 1295
1304 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 1296 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
1305 window.addEventListener("hashchange", onHashChange, false); 1297 window.addEventListener("hashchange", onHashChange, false);
1306 } 1298 }
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