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