admin
2024-04-24 363457b34e0e4f26ffe51aa80ecb227bf7873308
提交 | 用户 | 时间
363457 1 /* eslint-disable max-len */
A 2 import { trigger } from './config'
3
4 let confGlobal
5 let someSpanIsNot24
6
7 export function dialogWrapper(str) {
8   return `<el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="Dialog Title">
9     ${str}
10     <div slot="footer">
11       <el-button @click="close">取消</el-button>
12       <el-button type="primary" @click="handleConfirm">确定</el-button>
13     </div>
14   </el-dialog>`
15 }
16
17 export function vueTemplate(str) {
18   return `<template>
19     <div>
20       ${str}
21     </div>
22   </template>`
23 }
24
25 export function vueScript(str) {
26   return `<script>
27     ${str}
28   </script>`
29 }
30
31 export function cssStyle(cssStr) {
32   return `<style>
33     ${cssStr}
34   </style>`
35 }
36
37 function buildFormTemplate(conf, child, type) {
38   let labelPosition = ''
39   if (conf.labelPosition !== 'right') {
40     labelPosition = `label-position="${conf.labelPosition}"`
41   }
42   const disabled = conf.disabled ? `:disabled="${conf.disabled}"` : ''
43   let str = `<el-form ref="${conf.formRef}" :model="${conf.formModel}" :rules="${conf.formRules}" size="${conf.size}" ${disabled} label-width="${conf.labelWidth}px" ${labelPosition}>
44       ${child}
45       ${buildFromBtns(conf, type)}
46     </el-form>`
47   if (someSpanIsNot24) {
48     str = `<el-row :gutter="${conf.gutter}">
49         ${str}
50       </el-row>`
51   }
52   return str
53 }
54
55 function buildFromBtns(conf, type) {
56   let str = ''
57   if (conf.formBtns && type === 'file') {
58     str = `<el-form-item size="large">
59           <el-button type="primary" @click="submitForm">提交</el-button>
60           <el-button @click="resetForm">重置</el-button>
61         </el-form-item>`
62     if (someSpanIsNot24) {
63       str = `<el-col :span="24">
64           ${str}
65         </el-col>`
66     }
67   }
68   return str
69 }
70
71 // span不为24的用el-col包裹
72 function colWrapper(element, str) {
73   if (someSpanIsNot24 || element.span !== 24) {
74     return `<el-col :span="${element.span}">
75       ${str}
76     </el-col>`
77   }
78   return str
79 }
80
81 const layouts = {
82   colFormItem(element) {
83     let labelWidth = ''
84     if (element.labelWidth && element.labelWidth !== confGlobal.labelWidth) {
85       labelWidth = `label-width="${element.labelWidth}px"`
86     }
87     const required = !trigger[element.tag] && element.required ? 'required' : ''
88     const tagDom = tags[element.tag] ? tags[element.tag](element) : null
89     let str = `<el-form-item ${labelWidth} label="${element.label}" prop="${element.vModel}" ${required}>
90         ${tagDom}
91       </el-form-item>`
92     str = colWrapper(element, str)
93     return str
94   },
95   rowFormItem(element) {
96     const type = element.type === 'default' ? '' : `type="${element.type}"`
97     const justify = element.type === 'default' ? '' : `justify="${element.justify}"`
98     const align = element.type === 'default' ? '' : `align="${element.align}"`
99     const gutter = element.gutter ? `gutter="${element.gutter}"` : ''
100     const children = element.children.map(el => layouts[el.layout](el))
101     let str = `<el-row ${type} ${justify} ${align} ${gutter}>
102       ${children.join('\n')}
103     </el-row>`
104     str = colWrapper(element, str)
105     return str
106   }
107 }
108
109 const tags = {
110   'el-button': el => {
111     const {
112       tag, disabled
113     } = attrBuilder(el)
114     const type = el.type ? `type="${el.type}"` : ''
115     const icon = el.icon ? `icon="${el.icon}"` : ''
116     const size = el.size ? `size="${el.size}"` : ''
117     let child = buildElButtonChild(el)
118
119     if (child) child = `\n${child}\n` // 换行
120     return `<${el.tag} ${type} ${icon} ${size} ${disabled}>${child}</${el.tag}>`
121   },
122   'el-input': el => {
123     const {
124       disabled, vModel, clearable, placeholder, width
125     } = attrBuilder(el)
126     const maxlength = el.maxlength ? `:maxlength="${el.maxlength}"` : ''
127     const showWordLimit = el['show-word-limit'] ? 'show-word-limit' : ''
128     const readonly = el.readonly ? 'readonly' : ''
129     const prefixIcon = el['prefix-icon'] ? `prefix-icon='${el['prefix-icon']}'` : ''
130     const suffixIcon = el['suffix-icon'] ? `suffix-icon='${el['suffix-icon']}'` : ''
131     const showPassword = el['show-password'] ? 'show-password' : ''
132     const type = el.type ? `type="${el.type}"` : ''
133     const autosize = el.autosize && el.autosize.minRows
134       ? `:autosize="{minRows: ${el.autosize.minRows}, maxRows: ${el.autosize.maxRows}}"`
135       : ''
136     let child = buildElInputChild(el)
137
138     if (child) child = `\n${child}\n` // 换行
139     return `<${el.tag} ${vModel} ${type} ${placeholder} ${maxlength} ${showWordLimit} ${readonly} ${disabled} ${clearable} ${prefixIcon} ${suffixIcon} ${showPassword} ${autosize} ${width}>${child}</${el.tag}>`
140   },
141   'el-input-number': el => {
142     const { disabled, vModel, placeholder } = attrBuilder(el)
143     const controlsPosition = el['controls-position'] ? `controls-position=${el['controls-position']}` : ''
144     const min = el.min ? `:min='${el.min}'` : ''
145     const max = el.max ? `:max='${el.max}'` : ''
146     const step = el.step ? `:step='${el.step}'` : ''
147     const stepStrictly = el['step-strictly'] ? 'step-strictly' : ''
148     const precision = el.precision ? `:precision='${el.precision}'` : ''
149
150     return `<${el.tag} ${vModel} ${placeholder} ${step} ${stepStrictly} ${precision} ${controlsPosition} ${min} ${max} ${disabled}></${el.tag}>`
151   },
152   'el-select': el => {
153     const {
154       disabled, vModel, clearable, placeholder, width
155     } = attrBuilder(el)
156     const filterable = el.filterable ? 'filterable' : ''
157     const multiple = el.multiple ? 'multiple' : ''
158     let child = buildElSelectChild(el)
159
160     if (child) child = `\n${child}\n` // 换行
161     return `<${el.tag} ${vModel} ${placeholder} ${disabled} ${multiple} ${filterable} ${clearable} ${width}>${child}</${el.tag}>`
162   },
163   'el-radio-group': el => {
164     const { disabled, vModel } = attrBuilder(el)
165     const size = `size="${el.size}"`
166     let child = buildElRadioGroupChild(el)
167
168     if (child) child = `\n${child}\n` // 换行
169     return `<${el.tag} ${vModel} ${size} ${disabled}>${child}</${el.tag}>`
170   },
171   'el-checkbox-group': el => {
172     const { disabled, vModel } = attrBuilder(el)
173     const size = `size="${el.size}"`
174     const min = el.min ? `:min="${el.min}"` : ''
175     const max = el.max ? `:max="${el.max}"` : ''
176     let child = buildElCheckboxGroupChild(el)
177
178     if (child) child = `\n${child}\n` // 换行
179     return `<${el.tag} ${vModel} ${min} ${max} ${size} ${disabled}>${child}</${el.tag}>`
180   },
181   'el-switch': el => {
182     const { disabled, vModel } = attrBuilder(el)
183     const activeText = el['active-text'] ? `active-text="${el['active-text']}"` : ''
184     const inactiveText = el['inactive-text'] ? `inactive-text="${el['inactive-text']}"` : ''
185     const activeColor = el['active-color'] ? `active-color="${el['active-color']}"` : ''
186     const inactiveColor = el['inactive-color'] ? `inactive-color="${el['inactive-color']}"` : ''
187     const activeValue = el['active-value'] !== true ? `:active-value='${JSON.stringify(el['active-value'])}'` : ''
188     const inactiveValue = el['inactive-value'] !== false ? `:inactive-value='${JSON.stringify(el['inactive-value'])}'` : ''
189
190     return `<${el.tag} ${vModel} ${activeText} ${inactiveText} ${activeColor} ${inactiveColor} ${activeValue} ${inactiveValue} ${disabled}></${el.tag}>`
191   },
192   'el-cascader': el => {
193     const {
194       disabled, vModel, clearable, placeholder, width
195     } = attrBuilder(el)
196     const options = el.options ? `:options="${el.vModel}Options"` : ''
197     const props = el.props ? `:props="${el.vModel}Props"` : ''
198     const showAllLevels = el['show-all-levels'] ? '' : ':show-all-levels="false"'
199     const filterable = el.filterable ? 'filterable' : ''
200     const separator = el.separator === '/' ? '' : `separator="${el.separator}"`
201
202     return `<${el.tag} ${vModel} ${options} ${props} ${width} ${showAllLevels} ${placeholder} ${separator} ${filterable} ${clearable} ${disabled}></${el.tag}>`
203   },
204   'el-slider': el => {
205     const { disabled, vModel } = attrBuilder(el)
206     const min = el.min ? `:min='${el.min}'` : ''
207     const max = el.max ? `:max='${el.max}'` : ''
208     const step = el.step ? `:step='${el.step}'` : ''
209     const range = el.range ? 'range' : ''
210     const showStops = el['show-stops'] ? `:show-stops="${el['show-stops']}"` : ''
211
212     return `<${el.tag} ${min} ${max} ${step} ${vModel} ${range} ${showStops} ${disabled}></${el.tag}>`
213   },
214   'el-time-picker': el => {
215     const {
216       disabled, vModel, clearable, placeholder, width
217     } = attrBuilder(el)
218     const startPlaceholder = el['start-placeholder'] ? `start-placeholder="${el['start-placeholder']}"` : ''
219     const endPlaceholder = el['end-placeholder'] ? `end-placeholder="${el['end-placeholder']}"` : ''
220     const rangeSeparator = el['range-separator'] ? `range-separator="${el['range-separator']}"` : ''
221     const isRange = el['is-range'] ? 'is-range' : ''
222     const format = el.format ? `format="${el.format}"` : ''
223     const valueFormat = el['value-format'] ? `value-format="${el['value-format']}"` : ''
224     const pickerOptions = el['picker-options'] ? `:picker-options='${JSON.stringify(el['picker-options'])}'` : ''
225
226     return `<${el.tag} ${vModel} ${isRange} ${format} ${valueFormat} ${pickerOptions} ${width} ${placeholder} ${startPlaceholder} ${endPlaceholder} ${rangeSeparator} ${clearable} ${disabled}></${el.tag}>`
227   },
228   'el-date-picker': el => {
229     const {
230       disabled, vModel, clearable, placeholder, width
231     } = attrBuilder(el)
232     const startPlaceholder = el['start-placeholder'] ? `start-placeholder="${el['start-placeholder']}"` : ''
233     const endPlaceholder = el['end-placeholder'] ? `end-placeholder="${el['end-placeholder']}"` : ''
234     const rangeSeparator = el['range-separator'] ? `range-separator="${el['range-separator']}"` : ''
235     const format = el.format ? `format="${el.format}"` : ''
236     const valueFormat = el['value-format'] ? `value-format="${el['value-format']}"` : ''
237     const type = el.type === 'date' ? '' : `type="${el.type}"`
238     const readonly = el.readonly ? 'readonly' : ''
239
240     return `<${el.tag} ${type} ${vModel} ${format} ${valueFormat} ${width} ${placeholder} ${startPlaceholder} ${endPlaceholder} ${rangeSeparator} ${clearable} ${readonly} ${disabled}></${el.tag}>`
241   },
242   'el-rate': el => {
243     const { disabled, vModel } = attrBuilder(el)
244     const max = el.max ? `:max='${el.max}'` : ''
245     const allowHalf = el['allow-half'] ? 'allow-half' : ''
246     const showText = el['show-text'] ? 'show-text' : ''
247     const showScore = el['show-score'] ? 'show-score' : ''
248
249     return `<${el.tag} ${vModel} ${allowHalf} ${showText} ${showScore} ${disabled}></${el.tag}>`
250   },
251   'el-color-picker': el => {
252     const { disabled, vModel } = attrBuilder(el)
253     const size = `size="${el.size}"`
254     const showAlpha = el['show-alpha'] ? 'show-alpha' : ''
255     const colorFormat = el['color-format'] ? `color-format="${el['color-format']}"` : ''
256
257     return `<${el.tag} ${vModel} ${size} ${showAlpha} ${colorFormat} ${disabled}></${el.tag}>`
258   },
259   'el-upload': el => {
260     const disabled = el.disabled ? ':disabled=\'true\'' : ''
261     const action = el.action ? `:action="${el.vModel}Action"` : ''
262     const multiple = el.multiple ? 'multiple' : ''
263     const listType = el['list-type'] !== 'text' ? `list-type="${el['list-type']}"` : ''
264     const accept = el.accept ? `accept="${el.accept}"` : ''
265     const name = el.name !== 'file' ? `name="${el.name}"` : ''
266     const autoUpload = el['auto-upload'] === false ? ':auto-upload="false"' : ''
267     const beforeUpload = `:before-upload="${el.vModel}BeforeUpload"`
268     const fileList = `:file-list="${el.vModel}fileList"`
269     const ref = `ref="${el.vModel}"`
270     let child = buildElUploadChild(el)
271
272     if (child) child = `\n${child}\n` // 换行
273     return `<${el.tag} ${ref} ${fileList} ${action} ${autoUpload} ${multiple} ${beforeUpload} ${listType} ${accept} ${name} ${disabled}>${child}</${el.tag}>`
274   }
275 }
276
277 function attrBuilder(el) {
278   return {
279     vModel: `v-model="${confGlobal.formModel}.${el.vModel}"`,
280     clearable: el.clearable ? 'clearable' : '',
281     placeholder: el.placeholder ? `placeholder="${el.placeholder}"` : '',
282     width: el.style && el.style.width ? ':style="{width: \'100%\'}"' : '',
283     disabled: el.disabled ? ':disabled=\'true\'' : ''
284   }
285 }
286
287 // el-buttin 子级
288 function buildElButtonChild(conf) {
289   const children = []
290   if (conf.default) {
291     children.push(conf.default)
292   }
293   return children.join('\n')
294 }
295
296 // el-input innerHTML
297 function buildElInputChild(conf) {
298   const children = []
299   if (conf.prepend) {
300     children.push(`<template slot="prepend">${conf.prepend}</template>`)
301   }
302   if (conf.append) {
303     children.push(`<template slot="append">${conf.append}</template>`)
304   }
305   return children.join('\n')
306 }
307
308 function buildElSelectChild(conf) {
309   const children = []
310   if (conf.options && conf.options.length) {
311     children.push(`<el-option v-for="(item, index) in ${conf.vModel}Options" :key="index" :label="item.label" :value="item.value" :disabled="item.disabled"></el-option>`)
312   }
313   return children.join('\n')
314 }
315
316 function buildElRadioGroupChild(conf) {
317   const children = []
318   if (conf.options && conf.options.length) {
319     const tag = conf.optionType === 'button' ? 'el-radio-button' : 'el-radio'
320     const border = conf.border ? 'border' : ''
321     children.push(`<${tag} v-for="(item, index) in ${conf.vModel}Options" :key="index" :label="item.value" :disabled="item.disabled" ${border}>{{item.label}}</${tag}>`)
322   }
323   return children.join('\n')
324 }
325
326 function buildElCheckboxGroupChild(conf) {
327   const children = []
328   if (conf.options && conf.options.length) {
329     const tag = conf.optionType === 'button' ? 'el-checkbox-button' : 'el-checkbox'
330     const border = conf.border ? 'border' : ''
331     children.push(`<${tag} v-for="(item, index) in ${conf.vModel}Options" :key="index" :label="item.value" :disabled="item.disabled" ${border}>{{item.label}}</${tag}>`)
332   }
333   return children.join('\n')
334 }
335
336 function buildElUploadChild(conf) {
337   const list = []
338   if (conf['list-type'] === 'picture-card') list.push('<i class="el-icon-plus"></i>')
339   else list.push(`<el-button size="small" type="primary" icon="el-icon-upload">${conf.buttonText}</el-button>`)
340   if (conf.showTip) list.push(`<div slot="tip" class="el-upload__tip">只能上传不超过 ${conf.fileSize}${conf.sizeUnit} 的${conf.accept}文件</div>`)
341   return list.join('\n')
342 }
343
344 export function makeUpHtml(conf, type) {
345   const htmlList = []
346   confGlobal = conf
347   someSpanIsNot24 = conf.fields.some(item => item.span !== 24)
348   conf.fields.forEach(el => {
349     htmlList.push(layouts[el.layout](el))
350   })
351   const htmlStr = htmlList.join('\n')
352
353   let temp = buildFormTemplate(conf, htmlStr, type)
354   if (type === 'dialog') {
355     temp = dialogWrapper(temp)
356   }
357   confGlobal = null
358   return temp
359 }