import { Component, NewState, Util, $, State } from '@apigo.cc/state'
import { HTTP } from './http.js'
/**
* AutoForm 蓝图定义
*/
const AUTOFORM_BLUEPRINT = Util.makeDom(/*html*/`
`)
const AUTOFORM_STYLE = Util.makeDom(/*html*/``)
Component.register('AutoForm', container => {
if (!container.state.schema) container.state.schema = []
const ensureProxy = v => (v && typeof v === 'object' && !v.__isProxy) ? NewState(v) : v;
container.state.__watch('data', v => container.data = ensureProxy(v));
container.data = ensureProxy(container.state.data || {});
container.vertical = container.hasAttribute('vertical')
container.horizontal = container.hasAttribute('horizontal')
container.inline = container.hasAttribute('inline')
container.nobutton = container.hasAttribute('nobutton')
container.request = { method: 'POST' }
container.response = {}
container.result = null
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 }))
})
}
container.checkIf = (item) => {
if (!item.if) return true
try {
const fn = new Function('Hash', 'LocalStorage', 'State', 'item', 'data', 'return ' + item.if)
return fn.call(container, globalThis.Hash, globalThis.LocalStorage, globalThis.State, item, container.data)
} catch (e) {
return false
}
}
}, AUTOFORM_BLUEPRINT, AUTOFORM_STYLE)
const findAnchorInBlueprint = (root) => {
let f = root.querySelector('[control-wrapper]');
if (f) return f;
for (const t of root.querySelectorAll('template')) {
f = findAnchorInBlueprint(t.content); if (f) return f;
}
return null;
}
export const AutoForm = {
customTypes: [],
register: (name, typeName) => {
const type = typeName || name
if (!AutoForm.customTypes.find(t => t.name === name)) {
AutoForm.customTypes.push({ name, typeName: type })
AutoForm._addAutoFormComponent(name, type)
}
},
_addAutoFormComponent: (name, type) => {
const wrapper = findAnchorInBlueprint(AUTOFORM_BLUEPRINT)
if (wrapper) {
const node = Util.makeDom(`<${name} $if="item.type?.toLowerCase() === '${type.toLowerCase()}'" $name="item.name" $.="item.setting || {}" $bind="this.data[item.name]" class="w-100">${name}>`)
wrapper.appendChild(node)
}
}
}
/**
* TagsInput
* 100% 还原 base_original.js 逻辑与写法
*/
Component.register('TagsInput', container => {
container._thisObj = container; // 夺回上下文主权
container.addEventListener('bind', e => {
container.state.tags = Array.isArray(e.detail) ? e.detail : []
})
}, Util.makeDom(/*html*/`
`), Util.makeDom(/*html*/``))
AutoForm.register('TagsInput')