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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 the tooltip to the given element.
  77. *
  78. * @param element the element to set the tooltip to
  79. * @param key the tooltip data-i18n key
  80. * @param position the position of the tooltip in relation to the element
  81. */
  82. setTooltip: function (element, key, position) {
  83. let positions = {
  84. 'top': 's',
  85. 'top-left': 'se',
  86. 'left': 'e',
  87. 'bottom-left': 'ne',
  88. 'bottom': 'n',
  89. 'bottom-right': 'nw',
  90. 'right': 'w',
  91. 'top-right': 'sw'
  92. };
  93. element.setAttribute("data-i18n", "[content]" + key);
  94. APP.translation.translateElement($(element));
  95. AJS.$(element).tooltip({
  96. gravity: positions[position],
  97. title: this._getTooltipText.bind(this, element),
  98. html: true
  99. });
  100. },
  101. /**
  102. * Internal util function for generating tooltip title.
  103. *
  104. * @param element
  105. * @returns {string|*}
  106. * @private
  107. */
  108. _getTooltipText: function (element) {
  109. let title = element.getAttribute('content');
  110. let shortcut = element.getAttribute('shortcut');
  111. if(shortcut) {
  112. let shortcutString = KeyboardShortcut.getShortcutTooltip(shortcut);
  113. title += ` ${shortcutString}`;
  114. }
  115. return title;
  116. },
  117. /**
  118. * Inserts given child element as the first one into the container.
  119. * @param container the container to which new child element will be added
  120. * @param newChild the new element that will be inserted into the container
  121. */
  122. prependChild: function (container, newChild) {
  123. var firstChild = container.childNodes[0];
  124. if (firstChild) {
  125. container.insertBefore(newChild, firstChild);
  126. } else {
  127. container.appendChild(newChild);
  128. }
  129. },
  130. /**
  131. * Indicates if a toolbar button is enabled.
  132. * @param name the name of the setting section as defined in
  133. * interface_config.js and Toolbar.js
  134. * @returns {boolean} {true} to indicate that the given toolbar button
  135. * is enabled, {false} - otherwise
  136. */
  137. isButtonEnabled: function (name) {
  138. return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1
  139. || interfaceConfig.MAIN_TOOLBAR_BUTTONS.indexOf(name) !== -1;
  140. },
  141. /**
  142. * Indicates if the setting section is enabled.
  143. *
  144. * @param name the name of the setting section as defined in
  145. * interface_config.js and SettingsMenu.js
  146. * @returns {boolean} {true} to indicate that the given setting section
  147. * is enabled, {false} - otherwise
  148. */
  149. isSettingEnabled: function (name) {
  150. return interfaceConfig.SETTINGS_SECTIONS.indexOf(name) !== -1;
  151. },
  152. /**
  153. * Shows the element given by id.
  154. *
  155. * @param {String} the identifier of the element to show
  156. */
  157. showElement(id) {
  158. if ($("#"+id).hasClass("hide"))
  159. $("#"+id).removeClass("hide");
  160. $("#"+id).addClass("show");
  161. },
  162. /**
  163. * Hides the element given by id.
  164. *
  165. * @param {String} the identifier of the element to hide
  166. */
  167. hideElement(id) {
  168. if ($("#"+id).hasClass("show"))
  169. $("#"+id).removeClass("show");
  170. $("#"+id).addClass("hide");
  171. },
  172. hideDisabledButtons: function (mappings) {
  173. var selector = Object.keys(mappings)
  174. .map(function (buttonName) {
  175. return UIUtil.isButtonEnabled(buttonName)
  176. ? null : "#" + mappings[buttonName].id; })
  177. .filter(function (item) { return item; })
  178. .join(',');
  179. $(selector).hide();
  180. },
  181. redirect (url) {
  182. window.location.href = url;
  183. },
  184. isFullScreen () {
  185. return document.fullScreen
  186. || document.mozFullScreen
  187. || document.webkitIsFullScreen;
  188. },
  189. /**
  190. * Create html attributes string out of object properties.
  191. * @param {Object} attrs object with properties
  192. * @returns {String} string of html element attributes
  193. */
  194. attrsToString: function (attrs) {
  195. return Object.keys(attrs).map(
  196. key => ` ${key}="${attrs[key]}"`
  197. ).join(' ');
  198. },
  199. /**
  200. * Checks if the given DOM element is currently visible. The offsetParent
  201. * will be null if the "display" property of the element or any of its
  202. * parent containers is set to "none". This method will NOT check the
  203. * visibility property though.
  204. * @param {el} The DOM element we'd like to check for visibility
  205. */
  206. isVisible(el) {
  207. return (el.offsetParent !== null);
  208. },
  209. /**
  210. * Shows / hides the element given by {selector} and sets a timeout if the
  211. * {hideDelay} is set to a value > 0.
  212. * @param selector the jquery selector of the element to show/hide.
  213. * @param show a {boolean} that indicates if the element should be shown or
  214. * hidden
  215. * @param hideDelay the value in milliseconds to wait before hiding the
  216. * element
  217. */
  218. animateShowElement(selector, show, hideDelay) {
  219. if(show) {
  220. if (!selector.is(":visible"))
  221. selector.css("display", "inline-block");
  222. selector.fadeIn(300,
  223. () => {selector.css({opacity: 1});}
  224. );
  225. if (hideDelay && hideDelay > 0)
  226. setTimeout(
  227. function () {
  228. selector.fadeOut(300,
  229. () => {selector.css({opacity: 0});}
  230. );
  231. }, hideDelay);
  232. }
  233. else {
  234. selector.fadeOut(300,
  235. () => {selector.css({opacity: 0});}
  236. );
  237. }
  238. },
  239. /**
  240. * Parses the given cssValue as an Integer. If the value is not a number
  241. * we return 0 instead of NaN.
  242. * @param cssValue the string value we obtain when querying css properties
  243. */
  244. parseCssInt(cssValue) {
  245. return parseInt(cssValue) || 0;
  246. },
  247. /**
  248. * Adds href value to 'a' link jquery object. If link value is null,
  249. * undefined or empty string, disables the link.
  250. * @param {object} aLinkElement the jquery object
  251. * @param {string} link the link value
  252. */
  253. setLinkHref(aLinkElement, link) {
  254. if (link) {
  255. aLinkElement.attr('href', link);
  256. } else {
  257. aLinkElement.css({
  258. "pointer-events": "none",
  259. "cursor": "default"
  260. });
  261. }
  262. }
  263. };
  264. export default UIUtil;