Add event method/setting to domLense
This commit is contained in:
parent
fb000ba7a3
commit
451718cb74
1 changed files with 21 additions and 0 deletions
21
domLense.js
21
domLense.js
|
@ -1,3 +1,19 @@
|
||||||
|
class ChildObserver extends MutationObserver {
|
||||||
|
constructor() {
|
||||||
|
super(mutations => {
|
||||||
|
for (const mutation of mutations) {
|
||||||
|
mutation.target.dispatchEvent(new CustomEvent("change", {detail: mutation}))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
observe(element) {
|
||||||
|
MutationObserver.prototype.observe.call(this, element, { childList: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const childObserver = new ChildObserver()
|
||||||
|
|
||||||
const lense = (methods, extra) => {
|
const lense = (methods, extra) => {
|
||||||
if (extra) return lense(extra)(methods)
|
if (extra) return lense(extra)(methods)
|
||||||
|
|
||||||
|
@ -57,6 +73,11 @@ const lense = (methods, extra) => {
|
||||||
return element => {
|
return element => {
|
||||||
const proxy = new Proxy(element, traps)
|
const proxy = new Proxy(element, traps)
|
||||||
|
|
||||||
|
if (methods.event) childObserver.observe(element)
|
||||||
|
if (typeof methods.event === "function") element.addEventListener("change", event => {
|
||||||
|
methods.event(proxy, element, event.detail)
|
||||||
|
})
|
||||||
|
|
||||||
return proxy
|
return proxy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue