mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 07:10:41 +00:00
37 lines
798 B
TypeScript
37 lines
798 B
TypeScript
import { hexId } from '../Utils'
|
|
|
|
export class Mounter {
|
|
private registry: { [id: string]: (el: Element) => void } = {}
|
|
|
|
register(callback: (el: Element) => void): string {
|
|
const id = hexId()
|
|
this.registry[id] = callback
|
|
return id
|
|
}
|
|
|
|
on(type: string, callback: (el: Element) => void): string {
|
|
return this.register(el => {
|
|
el.addEventListener(type, evt => {
|
|
callback(el)
|
|
evt.stopPropagation()
|
|
})
|
|
})
|
|
}
|
|
|
|
onChange(callback: (el: Element) => void): string {
|
|
return this.on('change', callback)
|
|
}
|
|
|
|
onClick(callback: (el: Element) => void): string {
|
|
return this.on('click', callback)
|
|
}
|
|
|
|
mounted(el: Element): void {
|
|
el.querySelectorAll('[data-id]').forEach(el => {
|
|
const id = el.getAttribute('data-id')!
|
|
this.registry[id]?.(el)
|
|
})
|
|
this.registry = {}
|
|
}
|
|
}
|