DraggableItem.vue
10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<script>
import draggable from 'vuedraggable'
import render from '@/components/Generator/render/render'
import { dyOptionsList } from '@/components/Generator/generator/comConfig'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import { previewDataInterface } from '@/api/systemData/dataInterface'
let activeData = {}
const components = {
itemBtns(h, element, index, parent) {
const { copyItem, deleteItem } = this.$listeners
return [
<span class="drawing-item-copy" title="复制" onClick={event => {
copyItem(element, parent); event.stopPropagation()
}}>
<i class="el-icon-copy-document" />
</span>,
<el-popconfirm title="确定删除该组件?" class="drawing-item-delete" onConfirm={event => {
deleteItem(index, parent)
}}>
<span title="删除" slot="reference" style="width:100%;height:100%;display:inline-block">
<i class="el-icon-delete" />
</span>
</el-popconfirm>
]
}
}
const layouts = {
colFormItem(h, element, index, parent) {
const { activeItem } = this.$listeners
const config = element.__config__
let className = this.activeId === config.formId ? 'drawing-item active-from-item' : 'drawing-item'
if (this.formConf.unFocusedComponentBorder) className += ' unfocus-bordered'
let labelWidth = config.labelWidth ? `${config.labelWidth}px` : null
if (config.showLabel === false) labelWidth = '0'
return (
<el-col span={config.span} class={className}
nativeOnClick={event => { activeItem(element); event.stopPropagation() }}>
<el-form-item label-width={labelWidth}
label={config.showLabel ? config.label : ''} required={config.required}>
<render key={config.renderKey} conf={element} onInput={event => {
this.$set(config, 'defaultValue', event)
}} />
</el-form-item>
{components.itemBtns.apply(this, arguments)}
</el-col>
)
},
rowFormItem(h, element, index, parent) {
const { activeItem } = this.$listeners
const { put, end } = this.$attrs
const className = this.activeId === element.__config__.formId
? 'drawing-row-item active-from-item'
: 'drawing-row-item'
if (element.__config__.nccKey === 'tab') {
return (
<el-col span={element.__config__.span}>
<el-row gutter={element.__config__.gutter} class={className}
nativeOnClick={event => { activeItem(element); event.stopPropagation() }}>
<el-tabs type={element.type} tab-position={element['tab-position']} vModel={element.__config__.active}>
{
element.__config__.children.map((item, i) => {
let child = renderChildren.apply(this, [h, item, i, element])
let childgroup = { name: 'componentsGroup', put: (...arg) => put(...arg, item) }
let tip = ''
if (!item.__config__.children.length) {
tip = <div class="table-tip tab-tip">请将组件拖到此区域(可拖多个组件)</div>
}
return (
<el-tab-pane key={item.name} label={item.title} >
<el-col >
{tip}
<el-row gutter={element.__config__.gutter} style="padding-top:15px">
<draggable list={item.__config__.children} animation={340} group={childgroup} class="drag-wrapper">
{child}
</draggable>
</el-row>
</el-col>
</el-tab-pane>
)
})
}
</el-tabs>
{components.itemBtns.apply(this, arguments)}
</el-row>
</el-col>
)
}
if (element.__config__.nccKey === 'collapse') {
return (
<el-col span={element.__config__.span}>
<el-row gutter={element.__config__.gutter} class={className}
nativeOnClick={event => { activeItem(element); event.stopPropagation() }}>
<el-collapse vModel={element.__config__.active} accordion={element.accordion}>
{
element.__config__.children.map((item, i) => {
let child = renderChildren.apply(this, [h, item, i, element])
let childgroup = { name: 'componentsGroup', put: (...arg) => put(...arg, item) }
let tip = ''
if (!item.__config__.children.length) {
tip = <div class="table-tip card-tip">请将组件拖到此区域(可拖多个组件)</div>
}
return (
<el-collapse-item key={item.name} title={item.title} name={item.name} >
<el-col style="position:relative">
{tip}
<el-row gutter={element.__config__.gutter} style="padding-top:15px">
<draggable list={item.__config__.children} animation={340} group={childgroup} class="drag-wrapper">
{child}
</draggable>
</el-row>
</el-col>
</el-collapse-item>
)
})
}
</el-collapse>
{components.itemBtns.apply(this, arguments)}
</el-row>
</el-col>
)
}
let child = renderChildren.apply(this, arguments)
const group = { name: 'componentsGroup', put: (...arg) => put(...arg, element) }
const onEnd = (...arg) => end(...arg, activeData, element)
if (element.__config__.nccKey === 'row') {
if (element.type === 'flex') {
child = <el-row type={element.type} justify={element.justify} align={element.align}>
{child}
</el-row>
}
let tip = ''
if (!element.__config__.children.length) {
tip = <div class="table-tip">请将组件拖到此区域(可拖多个组件)</div>
}
return (
<el-col span={element.__config__.span}>
<el-row gutter={element.__config__.gutter} class={className}
nativeOnClick={event => { activeItem(element); event.stopPropagation() }}>
<span class="component-name">{element.__config__.componentName}</span>
{tip}
<draggable list={element.__config__.children} animation={340} group={group} class="drag-wrapper" style="margin-top:20px">
{child}
</draggable>
{components.itemBtns.apply(this, arguments)}
</el-row>
</el-col>
)
}
if (element.__config__.nccKey === 'table') {
let tip = ''
if (!element.__config__.children.length) {
tip = <div class="table-tip">请将组件拖到此区域(可拖多个组件)</div>
}
return (
<el-col span={element.__config__.span}>
<el-row gutter={element.__config__.gutter} class={className + ' drawing-row-item-table'}
nativeOnClick={event => { activeItem(element); event.stopPropagation() }}>
<span class="component-name">{element.__config__.label}</span>
{tip}
<el-form label-position="top">
<draggable list={element.__config__.children} animation={340} group={group} class="drag-wrapper table-wrapper table-wrapper-web" onEnd={onEnd} clone={cloneComponent}>
{child}
</draggable>
</el-form>
{components.itemBtns.apply(this, arguments)}
</el-row>
</el-col>
)
}
if (element.__config__.nccKey === 'card') {
let tip = ''
if (!element.__config__.children.length) {
tip = <div class="table-tip card-tip">请将组件拖到此区域(可拖多个组件)</div>
}
return (
<el-col span={element.__config__.span}>
<el-row gutter={element.__config__.gutter} class={className}
nativeOnClick={event => { activeItem(element); event.stopPropagation() }}>
<el-card shadow={element.shadow} header={element.header}>
<el-col>
<el-row gutter={element.__config__.gutter}
nativeOnClick={event => { activeItem(element); event.stopPropagation() }}>
{tip}
<draggable list={element.__config__.children} animation={340} group={group} class="drag-wrapper">
{child}
</draggable>
</el-row>
</el-col>
</el-card>
{components.itemBtns.apply(this, arguments)}
</el-row>
</el-col>
)
}
}
}
function cloneComponent(origin) {
activeData = origin
}
function renderChildren(h, element, index, parent) {
const config = element.__config__
if (!Array.isArray(config.children)) return null
return config.children.map((el, i) => {
const layout = layouts[el.__config__.layout]
if (layout) {
return layout.call(this, h, el, i, config.children)
}
return layoutIsNotFound.call(this)
})
}
function layoutIsNotFound() {
throw new Error(`没有与${this.element.__config__.layout}匹配的layout`)
}
function buildOptions(element) {
const config = element.__config__
if (dyOptionsList.indexOf(config.nccKey) > -1) {
let isTreeSelect = config.nccKey === 'treeSelect' || config.nccKey === 'cascader'
if (config.dataType === 'dictionary') {
if (!config.dictionaryType) return
getDictionaryDataSelector(config.dictionaryType).then(res => {
isTreeSelect ? element.options = res.data.list : element.__slot__.options = res.data.list
})
}
if (config.dataType === 'dynamic') {
if (!config.propsUrl) return
previewDataInterface(config.propsUrl).then(res => {
isTreeSelect ? element.options = res.data : element.__slot__.options = res.data
})
}
}
if (config.children && Array.isArray(config.children)) {
for (let i = 0; i < config.children.length; i++) {
buildOptions(config.children[i])
}
}
}
export default {
components: {
render,
draggable
},
props: [
'element',
'index',
'drawingList',
'activeId',
'formConf'
],
render(h) {
// buildOptions(this.element)
const layout = layouts[this.element.__config__.layout]
if (layout) {
return layout.call(this, h, this.element, this.index, this.drawingList)
}
return layoutIsNotFound.call(this)
}
}
</script>