src/js/utils.js
author hamidouk
Mon, 19 Dec 2011 15:25:22 +0100
branchpopcorn-port
changeset 481 a46cfeee6d77
parent 479 24308670f1bb
child 497 f671cbaae9db
permissions -rw-r--r--
using jquery ui draggable changes the state of an element from absolute to relative positioning, which breaks the way our seek button expands itself, so we need to force absolute positioning, quite uglily, using jquery.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     1
/* utils.js - various utils that don't belong anywhere else */
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     2
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     3
/* trace function, for debugging */
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     4
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     5
IriSP.traceNum = 0;
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     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
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     8
	if( IriSP.config.gui.debug === true ) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     9
		IriSP.traceNum += 1;
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    10
		IriSP.jQuery( "<div>"+IriSP.traceNum+" - "+msg+" : "+value+"</div>" ).appendTo( "#Ldt-output" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    11
	}
73
a8192c57c87d added a function to make closures (for use in callbacks).
hamidouk
parents: 60
diff changeset
    12
*/
31
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    13
};
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    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
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    20
  return function() {    
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    21
    var args = Array.prototype.slice.call(arguments, 0);
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    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
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    28
	var time = Math.abs(time);
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    29
  var timetotal = Math.abs(timetotal);
95c6b77f005e added more test for utility functions.
hamidouk
parents: 252
diff changeset
    30
  
160
d3f336807ec3 a little tweak to timeToPourcent.
hamidouk
parents: 103
diff changeset
    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
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    58
};
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    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
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    62
/* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    63
IriSP.formatTweet = function(tweet) {
374
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    64
  /*
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    65
    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
    66
  */
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    67
  var regExps = [
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    68
    /* 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
    69
    [/((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
    70
    [/@(\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
    71
    [/#(\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
    72
    [/(\+\+)/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
    73
    [/(--)/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
    74
    [/(==)/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
    75
    [/(\?\?)/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
    76
  ]; 
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    77
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    78
  var i = 0;
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    79
  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
    80
     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
    81
  }
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    82
  
374
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    83
  return tweet;
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    84
};
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    85
354
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    86
IriSP.countProperties = function(obj) {
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    87
    var count = 0;
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    88
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    89
    for(var prop in obj) {
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    90
        if(obj.hasOwnProperty(prop))
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    91
                ++count;
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    92
    }
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
    return count;
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    95
};
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    96
358
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
    97
// 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
    98
IriSP.DEC_HEXA_COLOR = function (dec) {
479
24308670f1bb fixed the display of colors.
hamidouk
parents: 473
diff changeset
    99
	 var hexa='0123456789ABCDEF';
24308670f1bb fixed the display of colors.
hamidouk
parents: 473
diff changeset
   100
   var hex='';
358
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   101
	 var tmp;
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   102
	 while (dec>15){
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   103
		  tmp = dec-(Math.floor(dec/16))*16;
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   104
		  hex = hexa.charAt(tmp)+hex;
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   105
		  dec = Math.floor(dec/16);
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   106
	 }
473
1a09ab7e7163 fixed display bugs in segmentsWidget
hamidouk
parents: 374
diff changeset
   107
	 hex = hexa.charAt(dec)+hex;	 
358
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   108
	 return(hex);
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   109
};
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
   110
362
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 358
diff changeset
   111
/* 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
   112
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
   113
  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
   114
  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
   115
};
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 358
diff changeset
   116
369
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   117
/* we need to be stricter than encodeURIComponent,
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   118
   because of twitter
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   119
*/  
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   120
IriSP.encodeURI = function(str) {
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   121
  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
   122
                                 replace(/\)/g, '%29').replace(/\*/g, '%2A');  
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   123
}  
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   124
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   125
60
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   126
/* for ie compatibility
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   127
if (Object.prototype.__defineGetter__&&!Object.defineProperty) {
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   128
   Object.defineProperty=function(obj,prop,desc) {
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   129
      if ("get" in desc) obj.__defineGetter__(prop,desc.get);
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   130
      if ("set" in desc) obj.__defineSetter__(prop,desc.set);
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   131
   }
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   132
}
308
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
   133
*/