1 /* utils.js - various utils that don't belong anywhere else */ |
1 /* utils.js - various utils that don't belong anywhere else */ |
|
2 |
|
3 IriSP.jqEscape = function(_text) { |
|
4 return text.replace(/(:|\.)/g,'\\$1'); |
|
5 } |
2 |
6 |
3 IriSP.getLib = function(lib) { |
7 IriSP.getLib = function(lib) { |
4 if (IriSP.libFiles.useCdn && typeof IriSP.libFiles.cdn[lib] == "string") { |
8 if (IriSP.libFiles.useCdn && typeof IriSP.libFiles.cdn[lib] == "string") { |
5 return IriSP.libFiles.cdn[lib]; |
9 return IriSP.libFiles.cdn[lib]; |
6 } |
10 } |
7 if (typeof IriSP.libFiles.locations[lib] == "string") { |
11 if (typeof IriSP.libFiles.locations[lib] == "string") { |
8 return IriSP.libFiles.locations[lib]; |
12 return IriSP.libFiles.locations[lib]; |
9 } |
13 } |
10 if (typeof IriSP.libFiles.inDefaultDir[lib] == "string") { |
14 if (typeof IriSP.libFiles.inDefaultDir[lib] == "string") { |
11 return IriSP.libFiles.defaultDir + IriSP.libFiles.inDefaultDir[lib]; |
15 return IriSP.libFiles.defaultDir + '/' + IriSP.libFiles.inDefaultDir[lib]; |
12 } |
16 } |
13 } |
17 } |
14 |
18 |
15 IriSP.loadCss = function(_cssFile) { |
19 IriSP.loadCss = function(_cssFile) { |
16 IriSP.jQuery("<link>", { |
20 IriSP.jQuery("<link>", { |
17 rel : "stylesheet", |
21 rel : "stylesheet", |
18 type : "text/css", |
22 type : "text/css", |
19 href : _cssFile |
23 href : _cssFile |
20 }).appendTo('head'); |
24 }).appendTo('head'); |
21 } |
25 } |
22 |
|
23 /* |
|
24 IriSP.padWithZeros = function(num) { |
|
25 if (Math.abs(num) < 10) { |
|
26 return "0" + num.toString(); |
|
27 } else { |
|
28 return num.toString(); |
|
29 } |
|
30 }; |
|
31 |
|
32 /* convert a number of milliseconds to a tuple of the form |
|
33 [hours, minutes, seconds] |
|
34 |
|
35 IriSP.msToTime = function(ms) { |
|
36 return IriSP.secondsToTime(ms / 1000); |
|
37 } |
|
38 |
|
39 /* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */ |
|
40 IriSP.formatTweet = function(tweet) { |
|
41 /* |
|
42 an array of arrays which hold a regexp and its replacement. |
|
43 */ |
|
44 var regExps = [ |
|
45 /* copied from http://codegolf.stackexchange.com/questions/464/shortest-url-regex-match-in-javascript/480#480 */ |
|
46 [/((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi, "<a href='$1'>$1</a>"], |
|
47 [/@(\w+)/gi, "<a href='http://twitter.com/$1'>@$1</a>"], // matches a @handle |
|
48 [/#(\w+)/gi, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>"],// matches a hashtag |
|
49 [/(\+\+)/gi, "<span class='Ldt-PolemicPlusPlus'>$1</span>"], |
|
50 [/(--)/gi, "<span class='Ldt-PolemicMinusMinus'>$1</span>"], |
|
51 [/(==)/gi, "<span class='Ldt-PolemicEqualEqual'>$1</span>"], |
|
52 [/(\?\?)/gi, "<span class='Ldt-PolemicQuestion'>$1</span>"] |
|
53 ]; |
|
54 |
|
55 var i = 0; |
|
56 for(i = 0; i < regExps.length; i++) { |
|
57 tweet = tweet.replace(regExps[i][0], regExps[i][1]); |
|
58 } |
|
59 |
|
60 return tweet; |
|
61 }; |
|
62 /* |
|
63 IriSP.countProperties = function(obj) { |
|
64 var count = 0; |
|
65 |
|
66 for(var prop in obj) { |
|
67 if(obj.hasOwnProperty(prop)) |
|
68 ++count; |
|
69 } |
|
70 |
|
71 return count; |
|
72 }; |
|
73 |
|
74 // conversion de couleur Decimal vers HexaDecimal || 000 si fff |
|
75 IriSP.DEC_HEXA_COLOR = function (dec) { |
|
76 var val = +dec; |
|
77 var str = val.toString(16); |
|
78 var zeroes = ""; |
|
79 if (str.length < 6) { |
|
80 for (var i = 0; i < 6 - str.length; i++) |
|
81 zeroes += "0"; |
|
82 } |
|
83 return zeroes + str; |
|
84 }; |
|
85 |
|
86 /* shortcut to have global variables in templates |
|
87 IriSP.templToHTML = function(template, values) { |
|
88 var params = IriSP._.extend( |
|
89 { |
|
90 "l10n" : IriSP.i18n.getMessages() |
|
91 }, |
|
92 values); |
|
93 return Mustache.to_html(template, params); |
|
94 }; |
|
95 |
|
96 /* we need to be stricter than encodeURIComponent, |
|
97 because of twitter |
|
98 */ |
|
99 IriSP.encodeURI = function(str) { |
|
100 return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28'). |
|
101 replace(/\)/g, '%29').replace(/\*/g, '%2A'); |
|
102 } |
|
103 |
|
104 IriSP.jqEscape = function(text) { |
|
105 return text.replace(/(:|\.)/g,'\\$1') |
|
106 } |
|
107 |
|
108 IriSP.jqId = function (text) { |
|
109 return IriSP.jQuery('#' + IriSP.jqEscape(text)); |
|
110 } |
|
111 |
|
112 /** returns an url to share on facebook */ |
|
113 IriSP.mkFbUrl = function(url, text) { |
|
114 if (typeof(text) === "undefined") |
|
115 text = "I'm watching "; |
|
116 |
|
117 return "http://www.facebook.com/share.php?u=" + IriSP.encodeURI(text) + IriSP.shorten_url(url); |
|
118 }; |
|
119 |
|
120 /** returns an url to share on twitter */ |
|
121 IriSP.mkTweetUrl = function(url, text) { |
|
122 if (typeof(text) === "undefined") |
|
123 text = "I'm watching "; |
|
124 |
|
125 return "http://twitter.com/home?status=" + IriSP.encodeURI(text) + IriSP.shorten_url(url); |
|
126 }; |
|
127 |
|
128 /** returns an url to share on google + */ |
|
129 IriSP.mkGplusUrl = function(url, text) { |
|
130 return "https://plusone.google.com/_/+1/confirm?hl=en&url=" + IriSP.shorten_url(url); |
|
131 }; |
|
132 |
|
133 /** get a property that can have multiple names **/ |
|
134 |
|
135 /** issue a call to an url shortener and return the shortened url */ |
|
136 IriSP.shorten_url = function(url) { |
|
137 return encodeURIComponent(url); |
|
138 }; |
|
139 |
|
140 |
|
141 /* for ie compatibility |
|
142 if (Object.prototype.__defineGetter__&&!Object.defineProperty) { |
|
143 Object.defineProperty=function(obj,prop,desc) { |
|
144 if ("get" in desc) obj.__defineGetter__(prop,desc.get); |
|
145 if ("set" in desc) obj.__defineSetter__(prop,desc.set); |
|
146 } |
|
147 } |
|
148 */ |
|
149 |
|
150 /* Creates regexps from text */ |
|
151 IriSP.regexpFromText = function(_text) { |
|
152 return new RegExp('(' + _text.replace(/(\W)/gim,'\\$1') + ')','gim'); |
|
153 } |
|