|
1 |
| -// http://stackoverflow.com/a/32726412/7571078 |
2 |
| -const isDetached = (element) => { |
3 |
| - if (element.parentNode === window.document) { |
4 |
| - return false |
5 |
| - } |
6 |
| - if (element.parentNode == null) return true |
7 |
| - return isDetached(element.parentNode) |
8 |
| -} |
9 |
| - |
10 | 1 | // https://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/
|
11 | 2 | const getMutationObserverClass = () => {
|
12 | 3 | return window.MutationObserver ||
|
13 | 4 | window.WebKitMutationObserver ||
|
14 | 5 | window.MozMutationObserver
|
15 | 6 | }
|
16 | 7 |
|
17 |
| -export default function (target) { |
18 |
| - target.prototype.trackRemoval = function () { |
19 |
| - if (this.removalObserver) { |
20 |
| - this.releaseRemovalTracker() |
| 8 | +const isMutationObserverAvailable = () => { |
| 9 | + return getMutationObserverClass() != null |
| 10 | +} |
| 11 | + |
| 12 | +class EventBasedRemovalTracker { |
| 13 | + constructor (tooltip) { |
| 14 | + this.tooltip = tooltip |
| 15 | + this.listeners = [] |
| 16 | + } |
| 17 | + |
| 18 | + attach (element) { |
| 19 | + const {tooltip} = this |
| 20 | + |
| 21 | + let listener = function (e) { |
| 22 | + if (e.currentTarget === tooltip.state.currentTarget) { |
| 23 | + tooltip.hideTooltip() |
| 24 | + this.listeners.splice(this.listeners.indexOf(listener), 1) |
| 25 | + } |
21 | 26 | }
|
| 27 | + listener = listener.bind(this) |
| 28 | + |
| 29 | + this.listeners.push({ |
| 30 | + element, |
| 31 | + listener |
| 32 | + }) |
22 | 33 |
|
23 |
| - this.tracked = [] |
| 34 | + element.addEventListener('DOMNodeRemovedFromDocument', listener) |
| 35 | + } |
| 36 | + |
| 37 | + unbind () { |
| 38 | + for (const {listener, element} of this.listeners) { |
| 39 | + element.removeEventListener('DOMNodeRemovedFromDocument', listener) |
| 40 | + } |
| 41 | + this.listeners = [] |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class MutationBasedRemovalTracker { |
| 46 | + constructor (tooltip) { |
| 47 | + this.tooltip = tooltip |
| 48 | + |
| 49 | + this.observer = null |
| 50 | + this.inited = false |
| 51 | + this.trackedElements = null |
| 52 | + } |
| 53 | + |
| 54 | + init () { |
| 55 | + if (this.inited) { |
| 56 | + this.unbind() |
| 57 | + } |
| 58 | + |
| 59 | + this.trackedElements = [] |
24 | 60 |
|
25 | 61 | const MutationObserver = getMutationObserverClass()
|
26 | 62 | if (MutationObserver) {
|
27 |
| - const observer = this.removalObserver = new MutationObserver(() => { |
28 |
| - for (const element of this.tracked) { |
29 |
| - if (isDetached(element) && element === this.state.currentTarget) { |
30 |
| - this.hideTooltip() |
| 63 | + this.observer = new MutationObserver(() => { |
| 64 | + for (const element of this.trackedElements) { |
| 65 | + if (this.isDetached(element) && element === this.tooltip.state.currentTarget) { |
| 66 | + this.tooltip.hideTooltip() |
31 | 67 | }
|
32 | 68 | }
|
33 | 69 | })
|
34 |
| - observer.observe(window.document, { childList: true, subtree: true }) |
| 70 | + this.observer.observe(window.document, { childList: true, subtree: true }) |
35 | 71 | }
|
36 |
| - } |
37 | 72 |
|
38 |
| - target.prototype.attachRemovalTracker = function (element) { |
39 |
| - this.tracked.push(element) |
40 |
| - |
41 |
| - const isMutationObserverAvailable = getMutationObserverClass() |
42 |
| - if (!isMutationObserverAvailable) { |
43 |
| - this.listeners = this.listeners || [] |
| 73 | + this.inited = true |
| 74 | + } |
44 | 75 |
|
45 |
| - let listener = function (e) { |
46 |
| - if (e.currentTarget === this.state.currentTarget) { |
47 |
| - this.hideTooltip() |
48 |
| - const idx = this.listeners.indexOf(listener) |
49 |
| - this.listeners.splice(idx, 1) |
50 |
| - } |
51 |
| - } |
52 |
| - listener = listener.bind(this) |
| 76 | + unbind () { |
| 77 | + if (this.observer) { |
| 78 | + this.observer.disconnect() |
| 79 | + this.observer = null |
| 80 | + this.trackedElements = null |
| 81 | + } |
| 82 | + this.inited = false |
| 83 | + } |
53 | 84 |
|
54 |
| - this.listeners.push({ |
55 |
| - element, |
56 |
| - listener |
57 |
| - }) |
| 85 | + attach (element) { |
| 86 | + this.trackedElements.push(element) |
| 87 | + } |
58 | 88 |
|
59 |
| - element.addEventListener('DOMNodeRemovedFromDocument', listener) |
| 89 | + // http://stackoverflow.com/a/32726412/7571078 |
| 90 | + isDetached (element) { |
| 91 | + if (element.parentNode === window.document) { |
| 92 | + return false |
60 | 93 | }
|
| 94 | + if (element.parentNode == null) return true |
| 95 | + return this.isDetached(element.parentNode) |
61 | 96 | }
|
| 97 | +} |
62 | 98 |
|
63 |
| - target.prototype.releaseRemovalTracker = function () { |
64 |
| - if (this.removalObserver) { |
65 |
| - this.removalObserver.disconnect() |
66 |
| - this.removalObserver = null |
67 |
| - this.tracked = [] |
68 |
| - } |
69 |
| - if (this.listeners) { |
70 |
| - for (const {listener, element} of this.listeners) { |
71 |
| - element.removeEventListener('DOMNodeRemovedFromDocument', listener) |
72 |
| - } |
73 |
| - this.listeners = [] |
| 99 | +export default function (target) { |
| 100 | + target.prototype.bindRemovalTracker = function () { |
| 101 | + if (isMutationObserverAvailable()) { |
| 102 | + this.removalTracker = new MutationBasedRemovalTracker(this) |
| 103 | + this.removalTracker.init() |
| 104 | + } else { |
| 105 | + this.removalTracker = new EventBasedRemovalTracker(this) |
74 | 106 | }
|
75 | 107 | }
|
| 108 | + |
| 109 | + target.prototype.attachRemovalTracker = function (element) { |
| 110 | + this.removalTracker.attach(element) |
| 111 | + } |
| 112 | + |
| 113 | + target.prototype.unbindRemovalTracker = function () { |
| 114 | + this.removalTracker.unbind() |
| 115 | + } |
76 | 116 | }
|
0 commit comments