1 /* utils.js - various utils that don't belong anywhere else */ |
1 /* utils.js - various utils that don't belong anywhere else */ |
2 |
2 |
3 /* trace function, for debugging */ |
3 IriSP.jqEscape = function(_text) { |
4 |
4 return _text.replace(/(:|\.)/g,'\\$1'); |
5 IriSP.traceNum = 0; |
|
6 IriSP.trace = function( msg, value ) { |
|
7 /* |
|
8 if( IriSP.config.gui.debug === true ) { |
|
9 IriSP.traceNum += 1; |
|
10 IriSP.jQuery( "<div>"+IriSP.traceNum+" - "+msg+" : "+value+"</div>" ).appendTo( "#Ldt-output" ); |
|
11 } |
|
12 */ |
|
13 }; |
|
14 |
|
15 /* used in callbacks - because in callbacks we lose "this", |
|
16 we need to have a special function which wraps "this" in |
|
17 a closure. This way, the |
|
18 */ |
|
19 IriSP.wrap = function (obj, fn) { |
|
20 return function() { |
|
21 var args = Array.prototype.slice.call(arguments, 0); |
|
22 return fn.apply(obj, args); |
|
23 } |
|
24 } |
5 } |
25 |
6 |
26 /* convert a time to a percentage in the media */ |
7 IriSP.getLib = function(lib) { |
27 IriSP.timeToPourcent = function(time, timetotal){ |
8 if (IriSP.libFiles.useCdn && typeof IriSP.libFiles.cdn[lib] == "string") { |
28 var time = Math.abs(time); |
9 return IriSP.libFiles.cdn[lib]; |
29 var timetotal = Math.abs(timetotal); |
|
30 |
|
31 return Math.floor((time/timetotal) * 100); |
|
32 }; |
|
33 |
|
34 IriSP.padWithZeros = function(num) { |
|
35 if (Math.abs(num) < 10) { |
|
36 return "0" + num.toString(); |
|
37 } else { |
|
38 return num.toString(); |
|
39 } |
|
40 }; |
|
41 |
|
42 /* convert a number of milliseconds to a tuple of the form |
|
43 [hours, minutes, seconds] |
|
44 */ |
|
45 IriSP.msToTime = function(ms) { |
|
46 return IriSP.secondsToTime(ms / 1000); |
|
47 } |
|
48 /* convert a number of seconds to a tuple of the form |
|
49 [hours, minutes, seconds] |
|
50 */ |
|
51 IriSP.secondsToTime = function(secs) { |
|
52 var hours = Math.abs(parseInt( secs / 3600 ) % 24); |
|
53 var minutes = Math.abs(parseInt( secs / 60 ) % 60); |
|
54 var seconds = parseFloat(Math.abs(secs % 60).toFixed(0)); |
|
55 |
|
56 var toString_fn = function() { |
|
57 var ret = ""; |
|
58 if (hours > 0) |
|
59 ret = IriSP.padWithZeros(this.hours) + ":"; |
|
60 ret += IriSP.padWithZeros(this.minutes) + ":" + IriSP.padWithZeros(this.seconds); |
|
61 |
|
62 return ret; |
|
63 } |
|
64 return {"hours" : hours, "minutes" : minutes, "seconds" : seconds, toString: toString_fn}; |
|
65 }; |
|
66 |
|
67 /* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */ |
|
68 IriSP.formatTweet = function(tweet) { |
|
69 /* |
|
70 an array of arrays which hold a regexp and its replacement. |
|
71 */ |
|
72 var regExps = [ |
|
73 /* copied from http://codegolf.stackexchange.com/questions/464/shortest-url-regex-match-in-javascript/480#480 */ |
|
74 [/((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi, "<a href='$1'>$1</a>"], |
|
75 [/@(\w+)/gi, "<a href='http://twitter.com/$1'>@$1</a>"], // matches a @handle |
|
76 [/#(\w+)/gi, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>"],// matches a hashtag |
|
77 [/(\+\+)/gi, "<span class='Ldt-PolemicPlusPlus'>$1</span>"], |
|
78 [/(--)/gi, "<span class='Ldt-PolemicMinusMinus'>$1</span>"], |
|
79 [/(==)/gi, "<span class='Ldt-PolemicEqualEqual'>$1</span>"], |
|
80 [/(\?\?)/gi, "<span class='Ldt-PolemicQuestion'>$1</span>"] |
|
81 ]; |
|
82 |
|
83 var i = 0; |
|
84 for(i = 0; i < regExps.length; i++) { |
|
85 tweet = tweet.replace(regExps[i][0], regExps[i][1]); |
|
86 } |
|
87 |
|
88 return tweet; |
|
89 }; |
|
90 |
|
91 IriSP.countProperties = function(obj) { |
|
92 var count = 0; |
|
93 |
|
94 for(var prop in obj) { |
|
95 if(obj.hasOwnProperty(prop)) |
|
96 ++count; |
|
97 } |
10 } |
98 |
11 if (typeof IriSP.libFiles.locations[lib] == "string") { |
99 return count; |
12 return IriSP.libFiles.locations[lib]; |
100 }; |
13 } |
101 |
14 if (typeof IriSP.libFiles.inDefaultDir[lib] == "string") { |
102 // conversion de couleur Decimal vers HexaDecimal || 000 si fff |
15 return IriSP.libFiles.defaultDir + '/' + IriSP.libFiles.inDefaultDir[lib]; |
103 IriSP.DEC_HEXA_COLOR = function (dec) { |
16 } |
104 var val = +dec; |
|
105 var str = val.toString(16); |
|
106 var zeroes = ""; |
|
107 if (str.length < 6) { |
|
108 for (var i = 0; i < 6 - str.length; i++) |
|
109 zeroes += "0"; |
|
110 } |
|
111 return zeroes + str; |
|
112 }; |
|
113 |
|
114 /* shortcut to have global variables in templates */ |
|
115 IriSP.templToHTML = function(template, values) { |
|
116 var params = IriSP.underscore.extend( |
|
117 { "defaults" : IriSP.default_templates_vars, |
|
118 "l10n" : IriSP.i18n.getMessages() |
|
119 }, |
|
120 values); |
|
121 return Mustache.to_html(template, params); |
|
122 }; |
|
123 |
|
124 /* we need to be stricter than encodeURIComponent, |
|
125 because of twitter |
|
126 */ |
|
127 IriSP.encodeURI = function(str) { |
|
128 return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28'). |
|
129 replace(/\)/g, '%29').replace(/\*/g, '%2A'); |
|
130 } |
17 } |
131 |
18 |
132 IriSP.jqEscape = function(text) { |
19 IriSP._cssCache = []; |
133 return text.replace(/(:|\.)/g,'\\$1') |
20 |
|
21 IriSP.loadCss = function(_cssFile) { |
|
22 if (IriSP._(IriSP._cssCache).indexOf(_cssFile) === -1) { |
|
23 IriSP.jQuery("<link>", { |
|
24 rel : "stylesheet", |
|
25 type : "text/css", |
|
26 href : _cssFile |
|
27 }).appendTo('head'); |
|
28 IriSP._cssCache.push(_cssFile); |
|
29 } |
134 } |
30 } |
135 |
31 |
136 IriSP.jqId = function (text) { |
32 IriSP.log = function() { |
137 return IriSP.jQuery('#' + IriSP.jqEscape(text)); |
33 if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) { |
138 } |
34 console.log.apply(console, arguments); |
139 |
|
140 IriSP.__guidCounter = 0; |
|
141 IriSP.guid = function(prefix) { |
|
142 IriSP.__guidCounter += 1; |
|
143 return prefix + IriSP.__guidCounter; |
|
144 }; |
|
145 |
|
146 /** returns an url to share on facebook */ |
|
147 IriSP.mkFbUrl = function(url, text) { |
|
148 if (typeof(text) === "undefined") |
|
149 text = "I'm watching "; |
|
150 |
|
151 return "http://www.facebook.com/share.php?u=" + IriSP.encodeURI(text) + IriSP.shorten_url(url); |
|
152 }; |
|
153 |
|
154 /** returns an url to share on twitter */ |
|
155 IriSP.mkTweetUrl = function(url, text) { |
|
156 if (typeof(text) === "undefined") |
|
157 text = "I'm watching "; |
|
158 |
|
159 return "http://twitter.com/home?status=" + IriSP.encodeURI(text) + IriSP.shorten_url(url); |
|
160 }; |
|
161 |
|
162 /** returns an url to share on google + */ |
|
163 IriSP.mkGplusUrl = function(url, text) { |
|
164 return "https://plusone.google.com/_/+1/confirm?hl=en&url=" + IriSP.shorten_url(url); |
|
165 }; |
|
166 |
|
167 /** test if a value is null or undefined */ |
|
168 IriSP.null_or_undefined = function(val) { |
|
169 return (typeof(val) === "undefined" || val === null); |
|
170 }; |
|
171 |
|
172 /** get a property that can have multiple names **/ |
|
173 |
|
174 IriSP.get_aliased = function(_obj, _aliases) { |
|
175 for (var _i = 0; _i < _aliases.length; _i++) { |
|
176 if (typeof _obj[_aliases[_i]] !== "undefined") { |
|
177 return _obj[_aliases[_i]]; |
|
178 } |
|
179 } |
35 } |
180 return null; |
|
181 } |
36 } |
182 |
|
183 /** issue a call to an url shortener and return the shortened url */ |
|
184 IriSP.shorten_url = function(url) { |
|
185 return encodeURIComponent(url); |
|
186 }; |
|
187 |
|
188 |
|
189 /* for ie compatibility |
|
190 if (Object.prototype.__defineGetter__&&!Object.defineProperty) { |
|
191 Object.defineProperty=function(obj,prop,desc) { |
|
192 if ("get" in desc) obj.__defineGetter__(prop,desc.get); |
|
193 if ("set" in desc) obj.__defineSetter__(prop,desc.set); |
|
194 } |
|
195 } |
|
196 */ |
|
197 |
|
198 /* Creates regexps from text */ |
|
199 IriSP.regexpFromText = function(_text) { |
|
200 return new RegExp('(' + _text.replace(/(\W)/gim,'\\$1') + ')','gim'); |
|
201 } |
|