This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
HeaderSettingsDnD.js
134 lines (121 loc) · 3.8 KB
/
HeaderSettingsDnD.js
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
/**
* apply drag-and-drop sortable feature to HeaderSettings
* note that this function should be invoked in `mounted`
* e.g.
<template>
<datatable v-bind="$data">
</template>
<script>
import dnd from 'vue2-datatable-component/plugins/HeaderSettingsDnD'
export default {
mounted () {
dnd(this) // done!
},
...
}
</script>
* `vm.columns` should also meet the requirement that:
* the same-group columns should be put together
* e.g.
[ // ok
{ field: 'a1', group: 'A' },
{ field: 'a2', group: 'A' },
{ field: 'b1', group: 'B' },
{ field: 'b2', group: 'B' },
{ field: 'c1', group: 'C' },
{ field: 'c2', group: 'C' }
]
[ // not ok
{ field: 'a1', group: 'A' },
{ field: 'b1', group: 'B' },
{ field: 'c1', group: 'C' },
{ field: 'a2', group: 'A' },
{ field: 'b2', group: 'B' },
{ field: 'c2', group: 'C' }
]
* @param {VueInstance} vm
*/
export default function dnd(vm) {
const $ColGroupContainer = $(vm.$el).find('div.-col-group-container')
const DRAGGABLE = 'li[draggable=true]'
const DROP_ZONE = 'li.-col-drop-zone'
function dropZoneGen(idx) {
return `<li class="-col-drop-zone" target-idx="${idx}"></li>`
}
/** generate adjacent drop zones for each column displayed as <li> */
function generateDropZones() {
$ColGroupContainer
.find(DROP_ZONE).remove().end() // ensure no drop zone exists
.find('li').each(function (idx) {
const $this = $(this)
$this
.attr('draggable', 'true')
.attr('source-idx', idx)
.before(
dropZoneGen(
$this.is('li:first-of-type') ? idx - 0.25 : idx
)
)
$this.is('li:last-of-type') && $this.after(dropZoneGen(idx + 0.25))
})
}
vm.$watch('columns', () => {
vm.$nextTick(generateDropZones)
}, { immediate: true, deep: true })
/*** ↑↑↑ preparatory work --- main logic ↓↓↓ ***/
let draggingIdx = null
$.fn.isAllowedToDrop = function () {
const targetIdx = +$(this).attr('target-idx')
return ![-0.25, 0, 0.25, 1].includes(targetIdx - draggingIdx) // filter adjacent drop zones
}
$ColGroupContainer
.on('dragstart', DRAGGABLE, function () {
draggingIdx = +$(this).addClass('-dragging').attr('source-idx')
})
.on('dragend', DRAGGABLE, function () {
draggingIdx = null
$(this).removeClass('-dragging')
})
.on('dragover', DROP_ZONE, function (e) {
e.preventDefault() // must
e.originalEvent.dataTransfer.dropEffect = $(this).isAllowedToDrop() ? 'move' : 'none'
})
.on('dragenter', DROP_ZONE, function () {
const $this = $(this)
$this.isAllowedToDrop() && $this.addClass('-droppable')
})
.on('drop', DROP_ZONE, function () {
const $this = $(this).removeClass('-droppable')
if (!$this.isAllowedToDrop()) return
const { columns } = vm
const targetIdx = +$this.attr('target-idx')
columns[draggingIdx].group = columns[Math.round(targetIdx)].group
arrMove(columns, draggingIdx, Math.ceil(targetIdx))
})
.on('dragleave', DROP_ZONE, function () {
const $this = $(this)
$this.isAllowedToDrop() && $this.removeClass('-droppable')
})
}
// similar to https://github.com/sindresorhus/array-move
function arrMove(arr, from, to) {
arr.splice((from < to ? to - 1 : to), 0, arr.splice(from, 1)[0])
}
$('head').append(`<style>
.-col-group > [draggable],
.-col-group > .-col-drop-zone {
margin-bottom: 0;
}
.-dragging {
opacity: 0.3;
}
.-col-drop-zone {
height: 10px;
transition: height .25s ease;
}
.-droppable {
height: 30px;
border: 1px dashed #ddd;
border-radius: 4px;
}
</style>`)