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

Side by Side Diff: lib/utils.js

Issue 9846017: Make JavaScript sources compile into the library; convert JavaScript files on the fly (Closed)
Patch Set: Review comments addressed Created March 15, 2013, 3:59 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 | « lib/prefs.js ('k') | libadblockplus.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 var Utils = exports.Utils = {
2 systemPrincipal: null,
3 getString: function(id)
4 {
5 return id;
6 },
7 runAsync: function(callback, thisPtr)
8 {
9 var params = Array.prototype.slice.call(arguments, 2);
10 window.setTimeout(function()
11 {
12 callback.apply(thisPtr, params);
13 }, 0);
14 },
15 get appLocale()
16 {
17 var locale = chrome.i18n.getMessage("@@ui_locale").replace(/_/g, "-");
18 this.__defineGetter__("appLocale", function() {return locale});
19 return this.appLocale;
20 },
21 generateChecksum: function(lines)
22 {
23 // We cannot calculate MD5 checksums yet :-(
24 return null;
25 },
26 makeURI: function(url)
27 {
28 return Services.io.newURI(url);
29 },
30
31 checkLocalePrefixMatch: function(prefixes)
32 {
33 if (!prefixes)
34 return null;
35
36 var list = prefixes.split(",");
37 for (var i = 0; i < list.length; i++)
38 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale))
39 return list[i];
40
41 return null;
42 },
43
44 chooseFilterSubscription: function(subscriptions)
45 {
46 var selectedItem = null;
47 var selectedPrefix = null;
48 var matchCount = 0;
49 for (var i = 0; i < subscriptions.length; i++)
50 {
51 var subscription = subscriptions[i];
52 if (!selectedItem)
53 selectedItem = subscription;
54
55 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes"));
56 if (prefix)
57 {
58 if (!selectedPrefix || selectedPrefix.length < prefix.length)
59 {
60 selectedItem = subscription;
61 selectedPrefix = prefix;
62 matchCount = 1;
63 }
64 else if (selectedPrefix && selectedPrefix.length == prefix.length)
65 {
66 matchCount++;
67
68 // If multiple items have a matching prefix of the same length:
69 // Select one of the items randomly, probability should be the same
70 // for all items. So we replace the previous match here with
71 // probability 1/N (N being the number of matches).
72 if (Math.random() * matchCount < 1)
73 {
74 selectedItem = subscription;
75 selectedPrefix = prefix;
76 }
77 }
78 }
79 }
80 return selectedItem;
81 }
82 };
OLDNEW
« no previous file with comments | « lib/prefs.js ('k') | libadblockplus.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld