| author | veltr |
| Fri, 27 Apr 2012 19:18:21 +0200 | |
| branch | new-model |
| changeset 881 | f11b234497f7 |
| parent 875 | 43629caa77bc |
| child 882 | 61c384dda19e |
| permissions | -rw-r--r-- |
| 31 | 1 |
/* utils.js - various utils that don't belong anywhere else */ |
2 |
||
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
3 |
IriSP.getLib = function(lib) { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
4 |
if (IriSP.libFiles.useCdn && typeof IriSP.libFiles.cdn[lib] == "string") { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
5 |
return IriSP.libFiles.cdn[lib]; |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
6 |
} |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
7 |
if (typeof IriSP.libFiles.locations[lib] == "string") { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
8 |
return IriSP.libFiles.locations[lib]; |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
9 |
} |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
10 |
if (typeof IriSP.libFiles.inDefaultDir[lib] == "string") { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
11 |
return IriSP.libFiles.defaultDir + IriSP.libFiles.inDefaultDir[lib]; |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
12 |
} |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
13 |
} |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
14 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
15 |
IriSP.loadCss = function(_cssFile) { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
16 |
IriSP.jQuery("<link>", { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
17 |
rel : "stylesheet", |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
18 |
type : "text/css", |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
19 |
href : _cssFile |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
20 |
}).appendTo('head'); |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
21 |
} |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
22 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
23 |
/* |
|
256
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
24 |
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
|
25 |
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
|
26 |
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
|
27 |
} else { |
|
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
28 |
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
|
29 |
} |
|
8e1181b855bd
added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents:
254
diff
changeset
|
30 |
}; |
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
31 |
|
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
32 |
/* 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
|
33 |
[hours, minutes, seconds] |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
34 |
|
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
35 |
IriSP.msToTime = function(ms) { |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
36 |
return IriSP.secondsToTime(ms / 1000); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
502
diff
changeset
|
37 |
} |
| 285 | 38 |
|
39 |
/* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */ |
|
40 |
IriSP.formatTweet = function(tweet) { |
|
|
374
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
41 |
/* |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
42 |
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
|
43 |
*/ |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
44 |
var regExps = [ |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
45 |
/* 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
|
46 |
[/((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
|
47 |
[/@(\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
|
48 |
[/#(\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
|
49 |
[/(\+\+)/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
|
50 |
[/(--)/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
|
51 |
[/(==)/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
|
52 |
[/(\?\?)/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
|
53 |
]; |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
54 |
|
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
55 |
var i = 0; |
|
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
} |
| 285 | 59 |
|
|
374
138e76fe73a6
extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents:
369
diff
changeset
|
60 |
return tweet; |
| 285 | 61 |
}; |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
62 |
/* |
|
354
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
63 |
IriSP.countProperties = function(obj) { |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
64 |
var count = 0; |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
65 |
|
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
66 |
for(var prop in obj) { |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
67 |
if(obj.hasOwnProperty(prop)) |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
68 |
++count; |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
69 |
} |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
70 |
|
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
71 |
return count; |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
72 |
}; |
|
002c314cabbf
added a function to count the number of fields of an object.
hamidouk
parents:
308
diff
changeset
|
73 |
|
|
358
430c1a7a09de
got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents:
354
diff
changeset
|
74 |
// 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
|
75 |
IriSP.DEC_HEXA_COLOR = function (dec) { |
|
640
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
76 |
var val = +dec; |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
77 |
var str = val.toString(16); |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
78 |
var zeroes = ""; |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
79 |
if (str.length < 6) { |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
80 |
for (var i = 0; i < 6 - str.length; i++) |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
81 |
zeroes += "0"; |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
82 |
} |
|
a6b64eeeb76d
redid samuel's decimal to hexa conversion function.
hamidouk
parents:
634
diff
changeset
|
83 |
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
|
84 |
}; |
|
430c1a7a09de
got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents:
354
diff
changeset
|
85 |
|
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
86 |
/* shortcut to have global variables in templates |
|
362
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
87 |
IriSP.templToHTML = function(template, values) { |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
870
diff
changeset
|
88 |
var params = IriSP._.extend( |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
852
diff
changeset
|
89 |
{ |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
852
diff
changeset
|
90 |
"l10n" : IriSP.i18n.getMessages() |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
852
diff
changeset
|
91 |
}, |
| 837 | 92 |
values); |
|
362
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
93 |
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
|
94 |
}; |
|
0b1bf06c28ed
added a wrapper around the templating engine, to be able to have default values
hamidouk
parents:
358
diff
changeset
|
95 |
|
|
369
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
96 |
/* we need to be stricter than encodeURIComponent, |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
97 |
because of twitter |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
98 |
*/ |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
99 |
IriSP.encodeURI = function(str) { |
|
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
100 |
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
|
101 |
replace(/\)/g, '%29').replace(/\*/g, '%2A'); |
| 834 | 102 |
} |
103 |
||
| 835 | 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)); |
|
| 834 | 110 |
} |
|
369
70bc85b31c6d
added a function to strictly encode uri components.
hamidouk
parents:
362
diff
changeset
|
111 |
|
| 575 | 112 |
/** returns an url to share on facebook */ |
| 650 | 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); |
|
| 575 | 118 |
}; |
119 |
||
120 |
/** returns an url to share on twitter */ |
|
| 650 | 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); |
|
| 575 | 126 |
}; |
127 |
||
128 |
/** returns an url to share on google + */ |
|
| 650 | 129 |
IriSP.mkGplusUrl = function(url, text) { |
| 720 | 130 |
return "https://plusone.google.com/_/+1/confirm?hl=en&url=" + IriSP.shorten_url(url); |
| 575 | 131 |
}; |
132 |
||
|
820
7968346b9689
Added compatibility with cinecast format (with get_aliased)
veltr
parents:
720
diff
changeset
|
133 |
/** get a property that can have multiple names **/ |
|
7968346b9689
Added compatibility with cinecast format (with get_aliased)
veltr
parents:
720
diff
changeset
|
134 |
|
| 650 | 135 |
/** issue a call to an url shortener and return the shortened url */ |
136 |
IriSP.shorten_url = function(url) { |
|
| 852 | 137 |
return encodeURIComponent(url); |
| 650 | 138 |
}; |
139 |
||
| 836 | 140 |
|
| 60 | 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 |
} |
|
|
308
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
148 |
*/ |
| 830 | 149 |
|
150 |
/* Creates regexps from text */ |
|
151 |
IriSP.regexpFromText = function(_text) { |
|
152 |
return new RegExp('(' + _text.replace(/(\W)/gim,'\\$1') + ')','gim'); |
|
153 |
} |