-
Notifications
You must be signed in to change notification settings - Fork 629
/
DragContainer.vue
135 lines (120 loc) · 3.42 KB
/
DragContainer.vue
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
<script>
// Implemention inspired by an excellent demo at:
// https://github.com/adamwathan/vue-shopify-sortable-demo
import {
Sortable,
Plugins,
Draggable,
} from '@shopify/draggable/lib/es5/draggable.bundle.legacy.js';
import { SORTABLE_CLASS, HANDLE_CLASS } from './classDefinitions';
export default {
name: 'DragContainer',
components: {},
props: {
items: {
type: Array,
required: true,
},
},
data() {
return {
sortable: null,
};
},
mounted() {
// next tick just to be safe
this.$nextTick(this.initialize);
},
beforeDestroy() {
this.sortable.destroy();
},
methods: {
initialize() {
this.sortable = new Sortable(this.$el, {
draggable: `.${SORTABLE_CLASS}`,
handle: `.${HANDLE_CLASS}`,
delay: 250,
mirror: {
constrainDimensions: true,
xAxis: false,
},
plugins: [Plugins.SwapAnimation],
});
// Remove default focusable plugin and undo damage.
// ref: https://github.com/Shopify/draggable/issues/317
this.sortable.removePlugin(Draggable.Plugins.Focusable);
this.$el.tabIndex = -1;
Array.from(this.$el.children).forEach(child => (child.tabIndex = -1));
// hook up event listeners
this.sortable.on('sortable:start', this.handleStart);
this.sortable.on('sortable:stop', this.handleStop);
},
handleStart() {
// handle cancelation of drags
// document.addEventListener('keyup', this.triggerMouseUpOnESC);
},
handleStop(event) {
const { oldIndex, newIndex } = event.data;
// Do nothing if the item hasn't been moved
if (oldIndex === newIndex) {
return;
}
const itemRemovedArray = [
...this.items.slice(0, oldIndex),
...this.items.slice(oldIndex + 1, this.items.length),
];
const newArray = [
...itemRemovedArray.slice(0, newIndex),
this.items[oldIndex],
...itemRemovedArray.slice(newIndex, itemRemovedArray.length),
];
this.$emit('sort', { newArray, oldIndex, newIndex });
// document.removeEventListener('keyup', this.triggerMouseUpOnESC);
},
},
triggerMouseUpOnESC(event) {
if (event.key === 'Escape' || event.key === 'Esc') {
// this.sortable.cancel();
// const clickEvent = document.createEvent("MouseEvents");
// clickEvent.initEvent("mouseup", true, true);
// document.dispatchEvent(clickEvent);
}
},
// render the first element passed in without a wrapper node
render() {
return this.$slots.default[0];
},
};
</script>
<style lang="scss" scoped>
@import '~kolibri-design-system/lib/styles/definitions';
/deep/ .draggable-mirror {
@extend %dropshadow-8dp;
z-index: 8;
cursor: grabbing;
border-radius: $radius;
}
/deep/ .draggable-source--is-dragging {
visibility: hidden;
}
/deep/ .draggable-source--placed {
animation-name: bounceIn;
animation-duration: $core-time;
}
@keyframes bounceIn {
from,
50%,
to {
animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}
0% {
transform: scale3d(01.15, 01.15, 01.15);
}
50% {
transform: scale3d(0.98, 0.98, 0.98);
}
to {
transform: scale3d(1, 1, 1);
}
}
</style>