You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* global $ */
  2. /**
  3. * Utility functions.
  4. */
  5. var Util = (function (my) {
  6. /**
  7. * Returns the text width for the given element.
  8. *
  9. * @param el the element
  10. */
  11. my.getTextWidth = function (el) {
  12. return (el.clientWidth + 1);
  13. };
  14. /**
  15. * Returns the text height for the given element.
  16. *
  17. * @param el the element
  18. */
  19. my.getTextHeight = function (el) {
  20. return (el.clientHeight + 1);
  21. };
  22. /**
  23. * Casts the given number to integer.
  24. *
  25. * @param number the number to cast
  26. */
  27. my.toInteger = function (number) {
  28. return Math.round(Number(number));
  29. };
  30. /**
  31. * Plays the sound given by id.
  32. *
  33. * @param id the identifier of the audio element.
  34. */
  35. my.playSoundNotification = function (id) {
  36. document.getElementById(id).play();
  37. };
  38. /**
  39. * Escapes the given text.
  40. */
  41. my.escapeHtml = function (unsafeText) {
  42. return $('<div/>').text(unsafeText).html();
  43. };
  44. my.imageToGrayScale = function (canvas) {
  45. var context = canvas.getContext('2d');
  46. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  47. var pixels = imgData.data;
  48. for (var i = 0, n = pixels.length; i < n; i += 4) {
  49. var grayscale
  50. = pixels[i] * .3 + pixels[i+1] * .59 + pixels[i+2] * .11;
  51. pixels[i ] = grayscale; // red
  52. pixels[i+1] = grayscale; // green
  53. pixels[i+2] = grayscale; // blue
  54. // pixels[i+3] is alpha
  55. }
  56. // redraw the image in black & white
  57. context.putImageData(imgData, 0, 0);
  58. };
  59. my.setTooltip = function (element, tooltipText, position) {
  60. element.setAttribute("data-content", tooltipText);
  61. element.setAttribute("data-toggle", "popover");
  62. element.setAttribute("data-placement", position);
  63. element.setAttribute("data-html", true);
  64. element.setAttribute("data-container", "body");
  65. };
  66. my.createExpBackoffTimer = function (step) {
  67. var count = 1;
  68. return function (reset) {
  69. // Reset call
  70. if (reset) {
  71. count = 1;
  72. return;
  73. }
  74. // Calculate next timeout
  75. var timeout = Math.pow(2, count - 1);
  76. count += 1;
  77. return timeout * step;
  78. };
  79. };
  80. return my;
  81. }(Util || {}));