mirror of
https://github.com/umap-project/umap.git
synced 2025-04-29 11:52:38 +02:00
chore(utils): use native events instead of array of callbacks for WithEvents
Co-authored-by: David Larlet <david@larlet.fr>
This commit is contained in:
parent
0ba69e41d0
commit
07c29abbec
3 changed files with 8 additions and 15 deletions
|
@ -668,9 +668,9 @@ export class DataLayer extends ServerStored {
|
||||||
]
|
]
|
||||||
DomUtil.createTitle(container, translate('Layer properties'), 'icon-layers')
|
DomUtil.createTitle(container, translate('Layer properties'), 'icon-layers')
|
||||||
let builder = new MutatingForm(this, metadataFields)
|
let builder = new MutatingForm(this, metadataFields)
|
||||||
builder.on('set', (helper) => {
|
builder.on('set', ({ detail }) => {
|
||||||
this._umap.onDataLayersChanged()
|
this._umap.onDataLayersChanged()
|
||||||
if (helper.field === 'options.type') {
|
if (detail.helper.field === 'options.type') {
|
||||||
this.edit()
|
this.edit()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -97,7 +97,7 @@ class BaseElement {
|
||||||
|
|
||||||
sync() {
|
sync() {
|
||||||
this.set()
|
this.set()
|
||||||
this.builder.fire('set', this)
|
this.builder.fire('set', { helper: this })
|
||||||
}
|
}
|
||||||
|
|
||||||
set() {
|
set() {
|
||||||
|
|
|
@ -451,24 +451,17 @@ export function eachElement(selector, callback) {
|
||||||
|
|
||||||
export class WithEvents {
|
export class WithEvents {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._callbacks = {}
|
this._target = new EventTarget()
|
||||||
}
|
}
|
||||||
|
|
||||||
on(eventType, callback) {
|
on(eventType, callback) {
|
||||||
if (typeof callback !== 'function') return
|
if (typeof callback !== 'function') return
|
||||||
if (this._callbacks[eventType] === undefined) {
|
this._target.addEventListener(eventType, callback)
|
||||||
this._callbacks[eventType] = []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._callbacks[eventType].push(callback)
|
fire(eventType, detail) {
|
||||||
}
|
const event = new CustomEvent(eventType, { detail })
|
||||||
|
this._target.dispatchEvent(event)
|
||||||
fire(eventType, ...args) {
|
|
||||||
if (this._callbacks[eventType] === undefined) return
|
|
||||||
|
|
||||||
for (const callback of this._callbacks[eventType]) {
|
|
||||||
callback(...args)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue