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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* global $, APP, config, AJS, interfaceConfig */
  2. import KeyboardShortcut from '../../keyboardshortcut/keyboardshortcut';
  3. /**
  4. * Created by hristo on 12/22/14.
  5. */
  6. var UIUtil = {
  7. /**
  8. * Returns the available video width.
  9. */
  10. getAvailableVideoWidth: function () {
  11. let rightPanelWidth = 0;
  12. return window.innerWidth - rightPanelWidth;
  13. },
  14. /**
  15. * Changes the style class of the element given by id.
  16. */
  17. buttonClick: function(id, classname) {
  18. // add the class to the clicked element
  19. $("#" + id).toggleClass(classname);
  20. },
  21. /**
  22. * Returns the text width for the given element.
  23. *
  24. * @param el the element
  25. */
  26. getTextWidth: function (el) {
  27. return (el.clientWidth + 1);
  28. },
  29. /**
  30. * Returns the text height for the given element.
  31. *
  32. * @param el the element
  33. */
  34. getTextHeight: function (el) {
  35. return (el.clientHeight + 1);
  36. },
  37. /**
  38. * Plays the sound given by id.
  39. *
  40. * @param id the identifier of the audio element.
  41. */
  42. playSoundNotification: function (id) {
  43. document.getElementById(id).play();
  44. },
  45. /**
  46. * Escapes the given text.
  47. */
  48. escapeHtml: function (unsafeText) {
  49. return $('<div/>').text(unsafeText).html();
  50. },
  51. /**
  52. * Unescapes the given text.
  53. *
  54. * @param {string} safe string which contains escaped html
  55. * @returns {string} unescaped html string.
  56. */
  57. unescapeHtml: function (safe) {
  58. return $('<div />').html(safe).text();
  59. },
  60. imageToGrayScale: function (canvas) {
  61. var context = canvas.getContext('2d');
  62. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  63. var pixels = imgData.data;
  64. for (var i = 0, n = pixels.length; i < n; i += 4) {
  65. var grayscale
  66. = pixels[i] * 0.3 + pixels[i+1] * 0.59 + pixels[i+2] * 0.11;
  67. pixels[i ] = grayscale; // red
  68. pixels[i+1] = grayscale; // green
  69. pixels[i+2] = grayscale; // blue
  70. // pixels[i+3] is alpha
  71. }
  72. // redraw the image in black & white
  73. context.putImageData(imgData, 0, 0);
  74. },
  75. /**
  76. * Sets global handler for all tooltips. Once this function is invoked
  77. * all you need to create new tooltip is to update DOM node with
  78. * appropriate class (ex. "tooltip-n") and "content" attribute.
  79. */
  80. activateTooltips: function () {
  81. function getTitle () {
  82. return this.getAttribute('content');
  83. }
  84. function getGravity () {
  85. return this.getAttribute('data-tooltip');
  86. }
  87. AJS.$('[data-tooltip]').tooltip({
  88. gravity: getGravity,
  89. title: getTitle,
  90. html: true, // handle multiline tooltips
  91. // two options to prevent tooltips from being stuck:
  92. live: true, // attach listener to document element
  93. hoverable: false // make custom tooltips to behave like native
  94. });
  95. },
  96. /**
  97. * Sets the tooltip to the given element.
  98. *
  99. * @param element the element to set the tooltip to
  100. * @param key the tooltip data-i18n key
  101. * @param position the position of the tooltip in relation to the element
  102. */
  103. setTooltip: function (element, key, position) {
  104. let positions = {
  105. 'top': 's',
  106. 'top-left': 'se',
  107. 'left': 'e',
  108. 'bottom-left': 'ne',
  109. 'bottom': 'n',
  110. 'bottom-right': 'nw',
  111. 'right': 'w',
  112. 'top-right': 'sw'
  113. };
  114. $(element).each(function () {
  115. var el = $(this);
  116. el.attr("data-i18n", "[content]" + key)
  117. .attr('data-tooltip', positions[position]);
  118. APP.translation.translateElement(el);
  119. });
  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. AJS.$(element).tooltip('destroy');
  128. },
  129. /**
  130. * Internal util function for generating tooltip title.
  131. *
  132. * @param element
  133. * @returns {string|*}
  134. * @private
  135. */
  136. _getTooltipText: function (element) {
  137. let title = element.getAttribute('content');
  138. let shortcut = element.getAttribute('shortcut');
  139. if(shortcut) {
  140. let shortcutString = KeyboardShortcut.getShortcutTooltip(shortcut);
  141. title += ` ${shortcutString}`;
  142. }
  143. return title;
  144. },
  145. /**
  146. * Inserts given child element as the first one into the container.
  147. * @param container the container to which new child element will be added
  148. * @param newChild the new element that will be inserted into the container
  149. */
  150. prependChild: function (container, newChild) {
  151. var firstChild = container.childNodes[0];
  152. if (firstChild) {
  153. container.insertBefore(newChild, firstChild);
  154. } else {
  155. container.appendChild(newChild);
  156. }
  157. },
  158. /**
  159. * Indicates if a toolbar button is enabled.
  160. * @param name the name of the setting section as defined in
  161. * interface_config.js and Toolbar.js
  162. * @returns {boolean} {true} to indicate that the given toolbar button
  163. * is enabled, {false} - otherwise
  164. */
  165. isButtonEnabled: function (name) {
  166. return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1
  167. || interfaceConfig.MAIN_TOOLBAR_BUTTONS.indexOf(name) !== -1;
  168. },
  169. /**
  170. * Indicates if the setting section is enabled.
  171. *
  172. * @param name the name of the setting section as defined in
  173. * interface_config.js and SettingsMenu.js
  174. * @returns {boolean} {true} to indicate that the given setting section
  175. * is enabled, {false} - otherwise
  176. */
  177. isSettingEnabled: function (name) {
  178. return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
  179. },
  180. /**
  181. * Shows the element given by id.
  182. *
  183. * @param {String} the identifier of the element to show
  184. */
  185. showElement(id) {
  186. if ($("#"+id).hasClass("hide"))
  187. $("#"+id).removeClass("hide");
  188. $("#"+id).addClass("show");
  189. },
  190. /**
  191. * Hides the element given by id.
  192. *
  193. * @param {String} the identifier of the element to hide
  194. */
  195. hideElement(id) {
  196. if ($("#"+id).hasClass("show"))
  197. $("#"+id).removeClass("show");
  198. $("#"+id).addClass("hide");
  199. },
  200. hideDisabledButtons: function (mappings) {
  201. var selector = Object.keys(mappings)
  202. .map(function (buttonName) {
  203. return UIUtil.isButtonEnabled(buttonName)
  204. ? null : "#" + mappings[buttonName].id; })
  205. .filter(function (item) { return item; })
  206. .join(',');
  207. $(selector).hide();
  208. },
  209. redirect (url) {
  210. window.location.href = url;
  211. },
  212. isFullScreen () {
  213. return document.fullScreen
  214. || document.mozFullScreen
  215. || document.webkitIsFullScreen;
  216. },
  217. /**
  218. * Create html attributes string out of object properties.
  219. * @param {Object} attrs object with properties
  220. * @returns {String} string of html element attributes
  221. */
  222. attrsToString: function (attrs) {
  223. return Object.keys(attrs).map(
  224. key => ` ${key}="${attrs[key]}"`
  225. ).join(' ');
  226. },
  227. /**
  228. * Checks if the given DOM element is currently visible. The offsetParent
  229. * will be null if the "display" property of the element or any of its
  230. * parent containers is set to "none". This method will NOT check the
  231. * visibility property though.
  232. * @param {el} The DOM element we'd like to check for visibility
  233. */
  234. isVisible(el) {
  235. return (el.offsetParent !== null);
  236. },
  237. /**
  238. * Shows / hides the element given by {selector} and sets a timeout if the
  239. * {hideDelay} is set to a value > 0.
  240. * @param selector the jquery selector of the element to show/hide.
  241. * @param show a {boolean} that indicates if the element should be shown or
  242. * hidden
  243. * @param hideDelay the value in milliseconds to wait before hiding the
  244. * element
  245. */
  246. animateShowElement(selector, show, hideDelay) {
  247. if(show) {
  248. if (!selector.is(":visible"))
  249. selector.css("display", "inline-block");
  250. selector.fadeIn(300,
  251. () => {selector.css({opacity: 1});}
  252. );
  253. if (hideDelay && hideDelay > 0)
  254. setTimeout(
  255. function () {
  256. selector.fadeOut(300,
  257. () => {selector.css({opacity: 0});}
  258. );
  259. }, hideDelay);
  260. }
  261. else {
  262. selector.fadeOut(300,
  263. () => {selector.css({opacity: 0});}
  264. );
  265. }
  266. },
  267. /**
  268. * Parses the given cssValue as an Integer. If the value is not a number
  269. * we return 0 instead of NaN.
  270. * @param cssValue the string value we obtain when querying css properties
  271. */
  272. parseCssInt(cssValue) {
  273. return parseInt(cssValue) || 0;
  274. },
  275. /**
  276. * Adds href value to 'a' link jquery object. If link value is null,
  277. * undefined or empty string, disables the link.
  278. * @param {object} aLinkElement the jquery object
  279. * @param {string} link the link value
  280. */
  281. setLinkHref(aLinkElement, link) {
  282. if (link) {
  283. aLinkElement.attr('href', link);
  284. } else {
  285. aLinkElement.css({
  286. "pointer-events": "none",
  287. "cursor": "default"
  288. });
  289. }
  290. }
  291. };
  292. export default UIUtil;