-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (60 loc) · 1.72 KB
/
Copy pathindex.js
File metadata and controls
68 lines (60 loc) · 1.72 KB
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
import { createDOMObserver, DOMObserverEvent } from '../../src'
import { createCard, createTooltip } from './elements'
import { log } from './log'
import { addElement, destroyElement } from './utils'
import './index.css'
const tooltip = createTooltip()
const cardTarget = document.querySelector('#container')
const addButton = document.querySelector('#add-button')
addButton.addEventListener('mouseenter', () => {
const tooltipContainer = document.querySelector('#add-button')
addElement(tooltip, tooltipContainer)
})
addButton.addEventListener('mouseleave', () => {
destroyElement(tooltip)
})
addButton.addEventListener('click', () => {
destroyElement(tooltip)
const el = document.querySelector('#card')
if (!el) {
const card = createCard()
addElement(card, cardTarget)
}
})
const onEvent = ({ node, event, options }) => {
switch (event) {
case DOMObserverEvent.ADD: {
log(`[ADD]\t\tElement id: ${node.id}`)
break
}
case DOMObserverEvent.REMOVE: {
log(`[REMOVE]\tElement id: ${node.id}`)
break
}
default: {
log(`[CHANGE]\tElement id: ${node.id} - Attribute: ${options?.attributeName}`)
}
}
}
const tooltipObserver = createDOMObserver()
tooltipObserver.observe(tooltip, onEvent, { events: [DOMObserverEvent.ADD, DOMObserverEvent.REMOVE] })
const observeCard = () => {
createDOMObserver()
.observeOnce(`#card`)
.then(({ node }) => {
log(`[ADD]\t\tElement id: ${node.id}`)
const cardObserver = createDOMObserver()
cardObserver.observe(
`#card`,
(payload) => {
onEvent(payload)
if (payload.event === DOMObserverEvent.REMOVE) {
cardObserver.disconnect()
observeCard()
}
},
{ events: [DOMObserverEvent.REMOVE, DOMObserverEvent.CHANGE] }
)
})
}
observeCard()