您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UIUtil.js 5.7KB

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