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

Tooltip.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* global $, APP, AJS */
  2. /**
  3. * Associates tooltip element position (in the terms of
  4. * {@link UIUtil#setTooltip} which do not look like CSS <tt>position</tt>) with
  5. * AUI tooltip <tt>gravity</tt>.
  6. */
  7. const TOOLTIP_POSITIONS = {
  8. 'bottom': 'n',
  9. 'bottom-left': 'ne',
  10. 'bottom-right': 'nw',
  11. 'left': 'e',
  12. 'right': 'w',
  13. 'top': 's',
  14. 'top-left': 'se',
  15. 'top-right': 'sw'
  16. };
  17. /**
  18. * Sets a global handler for all tooltips. Once invoked, create a new
  19. * tooltip by merely updating a DOM node with the appropriate class (e.g.
  20. * <tt>tooltip-n</tt>) and the attribute <tt>content</tt>.
  21. */
  22. export function activateTooltips() {
  23. AJS.$('[data-tooltip]').tooltip({
  24. gravity() {
  25. return this.getAttribute('data-tooltip');
  26. },
  27. title() {
  28. return this.getAttribute('content');
  29. },
  30. html: true, // Handle multiline tooltips.
  31. // The following two prevent tooltips from being stuck:
  32. hoverable: false, // Make custom tooltips behave like native ones.
  33. live: true // Attach listener to document element.
  34. });
  35. }
  36. /**
  37. * Sets the tooltip to the given element.
  38. *
  39. * @param element the element to set the tooltip to
  40. * @param key the tooltip data-i18n key
  41. * @param position the position of the tooltip in relation to the element
  42. */
  43. export function setTooltip(element, key, position) {
  44. if (element) {
  45. const selector = element.jquery ? element : $(element);
  46. selector.attr('data-tooltip', TOOLTIP_POSITIONS[position]);
  47. selector.attr('data-i18n', `[content]${key}`);
  48. APP.translation.translateElement(selector);
  49. }
  50. }
  51. /**
  52. * Sets the tooltip to the given element, but instead of using translation
  53. * key uses text value.
  54. *
  55. * @param element the element to set the tooltip to
  56. * @param text the tooltip text
  57. * @param position the position of the tooltip in relation to the element
  58. */
  59. export function setTooltipText(element, text, position) {
  60. if (element) {
  61. removeTooltip(element);
  62. element.setAttribute('data-tooltip', TOOLTIP_POSITIONS[position]);
  63. element.setAttribute('content', text);
  64. }
  65. }
  66. /**
  67. * Removes the tooltip to the given element.
  68. *
  69. * @param element the element to remove the tooltip from
  70. */
  71. export function removeTooltip(element) {
  72. element.removeAttribute('data-tooltip', '');
  73. element.removeAttribute('data-i18n','');
  74. element.removeAttribute('content','');
  75. }