@zhsz/cool-design-crud
Version:
277 lines (276 loc) • 6.99 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const tdesignVueNext = require("tdesign-vue-next");
require("../../utils/test.js");
const core = require("../../hooks/core.js");
const proxy = require("../../hooks/proxy.js");
require("@formily/core");
require("lodash-es");
const index$2 = require("../../utils/index.js");
require("../../hooks/table.js");
const index$1 = require("../form/index.js");
const index = /* @__PURE__ */ vue.defineComponent({
name: "cl-upsert",
components: {
ClForm: index$1.default
},
props: {
/** 弹窗宽度 */
width: String,
/** 是否页面弹窗 */
showBox: {
type: [Boolean, Function]
},
/** 表单协议 */
schema: {
type: Object
},
/** 协议表达式作用域 */
scope: Object,
/** 注册组件列表 */
registerComponents: Object,
/** <cl-form /> 参数 */
props: Object,
/** 副作用逻辑,用于实现各种联动逻辑 */
formEffects: Function,
/** 编辑时是否同步打开 */
sync: {
type: Boolean
},
/** 操作按钮参数 */
op: Object,
/** <cl-dialog /> 参数 */
dialog: Object,
/** 打开表单钩子 */
onOpen: Function,
/** 打开表单后钩子 */
onOpened: Function,
/** 关闭表单钩子 */
onClose: Function,
/** 关闭表单后钩子 */
onClosed: Function,
/** 获取表单数据钩子 */
onInfo: Function,
/** 表单提交钩子 */
onSubmit: Function
},
emits: ["opened", "close", "closed", "info", "submit"],
setup(props, {
slots,
expose
}) {
const {
crud
} = core.useCore();
const config = vue.reactive(index$2.mergeConfig(props, vue.inject("useUpsert__options") || {}));
const Form = vue.ref();
const mode = vue.ref("info");
function close(action) {
var _a;
(_a = Form.value) == null ? void 0 : _a.close(action);
}
function onClosed() {
var _a;
(_a = Form.value) == null ? void 0 : _a.hideLoading();
if (config.onClosed) {
config.onClosed();
}
}
function beforeClose(action, done2) {
function next() {
done2();
onClosed();
}
if (config.onClose) {
config.onClose(action, next);
} else {
next();
}
}
function submit(data) {
const {
service,
dict,
refresh
} = crud;
function done2() {
var _a;
(_a = Form.value) == null ? void 0 : _a.done();
}
function next(data2) {
return new Promise((resolve, reject) => {
service[dict.api[mode.value]](data2).then((res) => {
tdesignVueNext.MessagePlugin.success(dict.label.saveSuccess);
done2();
close("save");
refresh();
resolve(res);
}).catch((err) => {
done2();
reject(err);
});
});
}
if (config.onSubmit) {
config.onSubmit(data, {
done: done2,
next,
close() {
close("save");
}
});
} else {
next(data);
}
}
function open() {
const readOnly = mode.value === "info";
return new Promise((resolve) => {
var _a;
if (!Form.value) {
return console.error("<cl-upsert /> is not found");
}
(_a = Form.value) == null ? void 0 : _a.open({
title: crud.dict.label[mode.value],
height: config == null ? void 0 : config.height,
width: config == null ? void 0 : config.width,
showBox: index$2.parseFunction(config == null ? void 0 : config.showBox, false),
props: {
...config.props
},
op: {
...config.op,
hidden: readOnly
},
dialog: config.dialog,
schema: config.schema || {},
scope: {
mode,
...config.scope
},
effects: config.effects || void 0,
on: {
open(data) {
if (config.onOpen) {
config.onOpen(data);
}
resolve(true);
},
submit,
close: beforeClose
},
form: {},
_data: {
readOnly
},
isReset: false
}, config.plugins);
});
}
function onOpened() {
var _a;
const data = (_a = Form.value) == null ? void 0 : _a.getForm();
if (config.onOpened) {
config.onOpened(data);
}
}
async function add() {
mode.value = "add";
await open();
onOpened();
}
async function append(data) {
var _a;
mode.value = "add";
await open();
if (data) {
(_a = Form.value) == null ? void 0 : _a.bindForm(data);
}
onOpened();
}
function edit(data) {
mode.value = "update";
getInfo(data);
}
function info(data) {
mode.value = "info";
getInfo(data);
}
function getInfo(data) {
var _a;
(_a = Form.value) == null ? void 0 : _a.showLoading();
if (!config.sync) {
open();
}
async function done2(data2) {
var _a2, _b;
(_a2 = Form.value) == null ? void 0 : _a2.hideLoading();
if (data2) {
(_b = Form.value) == null ? void 0 : _b.bindForm(data2);
}
if (config.sync) {
await open();
}
onOpened();
}
function next(data2) {
return new Promise(async (resolve, reject) => {
var _a2;
await crud.service[crud.dict.api.info]({
[crud.dict.primaryId]: data2[crud.dict.primaryId]
}).then((res) => {
done2(res);
resolve(res);
}).catch((err) => {
reject(err);
});
(_a2 = Form.value) == null ? void 0 : _a2.hideLoading();
});
}
if (config.onInfo) {
config.onInfo(data, {
close,
next,
done: done2
});
} else {
next(data);
}
}
function done() {
var _a;
(_a = Form.value) == null ? void 0 : _a.hideLoading();
}
const ctx = {
config,
...vue.toRefs(config),
Form,
get form() {
var _a;
return ((_a = Form.value) == null ? void 0 : _a.form) || {};
},
mode,
add,
append,
edit,
info,
open,
close,
done,
submit
};
proxy.useProxy(ctx);
expose(ctx);
return () => {
return vue.createVNode("div", {
"class": "cl-upsert"
}, [vue.createVNode(index$1.default, {
"ref": Form,
"formEffects": props.formEffects,
"custom-class": `is-${mode.value}`
}, slots)]);
};
}
});
exports.default = index;