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

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