Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UIUtil.js 10.0KB

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