html.js 15 KB

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