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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* global $, APP, config, 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. element.setAttribute('data-tooltip', TOOLTIP_POSITIONS[position]);
  118. element.setAttribute('data-i18n', '[content]' + key);
  119. APP.translation.translateElement($(element));
  120. },
  121. /**
  122. * Removes the tooltip to the given element.
  123. *
  124. * @param element the element to remove the tooltip from
  125. */
  126. removeTooltip: function (element) {
  127. element.removeAttribute('data-tooltip', '');
  128. element.removeAttribute('data-i18n','');
  129. element.removeAttribute('content','');
  130. },
  131. /**
  132. * Internal util function for generating tooltip title.
  133. *
  134. * @param element
  135. * @returns {string|*}
  136. * @private
  137. */
  138. _getTooltipText: function (element) {
  139. let title = element.getAttribute('content');
  140. let shortcut = element.getAttribute('shortcut');
  141. if(shortcut) {
  142. let shortcutString = KeyboardShortcut.getShortcutTooltip(shortcut);
  143. title += ` ${shortcutString}`;
  144. }
  145. return title;
  146. },
  147. /**
  148. * Inserts given child element as the first one into the container.
  149. * @param container the container to which new child element will be added
  150. * @param newChild the new element that will be inserted into the container
  151. */
  152. prependChild: function (container, newChild) {
  153. var firstChild = container.childNodes[0];
  154. if (firstChild) {
  155. container.insertBefore(newChild, firstChild);
  156. } else {
  157. container.appendChild(newChild);
  158. }
  159. },
  160. /**
  161. * Indicates if a toolbar button is enabled.
  162. * @param name the name of the setting section as defined in
  163. * interface_config.js and Toolbar.js
  164. * @returns {boolean} {true} to indicate that the given toolbar button
  165. * is enabled, {false} - otherwise
  166. */
  167. isButtonEnabled: function (name) {
  168. return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1
  169. || interfaceConfig.MAIN_TOOLBAR_BUTTONS.indexOf(name) !== -1;
  170. },
  171. /**
  172. * Indicates if the setting section is enabled.
  173. *
  174. * @param name the name of the setting section as defined in
  175. * interface_config.js and SettingsMenu.js
  176. * @returns {boolean} {true} to indicate that the given setting section
  177. * is enabled, {false} - otherwise
  178. */
  179. isSettingEnabled: function (name) {
  180. return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
  181. },
  182. /**
  183. * Shows the element given by id.
  184. *
  185. * @param {String} the identifier of the element to show
  186. */
  187. showElement(id) {
  188. if ($("#"+id).hasClass("hide"))
  189. $("#"+id).removeClass("hide");
  190. $("#"+id).addClass("show");
  191. },
  192. /**
  193. * Hides the element given by id.
  194. *
  195. * @param {String} the identifier of the element to hide
  196. */
  197. hideElement(id) {
  198. if ($("#"+id).hasClass("show"))
  199. $("#"+id).removeClass("show");
  200. $("#"+id).addClass("hide");
  201. },
  202. hideDisabledButtons: function (mappings) {
  203. var selector = Object.keys(mappings)
  204. .map(function (buttonName) {
  205. return UIUtil.isButtonEnabled(buttonName)
  206. ? null : "#" + mappings[buttonName].id; })
  207. .filter(function (item) { return item; })
  208. .join(',');
  209. $(selector).hide();
  210. },
  211. redirect (url) {
  212. window.location.href = url;
  213. },
  214. isFullScreen () {
  215. return document.fullScreen
  216. || document.mozFullScreen
  217. || document.webkitIsFullScreen;
  218. },
  219. /**
  220. * Create html attributes string out of object properties.
  221. * @param {Object} attrs object with properties
  222. * @returns {String} string of html element attributes
  223. */
  224. attrsToString: function (attrs) {
  225. return Object.keys(attrs).map(
  226. key => ` ${key}="${attrs[key]}"`
  227. ).join(' ');
  228. },
  229. /**
  230. * Checks if the given DOM element is currently visible. The offsetParent
  231. * will be null if the "display" property of the element or any of its
  232. * parent containers is set to "none". This method will NOT check the
  233. * visibility property though.
  234. * @param {el} The DOM element we'd like to check for visibility
  235. */
  236. isVisible(el) {
  237. return (el.offsetParent !== null);
  238. },
  239. /**
  240. * Shows / hides the element given by {selector} and sets a timeout if the
  241. * {hideDelay} is set to a value > 0.
  242. * @param selector the jquery selector of the element to show/hide.
  243. * @param show a {boolean} that indicates if the element should be shown or
  244. * hidden
  245. * @param hideDelay the value in milliseconds to wait before hiding the
  246. * element
  247. */
  248. animateShowElement(selector, show, hideDelay) {
  249. if(show) {
  250. if (!selector.is(":visible"))
  251. selector.css("display", "inline-block");
  252. selector.fadeIn(300,
  253. () => {selector.css({opacity: 1});}
  254. );
  255. if (hideDelay && hideDelay > 0)
  256. setTimeout(
  257. function () {
  258. selector.fadeOut(300,
  259. () => {selector.css({opacity: 0});}
  260. );
  261. }, hideDelay);
  262. }
  263. else {
  264. selector.fadeOut(300,
  265. () => {selector.css({opacity: 0});}
  266. );
  267. }
  268. },
  269. /**
  270. * Parses the given cssValue as an Integer. If the value is not a number
  271. * we return 0 instead of NaN.
  272. * @param cssValue the string value we obtain when querying css properties
  273. */
  274. parseCssInt(cssValue) {
  275. return parseInt(cssValue) || 0;
  276. },
  277. /**
  278. * Adds href value to 'a' link jquery object. If link value is null,
  279. * undefined or empty string, disables the link.
  280. * @param {object} aLinkElement the jquery object
  281. * @param {string} link the link value
  282. */
  283. setLinkHref(aLinkElement, link) {
  284. if (link) {
  285. aLinkElement.attr('href', link);
  286. } else {
  287. aLinkElement.css({
  288. "pointer-events": "none",
  289. "cursor": "default"
  290. });
  291. }
  292. }
  293. };
  294. export default UIUtil;