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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. if ($("#"+id).hasClass("hide"))
  199. $("#"+id).removeClass("hide");
  200. $("#"+id).addClass("show");
  201. },
  202. /**
  203. * Hides the element given by id.
  204. *
  205. * @param {String} the identifier of the element to hide
  206. */
  207. hideElement(id) {
  208. if ($("#"+id).hasClass("show"))
  209. $("#"+id).removeClass("show");
  210. $("#"+id).addClass("hide");
  211. },
  212. /**
  213. * Shows / hides the element with the given jQuery selector.
  214. *
  215. * @param {jQuery} selector the jQuery selector of the element to show/hide
  216. * @param {boolean} isVisible
  217. */
  218. setVisibility(selector, isVisible) {
  219. if (selector && selector.length > 0) {
  220. selector.css("visibility", isVisible ? "visible" : "hidden");
  221. }
  222. },
  223. hideDisabledButtons: function (mappings) {
  224. var selector = Object.keys(mappings)
  225. .map(function (buttonName) {
  226. return UIUtil.isButtonEnabled(buttonName)
  227. ? null : "#" + mappings[buttonName].id; })
  228. .filter(function (item) { return item; })
  229. .join(',');
  230. $(selector).hide();
  231. },
  232. redirect (url) {
  233. window.location.href = url;
  234. },
  235. /**
  236. * Indicates if we're currently in full screen mode.
  237. *
  238. * @return {boolean} {true} to indicate that we're currently in full screen
  239. * mode, {false} otherwise
  240. */
  241. isFullScreen() {
  242. return document.fullscreenElement
  243. || document.mozFullScreenElement
  244. || document.webkitFullscreenElement
  245. || document.msFullscreenElement;
  246. },
  247. /**
  248. * Exits full screen mode.
  249. * @see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
  250. */
  251. exitFullScreen() {
  252. if (document.exitFullscreen) {
  253. document.exitFullscreen();
  254. } else if (document.msExitFullscreen) {
  255. document.msExitFullscreen();
  256. } else if (document.mozCancelFullScreen) {
  257. document.mozCancelFullScreen();
  258. } else if (document.webkitExitFullscreen) {
  259. document.webkitExitFullscreen();
  260. }
  261. },
  262. /**
  263. * Enter full screen mode.
  264. * @see https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API
  265. */
  266. enterFullScreen() {
  267. if (document.documentElement.requestFullscreen) {
  268. document.documentElement.requestFullscreen();
  269. } else if (document.documentElement.msRequestFullscreen) {
  270. document.documentElement.msRequestFullscreen();
  271. } else if (document.documentElement.mozRequestFullScreen) {
  272. document.documentElement.mozRequestFullScreen();
  273. } else if (document.documentElement.webkitRequestFullscreen) {
  274. document.documentElement
  275. .webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  276. }
  277. },
  278. /**
  279. * Create html attributes string out of object properties.
  280. * @param {Object} attrs object with properties
  281. * @returns {String} string of html element attributes
  282. */
  283. attrsToString: function (attrs) {
  284. return Object.keys(attrs).map(
  285. key => ` ${key}="${attrs[key]}"`
  286. ).join(' ');
  287. },
  288. /**
  289. * Checks if the given DOM element is currently visible. The offsetParent
  290. * will be null if the "display" property of the element or any of its
  291. * parent containers is set to "none". This method will NOT check the
  292. * visibility property though.
  293. * @param {el} The DOM element we'd like to check for visibility
  294. */
  295. isVisible(el) {
  296. return (el.offsetParent !== null);
  297. },
  298. /**
  299. * Shows / hides the element given by {selector} and sets a timeout if the
  300. * {hideDelay} is set to a value > 0.
  301. * @param selector the jquery selector of the element to show/hide.
  302. * @param show a {boolean} that indicates if the element should be shown or
  303. * hidden
  304. * @param hideDelay the value in milliseconds to wait before hiding the
  305. * element
  306. */
  307. animateShowElement(selector, show, hideDelay) {
  308. if(show) {
  309. if (!selector.is(":visible"))
  310. selector.css("display", "inline-block");
  311. selector.fadeIn(300,
  312. () => {selector.css({opacity: 1});}
  313. );
  314. if (hideDelay && hideDelay > 0)
  315. setTimeout(
  316. function () {
  317. selector.fadeOut(300,
  318. () => {selector.css({opacity: 0});}
  319. );
  320. }, hideDelay);
  321. }
  322. else {
  323. selector.fadeOut(300,
  324. () => {selector.css({opacity: 0});}
  325. );
  326. }
  327. },
  328. /**
  329. * Parses the given cssValue as an Integer. If the value is not a number
  330. * we return 0 instead of NaN.
  331. * @param cssValue the string value we obtain when querying css properties
  332. */
  333. parseCssInt(cssValue) {
  334. return parseInt(cssValue) || 0;
  335. },
  336. /**
  337. * Adds href value to 'a' link jquery object. If link value is null,
  338. * undefined or empty string, disables the link.
  339. * @param {object} aLinkElement the jquery object
  340. * @param {string} link the link value
  341. */
  342. setLinkHref(aLinkElement, link) {
  343. if (link) {
  344. aLinkElement.attr('href', link);
  345. } else {
  346. aLinkElement.css({
  347. "pointer-events": "none",
  348. "cursor": "default"
  349. });
  350. }
  351. }
  352. };
  353. export default UIUtil;