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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 the setting section is enabled.
  109. *
  110. * @param name the name of the setting section as defined in
  111. * interface_config.js and SettingsMenu.js
  112. * @returns {boolean} {true} to indicate that the given setting section
  113. * is enabled, {false} - otherwise
  114. */
  115. isSettingEnabled(name) {
  116. return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
  117. },
  118. /**
  119. * Indicates if Authentication Section should be shown
  120. *
  121. * @returns {boolean}
  122. */
  123. isAuthenticationEnabled() {
  124. return interfaceConfig.AUTHENTICATION_ENABLE;
  125. },
  126. /**
  127. * Shows / hides the element given by id.
  128. *
  129. * @param {string|HTMLElement} idOrElement the identifier or the element
  130. * to show/hide
  131. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  132. */
  133. setVisible(id, visible) {
  134. let element;
  135. if (id instanceof HTMLElement) {
  136. element = id;
  137. } else {
  138. element = document.getElementById(id);
  139. }
  140. if (!element) {
  141. return;
  142. }
  143. if (!visible) {
  144. element.classList.add('hide');
  145. } else if (element.classList.contains('hide')) {
  146. element.classList.remove('hide');
  147. }
  148. const type = this._getElementDefaultDisplay(element.tagName);
  149. const className = SHOW_CLASSES[type];
  150. if (visible) {
  151. element.classList.add(className);
  152. } else if (element.classList.contains(className)) {
  153. element.classList.remove(className);
  154. }
  155. },
  156. /**
  157. * Returns default display style for the tag
  158. * @param tag
  159. * @returns {*}
  160. * @private
  161. */
  162. _getElementDefaultDisplay(tag) {
  163. const tempElement = document.createElement(tag);
  164. document.body.appendChild(tempElement);
  165. const style = window.getComputedStyle(tempElement).display;
  166. document.body.removeChild(tempElement);
  167. return style;
  168. },
  169. /**
  170. * Shows / hides the element with the given jQuery selector.
  171. *
  172. * @param {jQuery} jquerySelector the jQuery selector of the element to
  173. * show / shide
  174. * @param {boolean} isVisible
  175. */
  176. setVisibleBySelector(jquerySelector, isVisible) {
  177. if (jquerySelector && jquerySelector.length > 0) {
  178. jquerySelector.css('visibility', isVisible ? 'visible' : 'hidden');
  179. }
  180. },
  181. /**
  182. * Redirects to a given URL.
  183. *
  184. * @param {string} url - The redirect URL.
  185. * NOTE: Currently used to redirect to 3rd party location for
  186. * authentication. In most cases redirectWithStoredParams action must be
  187. * used instead of this method in order to preserve curent URL params.
  188. */
  189. redirect(url) {
  190. window.location.href = url;
  191. },
  192. /**
  193. * Indicates if we're currently in full screen mode.
  194. *
  195. * @return {boolean} {true} to indicate that we're currently in full screen
  196. * mode, {false} otherwise
  197. */
  198. isFullScreen() {
  199. return document.fullscreenElement
  200. || document.mozFullScreenElement
  201. || document.webkitFullscreenElement
  202. || document.msFullscreenElement;
  203. },
  204. /**
  205. * Exits full screen mode.
  206. * @see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
  207. */
  208. exitFullScreen() {
  209. if (document.exitFullscreen) {
  210. document.exitFullscreen();
  211. } else if (document.msExitFullscreen) {
  212. document.msExitFullscreen();
  213. } else if (document.mozCancelFullScreen) {
  214. document.mozCancelFullScreen();
  215. } else if (document.webkitExitFullscreen) {
  216. document.webkitExitFullscreen();
  217. }
  218. },
  219. /**
  220. * Enter full screen mode.
  221. * @see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
  222. */
  223. enterFullScreen() {
  224. if (document.documentElement.requestFullscreen) {
  225. document.documentElement.requestFullscreen();
  226. } else if (document.documentElement.msRequestFullscreen) {
  227. document.documentElement.msRequestFullscreen();
  228. } else if (document.documentElement.mozRequestFullScreen) {
  229. document.documentElement.mozRequestFullScreen();
  230. } else if (document.documentElement.webkitRequestFullscreen) {
  231. document.documentElement
  232. .webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  233. }
  234. },
  235. /**
  236. * Create html attributes string out of object properties.
  237. * @param {Object} attrs object with properties
  238. * @returns {String} string of html element attributes
  239. */
  240. attrsToString(attrs) {
  241. return (
  242. Object.keys(attrs).map(key => ` ${key}="${attrs[key]}"`)
  243. .join(' '));
  244. },
  245. /**
  246. * Checks if the given DOM element is currently visible. The offsetParent
  247. * will be null if the "display" property of the element or any of its
  248. * parent containers is set to "none". This method will NOT check the
  249. * visibility property though.
  250. * @param {el} The DOM element we'd like to check for visibility
  251. */
  252. isVisible(el) {
  253. return el.offsetParent !== null;
  254. },
  255. /**
  256. * Shows / hides the element given by {selector} and sets a timeout if the
  257. * {hideDelay} is set to a value > 0.
  258. * @param selector the jquery selector of the element to show/hide.
  259. * @param show a {boolean} that indicates if the element should be shown or
  260. * hidden
  261. * @param hideDelay the value in milliseconds to wait before hiding the
  262. * element
  263. */
  264. animateShowElement(selector, show, hideDelay) {
  265. if (show) {
  266. if (!selector.is(':visible')) {
  267. selector.css('display', 'inline-block');
  268. }
  269. selector.fadeIn(300,
  270. () => {
  271. selector.css({ opacity: 1 });
  272. }
  273. );
  274. if (hideDelay && hideDelay > 0) {
  275. setTimeout(
  276. () => {
  277. selector.fadeOut(
  278. 300,
  279. () => {
  280. selector.css({ opacity: 0 });
  281. });
  282. },
  283. hideDelay);
  284. }
  285. } else {
  286. selector.fadeOut(300,
  287. () => {
  288. selector.css({ opacity: 0 });
  289. }
  290. );
  291. }
  292. },
  293. /**
  294. * Parses the given cssValue as an Integer. If the value is not a number
  295. * we return 0 instead of NaN.
  296. * @param cssValue the string value we obtain when querying css properties
  297. */
  298. parseCssInt(cssValue) {
  299. return parseInt(cssValue, 10) || 0;
  300. },
  301. /**
  302. * Adds href value to 'a' link jquery object. If link value is null,
  303. * undefined or empty string, disables the link.
  304. * @param {object} aLinkElement the jquery object
  305. * @param {string} link the link value
  306. */
  307. setLinkHref(aLinkElement, link) {
  308. if (link) {
  309. aLinkElement.attr('href', link);
  310. } else {
  311. aLinkElement.css({
  312. 'pointer-events': 'none',
  313. 'cursor': 'default'
  314. });
  315. }
  316. },
  317. /**
  318. * Returns font size for indicators according to current
  319. * height of thumbnail
  320. * @param {Number} [thumbnailHeight] - current height of thumbnail
  321. * @returns {Number} - font size for current height
  322. */
  323. getIndicatorFontSize(thumbnailHeight) {
  324. const height = typeof thumbnailHeight === 'undefined'
  325. ? $('#localVideoContainer').height() : thumbnailHeight;
  326. const { SMALL, MEDIUM } = ThumbnailSizes;
  327. let fontSize = IndicatorFontSizes.NORMAL;
  328. if (height <= SMALL) {
  329. fontSize = IndicatorFontSizes.SMALL;
  330. } else if (height > SMALL && height <= MEDIUM) {
  331. fontSize = IndicatorFontSizes.MEDIUM;
  332. }
  333. return fontSize;
  334. }
  335. };
  336. export default UIUtil;