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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* global $, APP, AJS, interfaceConfig */
  2. import KeyboardShortcut from '../../keyboardshortcut/keyboardshortcut';
  3. /**
  4. * Associates tooltip element position (in the terms of
  5. * {@link UIUtil#setTooltip} which do not look like CSS <tt>position</tt>) with
  6. * AUI tooltip <tt>gravity</tt>.
  7. */
  8. const TOOLTIP_POSITIONS = {
  9. 'bottom': 'n',
  10. 'bottom-left': 'ne',
  11. 'bottom-right': 'nw',
  12. 'left': 'e',
  13. 'right': 'w',
  14. 'top': 's',
  15. 'top-left': 'se',
  16. 'top-right': 'sw'
  17. };
  18. /**
  19. * Associates the default display type with corresponding CSS class
  20. */
  21. const SHOW_CLASSES = {
  22. 'block': 'show',
  23. 'inline': 'show-inline',
  24. 'list-item': 'show-list-item'
  25. };
  26. /**
  27. * Contains sizes of thumbnails
  28. * @type {{SMALL: number, MEDIUM: number}}
  29. */
  30. const ThumbnailSizes = {
  31. SMALL: 60,
  32. MEDIUM: 80
  33. };
  34. /**
  35. * Contains font sizes for thumbnail indicators
  36. * @type {{SMALL: number, MEDIUM: number}}
  37. */
  38. const IndicatorFontSizes = {
  39. SMALL: 5,
  40. MEDIUM: 6,
  41. NORMAL: 8
  42. };
  43. /**
  44. * Created by hristo on 12/22/14.
  45. */
  46. var UIUtil = {
  47. /**
  48. * Returns the available video width.
  49. */
  50. getAvailableVideoWidth() {
  51. return window.innerWidth;
  52. },
  53. /**
  54. * Changes the style class of the element given by id.
  55. */
  56. buttonClick: function(id, classname) {
  57. // add the class to the clicked element
  58. $("#" + id).toggleClass(classname);
  59. },
  60. /**
  61. * Returns the text width for the given element.
  62. *
  63. * @param el the element
  64. */
  65. getTextWidth(el) {
  66. return (el.clientWidth + 1);
  67. },
  68. /**
  69. * Returns the text height for the given element.
  70. *
  71. * @param el the element
  72. */
  73. getTextHeight(el) {
  74. return (el.clientHeight + 1);
  75. },
  76. /**
  77. * Plays the sound given by id.
  78. *
  79. * @param id the identifier of the audio element.
  80. */
  81. playSoundNotification(id) {
  82. document.getElementById(id).play();
  83. },
  84. /**
  85. * Escapes the given text.
  86. */
  87. escapeHtml(unsafeText) {
  88. return $('<div/>').text(unsafeText).html();
  89. },
  90. /**
  91. * Unescapes the given text.
  92. *
  93. * @param {string} safe string which contains escaped html
  94. * @returns {string} unescaped html string.
  95. */
  96. unescapeHtml(safe) {
  97. return $('<div />').html(safe).text();
  98. },
  99. imageToGrayScale(canvas) {
  100. var context = canvas.getContext('2d');
  101. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  102. var pixels = imgData.data;
  103. for (var i = 0, n = pixels.length; i < n; i += 4) {
  104. var grayscale
  105. = pixels[i] * 0.3 + pixels[i+1] * 0.59 + pixels[i+2] * 0.11;
  106. pixels[i ] = grayscale; // red
  107. pixels[i+1] = grayscale; // green
  108. pixels[i+2] = grayscale; // blue
  109. // pixels[i+3] is alpha
  110. }
  111. // redraw the image in black & white
  112. context.putImageData(imgData, 0, 0);
  113. },
  114. /**
  115. * Sets a global handler for all tooltips. Once invoked, create a new
  116. * tooltip by merely updating a DOM node with the appropriate class (e.g.
  117. * <tt>tooltip-n</tt>) and the attribute <tt>content</tt>.
  118. */
  119. activateTooltips() {
  120. AJS.$('[data-tooltip]').tooltip({
  121. gravity() {
  122. return this.getAttribute('data-tooltip');
  123. },
  124. title() {
  125. return this.getAttribute('content');
  126. },
  127. html: true, // Handle multiline tooltips.
  128. // The following two prevent tooltips from being stuck:
  129. hoverable: false, // Make custom tooltips behave like native ones.
  130. live: true // Attach listener to document element.
  131. });
  132. },
  133. /**
  134. * Sets the tooltip to the given element.
  135. *
  136. * @param element the element to set the tooltip to
  137. * @param key the tooltip data-i18n key
  138. * @param position the position of the tooltip in relation to the element
  139. */
  140. setTooltip(element, key, position) {
  141. if (element) {
  142. const selector = element.jquery ? element : $(element);
  143. selector.attr('data-tooltip', TOOLTIP_POSITIONS[position]);
  144. selector.attr('data-i18n', `[content]${key}`);
  145. APP.translation.translateElement(selector);
  146. }
  147. },
  148. /**
  149. * Sets the tooltip to the given element, but instead of using translation
  150. * key uses text value.
  151. *
  152. * @param element the element to set the tooltip to
  153. * @param text the tooltip text
  154. * @param position the position of the tooltip in relation to the element
  155. */
  156. setTooltipText(element, text, position) {
  157. if (element) {
  158. UIUtil.removeTooltip(element);
  159. element.setAttribute('data-tooltip', TOOLTIP_POSITIONS[position]);
  160. element.setAttribute('content', text);
  161. }
  162. },
  163. /**
  164. * Removes the tooltip to the given element.
  165. *
  166. * @param element the element to remove the tooltip from
  167. */
  168. removeTooltip(element) {
  169. element.removeAttribute('data-tooltip', '');
  170. element.removeAttribute('data-i18n','');
  171. element.removeAttribute('content','');
  172. },
  173. /**
  174. * Internal util function for generating tooltip title.
  175. *
  176. * @param element
  177. * @returns {string|*}
  178. * @private
  179. */
  180. _getTooltipText(element) {
  181. let title = element.getAttribute('content');
  182. let shortcut = element.getAttribute('shortcut');
  183. if(shortcut) {
  184. let shortcutString = KeyboardShortcut.getShortcutTooltip(shortcut);
  185. title += ` ${shortcutString}`;
  186. }
  187. return title;
  188. },
  189. /**
  190. * Inserts given child element as the first one into the container.
  191. * @param container the container to which new child element will be added
  192. * @param newChild the new element that will be inserted into the container
  193. */
  194. prependChild(container, newChild) {
  195. var firstChild = container.childNodes[0];
  196. if (firstChild) {
  197. container.insertBefore(newChild, firstChild);
  198. } else {
  199. container.appendChild(newChild);
  200. }
  201. },
  202. /**
  203. * Indicates if a toolbar button is enabled.
  204. * @param name the name of the setting section as defined in
  205. * interface_config.js and Toolbar.js
  206. * @returns {boolean} {true} to indicate that the given toolbar button
  207. * is enabled, {false} - otherwise
  208. */
  209. isButtonEnabled(name) {
  210. return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1
  211. || interfaceConfig.MAIN_TOOLBAR_BUTTONS.indexOf(name) !== -1;
  212. },
  213. /**
  214. * Indicates if the setting section is enabled.
  215. *
  216. * @param name the name of the setting section as defined in
  217. * interface_config.js and SettingsMenu.js
  218. * @returns {boolean} {true} to indicate that the given setting section
  219. * is enabled, {false} - otherwise
  220. */
  221. isSettingEnabled(name) {
  222. return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
  223. },
  224. /**
  225. * Indicates if Authentication Section should be shown
  226. *
  227. * @returns {boolean}
  228. */
  229. isAuthenticationEnabled() {
  230. return interfaceConfig.AUTHENTICATION_ENABLE;
  231. },
  232. /**
  233. * Shows / hides the element given by id.
  234. *
  235. * @param {string|HTMLElement} idOrElement the identifier or the element
  236. * to show/hide
  237. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  238. */
  239. setVisible(id, visible) {
  240. let element;
  241. if (id instanceof HTMLElement) {
  242. element = id;
  243. } else {
  244. element = document.getElementById(id);
  245. }
  246. if (!element) {
  247. return;
  248. }
  249. if (!visible)
  250. element.classList.add('hide');
  251. else if (element.classList.contains('hide')) {
  252. element.classList.remove('hide');
  253. }
  254. let type = this._getElementDefaultDisplay(element.tagName);
  255. let className = SHOW_CLASSES[type];
  256. if (visible) {
  257. element.classList.add(className);
  258. }
  259. else if (element.classList.contains(className))
  260. element.classList.remove(className);
  261. },
  262. /**
  263. * Returns default display style for the tag
  264. * @param tag
  265. * @returns {*}
  266. * @private
  267. */
  268. _getElementDefaultDisplay(tag) {
  269. let tempElement = document.createElement(tag);
  270. document.body.appendChild(tempElement);
  271. let style = window.getComputedStyle(tempElement).display;
  272. document.body.removeChild(tempElement);
  273. return style;
  274. },
  275. /**
  276. * Shows / hides the element with the given jQuery selector.
  277. *
  278. * @param {jQuery} jquerySelector the jQuery selector of the element to
  279. * show / shide
  280. * @param {boolean} isVisible
  281. */
  282. setVisibleBySelector(jquerySelector, isVisible) {
  283. if (jquerySelector && jquerySelector.length > 0) {
  284. jquerySelector.css("visibility", isVisible ? "visible" : "hidden");
  285. }
  286. },
  287. hideDisabledButtons(mappings) {
  288. var selector = Object.keys(mappings)
  289. .map(function (buttonName) {
  290. return UIUtil.isButtonEnabled(buttonName)
  291. ? null : "#" + mappings[buttonName].id; })
  292. .filter(function (item) { return item; })
  293. .join(',');
  294. $(selector).hide();
  295. },
  296. redirect(url) {
  297. window.location.href = url;
  298. },
  299. /**
  300. * Indicates if we're currently in full screen mode.
  301. *
  302. * @return {boolean} {true} to indicate that we're currently in full screen
  303. * mode, {false} otherwise
  304. */
  305. isFullScreen() {
  306. return document.fullscreenElement
  307. || document.mozFullScreenElement
  308. || document.webkitFullscreenElement
  309. || document.msFullscreenElement;
  310. },
  311. /**
  312. * Exits full screen mode.
  313. * @see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
  314. */
  315. exitFullScreen() {
  316. if (document.exitFullscreen) {
  317. document.exitFullscreen();
  318. } else if (document.msExitFullscreen) {
  319. document.msExitFullscreen();
  320. } else if (document.mozCancelFullScreen) {
  321. document.mozCancelFullScreen();
  322. } else if (document.webkitExitFullscreen) {
  323. document.webkitExitFullscreen();
  324. }
  325. },
  326. /**
  327. * Enter full screen mode.
  328. * @see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
  329. */
  330. enterFullScreen() {
  331. if (document.documentElement.requestFullscreen) {
  332. document.documentElement.requestFullscreen();
  333. } else if (document.documentElement.msRequestFullscreen) {
  334. document.documentElement.msRequestFullscreen();
  335. } else if (document.documentElement.mozRequestFullScreen) {
  336. document.documentElement.mozRequestFullScreen();
  337. } else if (document.documentElement.webkitRequestFullscreen) {
  338. document.documentElement
  339. .webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  340. }
  341. },
  342. /**
  343. * Create html attributes string out of object properties.
  344. * @param {Object} attrs object with properties
  345. * @returns {String} string of html element attributes
  346. */
  347. attrsToString(attrs) {
  348. return Object.keys(attrs).map(
  349. key => ` ${key}="${attrs[key]}"`
  350. ).join(' ');
  351. },
  352. /**
  353. * Checks if the given DOM element is currently visible. The offsetParent
  354. * will be null if the "display" property of the element or any of its
  355. * parent containers is set to "none". This method will NOT check the
  356. * visibility property though.
  357. * @param {el} The DOM element we'd like to check for visibility
  358. */
  359. isVisible(el) {
  360. return (el.offsetParent !== null);
  361. },
  362. /**
  363. * Shows / hides the element given by {selector} and sets a timeout if the
  364. * {hideDelay} is set to a value > 0.
  365. * @param selector the jquery selector of the element to show/hide.
  366. * @param show a {boolean} that indicates if the element should be shown or
  367. * hidden
  368. * @param hideDelay the value in milliseconds to wait before hiding the
  369. * element
  370. */
  371. animateShowElement(selector, show, hideDelay) {
  372. if(show) {
  373. if (!selector.is(":visible"))
  374. selector.css("display", "inline-block");
  375. selector.fadeIn(300,
  376. () => {selector.css({opacity: 1});}
  377. );
  378. if (hideDelay && hideDelay > 0)
  379. setTimeout(
  380. function () {
  381. selector.fadeOut(300,
  382. () => {selector.css({opacity: 0});}
  383. );
  384. }, hideDelay);
  385. }
  386. else {
  387. selector.fadeOut(300,
  388. () => {selector.css({opacity: 0});}
  389. );
  390. }
  391. },
  392. /**
  393. * Parses the given cssValue as an Integer. If the value is not a number
  394. * we return 0 instead of NaN.
  395. * @param cssValue the string value we obtain when querying css properties
  396. */
  397. parseCssInt(cssValue) {
  398. return parseInt(cssValue) || 0;
  399. },
  400. /**
  401. * Adds href value to 'a' link jquery object. If link value is null,
  402. * undefined or empty string, disables the link.
  403. * @param {object} aLinkElement the jquery object
  404. * @param {string} link the link value
  405. */
  406. setLinkHref(aLinkElement, link) {
  407. if (link) {
  408. aLinkElement.attr('href', link);
  409. } else {
  410. aLinkElement.css({
  411. "pointer-events": "none",
  412. "cursor": "default"
  413. });
  414. }
  415. },
  416. /**
  417. * Returns font size for indicators according to current
  418. * height of thumbnail
  419. * @param {Number} [thumbnailHeight] - current height of thumbnail
  420. * @returns {Number} - font size for current height
  421. */
  422. getIndicatorFontSize(thumbnailHeight) {
  423. const height = typeof thumbnailHeight === 'undefined'
  424. ? $('#localVideoContainer').height() : thumbnailHeight;
  425. const { SMALL, MEDIUM } = ThumbnailSizes;
  426. let fontSize = IndicatorFontSizes.NORMAL;
  427. if (height <= SMALL) {
  428. fontSize = IndicatorFontSizes.SMALL;
  429. } else if (height > SMALL && height <= MEDIUM) {
  430. fontSize = IndicatorFontSizes.MEDIUM;
  431. }
  432. return fontSize;
  433. }
  434. };
  435. export default UIUtil;