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

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