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

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