| author | hamidouk |
| Tue, 24 Jan 2012 17:30:01 +0100 | |
| branch | embed-playerapi-rewrite |
| changeset 702 | f1225d38c150 |
| parent 686 | f1dbe6a6e740 |
| child 720 | 08b8b3ce30e4 |
| permissions | -rw-r--r-- |
| 31 | 1 |
/* utils.js - various utils that don't belong anywhere else */ |
2 |
||
3 |
/* trace function, for debugging */ |
|
4 |
||
5 |
IriSP.traceNum = 0; |
|
6 |
IriSP.trace = function( msg, value ) { |
|
|
73
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
7 |
/* |
| 31 | 8 |
if( IriSP.config.gui.debug === true ) { |
9 |
IriSP.traceNum += 1; |
|
10 |
IriSP.jQuery( "<div>"+IriSP.traceNum+" - "+msg+" : "+value+"</div>" ).appendTo( "#Ldt-output" ); |
|
11 |
} |
|
|
73
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
12 |
*/ |
| 31 | 13 |
}; |
14 |
||
|
73
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
15 |
/* used in callbacks - because in callbacks we lose "this", |
|
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
16 |
we need to have a special function which wraps "this" in |
|
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
17 |
a closure. This way, the |
|
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
18 |
*/ |
|
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
19 |
IriSP.wrap = function (obj, fn) { |
| 253 | 20 |
return function() { |
21 |
var args = Array.prototype.slice.call(arguments, 0); |
|
22 |
return fn.apply(obj, args); |
|
|
73
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
23 |
} |
|
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
24 |
} |
|
a8192c57c87d
added a function to make closures (for use in callbacks).
hamidouk
parents:
60
diff
changeset
|
25 |
|
|
99
912f100fecb0
added a small utility function to convert a time to a progression percentage.
hamidouk
parents:
78
diff
changeset
|
26 |
/* convert a time to a percentage in the media */ |
|
103
2dfd89e91c3a
added a method to sort the json annotations. changed the tests accordingly.
hamidouk
parents:
99
diff
changeset
|
27 |
IriSP.timeToPourcent = function(time, timetotal){ |
| 253 | 28 |
var time = Math.abs(time); |
29 |
var timetotal = Math.abs(timetotal); |
|
30 |
|
|
| 160 | 31 |
return Math.floor((time/timetotal) * 100); |
|
99
912f100fecb0
added a small utility function to convert a time to a progression percentage.
hamidouk
parents:
78
diff
changeset
|
32 |
}; |
|
912f100fecb0
added a small utility function to convert a time to a progression percentage.
hamidouk
parents:
78
diff
changeset
|
33 |
|
|
256
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
34 |
IriSP.padWithZeros = function(num) { |
|
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
35 |
if (Math.abs(num) < 10) { |
|
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
36 |
return "0" + num.toString(); |
|
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
37 |
} else { |
|
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
38 |
return num.toString(); |
|
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
39 |
} |
|
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
40 |
}; |
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
41 |
|
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
42 |
/* convert a number of milliseconds to a tuple of the form |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
43 |
[hours, minutes, seconds] |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
44 |
*/ |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
45 |
IriSP.msToTime = function(ms) { |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
46 |
return IriSP.secondsToTime(ms / 1000); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
47 |
} |
|
252
fd84e0fb26d8
added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
160
diff
changeset
|
48 |
/* convert a number of seconds to a tuple of the form |
|
fd84e0fb26d8
added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
160
diff
changeset
|
49 |
[hours, minutes, seconds] |
|
fd84e0fb26d8
added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
160
diff
changeset
|
50 |
*/ |
|
254
cafaa694b709
fixed the conversion function to accomodate mustache.
hamidouk
parents:
253
diff
changeset
|
51 |
IriSP.secondsToTime = function(secs) { |
|
252
fd84e0fb26d8
added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
160
diff
changeset
|
52 |
var hours = Math.abs(parseInt( secs / 3600 ) % 24); |
|
fd84e0fb26d8
added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
160
diff
changeset
|
53 |
var minutes = Math.abs(parseInt( secs / 60 ) % 60); |
|
254
cafaa694b709
fixed the conversion function to accomodate mustache.
hamidouk
parents:
253
diff
changeset
|
54 |
var seconds = parseFloat(Math.abs(secs % 60).toFixed(0)); |
|
252
fd84e0fb26d8
added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
160
diff
changeset
|
55 |
|
|
308
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
56 |
var toString_fn = function() { |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
57 |
var ret = ""; |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
58 |
if (hours > 0) |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
59 |
ret = IriSP.padWithZeros(this.hours) + ":"; |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
60 |
ret += IriSP.padWithZeros(this.minutes) + ":" + IriSP.padWithZeros(this.seconds); |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
61 |
|
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
62 |
return ret; |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
63 |
} |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
64 |
return {"hours" : hours, "minutes" : minutes, "seconds" : seconds, toString: toString_fn}; |
| 285 | 65 |
}; |
66 |
||
67 |
/* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */ |
|
68 |
IriSP.formatTweet = function(tweet) { |
|
|
374
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
69 |
/* |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
70 |
an array of arrays which hold a regexp and its replacement. |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
71 |
*/ |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
72 |
var regExps = [ |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
73 |
/* copied from http://codegolf.stackexchange.com/questions/464/shortest-url-regex-match-in-javascript/480#480 */ |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
74 |
[/((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi, "<a href='$1'>$1</a>"], |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
75 |
[/@(\w+)/gi, "<a href='http://twitter.com/$1'>@$1</a>"], // matches a @handle |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
76 |
[/#(\w+)/gi, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>"],// matches a hashtag |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
77 |
[/(\+\+)/gi, "<span class='Ldt-PolemicPlusPlus'>$1</span>"], |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
78 |
[/(--)/gi, "<span class='Ldt-PolemicMinusMinus'>$1</span>"], |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
79 |
[/(==)/gi, "<span class='Ldt-PolemicEqualEqual'>$1</span>"], |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
80 |
[/(\?\?)/gi, "<span class='Ldt-PolemicQuestion'>$1</span>"] |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
81 |
]; |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
82 |
|
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
83 |
var i = 0; |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
84 |
for(i = 0; i < regExps.length; i++) { |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
85 |
tweet = tweet.replace(regExps[i][0], regExps[i][1]); |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
86 |
} |
| 285 | 87 |
|
|
374
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
88 |
return tweet; |
| 285 | 89 |
}; |
90 |
||
|
354
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
91 |
IriSP.countProperties = function(obj) { |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
92 |
var count = 0; |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
93 |
|
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
94 |
for(var prop in obj) { |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
95 |
if(obj.hasOwnProperty(prop)) |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
96 |
++count; |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
97 |
} |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
98 |
|
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
99 |
return count; |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
100 |
}; |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
101 |
|
|
358
430c1a7a09de
got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents:
354
diff
changeset
|
102 |
// conversion de couleur Decimal vers HexaDecimal || 000 si fff |
|
430c1a7a09de
got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents:
354
diff
changeset
|
103 |
IriSP.DEC_HEXA_COLOR = function (dec) { |
|
640
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
104 |
var val = +dec; |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
105 |
var str = val.toString(16); |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
106 |
var zeroes = ""; |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
107 |
if (str.length < 6) { |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
108 |
for (var i = 0; i < 6 - str.length; i++) |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
109 |
zeroes += "0"; |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
110 |
} |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
111 |
return zeroes + str; |
|
358
430c1a7a09de
got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents:
354
diff
changeset
|
112 |
}; |
|
430c1a7a09de
got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents:
354
diff
changeset
|
113 |
|
|
362
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
114 |
/* shortcut to have global variables in templates */ |
|
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
115 |
IriSP.templToHTML = function(template, values) { |
|
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
116 |
var params = IriSP.jQuery.extend(IriSP.default_templates_vars, values); |
|
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
117 |
return Mustache.to_html(template, params); |
|
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
118 |
}; |
|
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
119 |
|
|
369
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
120 |
/* we need to be stricter than encodeURIComponent, |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
121 |
because of twitter |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
122 |
*/ |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
123 |
IriSP.encodeURI = function(str) { |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
124 |
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28'). |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
125 |
replace(/\)/g, '%29').replace(/\*/g, '%2A'); |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
126 |
} |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
127 |
|
| 502 | 128 |
IriSP.__guidCounter = 0; |
| 497 | 129 |
IriSP.guid = function(prefix) { |
| 502 | 130 |
IriSP.__guidCounter += 1; |
131 |
return prefix + IriSP.__guidCounter; |
|
| 497 | 132 |
}; |
| 575 | 133 |
|
134 |
/** returns an url to share on facebook */ |
|
| 650 | 135 |
IriSP.mkFbUrl = function(url, text) { |
136 |
if (typeof(text) === "undefined") |
|
137 |
text = "I'm watching "; |
|
138 |
|
|
139 |
return "http://www.facebook.com/share.php?u=" + IriSP.encodeURI(text) + IriSP.shorten_url(url); |
|
| 575 | 140 |
}; |
141 |
||
142 |
/** returns an url to share on twitter */ |
|
| 650 | 143 |
IriSP.mkTweetUrl = function(url, text) { |
144 |
if (typeof(text) === "undefined") |
|
145 |
text = "I'm watching "; |
|
146 |
|
|
147 |
return "http://twitter.com/home?status=" + IriSP.encodeURI(text) + IriSP.shorten_url(url); |
|
| 575 | 148 |
}; |
149 |
||
150 |
/** returns an url to share on google + */ |
|
| 650 | 151 |
IriSP.mkGplusUrl = function(url, text) { |
| 575 | 152 |
return ""; |
153 |
}; |
|
154 |
||
|
634
9de33a5ef2b3
added a utility function to test for both null and undefined.
hamidouk
parents:
584
diff
changeset
|
155 |
/** test if a value is null or undefined */ |
|
9de33a5ef2b3
added a utility function to test for both null and undefined.
hamidouk
parents:
584
diff
changeset
|
156 |
IriSP.null_or_undefined = function(val) { |
|
9de33a5ef2b3
added a utility function to test for both null and undefined.
hamidouk
parents:
584
diff
changeset
|
157 |
return (typeof(val) === "undefined" || val === null); |
| 650 | 158 |
}; |
159 |
||
160 |
/** issue a call to an url shortener and return the shortened url */ |
|
161 |
IriSP.shorten_url = function(url) { |
|
162 |
if (IriSP.config.shortener.hasOwnProperty("shortening_function")) |
|
163 |
return IriSP.config.shortener.shortening_function(url); |
|
164 |
|
|
165 |
return url; |
|
166 |
}; |
|
167 |
||
| 686 | 168 |
/** Similar to substr but remove the last word if |
169 |
we're breaking a word in two. |
|
170 |
*/ |
|
171 |
IriSP.clean_substr = function(str, start, end) { |
|
172 |
var s = str.substr(start, end).substr(start, end).split(" "); |
|
173 |
s.pop(); |
|
174 |
return s.join(" "); |
|
175 |
}; |
|
| 60 | 176 |
/* for ie compatibility |
177 |
if (Object.prototype.__defineGetter__&&!Object.defineProperty) { |
|
178 |
Object.defineProperty=function(obj,prop,desc) { |
|
179 |
if ("get" in desc) obj.__defineGetter__(prop,desc.get); |
|
180 |
if ("set" in desc) obj.__defineSetter__(prop,desc.set); |
|
181 |
} |
|
182 |
} |
|
|
308
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
183 |
*/ |