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

UIUtil.js 9.8KB

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