src/js/utils.js
branchnew-model
changeset 870 2c025db10a10
parent 852 eefb64f74a3f
child 875 43629caa77bc
equal deleted inserted replaced
868:a525cc2214e7 870:2c025db10a10
     1 /* utils.js - various utils that don't belong anywhere else */
     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 ) {
       
     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 }
       
    25 
       
    26 /* convert a time to a percentage in the media */
       
    27 IriSP.timeToPourcent = function(time, timetotal){
       
    28 	var time = Math.abs(time);
       
    29   var timetotal = Math.abs(timetotal);
       
    30   
       
    31 	return Math.floor((time/timetotal) * 100);
       
    32 };
       
    33 
     2 
    34 IriSP.padWithZeros = function(num) {
     3 IriSP.padWithZeros = function(num) {
    35   if (Math.abs(num) < 10) {
     4   if (Math.abs(num) < 10) {
    36     return "0" + num.toString();
     5     return "0" + num.toString();
    37   } else {
     6   } else {
    43    [hours, minutes, seconds]
    12    [hours, minutes, seconds]
    44 */
    13 */
    45 IriSP.msToTime = function(ms) {
    14 IriSP.msToTime = function(ms) {
    46   return IriSP.secondsToTime(ms / 1000);
    15   return IriSP.secondsToTime(ms / 1000);
    47 }
    16 }
    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 
    17 
    67 /* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */
    18 /* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */
    68 IriSP.formatTweet = function(tweet) {
    19 IriSP.formatTweet = function(tweet) {
    69   /*
    20   /*
    70     an array of arrays which hold a regexp and its replacement.
    21     an array of arrays which hold a regexp and its replacement.
   112 };
    63 };
   113 
    64 
   114 /* shortcut to have global variables in templates */
    65 /* shortcut to have global variables in templates */
   115 IriSP.templToHTML = function(template, values) {
    66 IriSP.templToHTML = function(template, values) {
   116   var params = IriSP.underscore.extend(
    67   var params = IriSP.underscore.extend(
   117       { "defaults" : IriSP.default_templates_vars,
    68       {
   118         "l10n" : IriSP.i18n.getMessages()
    69           "l10n" : IriSP.i18n.getMessages()
   119         },
    70       },
   120       values);
    71       values);
   121   return Mustache.to_html(template, params);
    72   return Mustache.to_html(template, params);
   122 };
    73 };
   123 
    74 
   124 /* we need to be stricter than encodeURIComponent,
    75 /* we need to be stricter than encodeURIComponent,
   134 }
    85 }
   135 
    86 
   136 IriSP.jqId = function (text) { 
    87 IriSP.jqId = function (text) { 
   137    return IriSP.jQuery('#' + IriSP.jqEscape(text));
    88    return IriSP.jQuery('#' + IriSP.jqEscape(text));
   138  }  
    89  }  
   139 
       
   140 IriSP.__guidCounter = 0;
       
   141 IriSP.guid = function(prefix) {
       
   142   IriSP.__guidCounter += 1;
       
   143   return prefix + IriSP.__guidCounter;
       
   144 };
       
   145 
    90 
   146 /** returns an url to share on facebook */
    91 /** returns an url to share on facebook */
   147 IriSP.mkFbUrl = function(url, text) {
    92 IriSP.mkFbUrl = function(url, text) {
   148   if (typeof(text) === "undefined")
    93   if (typeof(text) === "undefined")
   149     text = "I'm watching ";
    94     text = "I'm watching ";
   162 /** returns an url to share on google + */
   107 /** returns an url to share on google + */
   163 IriSP.mkGplusUrl = function(url, text) {
   108 IriSP.mkGplusUrl = function(url, text) {
   164   return "https://plusone.google.com/_/+1/confirm?hl=en&url=" + IriSP.shorten_url(url);
   109   return "https://plusone.google.com/_/+1/confirm?hl=en&url=" + IriSP.shorten_url(url);
   165 };
   110 };
   166 
   111 
   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 **/
   112 /** 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     }
       
   180     return null;
       
   181 }
       
   182 
   113 
   183 /** issue a call to an url shortener and return the shortened url */
   114 /** issue a call to an url shortener and return the shortened url */
   184 IriSP.shorten_url = function(url) {
   115 IriSP.shorten_url = function(url) {
   185   return encodeURIComponent(url);
   116   return encodeURIComponent(url);
   186 };
   117 };