html.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* eslint-disable max-len */
  2. import ruleTrigger from './ruleTrigger'
  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(scheme, child, type) {
  32. let labelPosition = ''
  33. if (scheme.labelPosition !== 'right') {
  34. labelPosition = `label-position="${scheme.labelPosition}"`
  35. }
  36. const disabled = scheme.disabled ? `:disabled="${scheme.disabled}"` : ''
  37. let str = `<el-form ref="${scheme.formRef}" :model="${scheme.formModel}" :rules="${scheme.formRules}" size="${scheme.size}" ${disabled} label-width="${scheme.labelWidth}px" ${labelPosition}>
  38. ${child}
  39. ${buildFromBtns(scheme, type)}
  40. </el-form>`
  41. if (someSpanIsNot24) {
  42. str = `<el-row :gutter="${scheme.gutter}">
  43. ${str}
  44. </el-row>`
  45. }
  46. return str
  47. }
  48. function buildFromBtns(scheme, type) {
  49. let str = ''
  50. if (scheme.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(scheme, str) {
  65. if (someSpanIsNot24 || scheme.__config__.span !== 24) {
  66. return `<el-col :span="${scheme.__config__.span}">
  67. ${str}
  68. </el-col>`
  69. }
  70. return str
  71. }
  72. const layouts = {
  73. colFormItem(scheme) {
  74. const config = scheme.__config__
  75. let labelWidth = ''
  76. let label = `label="${config.label}"`
  77. if (config.labelWidth && config.labelWidth !== confGlobal.labelWidth) {
  78. labelWidth = `label-width="${config.labelWidth}px"`
  79. }
  80. if (config.showLabel === false) {
  81. labelWidth = 'label-width="0"'
  82. label = ''
  83. }
  84. const required = !ruleTrigger[config.tag] && config.required ? 'required' : ''
  85. const tagDom = tags[config.tag] ? tags[config.tag](scheme) : null
  86. let str = `<el-form-item ${labelWidth} ${label} prop="${scheme.__vModel__}" ${required}>
  87. ${tagDom}
  88. </el-form-item>`
  89. str = colWrapper(scheme, str)
  90. return str
  91. },
  92. rowFormItem(scheme) {
  93. const config = scheme.__config__
  94. const type = scheme.type === 'default' ? '' : `type="${scheme.type}"`
  95. const justify = scheme.type === 'default' ? '' : `justify="${scheme.justify}"`
  96. const align = scheme.type === 'default' ? '' : `align="${scheme.align}"`
  97. const gutter = scheme.gutter ? `:gutter="${scheme.gutter}"` : ''
  98. const children = config.children.map(el => layouts[el.__config__.layout](el))
  99. let str = `<el-row ${type} ${justify} ${align} ${gutter}>
  100. ${children.join('\n')}
  101. </el-row>`
  102. str = colWrapper(scheme, str)
  103. return str
  104. }
  105. }
  106. const tags = {
  107. 'el-button': el => {
  108. const {
  109. tag, disabled
  110. } = attrBuilder(el)
  111. const type = el.type ? `type="${el.type}"` : ''
  112. const icon = el.icon ? `icon="${el.icon}"` : ''
  113. const round = el.round ? 'round' : ''
  114. const size = el.size ? `size="${el.size}"` : ''
  115. const plain = el.plain ? 'plain' : ''
  116. const circle = el.circle ? 'circle' : ''
  117. let child = buildElButtonChild(el)
  118. if (child) child = `\n${child}\n` // 换行
  119. return `<${tag} ${type} ${icon} ${round} ${size} ${plain} ${disabled} ${circle}>${child}</${tag}>`
  120. },
  121. 'el-input': el => {
  122. const {
  123. tag, disabled, vModel, clearable, placeholder, width
  124. } = attrBuilder(el)
  125. const maxlength = el.maxlength ? `:maxlength="${el.maxlength}"` : ''
  126. const showWordLimit = el['show-word-limit'] ? 'show-word-limit' : ''
  127. const readonly = el.readonly ? 'readonly' : ''
  128. const prefixIcon = el['prefix-icon'] ? `prefix-icon='${el['prefix-icon']}'` : ''
  129. const suffixIcon = el['suffix-icon'] ? `suffix-icon='${el['suffix-icon']}'` : ''
  130. const showPassword = el['show-password'] ? 'show-password' : ''
  131. const type = el.type ? `type="${el.type}"` : ''
  132. const autosize = el.autosize && el.autosize.minRows
  133. ? `:autosize="{minRows: ${el.autosize.minRows}, maxRows: ${el.autosize.maxRows}}"`
  134. : ''
  135. let child = buildElInputChild(el)
  136. if (child) child = `\n${child}\n` // 换行
  137. return `<${tag} ${vModel} ${type} ${placeholder} ${maxlength} ${showWordLimit} ${readonly} ${disabled} ${clearable} ${prefixIcon} ${suffixIcon} ${showPassword} ${autosize} ${width}>${child}</${tag}>`
  138. },
  139. 'el-input-number': el => {
  140. const {
  141. tag, disabled, vModel, placeholder
  142. } = 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. return `<${tag} ${vModel} ${placeholder} ${step} ${stepStrictly} ${precision} ${controlsPosition} ${min} ${max} ${disabled}></${tag}>`
  150. },
  151. 'el-select': el => {
  152. const {
  153. tag, disabled, vModel, clearable, placeholder, width
  154. } = attrBuilder(el)
  155. const filterable = el.filterable ? 'filterable' : ''
  156. const multiple = el.multiple ? 'multiple' : ''
  157. let child = buildElSelectChild(el)
  158. if (child) child = `\n${child}\n` // 换行
  159. return `<${tag} ${vModel} ${placeholder} ${disabled} ${multiple} ${filterable} ${clearable} ${width}>${child}</${tag}>`
  160. },
  161. 'el-radio-group': el => {
  162. const { tag, disabled, vModel } = attrBuilder(el)
  163. const size = `size="${el.size}"`
  164. let child = buildElRadioGroupChild(el)
  165. if (child) child = `\n${child}\n` // 换行
  166. return `<${tag} ${vModel} ${size} ${disabled}>${child}</${tag}>`
  167. },
  168. 'el-checkbox-group': el => {
  169. const { tag, disabled, vModel } = attrBuilder(el)
  170. const size = `size="${el.size}"`
  171. const min = el.min ? `:min="${el.min}"` : ''
  172. const max = el.max ? `:max="${el.max}"` : ''
  173. let child = buildElCheckboxGroupChild(el)
  174. if (child) child = `\n${child}\n` // 换行
  175. return `<${tag} ${vModel} ${min} ${max} ${size} ${disabled}>${child}</${tag}>`
  176. },
  177. 'el-switch': el => {
  178. const { tag, disabled, vModel } = attrBuilder(el)
  179. const activeText = el['active-text'] ? `active-text="${el['active-text']}"` : ''
  180. const inactiveText = el['inactive-text'] ? `inactive-text="${el['inactive-text']}"` : ''
  181. const activeColor = el['active-color'] ? `active-color="${el['active-color']}"` : ''
  182. const inactiveColor = el['inactive-color'] ? `inactive-color="${el['inactive-color']}"` : ''
  183. const activeValue = el['active-value'] !== true ? `:active-value='${JSON.stringify(el['active-value'])}'` : ''
  184. const inactiveValue = el['inactive-value'] !== false ? `:inactive-value='${JSON.stringify(el['inactive-value'])}'` : ''
  185. return `<${tag} ${vModel} ${activeText} ${inactiveText} ${activeColor} ${inactiveColor} ${activeValue} ${inactiveValue} ${disabled}></${tag}>`
  186. },
  187. 'el-cascader': el => {
  188. const {
  189. tag, disabled, vModel, clearable, placeholder, width
  190. } = attrBuilder(el)
  191. const options = el.options ? `:options="${el.__vModel__}Options"` : ''
  192. const props = el.props ? `:props="${el.__vModel__}Props"` : ''
  193. const showAllLevels = el['show-all-levels'] ? '' : ':show-all-levels="false"'
  194. const filterable = el.filterable ? 'filterable' : ''
  195. const separator = el.separator === '/' ? '' : `separator="${el.separator}"`
  196. return `<${tag} ${vModel} ${options} ${props} ${width} ${showAllLevels} ${placeholder} ${separator} ${filterable} ${clearable} ${disabled}></${tag}>`
  197. },
  198. 'el-slider': el => {
  199. const { tag, disabled, vModel } = attrBuilder(el)
  200. const min = el.min ? `:min='${el.min}'` : ''
  201. const max = el.max ? `:max='${el.max}'` : ''
  202. const step = el.step ? `:step='${el.step}'` : ''
  203. const range = el.range ? 'range' : ''
  204. const showStops = el['show-stops'] ? `:show-stops="${el['show-stops']}"` : ''
  205. return `<${tag} ${min} ${max} ${step} ${vModel} ${range} ${showStops} ${disabled}></${tag}>`
  206. },
  207. 'el-time-picker': el => {
  208. const {
  209. tag, 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 isRange = el['is-range'] ? 'is-range' : ''
  215. const format = el.format ? `format="${el.format}"` : ''
  216. const valueFormat = el['value-format'] ? `value-format="${el['value-format']}"` : ''
  217. const pickerOptions = el['picker-options'] ? `:picker-options='${JSON.stringify(el['picker-options'])}'` : ''
  218. return `<${tag} ${vModel} ${isRange} ${format} ${valueFormat} ${pickerOptions} ${width} ${placeholder} ${startPlaceholder} ${endPlaceholder} ${rangeSeparator} ${clearable} ${disabled}></${tag}>`
  219. },
  220. 'el-date-picker': el => {
  221. const {
  222. tag, disabled, vModel, clearable, placeholder, width
  223. } = attrBuilder(el)
  224. const startPlaceholder = el['start-placeholder'] ? `start-placeholder="${el['start-placeholder']}"` : ''
  225. const endPlaceholder = el['end-placeholder'] ? `end-placeholder="${el['end-placeholder']}"` : ''
  226. const rangeSeparator = el['range-separator'] ? `range-separator="${el['range-separator']}"` : ''
  227. const format = el.format ? `format="${el.format}"` : ''
  228. const valueFormat = el['value-format'] ? `value-format="${el['value-format']}"` : ''
  229. const type = el.type === 'date' ? '' : `type="${el.type}"`
  230. const readonly = el.readonly ? 'readonly' : ''
  231. return `<${tag} ${type} ${vModel} ${format} ${valueFormat} ${width} ${placeholder} ${startPlaceholder} ${endPlaceholder} ${rangeSeparator} ${clearable} ${readonly} ${disabled}></${tag}>`
  232. },
  233. 'el-rate': el => {
  234. const { tag, disabled, vModel } = attrBuilder(el)
  235. const max = el.max ? `:max='${el.max}'` : ''
  236. const allowHalf = el['allow-half'] ? 'allow-half' : ''
  237. const showText = el['show-text'] ? 'show-text' : ''
  238. const showScore = el['show-score'] ? 'show-score' : ''
  239. return `<${tag} ${vModel} ${max} ${allowHalf} ${showText} ${showScore} ${disabled}></${tag}>`
  240. },
  241. 'el-color-picker': el => {
  242. const { tag, disabled, vModel } = attrBuilder(el)
  243. const size = `size="${el.size}"`
  244. const showAlpha = el['show-alpha'] ? 'show-alpha' : ''
  245. const colorFormat = el['color-format'] ? `color-format="${el['color-format']}"` : ''
  246. return `<${tag} ${vModel} ${size} ${showAlpha} ${colorFormat} ${disabled}></${tag}>`
  247. },
  248. 'el-upload': el => {
  249. const { tag } = el.__config__
  250. const disabled = el.disabled ? ':disabled=\'true\'' : ''
  251. const action = el.action ? `:action="${el.__vModel__}Action"` : ''
  252. const multiple = el.multiple ? 'multiple' : ''
  253. const listType = el['list-type'] !== 'text' ? `list-type="${el['list-type']}"` : ''
  254. const accept = el.accept ? `accept="${el.accept}"` : ''
  255. const name = el.name !== 'file' ? `name="${el.name}"` : ''
  256. const autoUpload = el['auto-upload'] === false ? ':auto-upload="false"' : ''
  257. const beforeUpload = `:before-upload="${el.__vModel__}BeforeUpload"`
  258. const fileList = `:file-list="${el.__vModel__}fileList"`
  259. const ref = `ref="${el.__vModel__}"`
  260. let child = buildElUploadChild(el)
  261. if (child) child = `\n${child}\n` // 换行
  262. return `<${tag} ${ref} ${fileList} ${action} ${autoUpload} ${multiple} ${beforeUpload} ${listType} ${accept} ${name} ${disabled}>${child}</${tag}>`
  263. },
  264. tinymce: el => {
  265. const { tag, vModel, placeholder } = attrBuilder(el)
  266. const height = el.height ? `:height="${el.height}"` : ''
  267. const branding = el.branding ? `:branding="${el.branding}"` : ''
  268. return `<${tag} ${vModel} ${placeholder} ${height} ${branding}></${tag}>`
  269. }
  270. }
  271. function attrBuilder(el) {
  272. return {
  273. tag: el.__config__.tag,
  274. vModel: `v-model="${confGlobal.formModel}.${el.__vModel__}"`,
  275. clearable: el.clearable ? 'clearable' : '',
  276. placeholder: el.placeholder ? `placeholder="${el.placeholder}"` : '',
  277. width: el.style && el.style.width ? ':style="{width: \'100%\'}"' : '',
  278. disabled: el.disabled ? ':disabled=\'true\'' : ''
  279. }
  280. }
  281. // el-buttin 子级
  282. function buildElButtonChild(scheme) {
  283. const children = []
  284. const slot = scheme.__slot__ || {}
  285. if (slot.default) {
  286. children.push(slot.default)
  287. }
  288. return children.join('\n')
  289. }
  290. // el-input 子级
  291. function buildElInputChild(scheme) {
  292. const children = []
  293. const slot = scheme.__slot__
  294. if (slot && slot.prepend) {
  295. children.push(`<template slot="prepend">${slot.prepend}</template>`)
  296. }
  297. if (slot && slot.append) {
  298. children.push(`<template slot="append">${slot.append}</template>`)
  299. }
  300. return children.join('\n')
  301. }
  302. // el-select 子级
  303. function buildElSelectChild(scheme) {
  304. const children = []
  305. const slot = scheme.__slot__
  306. if (slot && slot.options && slot.options.length) {
  307. children.push(`<el-option v-for="(item, index) in ${scheme.__vModel__}Options" :key="index" :label="item.label" :value="item.value" :disabled="item.disabled"></el-option>`)
  308. }
  309. return children.join('\n')
  310. }
  311. // el-radio-group 子级
  312. function buildElRadioGroupChild(scheme) {
  313. const children = []
  314. const slot = scheme.__slot__
  315. const config = scheme.__config__
  316. if (slot && slot.options && slot.options.length) {
  317. const tag = config.optionType === 'button' ? 'el-radio-button' : 'el-radio'
  318. const border = config.border ? 'border' : ''
  319. children.push(`<${tag} v-for="(item, index) in ${scheme.__vModel__}Options" :key="index" :label="item.value" :disabled="item.disabled" ${border}>{{item.label}}</${tag}>`)
  320. }
  321. return children.join('\n')
  322. }
  323. // el-checkbox-group 子级
  324. function buildElCheckboxGroupChild(scheme) {
  325. const children = []
  326. const slot = scheme.__slot__
  327. const config = scheme.__config__
  328. if (slot && slot.options && slot.options.length) {
  329. const tag = config.optionType === 'button' ? 'el-checkbox-button' : 'el-checkbox'
  330. const border = config.border ? 'border' : ''
  331. children.push(`<${tag} v-for="(item, index) in ${scheme.__vModel__}Options" :key="index" :label="item.value" :disabled="item.disabled" ${border}>{{item.label}}</${tag}>`)
  332. }
  333. return children.join('\n')
  334. }
  335. // el-upload 子级
  336. function buildElUploadChild(scheme) {
  337. const list = []
  338. const config = scheme.__config__
  339. if (scheme['list-type'] === 'picture-card') list.push('<i class="el-icon-plus"></i>')
  340. else list.push(`<el-button size="small" type="primary" icon="el-icon-upload">${config.buttonText}</el-button>`)
  341. if (config.showTip) list.push(`<div slot="tip" class="el-upload__tip">只能上传不超过 ${config.fileSize}${config.sizeUnit} 的${scheme.accept}文件</div>`)
  342. return list.join('\n')
  343. }
  344. /**
  345. * 组装html代码。【入口函数】
  346. * @param {Object} formConfig 整个表单配置
  347. * @param {String} type 生成类型,文件或弹窗等
  348. */
  349. export function makeUpHtml(formConfig, type) {
  350. const htmlList = []
  351. confGlobal = formConfig
  352. // 判断布局是否都沾满了24个栅格,以备后续简化代码结构
  353. someSpanIsNot24 = formConfig.fields.some(item => item.__config__.span !== 24)
  354. // 遍历渲染每个组件成html
  355. formConfig.fields.forEach(el => {
  356. htmlList.push(layouts[el.__config__.layout](el))
  357. })
  358. const htmlStr = htmlList.join('\n')
  359. // 将组件代码放进form标签
  360. let temp = buildFormTemplate(formConfig, htmlStr, type)
  361. // dialog标签包裹代码
  362. if (type === 'dialog') {
  363. temp = dialogWrapper(temp)
  364. }
  365. confGlobal = null
  366. return temp
  367. }