|
1 | 1 | import type { ReactiveController } from 'lit'; |
| 2 | +import service from './tooltip-service.js'; |
2 | 3 | import type IgcTooltipComponent from './tooltip.js'; |
3 | 4 |
|
4 | 5 | type TooltipAnchor = Element | null | undefined; |
5 | | -type TooltipTriggers = { |
6 | | - show: string[]; |
7 | | - hide: string[]; |
| 6 | + |
| 7 | +type TooltipCallbacks = { |
| 8 | + onShow: (event?: Event) => unknown; |
| 9 | + onHide: (event?: Event) => unknown; |
8 | 10 | }; |
9 | 11 |
|
10 | 12 | class TooltipController implements ReactiveController { |
11 | 13 | private readonly _tooltip: IgcTooltipComponent; |
12 | | - private _showTriggers: string[] = []; |
13 | | - private _hideTriggers: string[] = []; |
| 14 | + private _anchor: TooltipAnchor; |
14 | 15 |
|
15 | | - constructor(tooltip: IgcTooltipComponent) { |
16 | | - this._tooltip = tooltip; |
17 | | - this._tooltip.addController(this); |
| 16 | + private _options!: TooltipCallbacks; |
| 17 | + private _showTriggers = new Set(['pointerenter']); |
| 18 | + private _hideTriggers = new Set(['pointerleave']); |
| 19 | + |
| 20 | + /** |
| 21 | + * Returns the current tooltip anchor target if any. |
| 22 | + */ |
| 23 | + public get anchor(): TooltipAnchor { |
| 24 | + return this._anchor; |
18 | 25 | } |
19 | 26 |
|
20 | 27 | /** |
21 | | - * Sets the current collections of show/hide triggers on the given anchor for the tooltip. |
22 | | - * Removes any previously set triggers. |
| 28 | + * Removes all triggers from the previous `anchor` target and rebinds the current |
| 29 | + * sets back to the new value if it exists. |
23 | 30 | */ |
24 | | - public set(anchor: TooltipAnchor, triggers: TooltipTriggers): void { |
25 | | - if (!anchor) { |
26 | | - return; |
| 31 | + public set anchor(value: TooltipAnchor) { |
| 32 | + this._dispose(); |
| 33 | + this._anchor = value; |
| 34 | + |
| 35 | + for (const each of this._showTriggers) { |
| 36 | + this._anchor?.addEventListener(each, this); |
27 | 37 | } |
28 | 38 |
|
29 | | - const { show, hide } = triggers; |
30 | | - this._showTriggers = show; |
31 | | - this._hideTriggers = hide; |
| 39 | + for (const each of this._hideTriggers) { |
| 40 | + this._anchor?.addEventListener(each, this); |
| 41 | + } |
| 42 | + } |
32 | 43 |
|
33 | | - this.remove(anchor); |
| 44 | + /** |
| 45 | + * Returns the current set of hide triggers as a comma-separated string. |
| 46 | + */ |
| 47 | + public get hideTriggers(): string { |
| 48 | + return Array.from(this._hideTriggers).join(); |
| 49 | + } |
34 | 50 |
|
35 | | - for (const trigger of show) { |
36 | | - anchor.addEventListener(trigger, this._tooltip[showOnTrigger]); |
| 51 | + /** |
| 52 | + * Sets a new set of hide triggers from a comma-separated string. |
| 53 | + * |
| 54 | + * @remarks |
| 55 | + * If the tooltip already has an `anchor` bound it will remove the old |
| 56 | + * set of triggers from it and rebind it with the new one. |
| 57 | + */ |
| 58 | + public set hideTriggers(value: string) { |
| 59 | + const triggers = parseTriggers(value); |
| 60 | + |
| 61 | + if (this._anchor) { |
| 62 | + this._toggleTriggers(this._hideTriggers, triggers); |
37 | 63 | } |
38 | 64 |
|
39 | | - for (const trigger of hide) { |
40 | | - anchor.addEventListener(trigger, this._tooltip[hideOnTrigger]); |
| 65 | + this._hideTriggers = triggers; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the current set of show triggers as a comma-separated string. |
| 70 | + */ |
| 71 | + public get showTriggers(): string { |
| 72 | + return Array.from(this._showTriggers).join(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sets a new set of show triggers from a comma-separated string. |
| 77 | + * |
| 78 | + * @remarks |
| 79 | + * If the tooltip already has an `anchor` bound it will remove the old |
| 80 | + * set of triggers from it and rebind it with the new one. |
| 81 | + */ |
| 82 | + public set showTriggers(value: string) { |
| 83 | + const triggers = parseTriggers(value); |
| 84 | + |
| 85 | + if (this._anchor) { |
| 86 | + this._toggleTriggers(this._showTriggers, triggers); |
41 | 87 | } |
| 88 | + |
| 89 | + this._showTriggers = triggers; |
| 90 | + } |
| 91 | + |
| 92 | + constructor(tooltip: IgcTooltipComponent, options: TooltipCallbacks) { |
| 93 | + this._tooltip = tooltip; |
| 94 | + this._options = options; |
| 95 | + this._tooltip.addController(this); |
42 | 96 | } |
43 | 97 |
|
44 | | - /** Removes all tooltip trigger events from the given anchor */ |
45 | | - public remove(anchor?: TooltipAnchor): void { |
46 | | - if (!anchor) { |
47 | | - return; |
| 98 | + private _toggleTriggers(previous: Set<string>, current: Set<string>): void { |
| 99 | + for (const each of previous) { |
| 100 | + this._anchor?.removeEventListener(each, this); |
| 101 | + } |
| 102 | + |
| 103 | + for (const each of current) { |
| 104 | + this._anchor?.addEventListener(each, this); |
48 | 105 | } |
| 106 | + } |
49 | 107 |
|
50 | | - for (const trigger of this._showTriggers) { |
51 | | - anchor.removeEventListener(trigger, this._tooltip[showOnTrigger]); |
| 108 | + private _dispose(): void { |
| 109 | + for (const each of this._showTriggers) { |
| 110 | + this._anchor?.removeEventListener(each, this); |
52 | 111 | } |
53 | 112 |
|
54 | | - for (const trigger of this._hideTriggers) { |
55 | | - anchor.removeEventListener(trigger, this._tooltip[hideOnTrigger]); |
| 113 | + for (const each of this._hideTriggers) { |
| 114 | + this._anchor?.removeEventListener(each, this); |
56 | 115 | } |
| 116 | + |
| 117 | + this._anchor = null; |
57 | 118 | } |
58 | 119 |
|
59 | 120 | /** @internal */ |
60 | 121 | public hostConnected(): void { |
61 | | - this._tooltip.addEventListener( |
62 | | - 'pointerenter', |
63 | | - this._tooltip[showOnTrigger] |
64 | | - ); |
65 | | - this._tooltip.addEventListener( |
66 | | - 'pointerleave', |
67 | | - this._tooltip[hideOnTrigger] |
68 | | - ); |
| 122 | + this._tooltip.addEventListener('pointerenter', this); |
| 123 | + this._tooltip.addEventListener('pointerleave', this); |
69 | 124 | } |
70 | 125 |
|
71 | 126 | /** @internal */ |
72 | 127 | public hostDisconnected(): void { |
73 | | - this._tooltip.removeEventListener( |
74 | | - 'pointerenter', |
75 | | - this._tooltip[showOnTrigger] |
76 | | - ); |
77 | | - this._tooltip.removeEventListener( |
78 | | - 'pointerleave', |
79 | | - this._tooltip[hideOnTrigger] |
80 | | - ); |
| 128 | + // console.log('disconnected callback'); |
| 129 | + this._tooltip.hide(); |
| 130 | + service.remove(this._tooltip); |
| 131 | + this._tooltip.removeEventListener('pointerenter', this); |
| 132 | + this._tooltip.removeEventListener('pointerleave', this); |
| 133 | + } |
| 134 | + |
| 135 | + /** @internal */ |
| 136 | + public handleEvent(event: Event): void { |
| 137 | + // Tooltip handlers |
| 138 | + if (event.target === this._tooltip) { |
| 139 | + switch (event.type) { |
| 140 | + case 'pointerenter': |
| 141 | + this._options.onShow.call(this._tooltip, event); |
| 142 | + break; |
| 143 | + case 'pointerleave': |
| 144 | + this._options.onHide.call(this._tooltip, event); |
| 145 | + break; |
| 146 | + default: |
| 147 | + return; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Anchor handlers |
| 152 | + if (event.target === this._anchor) { |
| 153 | + if (this._showTriggers.has(event.type)) { |
| 154 | + this._options.onShow.call(this._tooltip, event); |
| 155 | + } |
| 156 | + |
| 157 | + if (this._hideTriggers.has(event.type)) { |
| 158 | + this._options.onHide.call(this._tooltip, event); |
| 159 | + } |
| 160 | + } |
81 | 161 | } |
82 | 162 | } |
83 | 163 |
|
84 | | -export const showOnTrigger = Symbol(); |
85 | | -export const hideOnTrigger = Symbol(); |
| 164 | +function parseTriggers(string: string): Set<string> { |
| 165 | + return new Set((string ?? '').split(',').map((part) => part.trim())); |
| 166 | +} |
86 | 167 |
|
87 | 168 | export function addTooltipController( |
88 | | - host: IgcTooltipComponent |
| 169 | + host: IgcTooltipComponent, |
| 170 | + options: TooltipCallbacks |
89 | 171 | ): TooltipController { |
90 | | - return new TooltipController(host); |
| 172 | + return new TooltipController(host, options); |
91 | 173 | } |
0 commit comments