formEditor.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. <template>
  2. <div class="container">
  3. <div class="left-board">
  4. <div class="logo-wrapper">
  5. <div class="logo">流程表单</div>
  6. </div>
  7. <el-scrollbar class="left-scrollbar">
  8. <!-- 左边:表单项 -->
  9. <div class="components-list">
  10. <div class="components-title">
  11. <svg-icon icon-class="component" />输入型组件
  12. </div>
  13. <draggable class="components-draggable" :list="inputComponents" :group="{ name: 'componentsGroup', pull: 'clone', put: false }"
  14. :clone="cloneComponent" draggable=".components-item" :sort="false" @end="onEnd">
  15. <div v-for="(element, index) in inputComponents" :key="index" class="components-item" @click="addComponent(element)">
  16. <div class="components-body">
  17. <svg-icon :icon-class="element.tagIcon" />
  18. {{ element.label }}
  19. </div>
  20. </div>
  21. </draggable>
  22. <div class="components-title">
  23. <svg-icon icon-class="component" />选择型组件
  24. </div>
  25. <draggable class="components-draggable" :list="selectComponents" :group="{ name: 'componentsGroup', pull: 'clone', put: false }"
  26. :clone="cloneComponent" draggable=".components-item" :sort="false" @end="onEnd">
  27. <div v-for="(element, index) in selectComponents" :key="index" class="components-item" @click="addComponent(element)">
  28. <div class="components-body">
  29. <svg-icon :icon-class="element.tagIcon" />
  30. {{ element.label }}
  31. </div>
  32. </div>
  33. </draggable>
  34. <div class="components-title">
  35. <svg-icon icon-class="component" />布局型组件
  36. </div>
  37. <draggable class="components-draggable" :list="layoutComponents" :group="{ name: 'componentsGroup', pull: 'clone', put: false }"
  38. :clone="cloneComponent" draggable=".components-item" :sort="false" @end="onEnd">
  39. <div v-for="(element, index) in layoutComponents" :key="index" class="components-item" @click="addComponent(element)">
  40. <div class="components-body">
  41. <svg-icon :icon-class="element.tagIcon" />
  42. {{ element.label }}
  43. </div>
  44. </div>
  45. </draggable>
  46. <!-- 左边:动态表单 -->
  47. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  48. <el-form-item label="表单名" prop="name">
  49. <el-input v-model="form.name" placeholder="请输入表单名" />
  50. </el-form-item>
  51. <el-form-item label="开启状态" prop="status">
  52. <el-radio-group v-model="form.status">
  53. <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.SYS_COMMON_STATUS)"
  54. :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
  55. </el-radio-group>
  56. </el-form-item>
  57. <el-form-item label="备注" prop="remark">
  58. <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
  59. </el-form-item>
  60. </el-form>
  61. </div>
  62. </el-scrollbar>
  63. </div>
  64. <div class="center-board">
  65. <!-- 上面:操作按钮 -->
  66. <div class="action-bar">
  67. <el-button icon="el-icon-check" type="text" @click="save">保存</el-button>
  68. <el-button class="delete-btn" icon="el-icon-delete" type="text" @click="empty">清空</el-button>
  69. </div>
  70. <!-- 中间,表单项 -->
  71. <el-scrollbar class="center-scrollbar">
  72. <el-row class="center-board-row" :gutter="formConf.gutter">
  73. <el-form :size="formConf.size" :label-position="formConf.labelPosition" :disabled="formConf.disabled"
  74. :label-width="formConf.labelWidth + 'px'">
  75. <draggable class="drawing-board" :list="drawingList" :animation="340" group="componentsGroup">
  76. <draggable-item v-for="(element, index) in drawingList" :key="element.renderKey" :drawing-list="drawingList"
  77. :element="element" :index="index" :active-id="activeId" :form-conf="formConf"
  78. @activeItem="activeFormItem" @copyItem="drawingItemCopy" @deleteItem="drawingItemDelete"/>
  79. </draggable>
  80. <div v-show="!drawingList.length" class="empty-info">
  81. 从左侧拖入或点选组件进行表单设计
  82. </div>
  83. </el-form>
  84. </el-row>
  85. </el-scrollbar>
  86. </div>
  87. <!-- 右边:组件属性/表单属性 -->
  88. <right-panel :active-data="activeData" :form-conf="formConf" :show-field="!!drawingList.length" @tag-change="tagChange"/>
  89. </div>
  90. </template>
  91. <script>
  92. import draggable from 'vuedraggable'
  93. import render from '@/utils/generator/render'
  94. import { inputComponents, selectComponents, layoutComponents, formConf} from '@/utils/generator/config'
  95. import drawingDefalut from '@/utils/generator/drawingDefalut'
  96. // import logo from '@/assets/logo/logo.png'
  97. import DraggableItem from './../../tool/build/DraggableItem'
  98. import RightPanel from './../../tool/build/RightPanel'
  99. import {createForm, getForm, updateForm} from "@/api/bpm/form";
  100. import {SysCommonStatusEnum} from "@/utils/constants";
  101. // const emptyActiveData = { style: {}, autosize: {} }
  102. let oldActiveId
  103. let tempActiveData
  104. export default {
  105. components: {
  106. draggable,
  107. render,
  108. RightPanel,
  109. DraggableItem
  110. },
  111. data() {
  112. return {
  113. // logo,
  114. idGlobal: 100, // 自动 ID 生成时,全局递增。例如说 field100
  115. formConf, // 表单格式
  116. inputComponents,
  117. selectComponents,
  118. layoutComponents,
  119. labelWidth: 100,
  120. drawingData: {}, // 生成后的表单数据
  121. drawingList: [], // 表单项的数组
  122. activeId: undefined,
  123. activeData: {},
  124. // drawerVisible: false,
  125. // formData: {},
  126. // dialogVisible: false,
  127. // showFileName: false,
  128. // 表单参数
  129. form: {
  130. status: SysCommonStatusEnum.ENABLE,
  131. },
  132. // 表单校验
  133. rules: {
  134. name: [{ required: true, message: "表单名不能为空", trigger: "blur" }],
  135. status: [{ required: true, message: "开启状态不能为空", trigger: "blur" }],
  136. }
  137. }
  138. },
  139. watch: {
  140. // eslint-disable-next-line func-names
  141. 'activeData.label': function (val, oldVal) {
  142. if (
  143. this.activeData.placeholder === undefined
  144. || !this.activeData.tag
  145. || oldActiveId !== this.activeId
  146. ) {
  147. return
  148. }
  149. this.activeData.placeholder = this.activeData.placeholder.replace(oldVal, '') + val
  150. },
  151. activeId: {
  152. handler(val) {
  153. oldActiveId = val
  154. },
  155. immediate: true
  156. }
  157. },
  158. created() {
  159. // 读取表单配置
  160. const formId = this.$route.query && this.$route.query.formId
  161. if (formId) {
  162. getForm(formId).then(response => {
  163. const data = response.data
  164. this.form = {
  165. id: data.id,
  166. name: data.name,
  167. status: data.status,
  168. remark: data.remark
  169. }
  170. this.formConf = JSON.parse(data.conf)
  171. this.drawingList = this.decodeFields(data.fields)
  172. // 设置 idGlobal,避免重复
  173. this.idGlobal += this.drawingList.length
  174. });
  175. }
  176. },
  177. methods: {
  178. activeFormItem(element) {
  179. this.activeData = element
  180. this.activeId = element.formId
  181. },
  182. onEnd(obj, a) {
  183. if (obj.from !== obj.to) {
  184. this.activeData = tempActiveData
  185. this.activeId = this.idGlobal
  186. }
  187. },
  188. addComponent(item) {
  189. const clone = this.cloneComponent(item)
  190. this.drawingList.push(clone)
  191. this.activeFormItem(clone)
  192. },
  193. cloneComponent(origin) {
  194. const clone = JSON.parse(JSON.stringify(origin))
  195. clone.formId = ++this.idGlobal
  196. clone.span = formConf.span
  197. clone.renderKey = +new Date() // 改变renderKey后可以实现强制更新组件
  198. if (!clone.layout) clone.layout = 'colFormItem'
  199. if (clone.layout === 'colFormItem') {
  200. clone.vModel = `field${this.idGlobal}`
  201. clone.placeholder !== undefined && (clone.placeholder += clone.label)
  202. tempActiveData = clone
  203. } else if (clone.layout === 'rowFormItem') {
  204. delete clone.label
  205. clone.componentName = `row${this.idGlobal}`
  206. clone.gutter = this.formConf.gutter
  207. tempActiveData = clone
  208. }
  209. return tempActiveData
  210. },
  211. // 获得表单数据
  212. AssembleFormData() {
  213. this.formData = {
  214. fields: JSON.parse(JSON.stringify(this.drawingList)),
  215. ...this.formConf
  216. }
  217. },
  218. save() {
  219. // this.AssembleFormData()
  220. // console.log(this.formData)
  221. this.$refs["form"].validate(valid => {
  222. if (!valid) {
  223. return;
  224. }
  225. const form = {
  226. conf: JSON.stringify(this.formConf), // 表单配置
  227. // fields: JSON.stringify(this.drawingList), // 表单项的数组
  228. fields: this.encodeFields(), // 表单项的数组
  229. ...this.form // 表单名等
  230. }
  231. // 修改的提交
  232. if (this.form.id != null) {
  233. updateForm(form).then(response => {
  234. this.msgSuccess("修改成功");
  235. this.close()
  236. });
  237. return;
  238. }
  239. // 添加的提交
  240. createForm(form).then(response => {
  241. this.msgSuccess("新增成功");
  242. this.close()
  243. });
  244. });
  245. },
  246. /** 关闭按钮 */
  247. close() {
  248. this.$store.dispatch("tagsView/delView", this.$route);
  249. this.$router.push({ path: "/bpm/manager/form", query: { t: Date.now()}})
  250. },
  251. encodeFields() {
  252. const fields = []
  253. this.drawingList.forEach(item => {
  254. fields.push(JSON.stringify(item))
  255. })
  256. return fields
  257. },
  258. decodeFields(fields) {
  259. const drawingList = []
  260. fields.forEach(item => {
  261. drawingList.push(JSON.parse(item))
  262. })
  263. return drawingList
  264. },
  265. empty() {
  266. this.$confirm('确定要清空所有组件吗?', '提示', { type: 'warning' }).then(
  267. () => {
  268. this.drawingList = []
  269. }
  270. )
  271. },
  272. drawingItemCopy(item, parent) {
  273. let clone = JSON.parse(JSON.stringify(item))
  274. clone = this.createIdAndKey(clone)
  275. parent.push(clone)
  276. this.activeFormItem(clone)
  277. },
  278. createIdAndKey(item) {
  279. item.formId = ++this.idGlobal
  280. item.renderKey = +new Date()
  281. if (item.layout === 'colFormItem') {
  282. item.vModel = `field${this.idGlobal}`
  283. } else if (item.layout === 'rowFormItem') {
  284. item.componentName = `row${this.idGlobal}`
  285. }
  286. if (Array.isArray(item.children)) {
  287. item.children = item.children.map(childItem => this.createIdAndKey(childItem))
  288. }
  289. return item
  290. },
  291. drawingItemDelete(index, parent) {
  292. parent.splice(index, 1)
  293. this.$nextTick(() => {
  294. const len = this.drawingList.length
  295. if (len) {
  296. this.activeFormItem(this.drawingList[len - 1])
  297. }
  298. })
  299. },
  300. tagChange(newTag) {
  301. newTag = this.cloneComponent(newTag)
  302. newTag.vModel = this.activeData.vModel
  303. newTag.formId = this.activeId
  304. newTag.span = this.activeData.span
  305. delete this.activeData.tag
  306. delete this.activeData.tagIcon
  307. delete this.activeData.document
  308. Object.keys(newTag).forEach(key => {
  309. if (this.activeData[key] !== undefined
  310. && typeof this.activeData[key] === typeof newTag[key]) {
  311. newTag[key] = this.activeData[key]
  312. }
  313. })
  314. this.activeData = newTag
  315. this.updateDrawingList(newTag, this.drawingList)
  316. },
  317. updateDrawingList(newTag, list) {
  318. const index = list.findIndex(item => item.formId === this.activeId)
  319. if (index > -1) {
  320. list.splice(index, 1, newTag)
  321. } else {
  322. list.forEach(item => {
  323. if (Array.isArray(item.children)) this.updateDrawingList(newTag, item.children)
  324. })
  325. }
  326. }
  327. }
  328. }
  329. </script>
  330. <style lang='scss'>
  331. body, html{
  332. margin: 0;
  333. padding: 0;
  334. background: #fff;
  335. -moz-osx-font-smoothing: grayscale;
  336. -webkit-font-smoothing: antialiased;
  337. text-rendering: optimizeLegibility;
  338. font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;
  339. }
  340. input, textarea{
  341. font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji;
  342. }
  343. .editor-tabs{
  344. background: #121315;
  345. .el-tabs__header{
  346. margin: 0;
  347. border-bottom-color: #121315;
  348. .el-tabs__nav{
  349. border-color: #121315;
  350. }
  351. }
  352. .el-tabs__item{
  353. height: 32px;
  354. line-height: 32px;
  355. color: #888a8e;
  356. border-left: 1px solid #121315 !important;
  357. background: #363636;
  358. margin-right: 5px;
  359. user-select: none;
  360. }
  361. .el-tabs__item.is-active{
  362. background: #1e1e1e;
  363. border-bottom-color: #1e1e1e!important;
  364. color: #fff;
  365. }
  366. .el-icon-edit{
  367. color: #f1fa8c;
  368. }
  369. .el-icon-document{
  370. color: #a95812;
  371. }
  372. }
  373. // home
  374. .right-scrollbar {
  375. .el-scrollbar__view {
  376. padding: 12px 18px 15px 15px;
  377. }
  378. }
  379. .left-scrollbar .el-scrollbar__wrap {
  380. box-sizing: border-box;
  381. overflow-x: hidden !important;
  382. margin-bottom: 0 !important;
  383. }
  384. .center-tabs{
  385. .el-tabs__header{
  386. margin-bottom: 0!important;
  387. }
  388. .el-tabs__item{
  389. width: 50%;
  390. text-align: center;
  391. }
  392. .el-tabs__nav{
  393. width: 100%;
  394. }
  395. }
  396. .reg-item{
  397. padding: 12px 6px;
  398. background: #f8f8f8;
  399. position: relative;
  400. border-radius: 4px;
  401. .close-btn{
  402. position: absolute;
  403. right: -6px;
  404. top: -6px;
  405. display: block;
  406. width: 16px;
  407. height: 16px;
  408. line-height: 16px;
  409. background: rgba(0, 0, 0, 0.2);
  410. border-radius: 50%;
  411. color: #fff;
  412. text-align: center;
  413. z-index: 1;
  414. cursor: pointer;
  415. font-size: 12px;
  416. &:hover{
  417. background: rgba(210, 23, 23, 0.5)
  418. }
  419. }
  420. & + .reg-item{
  421. margin-top: 18px;
  422. }
  423. }
  424. .action-bar{
  425. & .el-button+.el-button {
  426. margin-left: 15px;
  427. }
  428. & i {
  429. font-size: 20px;
  430. vertical-align: middle;
  431. position: relative;
  432. top: -1px;
  433. }
  434. }
  435. .custom-tree-node{
  436. width: 100%;
  437. font-size: 14px;
  438. .node-operation{
  439. float: right;
  440. }
  441. i[class*="el-icon"] + i[class*="el-icon"]{
  442. margin-left: 6px;
  443. }
  444. .el-icon-plus{
  445. color: #409EFF;
  446. }
  447. .el-icon-delete{
  448. color: #157a0c;
  449. }
  450. }
  451. .left-scrollbar .el-scrollbar__view{
  452. overflow-x: hidden;
  453. }
  454. .el-rate{
  455. display: inline-block;
  456. vertical-align: text-top;
  457. }
  458. .el-upload__tip{
  459. line-height: 1.2;
  460. }
  461. $selectedColor: #f6f7ff;
  462. $lighterBlue: #409EFF;
  463. .container {
  464. position: relative;
  465. width: 100%;
  466. height: 100%;
  467. }
  468. .components-list {
  469. padding: 8px;
  470. box-sizing: border-box;
  471. height: 100%;
  472. .components-item {
  473. display: inline-block;
  474. width: 48%;
  475. margin: 1%;
  476. transition: transform 0ms !important;
  477. }
  478. }
  479. .components-draggable{
  480. padding-bottom: 20px;
  481. }
  482. .components-title{
  483. font-size: 14px;
  484. color: #222;
  485. margin: 6px 2px;
  486. .svg-icon{
  487. color: #666;
  488. font-size: 18px;
  489. }
  490. }
  491. .components-body {
  492. padding: 8px 10px;
  493. background: $selectedColor;
  494. font-size: 12px;
  495. cursor: move;
  496. border: 1px dashed $selectedColor;
  497. border-radius: 3px;
  498. .svg-icon{
  499. color: #777;
  500. font-size: 15px;
  501. }
  502. &:hover {
  503. border: 1px dashed #787be8;
  504. color: #787be8;
  505. .svg-icon {
  506. color: #787be8;
  507. }
  508. }
  509. }
  510. .left-board {
  511. width: 260px;
  512. position: absolute;
  513. left: 0;
  514. top: 0;
  515. height: 100vh;
  516. }
  517. .left-scrollbar{
  518. height: calc(100vh - 42px);
  519. overflow: hidden;
  520. }
  521. .center-scrollbar {
  522. height: calc(100vh - 42px);
  523. overflow: hidden;
  524. border-left: 1px solid #f1e8e8;
  525. border-right: 1px solid #f1e8e8;
  526. box-sizing: border-box;
  527. }
  528. .center-board {
  529. height: 100vh;
  530. width: auto;
  531. margin: 0 350px 0 260px;
  532. box-sizing: border-box;
  533. }
  534. .empty-info{
  535. position: absolute;
  536. top: 46%;
  537. left: 0;
  538. right: 0;
  539. text-align: center;
  540. font-size: 18px;
  541. color: #ccb1ea;
  542. letter-spacing: 4px;
  543. }
  544. .action-bar{
  545. position: relative;
  546. height: 42px;
  547. text-align: right;
  548. padding: 0 15px;
  549. box-sizing: border-box;;
  550. border: 1px solid #f1e8e8;
  551. border-top: none;
  552. border-left: none;
  553. .delete-btn{
  554. color: #F56C6C;
  555. }
  556. }
  557. .logo-wrapper{
  558. position: relative;
  559. height: 42px;
  560. background: #fff;
  561. border-bottom: 1px solid #f1e8e8;
  562. box-sizing: border-box;
  563. }
  564. .logo{
  565. position: absolute;
  566. left: 12px;
  567. top: 6px;
  568. line-height: 30px;
  569. color: #00afff;
  570. font-weight: 600;
  571. font-size: 17px;
  572. white-space: nowrap;
  573. }
  574. .center-board-row {
  575. padding: 12px 12px 15px 12px;
  576. box-sizing: border-box;
  577. & > .el-form {
  578. // 69 = 12+15+42
  579. height: calc(100vh - 69px);
  580. }
  581. }
  582. .drawing-board {
  583. height: 100%;
  584. position: relative;
  585. .components-body {
  586. padding: 0;
  587. margin: 0;
  588. font-size: 0;
  589. }
  590. .sortable-ghost {
  591. position: relative;
  592. display: block;
  593. overflow: hidden;
  594. &::before {
  595. content: " ";
  596. position: absolute;
  597. left: 0;
  598. right: 0;
  599. top: 0;
  600. height: 3px;
  601. background: rgb(89, 89, 223);
  602. z-index: 2;
  603. }
  604. }
  605. .components-item.sortable-ghost {
  606. width: 100%;
  607. height: 60px;
  608. background-color: $selectedColor;
  609. }
  610. .active-from-item {
  611. & > .el-form-item{
  612. background: $selectedColor;
  613. border-radius: 6px;
  614. }
  615. & > .drawing-item-copy, & > .drawing-item-delete{
  616. display: initial;
  617. }
  618. & > .component-name{
  619. color: $lighterBlue;
  620. }
  621. }
  622. .el-form-item{
  623. margin-bottom: 15px;
  624. }
  625. }
  626. .drawing-item{
  627. position: relative;
  628. cursor: move;
  629. &.unfocus-bordered:not(.activeFromItem) > div:first-child {
  630. border: 1px dashed #ccc;
  631. }
  632. .el-form-item{
  633. padding: 12px 10px;
  634. }
  635. }
  636. .drawing-row-item{
  637. position: relative;
  638. cursor: move;
  639. box-sizing: border-box;
  640. border: 1px dashed #ccc;
  641. border-radius: 3px;
  642. padding: 0 2px;
  643. margin-bottom: 15px;
  644. .drawing-row-item {
  645. margin-bottom: 2px;
  646. }
  647. .el-col{
  648. margin-top: 22px;
  649. }
  650. .el-form-item{
  651. margin-bottom: 0;
  652. }
  653. .drag-wrapper{
  654. min-height: 80px;
  655. }
  656. &.active-from-item{
  657. border: 1px dashed $lighterBlue;
  658. }
  659. .component-name{
  660. position: absolute;
  661. top: 0;
  662. left: 0;
  663. font-size: 12px;
  664. color: #bbb;
  665. display: inline-block;
  666. padding: 0 6px;
  667. }
  668. }
  669. .drawing-item, .drawing-row-item{
  670. &:hover {
  671. & > .el-form-item{
  672. background: $selectedColor;
  673. border-radius: 6px;
  674. }
  675. & > .drawing-item-copy, & > .drawing-item-delete{
  676. display: initial;
  677. }
  678. }
  679. & > .drawing-item-copy, & > .drawing-item-delete{
  680. display: none;
  681. position: absolute;
  682. top: -10px;
  683. width: 22px;
  684. height: 22px;
  685. line-height: 22px;
  686. text-align: center;
  687. border-radius: 50%;
  688. font-size: 12px;
  689. border: 1px solid;
  690. cursor: pointer;
  691. z-index: 1;
  692. }
  693. & > .drawing-item-copy{
  694. right: 56px;
  695. border-color: $lighterBlue;
  696. color: $lighterBlue;
  697. background: #fff;
  698. &:hover{
  699. background: $lighterBlue;
  700. color: #fff;
  701. }
  702. }
  703. & > .drawing-item-delete{
  704. right: 24px;
  705. border-color: #F56C6C;
  706. color: #F56C6C;
  707. background: #fff;
  708. &:hover{
  709. background: #F56C6C;
  710. color: #fff;
  711. }
  712. }
  713. }
  714. </style>