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

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