wip(forms): use events instead of callback

This commit is contained in:
Yohan Boniface 2025-01-09 13:02:04 +01:00
parent fb4fecd337
commit 0ba69e41d0
8 changed files with 56 additions and 52 deletions

View file

@ -180,9 +180,8 @@ export default class Browser {
], ],
['options.inBbox', { handler: 'Switch', label: translate('Current map view') }], ['options.inBbox', { handler: 'Switch', label: translate('Current map view') }],
] ]
const builder = new Form(this, fields, { const builder = new Form(this, fields)
callback: () => this.onFormChange(), builder.on('set', () => this.onFormChange())
})
let filtersBuilder let filtersBuilder
this.formContainer.appendChild(builder.build()) this.formContainer.appendChild(builder.build())
DomEvent.on(builder.form, 'reset', () => { DomEvent.on(builder.form, 'reset', () => {
@ -190,9 +189,8 @@ export default class Browser {
}) })
if (this._umap.properties.facetKey) { if (this._umap.properties.facetKey) {
fields = this._umap.facets.build() fields = this._umap.facets.build()
filtersBuilder = new Form(this._umap.facets, fields, { filtersBuilder = new Form(this._umap.facets, fields)
callback: () => this.onFormChange(), filtersBuilder.on('set', () => this.onFormChange())
})
DomEvent.on(filtersBuilder.form, 'reset', () => { DomEvent.on(filtersBuilder.form, 'reset', () => {
window.setTimeout(filtersBuilder.syncAll.bind(filtersBuilder)) window.setTimeout(filtersBuilder.syncAll.bind(filtersBuilder))
}) })

View file

@ -226,15 +226,11 @@ class Feature {
`icon-${this.getClassName()}` `icon-${this.getClassName()}`
) )
let builder = new MutatingForm( let builder = new MutatingForm(this, [
this, ['datalayer', { handler: 'DataLayerSwitcher' }],
[['datalayer', { handler: 'DataLayerSwitcher' }]], ])
{ // removeLayer step will close the edit panel, let's reopen it
callback() { builder.on('set', () => this.edit(event))
this.edit(event)
}, // removeLayer step will close the edit panel, let's reopen it
}
)
container.appendChild(builder.build()) container.appendChild(builder.build())
const properties = [] const properties = []
@ -734,16 +730,15 @@ export class Point extends Feature {
['ui._latlng.lat', { handler: 'FloatInput', label: translate('Latitude') }], ['ui._latlng.lat', { handler: 'FloatInput', label: translate('Latitude') }],
['ui._latlng.lng', { handler: 'FloatInput', label: translate('Longitude') }], ['ui._latlng.lng', { handler: 'FloatInput', label: translate('Longitude') }],
] ]
const builder = new MutatingForm(this, coordinatesOptions, { const builder = new MutatingForm(this, coordinatesOptions)
callback: () => { builder.on('set', () => {
if (!this.ui._latlng.isValid()) { if (!this.ui._latlng.isValid()) {
Alert.error(translate('Invalid latitude or longitude')) Alert.error(translate('Invalid latitude or longitude'))
builder.restoreField('ui._latlng.lat') builder.restoreField('ui._latlng.lat')
builder.restoreField('ui._latlng.lng') builder.restoreField('ui._latlng.lng')
} }
this.pullGeometry() this.pullGeometry()
this.zoomTo({ easing: false }) this.zoomTo({ easing: false })
},
}) })
const fieldset = DomUtil.createFieldset(container, translate('Coordinates')) const fieldset = DomUtil.createFieldset(container, translate('Coordinates'))
fieldset.appendChild(builder.build()) fieldset.appendChild(builder.build())

View file

@ -667,14 +667,12 @@ 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)
callback: (helper) => { builder.on('set', (helper) => {
console.log(helper) this._umap.onDataLayersChanged()
this._umap.onDataLayersChanged() if (helper.field === 'options.type') {
if (helper.field === 'options.type') { this.edit()
this.edit() }
}
},
}) })
container.appendChild(builder.build()) container.appendChild(builder.build())

View file

@ -3,8 +3,9 @@ import * as Utils from '../utils.js'
import { SCHEMA } from '../schema.js' import { SCHEMA } from '../schema.js'
import { translate } from '../i18n.js' import { translate } from '../i18n.js'
export class Form { export class Form extends Utils.WithEvents {
constructor(obj, fields, properties) { constructor(obj, fields, properties) {
super()
this.setProperties(properties) this.setProperties(properties)
this.defaultProperties = {} this.defaultProperties = {}
this.obj = obj this.obj = obj

View file

@ -97,7 +97,7 @@ class BaseElement {
sync() { sync() {
this.set() this.set()
this.onPostSync() this.builder.fire('set', this)
} }
set() { set() {
@ -116,13 +116,6 @@ class BaseElement {
finish() {} finish() {}
onPostSync() {
if (this.properties.callback) {
this.properties.callback(this)
}
this.builder.onPostSync(this)
}
undefine() { undefine() {
this.root.classList.add('undefined') this.root.classList.add('undefined')
this.clear() this.clear()

View file

@ -126,9 +126,8 @@ export default class Share {
exportUrl.value = window.location.protocol + iframeExporter.buildUrl() exportUrl.value = window.location.protocol + iframeExporter.buildUrl()
} }
buildIframeCode() buildIframeCode()
const builder = new MutatingForm(iframeExporter, UIFields, { const builder = new MutatingForm(iframeExporter, UIFields)
callback: buildIframeCode, builder.on('set', buildIframeCode)
})
const iframeOptions = DomUtil.createFieldset( const iframeOptions = DomUtil.createFieldset(
this.container, this.container,
translate('Embed and link options') translate('Embed and link options')

View file

@ -1029,13 +1029,6 @@ export default class Umap extends ServerStored {
], ],
] ]
const slideshowBuilder = new MutatingForm(this, slideshowFields, { const slideshowBuilder = new MutatingForm(this, slideshowFields, {
callback: () => {
this.slideshow.load()
// FIXME when we refactor formbuilder: this callback is called in a 'postsync'
// event, which comes after the call of `setter` method, which will call the
// map.render method, which should do this redraw.
this.bottomBar.redraw()
},
umap: this, umap: this,
}) })
slideshow.appendChild(slideshowBuilder.build()) slideshow.appendChild(slideshowBuilder.build())
@ -1351,6 +1344,10 @@ export default class Umap extends ServerStored {
} }
this.topBar.redraw() this.topBar.redraw()
}, },
'properties.slideshow.active': () => {
this.slideshow.load()
this.bottomBar.redraw()
},
numberOfConnectedPeers: () => { numberOfConnectedPeers: () => {
Utils.eachElement('.connected-peers span', (el) => { Utils.eachElement('.connected-peers span', (el) => {
if (this.sync.websocketConnected) { if (this.sync.websocketConnected) {

View file

@ -449,6 +449,29 @@ export function eachElement(selector, callback) {
} }
} }
export class WithEvents {
constructor() {
this._callbacks = {}
}
on(eventType, callback) {
if (typeof callback !== 'function') return
if (this._callbacks[eventType] === undefined) {
this._callbacks[eventType] = []
}
this._callbacks[eventType].push(callback)
}
fire(eventType, ...args) {
if (this._callbacks[eventType] === undefined) return
for (const callback of this._callbacks[eventType]) {
callback(...args)
}
}
}
export const COLORS = [ export const COLORS = [
'Black', 'Black',
'Navy', 'Navy',