html.js 15 KB

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