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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /**
  95. * Indicates if a toolbar button is enabled.
  96. * @param name the name of the setting section as defined in
  97. * interface_config.js and Toolbar.js
  98. * @returns {boolean} {true} to indicate that the given toolbar button
  99. * is enabled, {false} - otherwise
  100. */
  101. isButtonEnabled: function (name) {
  102. return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1
  103. || interfaceConfig.MAIN_TOOLBAR_BUTTONS.indexOf(name) !== -1;
  104. },
  105. /**
  106. * Indicates if the setting section is enabled.
  107. *
  108. * @param name the name of the setting section as defined in
  109. * interface_config.js and SettingsMenu.js
  110. * @returns {boolean} {true} to indicate that the given setting section
  111. * is enabled, {false} - otherwise
  112. */
  113. isSettingEnabled: function (name) {
  114. return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
  115. },
  116. /**
  117. * Shows the element given by id.
  118. *
  119. * @param {String} the identifier of the element to show
  120. */
  121. showElement(id) {
  122. if ($("#"+id).hasClass("hide"))
  123. $("#"+id).removeClass("hide");
  124. $("#"+id).addClass("show");
  125. },
  126. /**
  127. * Hides the element given by id.
  128. *
  129. * @param {String} the identifier of the element to hide
  130. */
  131. hideElement(id) {
  132. if ($("#"+id).hasClass("show"))
  133. $("#"+id).removeClass("show");
  134. $("#"+id).addClass("hide");
  135. },
  136. hideDisabledButtons: function (mappings) {
  137. var selector = Object.keys(mappings)
  138. .map(function (buttonName) {
  139. return UIUtil.isButtonEnabled(buttonName)
  140. ? null : "#" + mappings[buttonName].id; })
  141. .filter(function (item) { return item; })
  142. .join(',');
  143. $(selector).hide();
  144. },
  145. redirect (url) {
  146. window.location.href = url;
  147. },
  148. isFullScreen () {
  149. return document.fullScreen
  150. || document.mozFullScreen
  151. || document.webkitIsFullScreen;
  152. },
  153. /**
  154. * Create html attributes string out of object properties.
  155. * @param {Object} attrs object with properties
  156. * @returns {String} string of html element attributes
  157. */
  158. attrsToString: function (attrs) {
  159. return Object.keys(attrs).map(
  160. key => ` ${key}="${attrs[key]}"`
  161. ).join(' ');
  162. },
  163. /**
  164. * Checks if the given DOM element is currently visible. The offsetParent
  165. * will be null if the "display" property of the element or any of its
  166. * parent containers is set to "none". This method will NOT check the
  167. * visibility property though.
  168. * @param {el} The DOM element we'd like to check for visibility
  169. */
  170. isVisible(el) {
  171. return (el.offsetParent !== null);
  172. },
  173. /**
  174. * Shows / hides the element given by {selector} and sets a timeout if the
  175. * {hideDelay} is set to a value > 0.
  176. * @param selector the jquery selector of the element to show/hide.
  177. * @param show a {boolean} that indicates if the element should be shown or
  178. * hidden
  179. * @param hideDelay the value in milliseconds to wait before hiding the
  180. * element
  181. */
  182. animateShowElement(selector, show, hideDelay) {
  183. if(show) {
  184. if (!selector.is(":visible"))
  185. selector.css("display", "inline-block");
  186. selector.fadeIn(300,
  187. () => {selector.css({opacity: 1});}
  188. );
  189. if (hideDelay && hideDelay > 0)
  190. setTimeout(
  191. function () {
  192. selector.fadeOut(300,
  193. () => {selector.css({opacity: 0});}
  194. );
  195. }, hideDelay);
  196. }
  197. else {
  198. selector.fadeOut(300,
  199. () => {selector.css({opacity: 0});}
  200. );
  201. }
  202. },
  203. /**
  204. * Parses the given cssValue as an Integer. If the value is not a number
  205. * we return 0 instead of NaN.
  206. * @param cssValue the string value we obtain when querying css properties
  207. */
  208. parseCssInt(cssValue) {
  209. return parseInt(cssValue) || 0;
  210. }
  211. };
  212. export default UIUtil;