quasar-ui-danx 0.0.8 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
>
|
8
8
|
<div class="flex items-center">
|
9
9
|
<div v-if="showHandle">
|
10
|
-
<
|
10
|
+
<SvgImg :svg="DragHandleIcon" class="w-4 h-4" alt="drag-handle" />
|
11
11
|
</div>
|
12
12
|
<div class="flex-grow">
|
13
13
|
<slot />
|
@@ -16,6 +16,7 @@
|
|
16
16
|
</div>
|
17
17
|
</template>
|
18
18
|
<script setup>
|
19
|
+
import SvgImg from "../Utility/SvgImg";
|
19
20
|
import { HandleDraggableDotsIcon as DragHandleIcon } from "./Icons";
|
20
21
|
import { ListDragAndDrop } from "./listDragAndDrop";
|
21
22
|
|
@@ -7,231 +7,231 @@ import { DragAndDrop } from "./dragAndDrop";
|
|
7
7
|
* @class
|
8
8
|
*/
|
9
9
|
export class ListDragAndDrop extends DragAndDrop {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
}
|
23
|
-
|
24
|
-
/**
|
25
|
-
* Callback that fires after dragging has ended and the list position has changed from the original
|
26
|
-
* @param cb
|
27
|
-
* @returns {ListDragAndDrop}
|
28
|
-
*/
|
29
|
-
onPositionChange(cb) {
|
30
|
-
this.onPositionChangeCb = cb;
|
31
|
-
return this;
|
32
|
-
}
|
33
|
-
|
34
|
-
/**
|
35
|
-
* Callback that fires while dragging the element when the cursor's position has changed in the list
|
36
|
-
* @param cb
|
37
|
-
* @returns {ListDragAndDrop}
|
38
|
-
*/
|
39
|
-
onDragPositionChange(cb) {
|
40
|
-
this.onDragPositionChangeCb = cb;
|
41
|
-
return this;
|
42
|
-
}
|
43
|
-
|
44
|
-
/**
|
45
|
-
* Start listening for drag events and prepare an element for drag/drop
|
46
|
-
* @param e
|
47
|
-
* @param data
|
48
|
-
*/
|
49
|
-
dragStart(e, data) {
|
50
|
-
super.dragStart(e, data);
|
51
|
-
|
52
|
-
if (this.currentDropZone) {
|
53
|
-
this.listPosition = this.getListPosition(e.target);
|
54
|
-
this.initialPosition = this.listPosition;
|
55
|
-
this.updateScrollPosition();
|
10
|
+
listPosition = 0;
|
11
|
+
cursorPosition = 0;
|
12
|
+
initialPosition = 0;
|
13
|
+
onPositionChangeCb = null;
|
14
|
+
onDragPositionChangeCb = null;
|
15
|
+
placeholder = null;
|
16
|
+
|
17
|
+
constructor(options = {}) {
|
18
|
+
super({
|
19
|
+
showPlaceholder: true,
|
20
|
+
...options,
|
21
|
+
});
|
56
22
|
}
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
// If our list position has changed, trigger the drop callback
|
68
|
-
if (this.listPosition !== this.initialPosition) {
|
69
|
-
this.onPositionChangeCb &&
|
70
|
-
this.onPositionChangeCb(this.listPosition, this.initialPosition, draggableData);
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Callback that fires after dragging has ended and the list position has changed from the original
|
26
|
+
* @param cb
|
27
|
+
* @returns {ListDragAndDrop}
|
28
|
+
*/
|
29
|
+
onPositionChange(cb) {
|
30
|
+
this.onPositionChangeCb = cb;
|
31
|
+
return this;
|
71
32
|
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
/**
|
84
|
-
* Notify if the targeted position of the cursor is different from the current position
|
85
|
-
* @param e
|
86
|
-
*/
|
87
|
-
updateListPosition(e) {
|
88
|
-
const prevPosition = this.listPosition;
|
89
|
-
const newPosition = this.getListPositionOfPoint({
|
90
|
-
x: e.clientX,
|
91
|
-
y: e.clientY,
|
92
|
-
});
|
93
|
-
|
94
|
-
// If the cursor position has changed, we should update the rendering and see if our actual list position has
|
95
|
-
// changed
|
96
|
-
if (this.cursorPosition !== newPosition) {
|
97
|
-
this.cursorPosition = newPosition;
|
98
|
-
this.listPosition =
|
99
|
-
this.initialPosition < this.cursorPosition
|
100
|
-
? this.cursorPosition - 1
|
101
|
-
: this.cursorPosition;
|
102
|
-
if (this.options.showPlaceholder) {
|
103
|
-
this.renderPlaceholder();
|
104
|
-
}
|
105
|
-
|
106
|
-
// The position has changed, trigger the callback
|
107
|
-
if (this.listPosition !== prevPosition) {
|
108
|
-
this.onDragPositionChangeCb &&
|
109
|
-
this.onDragPositionChangeCb(this.listPosition, this.draggableData);
|
110
|
-
}
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Callback that fires while dragging the element when the cursor's position has changed in the list
|
36
|
+
* @param cb
|
37
|
+
* @returns {ListDragAndDrop}
|
38
|
+
*/
|
39
|
+
onDragPositionChange(cb) {
|
40
|
+
this.onDragPositionChangeCb = cb;
|
41
|
+
return this;
|
111
42
|
}
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Start listening for drag events and prepare an element for drag/drop
|
46
|
+
* @param e
|
47
|
+
* @param data
|
48
|
+
*/
|
49
|
+
dragStart(e, data) {
|
50
|
+
super.dragStart(e, data);
|
51
|
+
|
52
|
+
if (this.currentDropZone) {
|
53
|
+
this.listPosition = this.getListPosition(e.target);
|
54
|
+
this.initialPosition = this.listPosition;
|
55
|
+
this.updateScrollPosition();
|
56
|
+
}
|
126
57
|
}
|
127
58
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
/**
|
142
|
-
* Find the element at the current cursor position in the given drop zone
|
143
|
-
* @param point
|
144
|
-
* @returns {null}
|
145
|
-
*/
|
146
|
-
getListPositionOfPoint(point) {
|
147
|
-
let index = 0;
|
148
|
-
const children = this.getChildren();
|
149
|
-
|
150
|
-
while (index < children.length) {
|
151
|
-
const rect = children[index].getBoundingClientRect();
|
152
|
-
if (this.isVertical()) {
|
153
|
-
if (point.y < rect.top + rect.height / 2) {
|
154
|
-
break;
|
59
|
+
/**
|
60
|
+
* When dragging has ended, check for list position changes and fire the onPositionChange callback if it has
|
61
|
+
*/
|
62
|
+
dragEnd(e) {
|
63
|
+
const draggableData = this.draggableData;
|
64
|
+
this.placeholder?.remove();
|
65
|
+
super.dragEnd(e);
|
66
|
+
|
67
|
+
// If our list position has changed, trigger the drop callback
|
68
|
+
if (this.listPosition !== this.initialPosition) {
|
69
|
+
this.onPositionChangeCb &&
|
70
|
+
this.onPositionChangeCb(this.listPosition, this.initialPosition, draggableData);
|
155
71
|
}
|
156
|
-
|
157
|
-
|
158
|
-
|
72
|
+
}
|
73
|
+
|
74
|
+
/**
|
75
|
+
* The dragging element is moving
|
76
|
+
* @param e
|
77
|
+
*/
|
78
|
+
dragOver(e) {
|
79
|
+
super.dragOver(e);
|
80
|
+
this.updateListPosition(e);
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* Notify if the targeted position of the cursor is different from the current position
|
85
|
+
* @param e
|
86
|
+
*/
|
87
|
+
updateListPosition(e) {
|
88
|
+
const prevPosition = this.listPosition;
|
89
|
+
const newPosition = this.getListPositionOfPoint({
|
90
|
+
x: e.clientX,
|
91
|
+
y: e.clientY,
|
92
|
+
});
|
93
|
+
|
94
|
+
// If the cursor position has changed, we should update the rendering and see if our actual list position has
|
95
|
+
// changed
|
96
|
+
if (this.cursorPosition !== newPosition) {
|
97
|
+
this.cursorPosition = newPosition;
|
98
|
+
this.listPosition =
|
99
|
+
this.initialPosition < this.cursorPosition
|
100
|
+
? this.cursorPosition - 1
|
101
|
+
: this.cursorPosition;
|
102
|
+
if (this.options.showPlaceholder) {
|
103
|
+
this.renderPlaceholder();
|
104
|
+
}
|
105
|
+
|
106
|
+
// The position has changed, trigger the callback
|
107
|
+
if (this.listPosition !== prevPosition) {
|
108
|
+
this.onDragPositionChangeCb &&
|
109
|
+
this.onDragPositionChangeCb(this.listPosition, this.draggableData);
|
110
|
+
}
|
159
111
|
}
|
160
|
-
}
|
161
|
-
index++;
|
162
112
|
}
|
163
113
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
const cursorPos = this.isVertical() ? this.cursorY : this.cursorX;
|
177
|
-
const rectStart = this.isVertical() ? rect.top : rect.left;
|
178
|
-
const rectEnd = this.isVertical() ? rect.bottom : rect.right;
|
179
|
-
const beforeDiff = rectStart + threshold - cursorPos;
|
180
|
-
const afterDiff = cursorPos - (rectEnd - threshold);
|
181
|
-
|
182
|
-
if (beforeDiff > 0) {
|
183
|
-
velocity = -velocityFn(beforeDiff);
|
184
|
-
} else if (afterDiff > 0) {
|
185
|
-
velocity = velocityFn(afterDiff);
|
186
|
-
}
|
187
|
-
|
188
|
-
if (velocity) {
|
189
|
-
if (this.isVertical()) {
|
190
|
-
this.currentDropZone.scrollTo({
|
191
|
-
top: this.currentDropZone.scrollTop + velocity,
|
192
|
-
behavior: "smooth",
|
193
|
-
});
|
194
|
-
} else {
|
195
|
-
this.currentDropZone.scrollTo({
|
196
|
-
left: this.currentDropZone.scrollLeft + velocity,
|
197
|
-
behavior: "smooth",
|
198
|
-
});
|
114
|
+
/**
|
115
|
+
* Find the numeric position of the element in the children of the list
|
116
|
+
* @returns {Number|null}
|
117
|
+
* @param item
|
118
|
+
*/
|
119
|
+
getListPosition(item) {
|
120
|
+
let index = 0;
|
121
|
+
for (const child of this.getChildren()) {
|
122
|
+
if (child === item) {
|
123
|
+
return index;
|
124
|
+
}
|
125
|
+
index++;
|
199
126
|
}
|
200
|
-
}
|
201
127
|
|
202
|
-
|
128
|
+
return null;
|
129
|
+
}
|
130
|
+
|
131
|
+
/**
|
132
|
+
* Get all the children of the current drop zone, excluding the placeholder
|
133
|
+
* @returns {*}
|
134
|
+
*/
|
135
|
+
getChildren() {
|
136
|
+
return [...this.currentDropZone.children].filter(
|
137
|
+
(c) => c.className.match(/drag-placeholder/) === null
|
138
|
+
);
|
203
139
|
}
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
140
|
+
|
141
|
+
/**
|
142
|
+
* Find the element at the current cursor position in the given drop zone
|
143
|
+
* @param point
|
144
|
+
* @returns {null}
|
145
|
+
*/
|
146
|
+
getListPositionOfPoint(point) {
|
147
|
+
let index = 0;
|
148
|
+
const children = this.getChildren();
|
149
|
+
|
150
|
+
while (index < children.length) {
|
151
|
+
const rect = children[index].getBoundingClientRect();
|
152
|
+
if (this.isVertical()) {
|
153
|
+
if (point.y < rect.top + rect.height / 2) {
|
154
|
+
break;
|
155
|
+
}
|
156
|
+
} else {
|
157
|
+
if (point.x < rect.left + rect.width / 2) {
|
158
|
+
break;
|
159
|
+
}
|
160
|
+
}
|
161
|
+
index++;
|
162
|
+
}
|
163
|
+
|
164
|
+
return index;
|
213
165
|
}
|
214
166
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
167
|
+
/**
|
168
|
+
* Updates the scroll position while dragging an element so a user can navigate a longer list while dragging
|
169
|
+
*/
|
170
|
+
updateScrollPosition() {
|
171
|
+
if (this.currentDropZone) {
|
172
|
+
const rect = this.currentDropZone.getBoundingClientRect();
|
173
|
+
const threshold = 100;
|
174
|
+
let velocity = 0;
|
175
|
+
const velocityFn = (x) => x * 5;
|
176
|
+
const cursorPos = this.isVertical() ? this.cursorY : this.cursorX;
|
177
|
+
const rectStart = this.isVertical() ? rect.top : rect.left;
|
178
|
+
const rectEnd = this.isVertical() ? rect.bottom : rect.right;
|
179
|
+
const beforeDiff = rectStart + threshold - cursorPos;
|
180
|
+
const afterDiff = cursorPos - (rectEnd - threshold);
|
181
|
+
|
182
|
+
if (beforeDiff > 0) {
|
183
|
+
velocity = -velocityFn(beforeDiff);
|
184
|
+
} else if (afterDiff > 0) {
|
185
|
+
velocity = velocityFn(afterDiff);
|
186
|
+
}
|
187
|
+
|
188
|
+
if (velocity) {
|
189
|
+
if (this.isVertical()) {
|
190
|
+
this.currentDropZone.scrollTo({
|
191
|
+
top: this.currentDropZone.scrollTop + velocity,
|
192
|
+
behavior: "smooth",
|
193
|
+
});
|
194
|
+
} else {
|
195
|
+
this.currentDropZone.scrollTo({
|
196
|
+
left: this.currentDropZone.scrollLeft + velocity,
|
197
|
+
behavior: "smooth",
|
198
|
+
});
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
setTimeout(() => this.updateScrollPosition(), 500);
|
203
|
+
}
|
225
204
|
}
|
226
205
|
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
206
|
+
/**
|
207
|
+
* Render a placeholder element at the given position (in between the elements)
|
208
|
+
*/
|
209
|
+
renderPlaceholder() {
|
210
|
+
if (!this.placeholder) {
|
211
|
+
this.placeholder = document.createElement("div");
|
212
|
+
this.placeholder.classList.add("drag-placeholder");
|
213
|
+
}
|
214
|
+
|
215
|
+
// Make sure the placeholder is oriented correctly
|
216
|
+
if (this.isVertical()) {
|
217
|
+
this.placeholder.classList.add("direction-vertical");
|
218
|
+
this.placeholder.classList.remove("direction-horizontal");
|
219
|
+
this.placeholder.style.height = undefined;
|
220
|
+
} else {
|
221
|
+
this.placeholder.classList.add("direction-horizontal");
|
222
|
+
this.placeholder.classList.remove("direction-vertical");
|
223
|
+
this.placeholder.style.height =
|
224
|
+
this.currentDropZone.getBoundingClientRect().height + "px";
|
225
|
+
}
|
226
|
+
|
227
|
+
const children = this.getChildren();
|
228
|
+
if (this.cursorPosition < children.length) {
|
229
|
+
this.currentDropZone.insertBefore(
|
230
|
+
this.placeholder,
|
231
|
+
children[this.cursorPosition]
|
232
|
+
);
|
233
|
+
} else {
|
234
|
+
this.currentDropZone.appendChild(this.placeholder);
|
235
|
+
}
|
235
236
|
}
|
236
|
-
}
|
237
237
|
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<template>
|
2
|
+
<img
|
3
|
+
v-if="isString"
|
4
|
+
:src="resolvedSvg"
|
5
|
+
:alt="alt"
|
6
|
+
/>
|
7
|
+
<component :is="resolvedSvg" v-else />
|
8
|
+
</template>
|
9
|
+
<script setup>
|
10
|
+
import { computed } from "vue";
|
11
|
+
|
12
|
+
const props = defineProps({
|
13
|
+
svg: {
|
14
|
+
type: [String, Object, Function],
|
15
|
+
required: true
|
16
|
+
},
|
17
|
+
alt: {
|
18
|
+
type: String,
|
19
|
+
default: ""
|
20
|
+
}
|
21
|
+
});
|
22
|
+
|
23
|
+
const resolvedSvg = computed(() => props.svg.default ? props.svg.default : props.svg);
|
24
|
+
const isString = computed(() => typeof resolvedSvg.value === "string");
|
25
|
+
</script>
|