html.js 16 KB

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