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

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