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

Delta Between Two Patch Sets: lib/utils.js

Issue 29371763: Issue 4795 - Use modern JavaScript syntax (Closed)
Left Patch Set: Created Jan. 13, 2017, 12:11 p.m.
Right Patch Set: Addressed some more feedback Created Jan. 18, 2017, 11:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/url.js ('k') | lib/whitelisting.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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";
19
18 let Utils = exports.Utils = { 20 let Utils = exports.Utils = {
19 systemPrincipal: null, 21 systemPrincipal: null,
20 getString: id => ext.i18n.getMessage("global_" + id), 22 getString(id)
21 runAsync: callback => 23 {
24 return ext.i18n.getMessage("global_" + id);
25 },
26 runAsync(callback)
22 { 27 {
23 if (document.readyState == "loading") 28 if (document.readyState == "loading")
24 { 29 {
25 // Make sure to not run asynchronous actions before all 30 // Make sure to not run asynchronous actions before all
26 // scripts loaded. This caused issues on Opera in the past. 31 // scripts loaded. This caused issues on Opera in the past.
27 let onDOMContentLoaded = () => 32 let onDOMContentLoaded = () =>
28 { 33 {
29 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded); 34 document.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
30 callback(); 35 callback();
31 }; 36 };
32 document.addEventListener("DOMContentLoaded", onDOMContentLoaded); 37 document.addEventListener("DOMContentLoaded", onDOMContentLoaded);
33 } 38 }
34 else 39 else
35 { 40 {
36 setTimeout(callback, 0); 41 setTimeout(callback, 0);
37 } 42 }
38 }, 43 },
39 get appLocale() 44 get appLocale()
40 { 45 {
41 let locale = ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-"); 46 let locale = ext.i18n.getMessage("@@ui_locale").replace(/_/g, "-");
42 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true}); 47 Object.defineProperty(this, "appLocale", {value: locale, enumerable: true});
43 return this.appLocale; 48 return this.appLocale;
44 }, 49 },
45 // We cannot calculate MD5 checksums yet :-( 50 generateChecksum(lines)
46 generateChecksum: lines => null, 51 {
47 checkLocalePrefixMatch: function(prefixes) 52 // We cannot calculate MD5 checksums yet :-(
53 return null;
54 },
55 checkLocalePrefixMatch(prefixes)
48 { 56 {
49 if (!prefixes) 57 if (!prefixes)
50 return null; 58 return null;
51 59
52 for (let prefix of prefixes.split(",")) 60 for (let prefix of prefixes.split(","))
53 if (new RegExp("^" + prefix + "\\b").test(this.appLocale)) 61 if (new RegExp("^" + prefix + "\\b").test(this.appLocale))
54 return prefix; 62 return prefix;
55 63
56 return null; 64 return null;
57 }, 65 },
58 66
59 chooseFilterSubscription: subscriptions => 67 chooseFilterSubscription(subscriptions)
60 { 68 {
61 let selectedItem = null; 69 let selectedItem = null;
62 let selectedPrefix = null; 70 let selectedPrefix = null;
63 let matchCount = 0; 71 let matchCount = 0;
64 for (let subscription of subscriptions) 72 for (let subscription of subscriptions)
65 { 73 {
66 if (!selectedItem) 74 if (!selectedItem)
67 selectedItem = subscription; 75 selectedItem = subscription;
68 76
69 let prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes")); 77 let prefix = Utils.checkLocalePrefixMatch(subscription.getAttribute("prefi xes"));
70 if (prefix) 78 if (prefix)
71 { 79 {
72 if (!selectedPrefix || selectedPrefix.length < prefix.length) 80 if (!selectedPrefix || selectedPrefix.length < prefix.length)
73 { 81 {
74 selectedItem = subscription; 82 selectedItem = subscription;
75 selectedPrefix = prefix; 83 selectedPrefix = prefix;
76 matchCount = 1; 84 matchCount = 1;
77 } 85 }
78 else if (selectedPrefix && selectedPrefix.length == prefix.length) 86 else if (selectedPrefix && selectedPrefix.length == prefix.length)
79 { 87 {
80 matchCount++; 88 matchCount++;
81 89
82 // If multiple items have a matching prefix of the same length: 90 // If multiple items have a matching prefix of the same length:
83 // Select one of the items randomly, probability should be the same 91 // Select one of the items randomly, probability should be the same
84 // for all items. So we replace the previous match here with 92 // for all items. So we replace the previous match here with
85 // probability 1/N (N being the number of matches). 93 // probability 1/N (N being the number of matches).
86 if (Math.random() * matchCount < 1) 94 if (Math.random() * matchCount < 1)
87 { 95 {
88 selectedItem = subscription; 96 selectedItem = subscription;
89 selectedPrefix = prefix; 97 selectedPrefix = prefix;
90 } 98 }
91 } 99 }
92 } 100 }
93 } 101 }
94 return selectedItem; 102 return selectedItem;
95 }, 103 },
96 104
97 getDocLink: linkID => 105 getDocLink(linkID)
98 { 106 {
99 let Prefs = require("prefs").Prefs; 107 let docLink = require("prefs").Prefs.documentation_link;
100 let docLink = Prefs.documentation_link;
101 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale ); 108 return docLink.replace(/%LINK%/g, linkID).replace(/%LANG%/g, Utils.appLocale );
102 }, 109 },
103 110
104 yield: () => 111 yield()
105 { 112 {
106 } 113 }
107 }; 114 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld