您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UIUtil.js 8.8KB

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