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.6KB

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