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

Side by Side Diff: options.js

Issue 29321198: Issue 2376 - Implement custom filters in new options page (Closed)
Patch Set: Mockup simplification and validation update Created July 14, 2015, 6:17 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
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-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.dataset.access; 108 var subscriptionUrl = e.target.parentNode.dataset.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.dataset.access; 124 var url = this.parentNode.dataset.access;
119 addEnableSubscription(url); 125 addEnableSubscription(url);
120 } 126 }
121 127
122 function onRemoveFilterClick() 128 function onRemoveFilterClick()
123 { 129 {
124 var filter = this.parentNode.dataset.access; 130 var filter = this.parentNode.dataset.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 27 matching lines...) Expand all
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
171 } 181 }
172 ]); 182 ]);
183 collections.customFilters = new Collection(
184 [
185 {
186 id: "custom-filters-table"
187 }
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
181 { 197 {
182 getAcceptableAdsURL(function(acceptableAdsUrl) 198 getAcceptableAdsURL(function(acceptableAdsUrl)
(...skipping 76 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 353
339 function onFindLanguageKeyUp() 354 function onFindLanguageKeyUp()
340 { 355 {
341 var searchStyle = E("search-style"); 356 var searchStyle = E("search-style");
342 if (!this.value) 357 if (!this.value)
343 searchStyle.innerHTML = ""; 358 searchStyle.innerHTML = "";
344 else 359 else
345 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this .value.toLowerCase() + "\"]) { display: none; }"; 360 searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this .value.toLowerCase() + "\"]) { display: none; }";
346 } 361 }
347 362
363 function isEnterPressed(e)
364 {
365 // e.keyCode has been deprecated so we attempt to use e.key
366 if ("key" in e)
367 return e.key == "Enter";
368 return e.keyCode == 13; // keyCode "13" corresponds to "Enter"
369 }
370
348 // Initialize navigation sidebar 371 // Initialize navigation sidebar
349 ext.backgroundPage.sendMessage( 372 ext.backgroundPage.sendMessage(
350 { 373 {
351 type: "app.get", 374 type: "app.get",
352 what: "addonVersion" 375 what: "addonVersion"
353 }, 376 },
354 function(addonVersion) 377 function(addonVersion)
355 { 378 {
356 E("abp-version").textContent = addonVersion; 379 E("abp-version").textContent = addonVersion;
357 }); 380 });
(...skipping 30 matching lines...) Expand all
388 { 411 {
389 var id = e.target.id; 412 var id = e.target.id;
390 if (id == "whitelisting-add-icon" || id == "whitelisting-enter-icon") 413 if (id == "whitelisting-add-icon" || id == "whitelisting-enter-icon")
391 addWhitelistedDomain(); 414 addWhitelistedDomain();
392 else if (id == "whitelisting-cancel-button") 415 else if (id == "whitelisting-cancel-button")
393 E("whitelisting-textbox").value = ""; 416 E("whitelisting-textbox").value = "";
394 }, false); 417 }, false);
395 E("whitelisting-add-button").addEventListener("click", addWhitelistedDomain, false); 418 E("whitelisting-add-button").addEventListener("click", addWhitelistedDomain, false);
396 E("whitelisting-textbox").addEventListener("keypress", function(e) 419 E("whitelisting-textbox").addEventListener("keypress", function(e)
397 { 420 {
398 // e.keyCode has been deprecated so we attempt to use e.key 421 if (isEnterPressed(e))
399 // keyCode "13" corresponds to "Enter"
400 if ((e.key && e.key == "Enter") || (!e.key && e.keyCode == 13))
401 addWhitelistedDomain(); 422 addWhitelistedDomain();
402 }, false); 423 }, false);
403 E("import-blockingList-button").addEventListener("click", function() 424 E("import-blockingList-button").addEventListener("click", function()
404 { 425 {
405 var url = E("blockingList-textbox").value; 426 var url = E("blockingList-textbox").value;
406 addEnableSubscription(url); 427 addEnableSubscription(url);
407 delete document.body.dataset.dialog; 428 delete document.body.dataset.dialog;
408 }, false); 429 }, false);
430
431 // Advanced tab
432 var filterTextbox = document.querySelector("#custom-filters-add input");
433 placeholderValue = ext.i18n.getMessage("options_customFilters_textbox_placeh older");
434 filterTextbox.setAttribute("placeholder", placeholderValue);
435 function addCustomFilters()
436 {
437 var filterText = filterTextbox.value;
438 ext.backgroundPage.sendMessage(
439 {
440 type: "filters.add",
441 text: filterText
442 });
443 filterTextbox.value = "";
444 }
445 E("custom-filters-add").addEventListener("submit", function(e)
446 {
447 e.preventDefault();
448 addCustomFilters();
449 }, false);
450 var customFilterEditButtons = document.querySelectorAll("#custom-filters-edi t-wrapper button");
451 E("custom-filters-edit-wrapper").addEventListener("click", function(e)
452 {
453 var target = null;
454 if (e.target.localName == "button")
455 target = e.target;
456 else if (e.target.parentElement.localName == "button")
457 target = e.target.parentElement;
458 else
459 return;
460
461 var id = target.id;
462 E("custom-filters").classList.toggle("mode-edit");
463 if (id == "custom-filters-show-edit")
464 editCustomFilters();
465 else if (id == "custom-filters-raw-save")
466 {
467 ext.backgroundPage.sendMessage(
468 {
469 type: "filters.importRaw",
470 text: E("custom-filters-raw").value
471 });
472 }
473 }, false);
409 } 474 }
410 475
411 function openDialog(name) 476 function openDialog(name)
412 { 477 {
413 document.body.dataset.dialog = name; 478 document.body.dataset.dialog = name;
414 } 479 }
415 480
416 function populateLists() 481 function populateLists()
417 { 482 {
418 subscriptionsMap = Object.create(null); 483 subscriptionsMap = Object.create(null);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 type: "filters.add", 543 type: "filters.add",
479 text: "@@||" + domain.value.toLowerCase() + "^$document" 544 text: "@@||" + domain.value.toLowerCase() + "^$document"
480 }); 545 });
481 } 546 }
482 547
483 domain.value = ""; 548 domain.value = "";
484 } 549 }
485 550
486 function editCustomFilters() 551 function editCustomFilters()
487 { 552 {
488 //TODO: NYI 553 var customFilterItems = collections.customFilters.items;
554 var filterTexts = [];
555 for (var i = 0; i < customFilterItems.length; i++)
556 filterTexts.push(customFilterItems[i].text);
557 E("custom-filters-raw").value = filterTexts.join("\n");
489 } 558 }
490 559
491 function getAcceptableAdsURL(callback) 560 function getAcceptableAdsURL(callback)
492 { 561 {
493 ext.backgroundPage.sendMessage( 562 ext.backgroundPage.sendMessage(
494 { 563 {
495 type: "prefs.get", 564 type: "prefs.get",
496 key: "subscriptions_exceptionsurl" 565 key: "subscriptions_exceptionsurl"
497 }, 566 },
498 function(value) 567 function(value)
(...skipping 20 matching lines...) Expand all
519 url: url 588 url: url
520 }; 589 };
521 if (title) 590 if (title)
522 message.title = title; 591 message.title = title;
523 if (homepage) 592 if (homepage)
524 message.homepage = homepage; 593 message.homepage = homepage;
525 594
526 ext.backgroundPage.sendMessage(message); 595 ext.backgroundPage.sendMessage(message);
527 } 596 }
528 597
529 function removeSubscription(url)
530 {
531 ext.backgroundPage.sendMessage(
532 {
533 type: "subscriptions.remove",
534 url: url
535 });
536 }
537
538 function removeFilter(filter)
539 {
540 ext.backgroundPage.sendMessage(
541 {
542 type: "filters.remove",
543 text: filter
544 });
545 }
546
547 function onFilterMessage(action, filter) 598 function onFilterMessage(action, filter)
548 { 599 {
549 switch (action) 600 switch (action)
550 { 601 {
551 case "added": 602 case "added":
552 updateFilter(filter); 603 updateFilter(filter);
553 updateShareLink(); 604 updateShareLink();
554 break; 605 break;
555 case "loaded": 606 case "loaded":
556 populateLists(); 607 populateLists();
557 break; 608 break;
558 case "removed": 609 case "removed":
559 var knownFilter = filtersMap[filter.text]; 610 var knownFilter = filtersMap[filter.text];
560 collections.whitelist.removeItem(knownFilter); 611 collections.whitelist.removeItem(knownFilter);
612 collections.customFilters.removeItem(knownFilter);
561 delete filtersMap[filter.text]; 613 delete filtersMap[filter.text];
562 updateShareLink(); 614 updateShareLink();
563 break; 615 break;
564 } 616 }
565 } 617 }
566 618
567 function onSubscriptionMessage(action, subscription) 619 function onSubscriptionMessage(action, subscription)
568 { 620 {
569 switch (action) 621 switch (action)
570 { 622 {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 link: link 692 link: link
641 }, callback); 693 }, callback);
642 } 694 }
643 695
644 ext.onMessage.addListener(function(message) 696 ext.onMessage.addListener(function(message)
645 { 697 {
646 switch (message.type) 698 switch (message.type)
647 { 699 {
648 case "app.listen": 700 case "app.listen":
649 if (message.action == "addSubscription") 701 if (message.action == "addSubscription")
650 showAddSubscriptionDialog(message.args[0]); 702 {
703 E("blockingList-textbox").value = message.args[0].url;
704 openDialog("customlist");
705 }
706 else if (message.action == "error")
707 {
708 alert(message.args.join("\n"));
709 }
651 break; 710 break;
652 case "filters.listen": 711 case "filters.listen":
653 onFilterMessage(message.action, message.args[0]); 712 onFilterMessage(message.action, message.args[0]);
654 break; 713 break;
655 case "subscriptions.listen": 714 case "subscriptions.listen":
656 onSubscriptionMessage(message.action, message.args[0]); 715 onSubscriptionMessage(message.action, message.args[0]);
657 break; 716 break;
658 } 717 }
659 }); 718 });
660 719
661 ext.backgroundPage.sendMessage( 720 ext.backgroundPage.sendMessage(
662 { 721 {
663 type: "app.listen", 722 type: "app.listen",
664 filter: ["addSubscription"] 723 filter: ["addSubscription", "error"]
665 }); 724 });
666 ext.backgroundPage.sendMessage( 725 ext.backgroundPage.sendMessage(
667 { 726 {
668 type: "filters.listen", 727 type: "filters.listen",
669 filter: ["added", "loaded", "removed"] 728 filter: ["added", "loaded", "removed"]
670 }); 729 });
671 ext.backgroundPage.sendMessage( 730 ext.backgroundPage.sendMessage(
672 { 731 {
673 type: "subscriptions.listen", 732 type: "subscriptions.listen",
674 filter: ["added", "disabled", "homepage", "removed", "title"] 733 filter: ["added", "disabled", "homepage", "removed", "title"]
675 }); 734 });
676 735
677 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 736 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
678 })(); 737 })();
OLDNEW
« messageResponder.js ('K') | « options.html ('k') | skin/options.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld