UNPKG

@zhsz/cool-design-crud

Version:

143 lines (142 loc) 2.93 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); require("../../utils/test.js"); const index = require("../../utils/index.js"); require("@formily/core"); require("lodash-es"); require("../../hooks/table.js"); function useAction({ config, form, Form }) { function set({ prop, key, path }, data) { const p = path || ""; if (path) { index.dataset(config, p, data); } else { let d; if (prop) { const deep = (arr) => { for (const key2 in arr) { if (Object.prototype.hasOwnProperty.call(arr, key2)) { const element = arr[key2]; if (key2 === prop) { d = element; } } } }; deep(config.schema); } if (d) { switch (key) { case "options": d.enum = data; break; case "props": d.cProps && Object.assign(d.cProps, data); break; default: Object.assign(d, data); break; } } else { console.error(`prop "${prop}" not found`); } } } function getForm(prop) { return prop ? Form.getValuesIn(prop) : form; } function setForm(prop, value) { form[prop] = value; Form.setValues(form, "overwrite"); } function setConfig(path, value) { set({ path }, value); } function formReset(pattern, options) { Form.reset(pattern, options); } function setHeader(value) { if (config.dialog) config.dialog.header = value; } return { getForm, setForm, setConfig, setHeader, formReset }; } function usePlugins({ visible }) { const that = vue.getCurrentInstance(); const ev = { onOpen: [], onClose: [], onSubmit: [] }; let timer = null; function create(plugins) { for (const i in ev) { ev[i] = []; } if (timer) { timer(); } if (plugins) { plugins.forEach((p) => { p({ exposed: that.exposed, onOpen(cb) { ev.onOpen.push(cb); }, onClose(cb) { ev.onClose.push(cb); }, onSubmit(cb) { ev.onSubmit.push(cb); } }); }); timer = vue.watch(visible, (val) => { if (val) { setTimeout(() => { ev.onOpen.forEach((e) => e()); }, 10); } else { ev.onClose.forEach((e) => e()); } }, { immediate: true }); } } async function submit(data) { let d = data; for (let i = 0; i < ev.onSubmit.length; i++) { const d2 = await ev.onSubmit[i](d); if (d2) { d = d2; } } return d; } return { create, submit }; } exports.useAction = useAction; exports.usePlugins = usePlugins;