-
Notifications
You must be signed in to change notification settings - Fork 425
/
element_observer.ts
160 lines (135 loc) · 4.19 KB
/
element_observer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
export interface ElementObserverDelegate {
matchElement(element: Element): boolean
matchElementsInTree(tree: Element): Element[]
elementMatched?(element: Element): void
elementUnmatched?(element: Element): void
elementAttributeChanged?(element: Element, attributeName: string): void
}
export class ElementObserver {
element: Element
started: boolean
private delegate: ElementObserverDelegate
private elements: Set<Element>
private mutationObserver: MutationObserver
constructor(element: Element, delegate: ElementObserverDelegate) {
this.element = element
this.started = false
this.delegate = delegate
this.elements = new Set
this.mutationObserver = new MutationObserver((mutations) => this.processMutations(mutations))
}
start() {
if (!this.started) {
this.started = true
this.mutationObserver.observe(this.element, { attributes: true, childList: true, subtree: true })
this.refresh()
}
}
stop() {
if (this.started) {
this.mutationObserver.takeRecords()
this.mutationObserver.disconnect()
this.started = false
}
}
refresh() {
if (this.started) {
const matches = new Set(this.matchElementsInTree())
for (const element of Array.from(this.elements)) {
if (!matches.has(element)) {
this.removeElement(element)
}
}
for (const element of Array.from(matches)) {
this.addElement(element)
}
}
}
// Mutation record processing
private processMutations(mutations: MutationRecord[]) {
if (this.started) {
for (const mutation of mutations) {
this.processMutation(mutation)
}
}
}
private processMutation(mutation: MutationRecord) {
if (mutation.type == "attributes") {
this.processAttributeChange(mutation.target, mutation.attributeName!)
} else if (mutation.type == "childList") {
this.processRemovedNodes(mutation.removedNodes)
this.processAddedNodes(mutation.addedNodes)
}
}
private processAttributeChange(node: Node, attributeName: string) {
const element = node as Element
if (this.elements.has(element)) {
if (this.delegate.elementAttributeChanged && this.matchElement(element)) {
this.delegate.elementAttributeChanged(element, attributeName)
} else {
this.removeElement(element)
}
} else if (this.matchElement(element)) {
this.addElement(element)
}
}
private processRemovedNodes(nodes: NodeList) {
for (const node of Array.from(nodes)) {
const element = this.elementFromNode(node)
if (element) {
this.processTree(element, this.removeElement)
}
}
}
private processAddedNodes(nodes: NodeList) {
for (const node of Array.from(nodes)) {
const element = this.elementFromNode(node)
if (element && this.elementIsActive(element)) {
this.processTree(element, this.addElement)
}
}
}
// Element matching
private matchElement(element: Element): boolean {
return this.delegate.matchElement(element)
}
private matchElementsInTree(tree: Element = this.element): Element[] {
return this.delegate.matchElementsInTree(tree)
}
private processTree(tree: Element, processor: (element: Element) => void) {
for (const element of this.matchElementsInTree(tree)) {
processor.call(this, element)
}
}
private elementFromNode(node: Node): Element | undefined {
if (node.nodeType == Node.ELEMENT_NODE) {
return node as Element
}
}
private elementIsActive(element: Element): boolean {
if (element.isConnected != this.element.isConnected) {
return false
} else {
return this.element.contains(element)
}
}
// Element tracking
private addElement(element: Element) {
if (!this.elements.has(element)) {
if (this.elementIsActive(element)) {
this.elements.add(element)
if (this.delegate.elementMatched) {
this.delegate.elementMatched(element)
}
}
}
}
private removeElement(element: Element) {
if (this.elements.has(element)) {
this.elements.delete(element)
if (this.delegate.elementUnmatched) {
this.delegate.elementUnmatched(element)
}
}
}
}