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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. * Plays the sound given by id.
  62. *
  63. * @param id the identifier of the audio element.
  64. */
  65. playSoundNotification(id) {
  66. document.getElementById(id).play();
  67. },
  68. /**
  69. * Escapes the given text.
  70. */
  71. escapeHtml(unsafeText) {
  72. return $('<div/>').text(unsafeText)
  73. .html();
  74. },
  75. /**
  76. * Unescapes the given text.
  77. *
  78. * @param {string} safe string which contains escaped html
  79. * @returns {string} unescaped html string.
  80. */
  81. unescapeHtml(safe) {
  82. return $('<div />').html(safe)
  83. .text();
  84. },
  85. imageToGrayScale(canvas) {
  86. const context = canvas.getContext('2d');
  87. const imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  88. const pixels = imgData.data;
  89. for (let i = 0, n = pixels.length; i < n; i += 4) {
  90. const grayscale
  91. = (pixels[i] * 0.3)
  92. + (pixels[i + 1] * 0.59)
  93. + (pixels[i + 2] * 0.11);
  94. pixels[i] = grayscale; // red
  95. pixels[i + 1] = grayscale; // green
  96. pixels[i + 2] = grayscale; // blue
  97. // pixels[i+3] is alpha
  98. }
  99. // redraw the image in black & white
  100. context.putImageData(imgData, 0, 0);
  101. },
  102. /**
  103. * Inserts given child element as the first one into the container.
  104. * @param container the container to which new child element will be added
  105. * @param newChild the new element that will be inserted into the container
  106. */
  107. prependChild(container, newChild) {
  108. const firstChild = container.childNodes[0];
  109. if (firstChild) {
  110. container.insertBefore(newChild, firstChild);
  111. } else {
  112. container.appendChild(newChild);
  113. }
  114. },
  115. /**
  116. * Indicates if the setting section is enabled.
  117. *
  118. * @param name the name of the setting section as defined in
  119. * interface_config.js and SettingsMenu.js
  120. * @returns {boolean} {true} to indicate that the given setting section
  121. * is enabled, {false} - otherwise
  122. */
  123. isSettingEnabled(name) {
  124. return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
  125. },
  126. /**
  127. * Indicates if Authentication Section should be shown
  128. *
  129. * @returns {boolean}
  130. */
  131. isAuthenticationEnabled() {
  132. return interfaceConfig.AUTHENTICATION_ENABLE;
  133. },
  134. /**
  135. * Shows / hides the element given by id.
  136. *
  137. * @param {string|HTMLElement} idOrElement the identifier or the element
  138. * to show/hide
  139. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  140. */
  141. setVisible(id, visible) {
  142. let element;
  143. if (id instanceof HTMLElement) {
  144. element = id;
  145. } else {
  146. element = document.getElementById(id);
  147. }
  148. if (!element) {
  149. return;
  150. }
  151. if (!visible) {
  152. element.classList.add('hide');
  153. } else if (element.classList.contains('hide')) {
  154. element.classList.remove('hide');
  155. }
  156. const type = this._getElementDefaultDisplay(element.tagName);
  157. const className = SHOW_CLASSES[type];
  158. if (visible) {
  159. element.classList.add(className);
  160. } else if (element.classList.contains(className)) {
  161. element.classList.remove(className);
  162. }
  163. },
  164. /**
  165. * Returns default display style for the tag
  166. * @param tag
  167. * @returns {*}
  168. * @private
  169. */
  170. _getElementDefaultDisplay(tag) {
  171. const tempElement = document.createElement(tag);
  172. document.body.appendChild(tempElement);
  173. const style = window.getComputedStyle(tempElement).display;
  174. document.body.removeChild(tempElement);
  175. return style;
  176. },
  177. /**
  178. * Shows / hides the element with the given jQuery selector.
  179. *
  180. * @param {jQuery} jquerySelector the jQuery selector of the element to
  181. * show / shide
  182. * @param {boolean} isVisible
  183. */
  184. setVisibleBySelector(jquerySelector, isVisible) {
  185. if (jquerySelector && jquerySelector.length > 0) {
  186. jquerySelector.css('visibility', isVisible ? 'visible' : 'hidden');
  187. }
  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;