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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* global $, interfaceConfig */
  2. /**
  3. * Associates the default display type with corresponding CSS class
  4. */
  5. const SHOW_CLASSES = {
  6. 'block': 'show',
  7. 'inline': 'show-inline',
  8. 'list-item': 'show-list-item'
  9. };
  10. /**
  11. * Contains sizes of thumbnails
  12. * @type {{SMALL: number, MEDIUM: number}}
  13. */
  14. const ThumbnailSizes = {
  15. SMALL: 60,
  16. MEDIUM: 80
  17. };
  18. /**
  19. * Created by hristo on 12/22/14.
  20. */
  21. const UIUtil = {
  22. /**
  23. * Returns the available video width.
  24. */
  25. getAvailableVideoWidth() {
  26. return window.innerWidth;
  27. },
  28. /**
  29. * Changes the style class of the element given by id.
  30. */
  31. buttonClick(id, classname) {
  32. // add the class to the clicked element
  33. $(`#${id}`).toggleClass(classname);
  34. },
  35. /**
  36. * Returns the text width for the given element.
  37. *
  38. * @param el the element
  39. */
  40. getTextWidth(el) {
  41. return el.clientWidth + 1;
  42. },
  43. /**
  44. * Returns the text height for the given element.
  45. *
  46. * @param el the element
  47. */
  48. getTextHeight(el) {
  49. return el.clientHeight + 1;
  50. },
  51. /**
  52. * Escapes the given text.
  53. */
  54. escapeHtml(unsafeText) {
  55. return $('<div/>').text(unsafeText)
  56. .html();
  57. },
  58. imageToGrayScale(canvas) {
  59. const context = canvas.getContext('2d');
  60. const imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  61. const pixels = imgData.data;
  62. for (let i = 0, n = pixels.length; i < n; i += 4) {
  63. const grayscale
  64. = (pixels[i] * 0.3)
  65. + (pixels[i + 1] * 0.59)
  66. + (pixels[i + 2] * 0.11);
  67. pixels[i] = grayscale; // red
  68. pixels[i + 1] = grayscale; // green
  69. pixels[i + 2] = grayscale; // blue
  70. // pixels[i+3] is alpha
  71. }
  72. // redraw the image in black & white
  73. context.putImageData(imgData, 0, 0);
  74. },
  75. /**
  76. * Inserts given child element as the first one into the container.
  77. * @param container the container to which new child element will be added
  78. * @param newChild the new element that will be inserted into the container
  79. */
  80. prependChild(container, newChild) {
  81. const firstChild = container.childNodes[0];
  82. if (firstChild) {
  83. container.insertBefore(newChild, firstChild);
  84. } else {
  85. container.appendChild(newChild);
  86. }
  87. },
  88. /**
  89. * Indicates if Authentication Section should be shown
  90. *
  91. * @returns {boolean}
  92. */
  93. isAuthenticationEnabled() {
  94. return interfaceConfig.AUTHENTICATION_ENABLE;
  95. },
  96. /**
  97. * Shows / hides the element given by id.
  98. *
  99. * @param {string|HTMLElement} idOrElement the identifier or the element
  100. * to show/hide
  101. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  102. */
  103. setVisible(id, visible) {
  104. let element;
  105. if (id instanceof HTMLElement) {
  106. element = id;
  107. } else {
  108. element = document.getElementById(id);
  109. }
  110. if (!element) {
  111. return;
  112. }
  113. if (!visible) {
  114. element.classList.add('hide');
  115. } else if (element.classList.contains('hide')) {
  116. element.classList.remove('hide');
  117. }
  118. const type = this._getElementDefaultDisplay(element.tagName);
  119. const className = SHOW_CLASSES[type];
  120. if (visible) {
  121. element.classList.add(className);
  122. } else if (element.classList.contains(className)) {
  123. element.classList.remove(className);
  124. }
  125. },
  126. /**
  127. * Returns default display style for the tag
  128. * @param tag
  129. * @returns {*}
  130. * @private
  131. */
  132. _getElementDefaultDisplay(tag) {
  133. const tempElement = document.createElement(tag);
  134. document.body.appendChild(tempElement);
  135. const style = window.getComputedStyle(tempElement).display;
  136. document.body.removeChild(tempElement);
  137. return style;
  138. },
  139. /**
  140. * Shows / hides the element with the given jQuery selector.
  141. *
  142. * @param {jQuery} jquerySelector the jQuery selector of the element to
  143. * show / shide
  144. * @param {boolean} isVisible
  145. */
  146. setVisibleBySelector(jquerySelector, isVisible) {
  147. if (jquerySelector && jquerySelector.length > 0) {
  148. jquerySelector.css('visibility', isVisible ? 'visible' : 'hidden');
  149. }
  150. },
  151. /**
  152. * Redirects to a given URL.
  153. *
  154. * @param {string} url - The redirect URL.
  155. * NOTE: Currently used to redirect to 3rd party location for
  156. * authentication. In most cases redirectWithStoredParams action must be
  157. * used instead of this method in order to preserve curent URL params.
  158. */
  159. redirect(url) {
  160. window.location.href = url;
  161. },
  162. /**
  163. * Indicates if we're currently in full screen mode.
  164. *
  165. * @return {boolean} {true} to indicate that we're currently in full screen
  166. * mode, {false} otherwise
  167. */
  168. isFullScreen() {
  169. return Boolean(document.fullscreenElement
  170. || document.mozFullScreenElement
  171. || document.webkitFullscreenElement
  172. || document.msFullscreenElement);
  173. },
  174. /**
  175. * Create html attributes string out of object properties.
  176. * @param {Object} attrs object with properties
  177. * @returns {String} string of html element attributes
  178. */
  179. attrsToString(attrs) {
  180. return (
  181. Object.keys(attrs).map(key => ` ${key}="${attrs[key]}"`)
  182. .join(' '));
  183. },
  184. /**
  185. * Checks if the given DOM element is currently visible. The offsetParent
  186. * will be null if the "display" property of the element or any of its
  187. * parent containers is set to "none". This method will NOT check the
  188. * visibility property though.
  189. * @param {el} The DOM element we'd like to check for visibility
  190. */
  191. isVisible(el) {
  192. return el.offsetParent !== null;
  193. },
  194. /**
  195. * Shows / hides the element given by {selector} and sets a timeout if the
  196. * {hideDelay} is set to a value > 0.
  197. * @param selector the jquery selector of the element to show/hide.
  198. * @param show a {boolean} that indicates if the element should be shown or
  199. * hidden
  200. * @param hideDelay the value in milliseconds to wait before hiding the
  201. * element
  202. */
  203. animateShowElement(selector, show, hideDelay) {
  204. if (show) {
  205. if (!selector.is(':visible')) {
  206. selector.css('display', 'inline-block');
  207. }
  208. selector.fadeIn(300,
  209. () => {
  210. selector.css({ opacity: 1 });
  211. }
  212. );
  213. if (hideDelay && hideDelay > 0) {
  214. setTimeout(
  215. () => {
  216. selector.fadeOut(
  217. 300,
  218. () => {
  219. selector.css({ opacity: 0 });
  220. });
  221. },
  222. hideDelay);
  223. }
  224. } else {
  225. selector.fadeOut(300,
  226. () => {
  227. selector.css({ opacity: 0 });
  228. }
  229. );
  230. }
  231. },
  232. /**
  233. * Parses the given cssValue as an Integer. If the value is not a number
  234. * we return 0 instead of NaN.
  235. * @param cssValue the string value we obtain when querying css properties
  236. */
  237. parseCssInt(cssValue) {
  238. return parseInt(cssValue, 10) || 0;
  239. },
  240. /**
  241. * Adds href value to 'a' link jquery object. If link value is null,
  242. * undefined or empty string, disables the link.
  243. * @param {object} aLinkElement the jquery object
  244. * @param {string} link the link value
  245. */
  246. setLinkHref(aLinkElement, link) {
  247. if (link) {
  248. aLinkElement.attr('href', link);
  249. } else {
  250. aLinkElement.css({
  251. 'pointer-events': 'none',
  252. 'cursor': 'default'
  253. });
  254. }
  255. },
  256. /**
  257. * Returns font size for indicators according to current
  258. * height of thumbnail
  259. * @param {Number} [thumbnailHeight] - current height of thumbnail
  260. * @returns {Number} - font size for current height
  261. */
  262. getIndicatorFontSize(thumbnailHeight) {
  263. const height = typeof thumbnailHeight === 'undefined'
  264. ? $('#localVideoContainer').height() : thumbnailHeight;
  265. const { SMALL, MEDIUM } = ThumbnailSizes;
  266. const IndicatorFontSizes = interfaceConfig.INDICATOR_FONT_SIZES || {
  267. SMALL: 5,
  268. MEDIUM: 6,
  269. NORMAL: 8
  270. };
  271. let fontSize = IndicatorFontSizes.NORMAL;
  272. if (height <= SMALL) {
  273. fontSize = IndicatorFontSizes.SMALL;
  274. } else if (height > SMALL && height <= MEDIUM) {
  275. fontSize = IndicatorFontSizes.MEDIUM;
  276. }
  277. return fontSize;
  278. }
  279. };
  280. export default UIUtil;