DraggableItem.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <script>
  2. import draggable from 'vuedraggable'
  3. import render from '@/components/FormGenerator/components/render/render'
  4. const components = {
  5. itemBtns(h, currentItem, index, list) {
  6. const { copyItem, deleteItem } = this.$listeners
  7. return [
  8. <span class="drawing-item-copy" title="复制" onClick={event => {
  9. copyItem(currentItem, list); event.stopPropagation()
  10. }}>
  11. <i class="el-icon-copy-document" />
  12. </span>,
  13. <span class="drawing-item-delete" title="删除" onClick={event => {
  14. deleteItem(index, list); event.stopPropagation()
  15. }}>
  16. <i class="el-icon-delete" />
  17. </span>
  18. ]
  19. }
  20. }
  21. const layouts = {
  22. colFormItem(h, currentItem, index, list) {
  23. const { activeItem } = this.$listeners
  24. const config = currentItem.__config__
  25. const child = renderChildren.apply(this, arguments)
  26. let className = this.activeId === config.formId ? 'drawing-item active-from-item' : 'drawing-item'
  27. if (this.formConf.unFocusedComponentBorder) className += ' unfocus-bordered'
  28. let labelWidth = config.labelWidth ? `${config.labelWidth}px` : null
  29. if (config.showLabel === false) labelWidth = '0'
  30. return (
  31. <el-col span={config.span} class={className}
  32. nativeOnClick={event => { activeItem(currentItem); event.stopPropagation() }}>
  33. <el-form-item label-width={labelWidth}
  34. label={config.showLabel ? config.label : ''} required={config.required}>
  35. <render key={config.renderKey} conf={currentItem} onInput={ event => {
  36. this.$set(config, 'defaultValue', event)
  37. }}>
  38. {child}
  39. </render>
  40. </el-form-item>
  41. {components.itemBtns.apply(this, arguments)}
  42. </el-col>
  43. )
  44. },
  45. rowFormItem(h, currentItem, index, list) {
  46. const { activeItem } = this.$listeners
  47. const config = currentItem.__config__
  48. const className = this.activeId === config.formId
  49. ? 'drawing-row-item active-from-item'
  50. : 'drawing-row-item'
  51. let child = renderChildren.apply(this, arguments)
  52. if (currentItem.type === 'flex') {
  53. child = <el-row type={currentItem.type} justify={currentItem.justify} align={currentItem.align}>
  54. {child}
  55. </el-row>
  56. }
  57. return (
  58. <el-col span={config.span}>
  59. <el-row gutter={config.gutter} class={className}
  60. nativeOnClick={event => { activeItem(currentItem); event.stopPropagation() }}>
  61. <span class="component-name">{config.componentName}</span>
  62. <draggable list={config.children || []} animation={340}
  63. group="componentsGroup" class="drag-wrapper">
  64. {child}
  65. </draggable>
  66. {components.itemBtns.apply(this, arguments)}
  67. </el-row>
  68. </el-col>
  69. )
  70. },
  71. raw(h, currentItem, index, list) {
  72. const config = currentItem.__config__
  73. const child = renderChildren.apply(this, arguments)
  74. return <render key={config.renderKey} conf={currentItem} onInput={ event => {
  75. this.$set(config, 'defaultValue', event)
  76. }}>
  77. {child}
  78. </render>
  79. }
  80. }
  81. function renderChildren(h, currentItem, index, list) {
  82. const config = currentItem.__config__
  83. if (!Array.isArray(config.children)) return null
  84. return config.children.map((el, i) => {
  85. const layout = layouts[el.__config__.layout]
  86. if (layout) {
  87. return layout.call(this, h, el, i, config.children)
  88. }
  89. return layoutIsNotFound.call(this)
  90. })
  91. }
  92. function layoutIsNotFound() {
  93. throw new Error(`没有与${this.currentItem.__config__.layout}匹配的layout`)
  94. }
  95. export default {
  96. components: {
  97. render,
  98. draggable
  99. },
  100. props: [
  101. 'currentItem',
  102. 'index',
  103. 'drawingList',
  104. 'activeId',
  105. 'formConf'
  106. ],
  107. render(h) {
  108. const layout = layouts[this.currentItem.__config__.layout]
  109. if (layout) {
  110. return layout.call(this, h, this.currentItem, this.index, this.drawingList)
  111. }
  112. return layoutIsNotFound.call(this)
  113. }
  114. }
  115. </script>