html.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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-input': el => {
  101. const {
  102. disabled, vModel, clearable, placeholder, width
  103. } = attrBuilder(el)
  104. const maxlength = el.maxlength ? `:maxlength="${el.maxlength}"` : ''
  105. const showWordLimit = el['show-word-limit'] ? 'show-word-limit' : ''
  106. const readonly = el.readonly ? 'readonly' : ''
  107. const prefixIcon = el['prefix-icon'] ? `prefix-icon='${el['prefix-icon']}'` : ''
  108. const suffixIcon = el['suffix-icon'] ? `suffix-icon='${el['suffix-icon']}'` : ''
  109. const showPassword = el['show-password'] ? 'show-password' : ''
  110. const type = el.type ? `type="${el.type}"` : ''
  111. const autosize = el.autosize && el.autosize.minRows
  112. ? `:autosize="{minRows: ${el.autosize.minRows}, maxRows: ${el.autosize.maxRows}}"`
  113. : ''
  114. let child = buildElInputChild(el)
  115. if (child) child = `\n${child}\n` // 换行
  116. return `<${el.tag} ${vModel} ${type} ${placeholder} ${maxlength} ${showWordLimit} ${readonly} ${disabled} ${clearable} ${prefixIcon} ${suffixIcon} ${showPassword} ${autosize} ${width}>${child}</${el.tag}>`
  117. },
  118. 'el-input-number': el => {
  119. const { disabled, vModel, placeholder } = attrBuilder(el)
  120. const controlsPosition = el['controls-position'] ? `controls-position=${el['controls-position']}` : ''
  121. const min = el.min ? `:min='${el.min}'` : ''
  122. const max = el.max ? `:max='${el.max}'` : ''
  123. const step = el.step ? `:step='${el.step}'` : ''
  124. const stepStrictly = el['step-strictly'] ? 'step-strictly' : ''
  125. const precision = el.precision ? `:precision='${el.precision}'` : ''
  126. return `<${el.tag} ${vModel} ${placeholder} ${step} ${stepStrictly} ${precision} ${controlsPosition} ${min} ${max} ${disabled}></${el.tag}>`
  127. },
  128. 'el-select': el => {
  129. const {
  130. disabled, vModel, clearable, placeholder, width
  131. } = attrBuilder(el)
  132. const filterable = el.filterable ? 'filterable' : ''
  133. const multiple = el.multiple ? 'multiple' : ''
  134. let child = buildElSelectChild(el)
  135. if (child) child = `\n${child}\n` // 换行
  136. return `<${el.tag} ${vModel} ${placeholder} ${disabled} ${multiple} ${filterable} ${clearable} ${width}>${child}</${el.tag}>`
  137. },
  138. 'el-radio-group': el => {
  139. const { disabled, vModel } = attrBuilder(el)
  140. const size = `size="${el.size}"`
  141. let child = buildElRadioGroupChild(el)
  142. if (child) child = `\n${child}\n` // 换行
  143. return `<${el.tag} ${vModel} ${size} ${disabled}>${child}</${el.tag}>`
  144. },
  145. 'el-checkbox-group': el => {
  146. const { disabled, vModel } = attrBuilder(el)
  147. const size = `size="${el.size}"`
  148. const min = el.min ? `:min="${el.min}"` : ''
  149. const max = el.max ? `:max="${el.max}"` : ''
  150. let child = buildElCheckboxGroupChild(el)
  151. if (child) child = `\n${child}\n` // 换行
  152. return `<${el.tag} ${vModel} ${min} ${max} ${size} ${disabled}>${child}</${el.tag}>`
  153. },
  154. 'el-switch': el => {
  155. const { disabled, vModel } = attrBuilder(el)
  156. const activeText = el['active-text'] ? `active-text="${el['active-text']}"` : ''
  157. const inactiveText = el['inactive-text'] ? `inactive-text="${el['inactive-text']}"` : ''
  158. const activeColor = el['active-color'] ? `active-color="${el['active-color']}"` : ''
  159. const inactiveColor = el['inactive-color'] ? `inactive-color="${el['inactive-color']}"` : ''
  160. const activeValue = el['active-value'] !== true ? `:active-value='${JSON.stringify(el['active-value'])}'` : ''
  161. const inactiveValue = el['inactive-value'] !== false ? `:inactive-value='${JSON.stringify(el['inactive-value'])}'` : ''
  162. return `<${el.tag} ${vModel} ${activeText} ${inactiveText} ${activeColor} ${inactiveColor} ${activeValue} ${inactiveValue} ${disabled}></${el.tag}>`
  163. },
  164. 'el-cascader': el => {
  165. const {
  166. disabled, vModel, clearable, placeholder, width
  167. } = attrBuilder(el)
  168. const options = el.options ? `:options="${el.vModel}Options"` : ''
  169. const props = el.props ? `:props="${el.vModel}Props"` : ''
  170. const showAllLevels = el['show-all-levels'] ? '' : ':show-all-levels="false"'
  171. const filterable = el.filterable ? 'filterable' : ''
  172. const separator = el.separator === '/' ? '' : `separator="${el.separator}"`
  173. return `<${el.tag} ${vModel} ${options} ${props} ${width} ${showAllLevels} ${placeholder} ${separator} ${filterable} ${clearable} ${disabled}></${el.tag}>`
  174. },
  175. 'el-slider': el => {
  176. const { disabled, vModel } = attrBuilder(el)
  177. const min = el.min ? `:min='${el.min}'` : ''
  178. const max = el.max ? `:max='${el.max}'` : ''
  179. const step = el.step ? `:step='${el.step}'` : ''
  180. const range = el.range ? 'range' : ''
  181. const showStops = el['show-stops'] ? `:show-stops="${el['show-stops']}"` : ''
  182. return `<${el.tag} ${min} ${max} ${step} ${vModel} ${range} ${showStops} ${disabled}></${el.tag}>`
  183. },
  184. 'el-time-picker': el => {
  185. const {
  186. disabled, vModel, clearable, placeholder, width
  187. } = attrBuilder(el)
  188. const startPlaceholder = el['start-placeholder'] ? `start-placeholder="${el['start-placeholder']}"` : ''
  189. const endPlaceholder = el['end-placeholder'] ? `end-placeholder="${el['end-placeholder']}"` : ''
  190. const rangeSeparator = el['range-separator'] ? `range-separator="${el['range-separator']}"` : ''
  191. const isRange = el['is-range'] ? 'is-range' : ''
  192. const format = el.format ? `format="${el.format}"` : ''
  193. const valueFormat = el['value-format'] ? `value-format="${el['value-format']}"` : ''
  194. const pickerOptions = el['picker-options'] ? `:picker-options='${JSON.stringify(el['picker-options'])}'` : ''
  195. return `<${el.tag} ${vModel} ${isRange} ${format} ${valueFormat} ${pickerOptions} ${width} ${placeholder} ${startPlaceholder} ${endPlaceholder} ${rangeSeparator} ${clearable} ${disabled}></${el.tag}>`
  196. },
  197. 'el-date-picker': el => {
  198. const {
  199. disabled, vModel, clearable, placeholder, width
  200. } = attrBuilder(el)
  201. const startPlaceholder = el['start-placeholder'] ? `start-placeholder="${el['start-placeholder']}"` : ''
  202. const endPlaceholder = el['end-placeholder'] ? `end-placeholder="${el['end-placeholder']}"` : ''
  203. const rangeSeparator = el['range-separator'] ? `range-separator="${el['range-separator']}"` : ''
  204. const format = el.format ? `format="${el.format}"` : ''
  205. const valueFormat = el['value-format'] ? `value-format="${el['value-format']}"` : ''
  206. const type = el.type === 'date' ? '' : `type="${el.type}"`
  207. const readonly = el.readonly ? 'readonly' : ''
  208. return `<${el.tag} ${type} ${vModel} ${format} ${valueFormat} ${width} ${placeholder} ${startPlaceholder} ${endPlaceholder} ${rangeSeparator} ${clearable} ${readonly} ${disabled}></${el.tag}>`
  209. },
  210. 'el-rate': el => {
  211. const { disabled, vModel } = attrBuilder(el)
  212. const max = el.max ? `:max='${el.max}'` : ''
  213. const allowHalf = el['allow-half'] ? 'allow-half' : ''
  214. const showText = el['show-text'] ? 'show-text' : ''
  215. const showScore = el['show-score'] ? 'show-score' : ''
  216. return `<${el.tag} ${vModel} ${allowHalf} ${showText} ${showScore} ${disabled}></${el.tag}>`
  217. },
  218. 'el-color-picker': el => {
  219. const { disabled, vModel } = attrBuilder(el)
  220. const size = `size="${el.size}"`
  221. const showAlpha = el['show-alpha'] ? 'show-alpha' : ''
  222. const colorFormat = el['color-format'] ? `color-format="${el['color-format']}"` : ''
  223. return `<${el.tag} ${vModel} ${size} ${showAlpha} ${colorFormat} ${disabled}></${el.tag}>`
  224. },
  225. 'el-upload': el => {
  226. const disabled = el.disabled ? ':disabled=\'true\'' : ''
  227. const action = el.action ? `:action="${el.vModel}Action"` : ''
  228. const multiple = el.multiple ? 'multiple' : ''
  229. const listType = el['list-type'] !== 'text' ? `list-type="${el['list-type']}"` : ''
  230. const accept = el.accept ? `accept="${el.accept}"` : ''
  231. const name = el.name !== 'file' ? `name="${el.name}"` : ''
  232. const autoUpload = el['auto-upload'] === false ? ':auto-upload="false"' : ''
  233. const beforeUpload = `:before-upload="${el.vModel}BeforeUpload"`
  234. const fileList = `:file-list="${el.vModel}fileList"`
  235. const ref = `ref="${el.vModel}"`
  236. let child = buildElUploadChild(el)
  237. if (child) child = `\n${child}\n` // 换行
  238. return `<${el.tag} ${ref} ${fileList} ${action} ${autoUpload} ${multiple} ${beforeUpload} ${listType} ${accept} ${name} ${disabled}>${child}</${el.tag}>`
  239. }
  240. }
  241. function attrBuilder(el) {
  242. return {
  243. vModel: `v-model="${confGlobal.formModel}.${el.vModel}"`,
  244. clearable: el.clearable ? 'clearable' : '',
  245. placeholder: el.placeholder ? `placeholder="${el.placeholder}"` : '',
  246. width: el.style && el.style.width ? ':style="{width: \'100%\'}"' : '',
  247. disabled: el.disabled ? ':disabled=\'true\'' : ''
  248. }
  249. }
  250. // el-input innerHTML
  251. function buildElInputChild(conf) {
  252. const children = []
  253. if (conf.prepend) {
  254. children.push(`<template slot="prepend">${conf.prepend}</template>`)
  255. }
  256. if (conf.append) {
  257. children.push(`<template slot="append">${conf.append}</template>`)
  258. }
  259. return children.join('\n')
  260. }
  261. function buildElSelectChild(conf) {
  262. const children = []
  263. if (conf.options && conf.options.length) {
  264. 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>`)
  265. }
  266. return children.join('\n')
  267. }
  268. function buildElRadioGroupChild(conf) {
  269. const children = []
  270. if (conf.options && conf.options.length) {
  271. const tag = conf.optionType === 'button' ? 'el-radio-button' : 'el-radio'
  272. const border = conf.border ? 'border' : ''
  273. children.push(`<${tag} v-for="(item, index) in ${conf.vModel}Options" :key="index" :label="item.value" :disabled="item.disabled" ${border}>{{item.label}}</${tag}>`)
  274. }
  275. return children.join('\n')
  276. }
  277. function buildElCheckboxGroupChild(conf) {
  278. const children = []
  279. if (conf.options && conf.options.length) {
  280. const tag = conf.optionType === 'button' ? 'el-checkbox-button' : 'el-checkbox'
  281. const border = conf.border ? 'border' : ''
  282. children.push(`<${tag} v-for="(item, index) in ${conf.vModel}Options" :key="index" :label="item.value" :disabled="item.disabled" ${border}>{{item.label}}</${tag}>`)
  283. }
  284. return children.join('\n')
  285. }
  286. function buildElUploadChild(conf) {
  287. const list = []
  288. if (conf['list-type'] === 'picture-card') list.push('<i class="el-icon-plus"></i>')
  289. else list.push(`<el-button size="small" type="primary" icon="el-icon-upload">${conf.buttonText}</el-button>`)
  290. if (conf.showTip) list.push(`<div slot="tip" class="el-upload__tip">只能上传不超过 ${conf.fileSize}${conf.sizeUnit} 的${conf.accept}文件</div>`)
  291. return list.join('\n')
  292. }
  293. export function makeUpHtml(conf, type) {
  294. const htmlList = []
  295. confGlobal = conf
  296. someSpanIsNot24 = conf.fields.some(item => item.span !== 24)
  297. conf.fields.forEach(el => {
  298. htmlList.push(layouts[el.layout](el))
  299. })
  300. const htmlStr = htmlList.join('\n')
  301. let temp = buildFormTemplate(conf, htmlStr, type)
  302. if (type === 'dialog') {
  303. temp = dialogWrapper(temp)
  304. }
  305. confGlobal = null
  306. return temp
  307. }