forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDragDrop.vue
More file actions
213 lines (186 loc) · 4.82 KB
/
DragDrop.vue
File metadata and controls
213 lines (186 loc) · 4.82 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
<template>
<div>
<div class="row no-wrap justify-around q-px-md q-pt-md">
<div
v-mutation="handler1"
@dragenter="onDragEnter"
@dragleave="onDragLeave"
@dragover="onDragOver"
@drop="onDrop"
class="drop-target rounded-borders overflow-hidden"
>
<div
id="box1"
draggable="true"
@dragstart="onDragStart"
class="box navy"
/>
<div
id="box2"
draggable="true"
@dragstart="onDragStart"
class="box red"
/>
<div
id="box3"
draggable="true"
@dragstart="onDragStart"
class="box green"
/>
<div
id="box4"
draggable="true"
@dragstart="onDragStart"
class="box orange"
/>
<div
id="box5"
draggable="true"
@dragstart="onDragStart"
class="box navy"
/>
<div
id="box6"
draggable="true"
@dragstart="onDragStart"
class="box red"
/>
<div
id="box7"
draggable="true"
@dragstart="onDragStart"
class="box green"
/>
<div
id="box8"
draggable="true"
@dragstart="onDragStart"
class="box orange"
/>
</div>
<div
v-mutation="handler2"
@dragenter="onDragEnter"
@dragleave="onDragLeave"
@dragover="onDragOver"
@drop="onDrop"
class="drop-target rounded-borders overflow-hidden"
/>
</div>
<div class="row justify-around items-start">
<div class="col row justify-center q-pa-md">
<div class="text-subtitle1">
Mutation Info
</div>
<div v-for="status in status1" :key="status">
{{ status }}
</div>
</div>
<div class="col row justify-center q-pa-md">
<div class="text-subtitle1">
Mutation Info
</div>
<div v-for="status in status2" :key="status">
{{ status }}
</div>
</div>
</div>
</div>
</template>
<script>
import { ref } from 'vue'
export default {
setup () {
const status1 = ref([])
const status2 = ref([])
return {
status1,
status2,
handler1 (mutationRecords) {
status1.value = []
for (const index in mutationRecords) {
const record = mutationRecords[ index ]
const info = `type: ${record.type}, nodes added: ${record.addedNodes.length > 0 ? 'true' : 'false'}, nodes removed: ${record.removedNodes.length > 0 ? 'true' : 'false'}, oldValue: ${record.oldValue}`
status1.value.push(info)
}
},
handler2 (mutationRecords) {
status2.value = []
for (const index in mutationRecords) {
const record = mutationRecords[ index ]
const info = `type: ${record.type}, nodes added: ${record.addedNodes.length > 0 ? 'true' : 'false'}, nodes removed: ${record.removedNodes.length > 0 ? 'true' : 'false'}, oldValue: ${record.oldValue}`
status2.value.push(info)
}
},
// store the id of the draggable element
onDragStart (e) {
e.dataTransfer.setData('text', e.target.id)
e.dataTransfer.dropEffect = 'move'
},
onDragEnter (e) {
// don't drop on other draggables
if (e.target.draggable !== true) {
e.target.classList.add('drag-enter')
}
},
onDragLeave (e) {
e.target.classList.remove('drag-enter')
},
onDragOver (e) {
e.preventDefault()
},
onDrop (e) {
e.preventDefault()
// don't drop on other draggables
if (e.target.draggable === true) {
return
}
const draggedId = e.dataTransfer.getData('text')
const draggedEl = document.getElementById(draggedId)
// check if original parent node
if (draggedEl.parentNode === e.target) {
e.target.classList.remove('drag-enter')
return
}
// make the exchange
draggedEl.parentNode.removeChild(draggedEl)
e.target.appendChild(draggedEl)
e.target.classList.remove('drag-enter')
}
}
}
}
</script>
<style scoped lang="sass">
.drop-target
height: 400px
width: 200px
min-width: 200px
background-color: gainsboro
.drag-enter
outline-style: dashed
.box
width: 100px
height: 100px
float: left
cursor: pointer
@media only screen and (max-width: 500px)
.drop-target
height: 200px
width: 100px
min-width: 100px
background-color: gainsboro
.box
width: 50px
height: 50px
.box:nth-child(3)
clear: both
.navy
background-color: navy
.red
background-color: firebrick
.green
background-color: darkgreen
.orange
background-color: orange
</style>