import { Component, NewState, Util, $ } from '@web/state' import { HTTP } from './http.js' Component.register('AutoForm', container => { if (!container.state.schema) container.state.schema = [] container.vertical = container.hasAttribute('vertical') container.request = { method: 'POST' } container.response = {} container.result = null container.data = NewState(container.data || {}) container.form = $(container, 'form') container.submit = (opt = {}) => { if (!container.form.reportValidity()) return globalThis.UI?.toast?.('{#verify failed#}', { type: 'danger' }) if (!container.dispatchEvent(new CustomEvent('submit', { detail: container.data, cancelable: true, bubbles: false }))) return const req = { ...container.request, data: container.data, noui: true, ...opt } let task = null if (container.api) task = container.api.do(req) else if (container.request.url) task = HTTP.request(req) else return console.warn('{#please config .api or .request.url to auto submit#}') task.then(resp => { container.response = resp container.result = resp.result if (typeof resp.result === 'object' && resp.result.error) throw new Error(resp.result.error) container.dispatchEvent(new CustomEvent('response', { detail: resp, bubbles: false })) }).catch(err => { if (globalThis.UI?.toast) UI.toast(err.message, { type: 'danger' }) container.dispatchEvent(new CustomEvent('error', { detail: err, bubbles: true })) }) } }, Util.makeDom(/*html*/`
`), Util.makeDom(/*html*/``)) const _pendingAutoFormComponents = [] export const AutoForm = { register: name => { if (typeof document !== 'undefined') { if (document.readyState !== 'loading' && Component.getTemplate('AutoForm')) AutoForm._addAutoFormComponent(name) else _pendingAutoFormComponents.push(name) } }, _addAutoFormComponent: name => { const template = Component.getTemplate('AutoForm') if (template) { $(template.content, '[control-wrapper]')?.appendChild(Util.makeDom(`<${name} $if="item.type.toUpperCase() === '${name.toUpperCase()}'" $name="item.name" $.="item.setting" $bind="this.data[item.name]">`)) } else if (document.readyState !== 'loading') { // If template still missing after DOM ready, something is wrong console.error('AutoForm template not found during registration of', name) } } } if (typeof document !== 'undefined') { const initAutoForm = () => { _pendingAutoFormComponents.forEach(name => AutoForm._addAutoFormComponent(name)) _pendingAutoFormComponents.length = 0 } if (document.readyState !== 'loading') setTimeout(initAutoForm, 100) // Wait slightly for component templates to be added to body else document.addEventListener('DOMContentLoaded', () => setTimeout(initAutoForm, 100), true) } Component.register('TagsInput', container => { container.addEventListener('bind', e => { container.state.tags = e.detail || [] }) }, Util.makeDom(/*html*/`
`), Util.makeDom(/*html*/``)) AutoForm.register('TagsInput')