src/js/utils.js
author veltr
Thu, 19 Apr 2012 19:20:41 +0200
branchnew-model
changeset 872 d777d05a16e4
parent 870 2c025db10a10
child 875 43629caa77bc
permissions -rw-r--r--
finished AnnotationsList and started New PolemicWidget
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
256
8e1181b855bd added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents: 254
diff changeset
     3
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
     4
  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
     5
    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
     6
  } else {
8e1181b855bd added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents: 254
diff changeset
     7
    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
     8
  }
8e1181b855bd added a function to pad a number with zeros. waiting for the helpers to land into
hamidouk
parents: 254
diff changeset
     9
};
553
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 502
diff changeset
    10
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 502
diff changeset
    11
/* 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
    12
   [hours, minutes, seconds]
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 502
diff changeset
    13
*/
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 502
diff changeset
    14
IriSP.msToTime = function(ms) {
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 502
diff changeset
    15
  return IriSP.secondsToTime(ms / 1000);
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 502
diff changeset
    16
}
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    17
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    18
/* 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
    19
IriSP.formatTweet = function(tweet) {
374
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    20
  /*
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    21
    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
    22
  */
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    23
  var regExps = [
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    24
    /* 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
    25
    [/((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
    26
    [/@(\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
    27
    [/#(\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
    28
    [/(\+\+)/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
    29
    [/(--)/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
    30
    [/(==)/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
    31
    [/(\?\?)/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
    32
  ]; 
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    33
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    34
  var i = 0;
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    35
  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
    36
     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
    37
  }
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    38
  
374
138e76fe73a6 extended the function to format tweet to apply styles to the polemic syntax.
hamidouk
parents: 369
diff changeset
    39
  return tweet;
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    40
};
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 256
diff changeset
    41
354
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    42
IriSP.countProperties = function(obj) {
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    43
    var count = 0;
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    44
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    45
    for(var prop in obj) {
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    46
        if(obj.hasOwnProperty(prop))
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    47
                ++count;
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    48
    }
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    49
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    50
    return count;
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    51
};
002c314cabbf added a function to count the number of fields of an object.
hamidouk
parents: 308
diff changeset
    52
358
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
    53
// 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
    54
IriSP.DEC_HEXA_COLOR = function (dec) {
640
a6b64eeeb76d redid samuel's decimal to hexa conversion function.
hamidouk
parents: 634
diff changeset
    55
  var val = +dec;
a6b64eeeb76d redid samuel's decimal to hexa conversion function.
hamidouk
parents: 634
diff changeset
    56
  var str = val.toString(16);
a6b64eeeb76d redid samuel's decimal to hexa conversion function.
hamidouk
parents: 634
diff changeset
    57
  var zeroes = "";
a6b64eeeb76d redid samuel's decimal to hexa conversion function.
hamidouk
parents: 634
diff changeset
    58
  if (str.length < 6) {
a6b64eeeb76d redid samuel's decimal to hexa conversion function.
hamidouk
parents: 634
diff changeset
    59
    for (var i = 0; i < 6 - str.length; i++)
a6b64eeeb76d redid samuel's decimal to hexa conversion function.
hamidouk
parents: 634
diff changeset
    60
      zeroes += "0";
a6b64eeeb76d redid samuel's decimal to hexa conversion function.
hamidouk
parents: 634
diff changeset
    61
  }
a6b64eeeb76d redid samuel's decimal to hexa conversion function.
hamidouk
parents: 634
diff changeset
    62
  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
    63
};
430c1a7a09de got rid of useless LdtPlayer.js -moved the necessary lines of code in main.js
hamidouk
parents: 354
diff changeset
    64
362
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 358
diff changeset
    65
/* 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
    66
IriSP.templToHTML = function(template, values) {
840
ac66e2240e1e bugfixes
veltr
parents: 837
diff changeset
    67
  var params = IriSP.underscore.extend(
870
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 852
diff changeset
    68
      {
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 852
diff changeset
    69
          "l10n" : IriSP.i18n.getMessages()
2c025db10a10 Migrated playerWidget and started annotationsListWidget
veltr
parents: 852
diff changeset
    70
      },
837
353a78021ebc Added Internationalization
veltr
parents: 836
diff changeset
    71
      values);
362
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 358
diff changeset
    72
  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
    73
};
0b1bf06c28ed added a wrapper around the templating engine, to be able to have default values
hamidouk
parents: 358
diff changeset
    74
369
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    75
/* we need to be stricter than encodeURIComponent,
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    76
   because of twitter
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    77
*/  
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    78
IriSP.encodeURI = function(str) {
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    79
  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
    80
                                 replace(/\)/g, '%29').replace(/\*/g, '%2A');  
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 830
diff changeset
    81
}
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 830
diff changeset
    82
835
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
    83
IriSP.jqEscape = function(text) {
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
    84
   return text.replace(/(:|\.)/g,'\\$1')
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
    85
}
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
    86
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
    87
IriSP.jqId = function (text) { 
a8af9da7c622 Integrated trace manager
veltr
parents: 834
diff changeset
    88
   return IriSP.jQuery('#' + IriSP.jqEscape(text));
834
573c7ca752e0 Adapted Stackgraph for Cinecast
veltr
parents: 830
diff changeset
    89
 }  
369
70bc85b31c6d added a function to strictly encode uri components.
hamidouk
parents: 362
diff changeset
    90
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
    91
/** returns an url to share on facebook */
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
    92
IriSP.mkFbUrl = function(url, text) {
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
    93
  if (typeof(text) === "undefined")
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
    94
    text = "I'm watching ";
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
    95
  
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
    96
  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
    97
};
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
    98
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
    99
/** returns an url to share on twitter */
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   100
IriSP.mkTweetUrl = function(url, text) {
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   101
  if (typeof(text) === "undefined")
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   102
    text = "I'm watching ";
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   103
  
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   104
  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
   105
};
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   106
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   107
/** returns an url to share on google + */
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   108
IriSP.mkGplusUrl = function(url, text) {
720
08b8b3ce30e4 added a function to share on google plus.
hamidouk
parents: 686
diff changeset
   109
  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
   110
};
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 553
diff changeset
   111
820
7968346b9689 Added compatibility with cinecast format (with get_aliased)
veltr
parents: 720
diff changeset
   112
/** get a property that can have multiple names **/
7968346b9689 Added compatibility with cinecast format (with get_aliased)
veltr
parents: 720
diff changeset
   113
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   114
/** 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
   115
IriSP.shorten_url = function(url) {
852
eefb64f74a3f Cinecast bugfixes
veltr
parents: 840
diff changeset
   116
  return encodeURIComponent(url);
650
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   117
};
54ad76d6f9fe added a function to shorten an url
hamidouk
parents: 640
diff changeset
   118
836
526f91f5253e Changes for Cinecast
veltr
parents: 835
diff changeset
   119
60
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   120
/* for ie compatibility
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   121
if (Object.prototype.__defineGetter__&&!Object.defineProperty) {
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   122
   Object.defineProperty=function(obj,prop,desc) {
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   123
      if ("get" in desc) obj.__defineGetter__(prop,desc.get);
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   124
      if ("set" in desc) obj.__defineSetter__(prop,desc.set);
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   125
   }
ffc038fdb8f1 commented ie8 helper function
hamidouk
parents: 31
diff changeset
   126
}
308
495ef0f3e483 added a toString method to the object returned by IriSP.secondsToTime.
hamidouk
parents: 297
diff changeset
   127
*/
830
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   128
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   129
/* Creates regexps from text */
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   130
IriSP.regexpFromText = function(_text) {
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   131
    return new RegExp('(' + _text.replace(/(\W)/gim,'\\$1') + ')','gim');
18ca612e9ff0 Lots of changes
veltr
parents: 820
diff changeset
   132
}