utils.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. define(["jquery"], function ($) {
  2. $.fn.hasScrollBar = function() {
  3. if (!$.contains(document, this.get(0))) {
  4. return false;
  5. }
  6. if(this.parent().height() < this.get(0).scrollHeight) {
  7. return true;
  8. }
  9. return false;
  10. };
  11. $.fn.addHyperlinks = function () {
  12. if (this.length > 0) {
  13. this.each(function (i, obj) {
  14. var x = $(obj).html();
  15. var list = x.match(/\b(https?:\/\/|www\.|https?:\/\/www\.)[^\s<]{2,200}\b/g );
  16. if (list) {
  17. for (i=0; i<list.length; i++) {
  18. var prot = list[i].indexOf('http://') === 0 || list[i].indexOf('https://') === 0 ? '' : 'http://';
  19. var escaped_url = encodeURI(decodeURI(list[i])).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
  20. x = x.replace(list[i], "<a target='_blank' href='" + prot + escaped_url + "'>"+ list[i] + "</a>" );
  21. }
  22. }
  23. $(obj).html(x);
  24. });
  25. }
  26. return this;
  27. };
  28. var utils = {
  29. // Translation machinery
  30. // ---------------------
  31. __: $.proxy(function (str) {
  32. // Translation factory
  33. if (this.i18n === undefined) {
  34. this.i18n = locales.en;
  35. }
  36. var t = this.i18n.translate(str);
  37. if (arguments.length>1) {
  38. return t.fetch.apply(t, [].slice.call(arguments,1));
  39. } else {
  40. return t.fetch();
  41. }
  42. }, this),
  43. ___: function (str) {
  44. /* XXX: This is part of a hack to get gettext to scan strings to be
  45. * translated. Strings we cannot send to the function above because
  46. * they require variable interpolation and we don't yet have the
  47. * variables at scan time.
  48. *
  49. * See actionInfoMessages
  50. */
  51. return str;
  52. }
  53. };
  54. return utils;
  55. });