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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 Boolean(document.fullscreenElement
  200. || document.mozFullScreenElement
  201. || document.webkitFullscreenElement
  202. || document.msFullscreenElement);
  203. },
  204. /**
  205. * Create html attributes string out of object properties.
  206. * @param {Object} attrs object with properties
  207. * @returns {String} string of html element attributes
  208. */
  209. attrsToString(attrs) {
  210. return (
  211. Object.keys(attrs).map(key => ` ${key}="${attrs[key]}"`)
  212. .join(' '));
  213. },
  214. /**
  215. * Checks if the given DOM element is currently visible. The offsetParent
  216. * will be null if the "display" property of the element or any of its
  217. * parent containers is set to "none". This method will NOT check the
  218. * visibility property though.
  219. * @param {el} The DOM element we'd like to check for visibility
  220. */
  221. isVisible(el) {
  222. return el.offsetParent !== null;
  223. },
  224. /**
  225. * Shows / hides the element given by {selector} and sets a timeout if the
  226. * {hideDelay} is set to a value > 0.
  227. * @param selector the jquery selector of the element to show/hide.
  228. * @param show a {boolean} that indicates if the element should be shown or
  229. * hidden
  230. * @param hideDelay the value in milliseconds to wait before hiding the
  231. * element
  232. */
  233. animateShowElement(selector, show, hideDelay) {
  234. if (show) {
  235. if (!selector.is(':visible')) {
  236. selector.css('display', 'inline-block');
  237. }
  238. selector.fadeIn(300,
  239. () => {
  240. selector.css({ opacity: 1 });
  241. }
  242. );
  243. if (hideDelay && hideDelay > 0) {
  244. setTimeout(
  245. () => {
  246. selector.fadeOut(
  247. 300,
  248. () => {
  249. selector.css({ opacity: 0 });
  250. });
  251. },
  252. hideDelay);
  253. }
  254. } else {
  255. selector.fadeOut(300,
  256. () => {
  257. selector.css({ opacity: 0 });
  258. }
  259. );
  260. }
  261. },
  262. /**
  263. * Parses the given cssValue as an Integer. If the value is not a number
  264. * we return 0 instead of NaN.
  265. * @param cssValue the string value we obtain when querying css properties
  266. */
  267. parseCssInt(cssValue) {
  268. return parseInt(cssValue, 10) || 0;
  269. },
  270. /**
  271. * Adds href value to 'a' link jquery object. If link value is null,
  272. * undefined or empty string, disables the link.
  273. * @param {object} aLinkElement the jquery object
  274. * @param {string} link the link value
  275. */
  276. setLinkHref(aLinkElement, link) {
  277. if (link) {
  278. aLinkElement.attr('href', link);
  279. } else {
  280. aLinkElement.css({
  281. 'pointer-events': 'none',
  282. 'cursor': 'default'
  283. });
  284. }
  285. },
  286. /**
  287. * Returns font size for indicators according to current
  288. * height of thumbnail
  289. * @param {Number} [thumbnailHeight] - current height of thumbnail
  290. * @returns {Number} - font size for current height
  291. */
  292. getIndicatorFontSize(thumbnailHeight) {
  293. const height = typeof thumbnailHeight === 'undefined'
  294. ? $('#localVideoContainer').height() : thumbnailHeight;
  295. const { SMALL, MEDIUM } = ThumbnailSizes;
  296. let fontSize = IndicatorFontSizes.NORMAL;
  297. if (height <= SMALL) {
  298. fontSize = IndicatorFontSizes.SMALL;
  299. } else if (height > SMALL && height <= MEDIUM) {
  300. fontSize = IndicatorFontSizes.MEDIUM;
  301. }
  302. return fontSize;
  303. }
  304. };
  305. export default UIUtil;