You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12 KiB

3 years ago
import { domMap } from "@/const/inputType";
import { addPropsMap } from "@/const/addProps";
3 years ago
export class CreateDialog {
3 years ago
replaces;
self;
$createElement;
options;
constructor(self, replaces = [], options) {
3 years ago
/*
replace = [
{
key: '',
label: '',
render: () => {}
}
]
*/
3 years ago
this.self = self;
this.$createElement = self.$createElement;
this.replaces = replaces;
this.options = options;
3 years ago
}
3 years ago
getEventType(info) {
3 years ago
if (info === "checkbox") {
return "change";
}
return "input";
}
fileRemoveHandler(file, field) {
3 years ago
let that = this.self;
3 years ago
that.file[field] = that.file[field].filter((item) => item !== file);
that.file = Object.assign({}, that.file);
}
3 years ago
extraProps(info) {
let that = this.self;
3 years ago
let props = {};
if (info.edit_input === "file" || info.edit_input === "files") {
props.fileList = that.file[info.field];
props.beforeUpload = (file) => {
2 years ago
if (file.size / 1000 > (50 * 1024)) {
3 years ago
that.$message({
type: "warning",
2 years ago
message: "上传图片大小超过50M",
3 years ago
});
return false;
}
};
props.onSuccess = (response, file, fileList) => {
that.file[info.field] = fileList;
};
props.onRemove = (file, fileList) => {
that.file[info.field] = fileList;
};
props.onError = (err, file, fileList) => {
that.file[info.field] = fileList;
that.$message({
type: "warning",
message: err,
});
};
}
return props;
}
optionsRender(h, info) {
3 years ago
let that = this.self;
3 years ago
if (info.edit_input === "checkbox" || info.edit_input === "radio") {
return info._params && info._params instanceof Array
? info._params.map((i) =>
3 years ago
h("el-option", {
props: {
label:
i.name ||
i.mingcheng ||
i.label ||
i.key ||
i.value ||
i.id ||
i.no,
value: i.id || i.value,
},
})
)
3 years ago
: [];
}
if (info.edit_input === "file" || info.edit_input === "files") {
return [
h(
"el-button",
{
slot: "trigger",
props: {
size: "small",
type: "primary",
},
},
"选取文件"
),
h(
"el-button",
{
style: {
"margin-left": "10px",
},
props: {
size: "small",
type: "success",
},
on: {
["click"]: (e) => {
that.$refs[`elEdit_${info.field}`].submit();
},
},
},
"上传到服务器"
),
h(
"div",
{
class: "el-upload__tip",
slot: "tip",
},
"文件不超过500kb"
),
];
}
}
3 years ago
render() {
let that = this.self;
const h = this.$createElement;
3 years ago
return h(
"el-dialog",
{
3 years ago
class: "dialog",
ref: "dialog",
3 years ago
props: {
top: "8vh",
3 years ago
title: that.title
? that.title
: that.type === "add"
? "新增"
: "编辑",
3 years ago
visible: that.dialogVisible,
width: this.options?.width ? this.options.width : "800px",
},
on: {
"update:visible": (val) => {
that.dialogVisible = val;
},
},
},
[
h(
"el-scrollbar",
{
style: {
height: "58vh",
},
},
[
h(
"el-form",
{
class: "form-body",
ref: "elForm",
style: {
"padding-right": "12px",
},
props: {
model: that.form,
labelWidth: "80px",
rules: that.rules,
labelPosition: "right",
size: "small",
},
},
(() => {
let dom = [];
(this.options?.formInfo ? this.options.formInfo : that.formInfo)
.filter((i) => i.form_show)
.forEach((i, index) => {
3 years ago
let replace = this.replaces.find((j) => j.key === i.field);
dom.push(
h(
"el-form-item",
{
ref: `elFormItem${i.field}`,
style: {
width: "100%",
3 years ago
},
3 years ago
props: {
label: i.name,
prop: i.field,
required:
i.validation instanceof Array
? !!i.validation.find((i) => i === "required")
: false,
},
},
replace && replace.render
? [replace.render]
: [
h(
domMap.get(i.edit_input),
{
ref: `elEdit_${i.field}`,
style: {
width: "100%",
},
props: {
...addPropsMap.get(i.edit_input),
...this.extraProps(i),
placeholder: i.help,
value: that.form[i.field],
readonly: that.type === "show",
//disabled: that.type === 'show',
},
attrs: {
placeholder: i.help || `请填写${i.name}`,
},
on: {
[this.getEventType(i.edit_input)]: (
e
) => {
if (i.field) {
that.form[i.field] = e;
that.form = Object.assign(
{},
that.form
);
}
3 years ago
},
3 years ago
},
scopedSlots:
i.edit_input === "file" ||
i.edit_input === "files"
? {
file: (scope) => {
let { file } = scope;
return [
h("div", {}, [
h("i", {
class: {
"el-icon-circle-check":
file.status ===
"success",
"el-icon-loading":
file.status ===
"uploading",
},
style: {
color:
file.status ===
"success"
? "green"
: "",
},
}),
h(
"a",
{
attrs: {
href: file.url,
download: file.name,
},
3 years ago
class: {
3 years ago
"uploaded-a":
3 years ago
file.status ===
"success",
},
style: {
3 years ago
padding: "0 4px",
3 years ago
},
},
3 years ago
file.name
),
]),
h("i", {
class: "el-icon-close",
on: {
["click"]: () =>
this.fileRemoveHandler(
file,
i.field
),
},
}),
];
},
}
: "",
},
this.optionsRender(h, i)
),
]
)
);
3 years ago
});
3 years ago
this.replaces.forEach((replace) => {
let info = that.formInfo.find((i) => i.field === replace.key);
3 years ago
if (!info) {
if (replace.label) {
3 years ago
dom.push(
h(
"el-form-item",
{
ref: `elFormItem${replace.key}`,
style: {
width: "100%",
...replace.rowStyle,
},
props: {
label: replace.label,
prop: replace.key,
},
3 years ago
},
3 years ago
[replace.render]
)
);
3 years ago
} else {
3 years ago
dom.push(replace.render);
3 years ago
}
}
3 years ago
});
3 years ago
return dom;
})()
),
]
),
h("template", { slot: "footer" }, [
h(
"el-button",
{
props: {
3 years ago
size: "mini",
3 years ago
},
on: {
click: () => (that.dialogVisible = false),
},
},
"取 消"
),
h(
"el-button",
{
props: {
type: "warning",
plain: true,
3 years ago
size: "mini",
3 years ago
},
on: {
click: () => that.init(),
},
},
"重 置"
),
h(
"el-button",
{
props: {
type: "primary",
3 years ago
size: "mini",
3 years ago
},
on: {
click: that.submit,
},
},
"确 定"
),
]),
]
);
}
}