|
| 1 | +// Tooltip element |
| 2 | + |
| 3 | +import { Tooltip as TooltipBS } from 'bootstrap'; |
| 4 | +import View from '../view'; |
| 5 | + |
| 6 | +// //////////////////////////////////////////////////////////////////////////// |
| 7 | +// CONSTANTS |
| 8 | + |
| 9 | +const EVENT_ROOT = 'tooltip'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tooltip show event, which is emitted when a element is about to be made visible. |
| 13 | + * |
| 14 | + * @event Tooltip#tooltip:show |
| 15 | + * @arg {Tooltip} sender - The view that emitted the event. |
| 16 | + */ |
| 17 | +const EVENT_SHOW = `${EVENT_ROOT}:show`; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tooltip hide event, which is emitted when a element has been hidden. |
| 21 | + * |
| 22 | + * @event Tooltip#tooltip:hide |
| 23 | + * @arg {Tooltip} sender - The view that emitted the event. |
| 24 | + */ |
| 25 | +const EVENT_HIDE = `${EVENT_ROOT}:hide`; |
| 26 | + |
| 27 | +// //////////////////////////////////////////////////////////////////////////// |
| 28 | + |
| 29 | +/** |
| 30 | + * @class Tooltip |
| 31 | + * @implements {View} |
| 32 | + * @classdesc This class is constructed with a DOM element and |
| 33 | + * controls an existing |
| 34 | + * [Bootstrap Tooltip]{@link https://getbootstrap.com/docs/5.0/components/tooltips/} |
| 35 | + * and is generally used for showing additional information above an existing element. |
| 36 | + * |
| 37 | + * @arg {Node} node - The node to attach the view to. Throws an error if the node |
| 38 | + * is not provided. Include the tooltip text in the "title" attribute of the view. |
| 39 | + */ |
| 40 | +export default class Tooltip extends View { |
| 41 | + constructor(node) { |
| 42 | + super(node); |
| 43 | + this.$tooltip = new TooltipBS(node, { |
| 44 | + boundary: document.body, |
| 45 | + }); |
| 46 | + node.addEventListener('show.bs.tooltip', () => { |
| 47 | + this.dispatchEvent(EVENT_SHOW, this); |
| 48 | + }); |
| 49 | + node.addEventListener('hidden.bs.tooltip', () => { |
| 50 | + this.dispatchEvent(EVENT_HIDE, this); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Make the tooltip view visible |
| 56 | + * @fires Offcanvas#offcanvas:show |
| 57 | + * @throws Error |
| 58 | + */ |
| 59 | + show() { |
| 60 | + this.$tooltip.show(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Make the tooltip view hidden. |
| 65 | + * @fires Offcanvas#offcanvas:hide |
| 66 | + */ |
| 67 | + hide() { |
| 68 | + this.$tooltip.hide(); |
| 69 | + } |
| 70 | +} |
0 commit comments