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

Side by Side Diff: new-options.js

Issue 29340967: Issue 3971 - Refactor away Object.observe (Closed)
Patch Set: Created April 29, 2016, 12:42 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 | « no previous file | 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
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 ]); 379 ]);
380 collections.filterLists = new Collection( 380 collections.filterLists = new Collection(
381 [ 381 [
382 { 382 {
383 id: "all-filter-lists-table", 383 id: "all-filter-lists-table",
384 onClick: toggleDisableSubscription, 384 onClick: toggleDisableSubscription,
385 useOriginalTitle: true 385 useOriginalTitle: true
386 } 386 }
387 ]); 387 ]);
388 388
389 function observeSubscription(subscription) 389 function updateLanguageCollections(subscription)
390 { 390 {
391 function onObjectChanged(change) 391 var recommendation = recommendationsMap[subscription.url];
392
393 if (recommendation && recommendation.type == "ads")
392 { 394 {
393 for (var i = 0; i < change.length; i++) 395 if (subscription.disabled)
394 { 396 {
395 if (change[i].name == "disabled") 397 collections.allLangs.addItems(subscription);
396 { 398 collections.langs.removeItem(subscription);
397 var recommendation = recommendationsMap[subscription.url];
398 if (recommendation && recommendation.type == "ads")
399 {
400 if (subscription.disabled == false)
401 {
402 collections.allLangs.removeItem(subscription);
403 collections.langs.addItems(subscription);
404 }
405 else
406 {
407 collections.allLangs.addItems(subscription);
408 collections.langs.removeItem(subscription);
409 }
410 }
411 }
412 } 399 }
400 else
401 {
402 collections.allLangs.removeItem(subscription);
403 collections.langs.addItems(subscription);
404 }
405 }
406 }
413 407
414 for (var name in collections) 408 function addSubscription(subscription)
415 collections[name].updateItem(subscription); 409 {
410 var collection;
411 if (subscription.url in recommendationsMap)
412 {
413 var recommendation = recommendationsMap[subscription.url];
414 if (recommendation.type != "ads")
415 collection = collections.popular;
416 else if (subscription.disabled == false)
417 collection = collections.langs;
418 else
419 collection = collections.allLangs;
416 } 420 }
421 else if (subscription.url == acceptableAdsUrl)
422 collection = collections.acceptableAds;
423 else
424 collection = collections.custom;
417 425
418 if (!Object.observe) 426 collection.addItems(subscription);
419 { 427 subscriptionsMap[subscription.url] = subscription;
420 Object.keys(subscription).forEach(function(property)
421 {
422 var value = subscription[property];
423 Object.defineProperty(subscription, property,
424 {
425 get: function()
426 {
427 return value;
428 },
429 set: function(newValue)
430 {
431 if (value != newValue)
432 {
433 value = newValue;
434 onObjectChanged([{name: property}]);
435 }
436 }
437 });
438 });
439 }
440 else
441 {
442 Object.observe(subscription, onObjectChanged);
443 }
444 } 428 }
445 429
446 function updateSubscription(subscription) 430 function updateSubscription(subscription)
447 { 431 {
448 var subscriptionUrl = subscription.url; 432 var knownSubscription = subscriptionsMap[subscription.url];
449 var knownSubscription = subscriptionsMap[subscriptionUrl]; 433 for (var property in subscription)
450 if (knownSubscription)
451 { 434 {
452 for (var property in subscription) 435 if (property == "title" && subscription.url in recommendationsMap)
453 { 436 knownSubscription.originalTitle = subscription.title;
454 if (property == "title" && subscriptionUrl in recommendationsMap) 437 else
455 knownSubscription.originalTitle = subscription.title; 438 knownSubscription[property] = subscription[property];
456 else
457 knownSubscription[property] = subscription[property];
458 }
459 } 439 }
460 else
461 {
462 observeSubscription(subscription);
463 440
464 var collection; 441 for (var name in collections)
465 if (subscriptionUrl in recommendationsMap) 442 collections[name].updateItem(knownSubscription);
466 {
467 var recommendation = recommendationsMap[subscriptionUrl];
468 if (recommendation.type != "ads")
469 collection = collections.popular;
470 else if (subscription.disabled == false)
471 collection = collections.langs;
472 else
473 collection = collections.allLangs;
474 }
475 else if (subscriptionUrl == acceptableAdsUrl)
476 collection = collections.acceptableAds;
477 else
478 collection = collections.custom;
479 443
480 collection.addItems(subscription); 444 return knownSubscription;
481 subscriptionsMap[subscriptionUrl] = subscription;
482 }
483 } 445 }
484 446
485 function updateFilter(filter) 447 function updateFilter(filter)
486 { 448 {
487 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/); 449 var match = filter.text.match(/^@@\|\|([^\/:]+)\^\$document$/);
488 if (match && !filtersMap[filter.text]) 450 if (match && !filtersMap[filter.text])
489 { 451 {
490 filter.title = match[1]; 452 filter.title = match[1];
491 collections.whitelist.addItems(filter); 453 collections.whitelist.addItems(filter);
492 } 454 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 prefix = prefix.replace(/\W/g, "_"); 487 prefix = prefix.replace(/\W/g, "_");
526 subscription.title = getMessage("options_language_" + prefix); 488 subscription.title = getMessage("options_language_" + prefix);
527 } 489 }
528 else 490 else
529 { 491 {
530 var type = recommendation.type.replace(/\W/g, "_"); 492 var type = recommendation.type.replace(/\W/g, "_");
531 subscription.title = getMessage("common_feature_" + type + "_title") ; 493 subscription.title = getMessage("common_feature_" + type + "_title") ;
532 } 494 }
533 495
534 recommendationsMap[subscription.url] = recommendation; 496 recommendationsMap[subscription.url] = recommendation;
535 updateSubscription(subscription); 497 addSubscription(subscription);
536 } 498 }
537 }); 499 });
538 } 500 }
539 501
540 function findParentData(element, dataName, returnElement) 502 function findParentData(element, dataName, returnElement)
541 { 503 {
542 while (element) 504 while (element)
543 { 505 {
544 if (element.hasAttribute("data-" + dataName)) 506 if (element.hasAttribute("data-" + dataName))
545 return returnElement ? element : element.getAttribute("data-" + dataName ); 507 return returnElement ? element : element.getAttribute("data-" + dataName );
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 }); 830 });
869 loadRecommendations(); 831 loadRecommendations();
870 ext.backgroundPage.sendMessage( 832 ext.backgroundPage.sendMessage(
871 { 833 {
872 type: "prefs.get", 834 type: "prefs.get",
873 key: "subscriptions_exceptionsurl" 835 key: "subscriptions_exceptionsurl"
874 }, 836 },
875 function(url) 837 function(url)
876 { 838 {
877 acceptableAdsUrl = url; 839 acceptableAdsUrl = url;
878 updateSubscription({ 840 addSubscription({
879 url: acceptableAdsUrl, 841 url: acceptableAdsUrl,
880 disabled: true 842 disabled: true
881 }); 843 });
882 844
883 // Load user subscriptions 845 // Load user subscriptions
884 ext.backgroundPage.sendMessage( 846 ext.backgroundPage.sendMessage(
885 { 847 {
886 type: "subscriptions.get", 848 type: "subscriptions.get",
887 downloadable: true 849 downloadable: true
888 }, 850 },
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
958 delete filtersMap[filter.text]; 920 delete filtersMap[filter.text];
959 updateShareLink(); 921 updateShareLink();
960 break; 922 break;
961 } 923 }
962 } 924 }
963 925
964 function onSubscriptionMessage(action, subscription) 926 function onSubscriptionMessage(action, subscription)
965 { 927 {
966 switch (action) 928 switch (action)
967 { 929 {
930 case "disabled":
931 subscription = updateSubscription(subscription);
932 updateLanguageCollections(subscription);
933 break;
934 case "downloading":
935 case "downloadStatus":
936 case "homepage":
937 case "lastDownload":
938 case "title":
939 updateSubscription(subscription);
940 break;
968 case "added": 941 case "added":
969 updateSubscription(subscription); 942 if (subscription.url in subscriptionsMap)
970 updateShareLink(); 943 subscription = updateSubscription(subscription);
944 else
945 addSubscription(subscription);
971 946
972 var knownSubscription = subscriptionsMap[subscription.url]; 947 collections.filterLists.addItems(subscription);
973 if (knownSubscription) 948 updateLanguageCollections(subscription);
974 collections.filterLists.addItems(knownSubscription);
975 else
976 collections.filterLists.addItems(subscription);
977 break;
978 case "disabled":
979 updateSubscription(subscription);
980 updateShareLink();
981 break; 949 break;
982 case "removed": 950 case "removed":
983 var knownSubscription = subscriptionsMap[subscription.url]; 951 var knownSubscription = subscriptionsMap[subscription.url];
984 if (subscription.url == acceptableAdsUrl) 952
953 if (subscription.url == acceptableAdsUrl ||
954 subscription.url in recommendationsMap)
985 { 955 {
986 subscription.disabled = true; 956 subscription.disabled = true;
987 updateSubscription(subscription); 957 onSubscriptionMessage("disabled", subscription);
988 } 958 }
989 else 959 else
990 { 960 {
991 if (subscription.url in recommendationsMap) 961 collections.custom.removeItem(knownSubscription);
992 knownSubscription.disabled = true; 962 delete subscriptionsMap[subscription.url];
993 else
994 {
995 collections.custom.removeItem(knownSubscription);
996 delete subscriptionsMap[subscription.url];
997 }
998 } 963 }
999 updateShareLink();
1000 collections.filterLists.removeItem(knownSubscription); 964 collections.filterLists.removeItem(knownSubscription);
1001 break; 965 break;
1002 default:
1003 updateSubscription(subscription);
1004 break;
1005 } 966 }
967
968 updateShareLink();
1006 } 969 }
1007 970
1008 function hidePref(key, value) 971 function hidePref(key, value)
1009 { 972 {
1010 var element = document.querySelector("[data-pref='" + key + "']"); 973 var element = document.querySelector("[data-pref='" + key + "']");
1011 if (element) 974 if (element)
1012 element.setAttribute("aria-hidden", value); 975 element.setAttribute("aria-hidden", value);
1013 } 976 }
1014 977
1015 function getPref(key, callback) 978 function getPref(key, callback)
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1154 }); 1117 });
1155 ext.backgroundPage.sendMessage( 1118 ext.backgroundPage.sendMessage(
1156 { 1119 {
1157 type: "subscriptions.listen", 1120 type: "subscriptions.listen",
1158 filter: ["added", "disabled", "homepage", "lastDownload", "removed", 1121 filter: ["added", "disabled", "homepage", "lastDownload", "removed",
1159 "title", "downloadStatus", "downloading"] 1122 "title", "downloadStatus", "downloading"]
1160 }); 1123 });
1161 1124
1162 window.addEventListener("DOMContentLoaded", onDOMLoaded, false); 1125 window.addEventListener("DOMContentLoaded", onDOMLoaded, false);
1163 })(); 1126 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld