 Issue 29373665:
  issue 4264 - centralize action handling  (Closed)
    
  
    Issue 29373665:
  issue 4264 - centralize action handling  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 899 { | 886 { | 
| 900 ext.backgroundPage.sendMessage( | 887 ext.backgroundPage.sendMessage( | 
| 901 { | 888 { | 
| 902 type: "filters.get", | 889 type: "filters.get", | 
| 903 subscriptionUrl: subscriptions[i].url | 890 subscriptionUrl: subscriptions[i].url | 
| 904 }, | 891 }, | 
| 905 function(filters) | 892 function(filters) | 
| 906 { | 893 { | 
| 907 for (var i = 0; i < filters.length; i++) | 894 for (var i = 0; i < filters.length; i++) | 
| 908 updateFilter(filters[i]); | 895 updateFilter(filters[i]); | 
| 896 | |
| 897 isCustomFiltersLoaded = true; | |
| 909 }); | 898 }); | 
| 910 } | 899 } | 
| 911 }); | 900 }); | 
| 912 loadRecommendations(); | 901 loadRecommendations(); | 
| 913 ext.backgroundPage.sendMessage( | 902 ext.backgroundPage.sendMessage( | 
| 914 { | 903 { | 
| 915 type: "prefs.get", | 904 type: "prefs.get", | 
| 916 key: "subscriptions_exceptionsurl" | 905 key: "subscriptions_exceptionsurl" | 
| 917 }, | 906 }, | 
| 918 function(url) | 907 function(url) | 
| (...skipping 29 matching lines...) Expand all Loading... | |
| 948 text: "@@||" + domain.value.toLowerCase() + "^$document" | 937 text: "@@||" + domain.value.toLowerCase() + "^$document" | 
| 949 }); | 938 }); | 
| 950 } | 939 } | 
| 951 | 940 | 
| 952 domain.value = ""; | 941 domain.value = ""; | 
| 953 document.querySelector("#whitelisting .controls").classList.remove("mode-edi t"); | 942 document.querySelector("#whitelisting .controls").classList.remove("mode-edi t"); | 
| 954 } | 943 } | 
| 955 | 944 | 
| 956 function editCustomFilters() | 945 function editCustomFilters() | 
| 957 { | 946 { | 
| 947 if (!isCustomFiltersLoaded) | |
| 
saroyanm
2017/02/08 12:14:01
Theoretically user will not need this check, but t
 
Thomas Greiner
2017/03/02 15:10:13
Nevertheless it'd be good to use `console.error()`
 
saroyanm
2017/03/03 13:10:20
Agree, done
 | |
| 948 return; | |
| 949 | |
| 950 E("custom-filters").classList.add("mode-edit"); | |
| 958 var customFilterItems = collections.customFilters.items; | 951 var customFilterItems = collections.customFilters.items; | 
| 959 var filterTexts = []; | 952 var filterTexts = []; | 
| 960 for (var i = 0; i < customFilterItems.length; i++) | 953 for (var i = 0; i < customFilterItems.length; i++) | 
| 961 filterTexts.push(customFilterItems[i].text); | 954 filterTexts.push(customFilterItems[i].text); | 
| 962 E("custom-filters-raw").value = filterTexts.join("\n"); | 955 E("custom-filters-raw").value = filterTexts.join("\n"); | 
| 963 } | 956 } | 
| 964 | 957 | 
| 965 function addEnableSubscription(url, title, homepage) | 958 function addEnableSubscription(url, title, homepage) | 
| 966 { | 959 { | 
| 967 var messageType = null; | 960 var messageType = null; | 
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1265 ext.backgroundPage.sendMessage( | 1258 ext.backgroundPage.sendMessage( | 
| 1266 { | 1259 { | 
| 1267 type: "subscriptions.listen", | 1260 type: "subscriptions.listen", | 
| 1268 filter: ["added", "disabled", "homepage", "lastDownload", "removed", | 1261 filter: ["added", "disabled", "homepage", "lastDownload", "removed", | 
| 1269 "title", "downloadStatus", "downloading"] | 1262 "title", "downloadStatus", "downloading"] | 
| 1270 }); | 1263 }); | 
| 1271 | 1264 | 
| 1272 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); | 1265 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); | 
| 1273 window.addEventListener("hashchange", onHashChange, false); | 1266 window.addEventListener("hashchange", onHashChange, false); | 
| 1274 })(); | 1267 })(); | 
| OLD | NEW |