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

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