|
| 1 | +/** |
| 2 | + * Tracking target removing from DOM. |
| 3 | + * It's nessesary to hide tooltip when it's target disappears. |
| 4 | + * Otherwise, the tooltip would be shown forever until another target |
| 5 | + * is triggered. |
| 6 | + * |
| 7 | + * If MutationObserver is not available, this feature just doesn't work. |
| 8 | + */ |
| 9 | + |
1 | 10 | // https://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/ |
2 | 11 | const getMutationObserverClass = () => { |
3 | 12 | return window.MutationObserver || |
4 | 13 | window.WebKitMutationObserver || |
5 | 14 | window.MozMutationObserver |
6 | 15 | } |
7 | 16 |
|
8 | | -const isMutationObserverAvailable = () => { |
9 | | - return getMutationObserverClass() != null |
10 | | -} |
11 | | - |
12 | | -class ObserverBasedRemovalTracker { |
13 | | - constructor (tooltip) { |
14 | | - this.tooltip = tooltip |
15 | | - |
16 | | - this.observer = null |
17 | | - this.inited = false |
18 | | - } |
19 | | - |
20 | | - init () { |
21 | | - if (this.inited) { |
22 | | - this.unbind() |
23 | | - } |
24 | | - this.inited = true |
25 | | - |
| 17 | +export default function (target) { |
| 18 | + target.prototype.bindRemovalTracker = function () { |
26 | 19 | const MutationObserver = getMutationObserverClass() |
27 | | - if (!MutationObserver) { |
28 | | - return |
29 | | - } |
| 20 | + if (MutationObserver == null) return |
30 | 21 |
|
31 | | - this.observer = new MutationObserver((mutations) => { |
| 22 | + const observer = new MutationObserver((mutations) => { |
32 | 23 | for (const mutation of mutations) { |
33 | 24 | for (const element of mutation.removedNodes) { |
34 | | - if (element === this.tooltip.state.currentTarget) { |
35 | | - this.tooltip.hideTooltip() |
| 25 | + if (element === this.state.currentTarget) { |
| 26 | + this.hideTooltip() |
36 | 27 | return |
37 | 28 | } |
38 | 29 | } |
39 | 30 | } |
40 | 31 | }) |
41 | 32 |
|
42 | | - this.observer.observe(window.document, { childList: true, subtree: true }) |
43 | | - } |
44 | | - |
45 | | - unbind () { |
46 | | - if (this.observer) { |
47 | | - this.observer.disconnect() |
48 | | - this.observer = null |
49 | | - } |
50 | | - this.inited = false |
51 | | - } |
52 | | -} |
| 33 | + observer.observe(window.document, { childList: true, subtree: true }) |
53 | 34 |
|
54 | | -export default function (target) { |
55 | | - target.prototype.bindRemovalTracker = function () { |
56 | | - if (isMutationObserverAvailable()) { |
57 | | - this.removalTracker = new ObserverBasedRemovalTracker(this) |
58 | | - this.removalTracker.init() |
59 | | - } |
| 35 | + this.removalTracker = observer |
60 | 36 | } |
61 | 37 |
|
62 | 38 | target.prototype.unbindRemovalTracker = function () { |
63 | | - this.removalTracker.unbind() |
64 | | - this.removalTracker = null |
| 39 | + if (this.removalTracker) { |
| 40 | + this.removalTracker.disconnect() |
| 41 | + this.removalTracker = null |
| 42 | + } |
65 | 43 | } |
66 | 44 | } |
0 commit comments