| author | hamidouk |
| Fri, 25 Nov 2011 11:10:19 +0100 | |
| branch | popcorn-port |
| changeset 331 | ef3447aa6920 |
| parent 308 | 495ef0f3e483 |
| child 354 | 002c314cabbf |
| 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 |
}; |
|
252
fd84e0fb26d8
added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
160
diff
changeset
|
41 |
/* 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
|
42 |
[hours, minutes, seconds] |
|
fd84e0fb26d8
added a function to convert a number of seconds to an hour, minutes, seconds.
hamidouk
parents:
160
diff
changeset
|
43 |
*/ |
|
254
cafaa694b709
fixed the conversion function to accomodate mustache.
hamidouk
parents:
253
diff
changeset
|
44 |
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
|
45 |
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
|
46 |
var minutes = Math.abs(parseInt( secs / 60 ) % 60); |
|
254
cafaa694b709
fixed the conversion function to accomodate mustache.
hamidouk
parents:
253
diff
changeset
|
47 |
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
|
48 |
|
|
308
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
49 |
var toString_fn = function() { |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
50 |
var ret = ""; |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
51 |
if (hours > 0) |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
52 |
ret = IriSP.padWithZeros(this.hours) + ":"; |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
53 |
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
|
54 |
|
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
55 |
return ret; |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
56 |
} |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
57 |
return {"hours" : hours, "minutes" : minutes, "seconds" : seconds, toString: toString_fn}; |
| 285 | 58 |
}; |
59 |
||
|
308
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
60 |
IriSP.secondsToString |
|
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
61 |
|
| 285 | 62 |
/* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */ |
63 |
IriSP.formatTweet = function(tweet) { |
|
| 297 | 64 |
var rNickname = /@(\w+)/gi; // matches a @handle |
65 |
var rHashtag = /#(\w+)/gi; // matches a hashtag |
|
66 |
var rUrl = /((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi; // copied from http://codegolf.stackexchange.com/questions/464/shortest-url-regex-match-in-javascript/480#480 |
|
| 285 | 67 |
|
| 297 | 68 |
var i1 = tweet.replace(rUrl, "<a href='$1'>$1</a>"); |
69 |
var i2 = i1.replace(rNickname, "<a href='http://twitter.com/$1'>@$1</a>"); |
|
70 |
var i3 = i2.replace(rHashtag, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>"); |
|
71 |
||
72 |
return i3; |
|
| 285 | 73 |
}; |
74 |
||
| 60 | 75 |
/* for ie compatibility |
76 |
if (Object.prototype.__defineGetter__&&!Object.defineProperty) { |
|
77 |
Object.defineProperty=function(obj,prop,desc) { |
|
78 |
if ("get" in desc) obj.__defineGetter__(prop,desc.get); |
|
79 |
if ("set" in desc) obj.__defineSetter__(prop,desc.set); |
|
80 |
} |
|
81 |
} |
|
|
308
495ef0f3e483
added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents:
297
diff
changeset
|
82 |
*/ |