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 6.2KB

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