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.

util.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  45. * Returns the available video width.
  46. */
  47. my.getAvailableVideoWidth = function () {
  48. var rightPanelWidth
  49. = PanelToggler.isVisible() ? PanelToggler.getPanelSize()[0] : 0;
  50. return window.innerWidth - rightPanelWidth;
  51. };
  52. my.imageToGrayScale = function (canvas) {
  53. var context = canvas.getContext('2d');
  54. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  55. var pixels = imgData.data;
  56. for (var i = 0, n = pixels.length; i < n; i += 4) {
  57. var grayscale
  58. = pixels[i] * .3 + pixels[i+1] * .59 + pixels[i+2] * .11;
  59. pixels[i ] = grayscale; // red
  60. pixels[i+1] = grayscale; // green
  61. pixels[i+2] = grayscale; // blue
  62. // pixels[i+3] is alpha
  63. }
  64. // redraw the image in black & white
  65. context.putImageData(imgData, 0, 0);
  66. };
  67. my.setTooltip = function (element, tooltipText, position) {
  68. element.setAttribute("data-content", tooltipText);
  69. element.setAttribute("data-toggle", "popover");
  70. element.setAttribute("data-placement", position);
  71. element.setAttribute("data-html", true);
  72. element.setAttribute("data-container", "body");
  73. };
  74. my.createExpBackoffTimer = function (step) {
  75. var count = 1;
  76. return function (reset) {
  77. // Reset call
  78. if (reset) {
  79. count = 1;
  80. return;
  81. }
  82. // Calculate next timeout
  83. var timeout = Math.pow(2, count - 1);
  84. count += 1;
  85. return timeout * step;
  86. };
  87. };
  88. return my;
  89. }(Util || {}));