src/js/utils.js
author veltr
Fri, 27 Apr 2012 19:18:21 +0200
branchnew-model
changeset 881 f11b234497f7
parent 875 43629caa77bc
child 882 61c384dda19e
permissions -rw-r--r--
Finished Mediafragment
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
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
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    38
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    39
/* 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
    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
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    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
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    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
353a78021ebc Added Internationalization
veltr
parents: 836
diff changeset
    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
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 830
diff changeset
   102
}
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 830
diff changeset
   103
835
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
   104
IriSP.jqEscape = function(text) {
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
   105
   return text.replace(/(:|\.)/g,'\\$1')
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
   106
}
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
   107
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
   108
IriSP.jqId = function (text) { 
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
   109
   return IriSP.jQuery('#' + IriSP.jqEscape(text));
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 830
diff changeset
   110
 }  
369
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
   111
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   112
/** returns an url to share on facebook */
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   113
IriSP.mkFbUrl = function(url, text) {
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   114
  if (typeof(text) === "undefined")
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   115
    text = "I'm watching ";
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   116
  
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   117
  return "http://www.facebook.com/share.php?u=" + IriSP.encodeURI(text) + IriSP.shorten_url(url);
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   118
};
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   119
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   120
/** returns an url to share on twitter */
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   121
IriSP.mkTweetUrl = function(url, text) {
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   122
  if (typeof(text) === "undefined")
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   123
    text = "I'm watching ";
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   124
  
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   125
  return "http://twitter.com/home?status=" + IriSP.encodeURI(text) + IriSP.shorten_url(url);
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   126
};
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   127
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   128
/** returns an url to share on google + */
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   129
IriSP.mkGplusUrl = function(url, text) {
720
08b8b3ce30e4 added a function to share on google plus.
hamidouk
parents: 686
diff changeset
   130
  return "https://plusone.google.com/_/+1/confirm?hl=en&url=" + IriSP.shorten_url(url);
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   131
};
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   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
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   135
/** issue a call to an url shortener and return the shortened url */
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   136
IriSP.shorten_url = function(url) {
852
eefb64f74a3f Cinecast bugfixes
veltr
parents: 840
diff changeset
   137
  return encodeURIComponent(url);
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   138
};
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   139
836
526f91f5253e Changes for Cinecast
veltr
parents: 835
diff changeset
   140
60
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   141
/* for ie compatibility
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   142
if (Object.prototype.__defineGetter__&&!Object.defineProperty) {
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   143
   Object.defineProperty=function(obj,prop,desc) {
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   144
      if ("get" in desc) obj.__defineGetter__(prop,desc.get);
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   145
      if ("set" in desc) obj.__defineSetter__(prop,desc.set);
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   146
   }
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   147
}
308
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
   148
*/
830
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   149
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   150
/* Creates regexps from text */
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   151
IriSP.regexpFromText = function(_text) {
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   152
    return new RegExp('(' + _text.replace(/(\W)/gim,'\\$1') + ')','gim');
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   153
}