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.

UIUtil.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* global $, config, interfaceConfig */
  2. /**
  3. * Created by hristo on 12/22/14.
  4. */
  5. var UIUtil = {
  6. /**
  7. * Returns the available video width.
  8. */
  9. getAvailableVideoWidth: function () {
  10. let rightPanelWidth = 0;
  11. return window.innerWidth - rightPanelWidth;
  12. },
  13. /**
  14. * Changes the style class of the element given by id.
  15. */
  16. buttonClick: function(id, classname) {
  17. $(id).toggleClass(classname); // add the class to the clicked element
  18. },
  19. /**
  20. * Returns the text width for the given element.
  21. *
  22. * @param el the element
  23. */
  24. getTextWidth: function (el) {
  25. return (el.clientWidth + 1);
  26. },
  27. /**
  28. * Returns the text height for the given element.
  29. *
  30. * @param el the element
  31. */
  32. getTextHeight: function (el) {
  33. return (el.clientHeight + 1);
  34. },
  35. /**
  36. * Plays the sound given by id.
  37. *
  38. * @param id the identifier of the audio element.
  39. */
  40. playSoundNotification: function (id) {
  41. document.getElementById(id).play();
  42. },
  43. /**
  44. * Escapes the given text.
  45. */
  46. escapeHtml: function (unsafeText) {
  47. return $('<div/>').text(unsafeText).html();
  48. },
  49. /**
  50. * Unescapes the given text.
  51. *
  52. * @param {string} safe string which contains escaped html
  53. * @returns {string} unescaped html string.
  54. */
  55. unescapeHtml: function (safe) {
  56. return $('<div />').html(safe).text();
  57. },
  58. imageToGrayScale: function (canvas) {
  59. var context = canvas.getContext('2d');
  60. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  61. var pixels = imgData.data;
  62. for (var i = 0, n = pixels.length; i < n; i += 4) {
  63. var grayscale
  64. = pixels[i] * 0.3 + pixels[i+1] * 0.59 + pixels[i+2] * 0.11;
  65. pixels[i ] = grayscale; // red
  66. pixels[i+1] = grayscale; // green
  67. pixels[i+2] = grayscale; // blue
  68. // pixels[i+3] is alpha
  69. }
  70. // redraw the image in black & white
  71. context.putImageData(imgData, 0, 0);
  72. },
  73. setTooltip: function (element, key, position) {
  74. element.setAttribute("data-i18n", "[data-content]" + key);
  75. element.setAttribute("data-toggle", "popover");
  76. element.setAttribute("data-placement", position);
  77. element.setAttribute("data-html", true);
  78. element.setAttribute("data-container", "body");
  79. },
  80. /**
  81. * Inserts given child element as the first one into the container.
  82. * @param container the container to which new child element will be added
  83. * @param newChild the new element that will be inserted into the container
  84. */
  85. prependChild: function (container, newChild) {
  86. var firstChild = container.childNodes[0];
  87. if (firstChild) {
  88. container.insertBefore(newChild, firstChild);
  89. } else {
  90. container.appendChild(newChild);
  91. }
  92. },
  93. isButtonEnabled: function (name) {
  94. return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1;
  95. },
  96. hideDisabledButtons: function (mappings) {
  97. var selector = Object.keys(mappings)
  98. .map(function (buttonName) {
  99. return UIUtil.isButtonEnabled(buttonName)
  100. ? null : mappings[buttonName].id; })
  101. .filter(function (item) { return item; })
  102. .join(',');
  103. $(selector).hide();
  104. },
  105. redirect (url) {
  106. window.location.href = url;
  107. },
  108. isFullScreen () {
  109. return document.fullScreen
  110. || document.mozFullScreen
  111. || document.webkitIsFullScreen;
  112. },
  113. /**
  114. * Create html attributes string out of object properties.
  115. * @param {Object} attrs object with properties
  116. * @returns {String} string of html element attributes
  117. */
  118. attrsToString: function (attrs) {
  119. return Object.keys(attrs).map(
  120. key => ` ${key}="${attrs[key]}"`
  121. ).join(' ');
  122. },
  123. /**
  124. * Checks if the given DOM element is currently visible. The offsetParent
  125. * will be null if the "display" property of the element or any of its
  126. * parent containers is set to "none". This method will NOT check the
  127. * visibility property though.
  128. * @param {el} The DOM element we'd like to check for visibility
  129. */
  130. isVisible(el) {
  131. return (el.offsetParent !== null);
  132. },
  133. /**
  134. * Shows / hides the element given by {selector} and sets a timeout if the
  135. * {hideDelay} is set to a value > 0.
  136. * @param selector the jquery selector of the element to show/hide.
  137. * @param show a {boolean} that indicates if the element should be shown or
  138. * hidden
  139. * @param hideDelay the value in milliseconds to wait before hiding the
  140. * element
  141. */
  142. animateShowElement(selector, show, hideDelay) {
  143. if(show) {
  144. if (!selector.is(":visible"))
  145. selector.css("display", "inline-block");
  146. selector.fadeIn(300,
  147. () => {selector.css({opacity: 1});}
  148. );
  149. if (hideDelay && hideDelay > 0)
  150. setTimeout(
  151. function () {
  152. selector.fadeOut(300,
  153. () => {selector.css({opacity: 0});}
  154. );
  155. }, hideDelay);
  156. }
  157. else {
  158. selector.fadeOut(300,
  159. () => {selector.css({opacity: 0});}
  160. );
  161. }
  162. },
  163. /**
  164. * Parses the given cssValue as an Integer. If the value is not a number
  165. * we return 0 instead of NaN.
  166. * @param cssValue the string value we obtain when querying css properties
  167. */
  168. parseCssInt(cssValue) {
  169. return parseInt(cssValue) || 0;
  170. }
  171. };
  172. export default UIUtil;