DraggableItem.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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
  9. class="drawing-item-copy"
  10. title="复制"
  11. onClick={(event) => {
  12. copyItem(currentItem, list);
  13. event.stopPropagation();
  14. }}
  15. >
  16. <i class="el-icon-copy-document" />
  17. </span>,
  18. <span
  19. class="drawing-item-delete"
  20. title="删除"
  21. onClick={(event) => {
  22. deleteItem(index, list);
  23. event.stopPropagation();
  24. }}
  25. >
  26. <i class="el-icon-delete" />
  27. </span>
  28. ];
  29. }
  30. };
  31. const layouts = {
  32. colFormItem(h, currentItem, index, list) {
  33. const { activeItem } = this.$listeners;
  34. const config = currentItem.__config__;
  35. const child = renderChildren.apply(this, arguments);
  36. let className =
  37. this.activeId === config.formId
  38. ? 'drawing-item active-from-item'
  39. : 'drawing-item';
  40. if (this.formConf.unFocusedComponentBorder)
  41. className += ' unfocus-bordered';
  42. let labelWidth = config.labelWidth ? `${config.labelWidth}px` : null;
  43. if (config.showLabel === false) labelWidth = '0';
  44. return (
  45. <el-col
  46. span={config.span}
  47. class={className}
  48. nativeOnClick={(event) => {
  49. activeItem(currentItem);
  50. event.stopPropagation();
  51. }}
  52. >
  53. <el-form-item
  54. label-width={labelWidth}
  55. label={config.showLabel ? config.label : ''}
  56. required={config.required}
  57. >
  58. <render
  59. style="width: 100%"
  60. key={config.renderKey}
  61. conf={currentItem}
  62. onInput={(event) => {
  63. console.log(event, 8888);
  64. this.$set(config, 'defaultValue', event);
  65. }}
  66. >
  67. {child}
  68. </render>
  69. </el-form-item>
  70. {components.itemBtns.apply(this, arguments)}
  71. </el-col>
  72. );
  73. },
  74. rowFormItem(h, currentItem, index, list) {
  75. const { activeItem } = this.$listeners;
  76. const config = currentItem.__config__;
  77. const className =
  78. this.activeId === config.formId
  79. ? 'drawing-row-item active-from-item'
  80. : 'drawing-row-item';
  81. let child = renderChildren.apply(this, arguments);
  82. if (currentItem.type === 'flex') {
  83. child = (
  84. <el-row
  85. type={currentItem.type}
  86. justify={currentItem.justify}
  87. align={currentItem.align}
  88. >
  89. {child}
  90. </el-row>
  91. );
  92. }
  93. return (
  94. <el-col span={config.span}>
  95. <el-row
  96. gutter={config.gutter}
  97. class={className}
  98. nativeOnClick={(event) => {
  99. activeItem(currentItem);
  100. event.stopPropagation();
  101. }}
  102. >
  103. <span class="component-name">{config.componentName}</span>
  104. <draggable
  105. list={config.children || []}
  106. animation={340}
  107. group="componentsGroup"
  108. class="drag-wrapper"
  109. >
  110. {child}
  111. </draggable>
  112. {components.itemBtns.apply(this, arguments)}
  113. </el-row>
  114. </el-col>
  115. );
  116. },
  117. raw(h, currentItem, index, list) {
  118. const config = currentItem.__config__;
  119. const child = renderChildren.apply(this, arguments);
  120. return (
  121. <render
  122. key={config.renderKey}
  123. conf={currentItem}
  124. onInput={(event) => {
  125. this.$set(config, 'defaultValue', event);
  126. }}
  127. >
  128. {child}
  129. </render>
  130. );
  131. }
  132. };
  133. function renderChildren(h, currentItem, index, list) {
  134. const config = currentItem.__config__;
  135. if (!Array.isArray(config.children)) return null;
  136. return config.children.map((el, i) => {
  137. const layout = layouts[el.__config__.layout];
  138. if (layout) {
  139. return layout.call(this, h, el, i, config.children);
  140. }
  141. return layoutIsNotFound.call(this);
  142. });
  143. }
  144. function layoutIsNotFound() {
  145. throw new Error(`没有与${this.currentItem.__config__.layout}匹配的layout`);
  146. }
  147. export default {
  148. components: {
  149. render,
  150. draggable
  151. },
  152. props: ['currentItem', 'index', 'drawingList', 'activeId', 'formConf'],
  153. render(h) {
  154. const layout = layouts[this.currentItem.__config__.layout];
  155. if (layout) {
  156. return layout.call(
  157. this,
  158. h,
  159. this.currentItem,
  160. this.index,
  161. this.drawingList
  162. );
  163. }
  164. return layoutIsNotFound.call(this);
  165. }
  166. };
  167. </script>