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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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: function () {
  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: function (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: function (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: function (id) {
  82. document.getElementById(id).play();
  83. },
  84. /**
  85. * Escapes the given text.
  86. */
  87. escapeHtml: function (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: function (safe) {
  97. return $('<div />').html(safe).text();
  98. },
  99. imageToGrayScale: function (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: function (element, key, position) {
  141. if (element !== null) {
  142. element.setAttribute('data-tooltip', TOOLTIP_POSITIONS[position]);
  143. element.setAttribute('data-i18n', '[content]' + key);
  144. APP.translation.translateElement($(element));
  145. }
  146. },
  147. /**
  148. * Removes the tooltip to the given element.
  149. *
  150. * @param element the element to remove the tooltip from
  151. */
  152. removeTooltip: function (element) {
  153. element.removeAttribute('data-tooltip', '');
  154. element.removeAttribute('data-i18n','');
  155. element.removeAttribute('content','');
  156. },
  157. /**
  158. * Internal util function for generating tooltip title.
  159. *
  160. * @param element
  161. * @returns {string|*}
  162. * @private
  163. */
  164. _getTooltipText: function (element) {
  165. let title = element.getAttribute('content');
  166. let shortcut = element.getAttribute('shortcut');
  167. if(shortcut) {
  168. let shortcutString = KeyboardShortcut.getShortcutTooltip(shortcut);
  169. title += ` ${shortcutString}`;
  170. }
  171. return title;
  172. },
  173. /**
  174. * Inserts given child element as the first one into the container.
  175. * @param container the container to which new child element will be added
  176. * @param newChild the new element that will be inserted into the container
  177. */
  178. prependChild: function (container, newChild) {
  179. var firstChild = container.childNodes[0];
  180. if (firstChild) {
  181. container.insertBefore(newChild, firstChild);
  182. } else {
  183. container.appendChild(newChild);
  184. }
  185. },
  186. /**
  187. * Indicates if a toolbar button is enabled.
  188. * @param name the name of the setting section as defined in
  189. * interface_config.js and Toolbar.js
  190. * @returns {boolean} {true} to indicate that the given toolbar button
  191. * is enabled, {false} - otherwise
  192. */
  193. isButtonEnabled: function (name) {
  194. return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1
  195. || interfaceConfig.MAIN_TOOLBAR_BUTTONS.indexOf(name) !== -1;
  196. },
  197. /**
  198. * Indicates if the setting section is enabled.
  199. *
  200. * @param name the name of the setting section as defined in
  201. * interface_config.js and SettingsMenu.js
  202. * @returns {boolean} {true} to indicate that the given setting section
  203. * is enabled, {false} - otherwise
  204. */
  205. isSettingEnabled: function (name) {
  206. return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
  207. },
  208. /**
  209. * Indicates if Authentication Section should be shown
  210. *
  211. * @returns {boolean}
  212. */
  213. isAuthenticationEnabled: function() {
  214. return interfaceConfig.AUTHENTICATION_ENABLE;
  215. },
  216. /**
  217. * Shows the element given by id.
  218. *
  219. * @param {String} the identifier of the element to show
  220. */
  221. showElement(id) {
  222. let element;
  223. if (id instanceof HTMLElement) {
  224. element = id;
  225. } else {
  226. element = document.getElementById(id);
  227. }
  228. if (!element) {
  229. return;
  230. }
  231. if(element.classList.contains('hide')) {
  232. element.classList.remove('hide');
  233. }
  234. let type = this._getElementDefaultDisplay(element.tagName);
  235. let className = SHOW_CLASSES[type];
  236. element.classList.add(className);
  237. },
  238. /**
  239. * Shows or hides the element given (optionally by id).
  240. *
  241. * @param {string|HTMLElement} idOrElement the identifier or the element
  242. * to show/hide
  243. * @param {boolean} show <tt>true</tt> to show or <tt>false</tt> to hide
  244. */
  245. showOrHideElement(idOrElement, show) {
  246. if (show) {
  247. this.showElement(idOrElement);
  248. } else {
  249. this.hideElement(idOrElement);
  250. }
  251. },
  252. /**
  253. * Hides the element given by id.
  254. *
  255. * @param {String} the identifier of the element to hide
  256. */
  257. hideElement(id) {
  258. let element;
  259. if (id instanceof HTMLElement) {
  260. element = id;
  261. } else {
  262. element = document.getElementById(id);
  263. }
  264. if (!element) {
  265. return;
  266. }
  267. let type = this._getElementDefaultDisplay(element.tagName);
  268. let className = SHOW_CLASSES[type];
  269. if(element.classList.contains(className)) {
  270. element.classList.remove(className);
  271. }
  272. element.classList.add('hide');
  273. },
  274. /**
  275. * Returns default display style for the tag
  276. * @param tag
  277. * @returns {*}
  278. * @private
  279. */
  280. _getElementDefaultDisplay(tag) {
  281. let tempElement = document.createElement(tag);
  282. document.body.appendChild(tempElement);
  283. let style = window.getComputedStyle(tempElement).display;
  284. document.body.removeChild(tempElement);
  285. return style;
  286. },
  287. /**
  288. * Shows / hides the element with the given jQuery selector.
  289. *
  290. * @param {jQuery} selector the jQuery selector of the element to show/hide
  291. * @param {boolean} isVisible
  292. */
  293. setVisibility(selector, isVisible) {
  294. if (selector && selector.length > 0) {
  295. selector.css("visibility", isVisible ? "visible" : "hidden");
  296. }
  297. },
  298. hideDisabledButtons: function (mappings) {
  299. var selector = Object.keys(mappings)
  300. .map(function (buttonName) {
  301. return UIUtil.isButtonEnabled(buttonName)
  302. ? null : "#" + mappings[buttonName].id; })
  303. .filter(function (item) { return item; })
  304. .join(',');
  305. $(selector).hide();
  306. },
  307. redirect (url) {
  308. window.location.href = url;
  309. },
  310. /**
  311. * Indicates if we're currently in full screen mode.
  312. *
  313. * @return {boolean} {true} to indicate that we're currently in full screen
  314. * mode, {false} otherwise
  315. */
  316. isFullScreen() {
  317. return document.fullscreenElement
  318. || document.mozFullScreenElement
  319. || document.webkitFullscreenElement
  320. || document.msFullscreenElement;
  321. },
  322. /**
  323. * Exits full screen mode.
  324. * @see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
  325. */
  326. exitFullScreen() {
  327. if (document.exitFullscreen) {
  328. document.exitFullscreen();
  329. } else if (document.msExitFullscreen) {
  330. document.msExitFullscreen();
  331. } else if (document.mozCancelFullScreen) {
  332. document.mozCancelFullScreen();
  333. } else if (document.webkitExitFullscreen) {
  334. document.webkitExitFullscreen();
  335. }
  336. },
  337. /**
  338. * Enter full screen mode.
  339. * @see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
  340. */
  341. enterFullScreen() {
  342. if (document.documentElement.requestFullscreen) {
  343. document.documentElement.requestFullscreen();
  344. } else if (document.documentElement.msRequestFullscreen) {
  345. document.documentElement.msRequestFullscreen();
  346. } else if (document.documentElement.mozRequestFullScreen) {
  347. document.documentElement.mozRequestFullScreen();
  348. } else if (document.documentElement.webkitRequestFullscreen) {
  349. document.documentElement
  350. .webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  351. }
  352. },
  353. /**
  354. * Create html attributes string out of object properties.
  355. * @param {Object} attrs object with properties
  356. * @returns {String} string of html element attributes
  357. */
  358. attrsToString: function (attrs) {
  359. return Object.keys(attrs).map(
  360. key => ` ${key}="${attrs[key]}"`
  361. ).join(' ');
  362. },
  363. /**
  364. * Checks if the given DOM element is currently visible. The offsetParent
  365. * will be null if the "display" property of the element or any of its
  366. * parent containers is set to "none". This method will NOT check the
  367. * visibility property though.
  368. * @param {el} The DOM element we'd like to check for visibility
  369. */
  370. isVisible(el) {
  371. return (el.offsetParent !== null);
  372. },
  373. /**
  374. * Shows / hides the element given by {selector} and sets a timeout if the
  375. * {hideDelay} is set to a value > 0.
  376. * @param selector the jquery selector of the element to show/hide.
  377. * @param show a {boolean} that indicates if the element should be shown or
  378. * hidden
  379. * @param hideDelay the value in milliseconds to wait before hiding the
  380. * element
  381. */
  382. animateShowElement(selector, show, hideDelay) {
  383. if(show) {
  384. if (!selector.is(":visible"))
  385. selector.css("display", "inline-block");
  386. selector.fadeIn(300,
  387. () => {selector.css({opacity: 1});}
  388. );
  389. if (hideDelay && hideDelay > 0)
  390. setTimeout(
  391. function () {
  392. selector.fadeOut(300,
  393. () => {selector.css({opacity: 0});}
  394. );
  395. }, hideDelay);
  396. }
  397. else {
  398. selector.fadeOut(300,
  399. () => {selector.css({opacity: 0});}
  400. );
  401. }
  402. },
  403. /**
  404. * Parses the given cssValue as an Integer. If the value is not a number
  405. * we return 0 instead of NaN.
  406. * @param cssValue the string value we obtain when querying css properties
  407. */
  408. parseCssInt(cssValue) {
  409. return parseInt(cssValue) || 0;
  410. },
  411. /**
  412. * Adds href value to 'a' link jquery object. If link value is null,
  413. * undefined or empty string, disables the link.
  414. * @param {object} aLinkElement the jquery object
  415. * @param {string} link the link value
  416. */
  417. setLinkHref(aLinkElement, link) {
  418. if (link) {
  419. aLinkElement.attr('href', link);
  420. } else {
  421. aLinkElement.css({
  422. "pointer-events": "none",
  423. "cursor": "default"
  424. });
  425. }
  426. },
  427. /**
  428. * Gets an "indicator" span for a video thumbnail.
  429. * If element doesn't exist then creates it and appends
  430. * video span container.
  431. *
  432. * @param {object} opts
  433. * @param opts.indicatorId {String} - identificator of indicator
  434. * @param opts.videoSpanId {String} - identificator of video span
  435. * @param opts.content {String} HTML content of indicator
  436. * @param opts.tooltip {String} - tooltip key for translation
  437. *
  438. * @returns {HTMLSpanElement} indicatorSpan
  439. */
  440. getVideoThumbnailIndicatorSpan(opts = {}) {
  441. let indicatorId = opts.indicatorId;
  442. let videoSpanId = opts.videoSpanId;
  443. let indicators = $(`#${videoSpanId} [id="${indicatorId}"]`);
  444. let indicatorSpan;
  445. if (indicators.length <= 0) {
  446. indicatorSpan = document.createElement('span');
  447. indicatorSpan.className = 'indicator';
  448. indicatorSpan.id = indicatorId;
  449. if(opts.content) {
  450. indicatorSpan.innerHTML = opts.content;
  451. }
  452. if (opts.tooltip) {
  453. this.setTooltip(indicatorSpan, opts.tooltip, "top");
  454. APP.translation.translateElement($(indicatorSpan));
  455. }
  456. this._resizeIndicator(indicatorSpan);
  457. document.getElementById(videoSpanId)
  458. .querySelector('.videocontainer__toptoolbar')
  459. .appendChild(indicatorSpan);
  460. } else {
  461. indicatorSpan = indicators[0];
  462. }
  463. return indicatorSpan;
  464. },
  465. /**
  466. * Resizing indicator element passing via argument
  467. * according to the current thumbnail size
  468. * @param {HTMLElement} indicator - indicator element
  469. * @private
  470. */
  471. _resizeIndicator(indicator) {
  472. let height = $('#localVideoContainer').height();
  473. let fontSize = this.getIndicatorFontSize(height);
  474. $(indicator).css('font-size', fontSize);
  475. },
  476. /**
  477. * Returns font size for indicators according to current
  478. * height of thumbnail
  479. * @param {Number} - height - current height of thumbnail
  480. * @returns {Number} - font size for current height
  481. */
  482. getIndicatorFontSize(height) {
  483. const { SMALL, MEDIUM } = ThumbnailSizes;
  484. let fontSize = IndicatorFontSizes.NORMAL;
  485. if (height <= SMALL) {
  486. fontSize = IndicatorFontSizes.SMALL;
  487. } else if (height > SMALL && height <= MEDIUM) {
  488. fontSize = IndicatorFontSizes.MEDIUM;
  489. }
  490. return fontSize;
  491. }
  492. };
  493. export default UIUtil;