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

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