-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraganddrop.js
103 lines (96 loc) · 3.78 KB
/
draganddrop.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
module.exports = function (mpcp) {
// drag and drop operations
var drake = dragula([
document.getElementById(mpcp.browser.tbodyid),
document.getElementById(mpcp.playlist.tbodyid),
document.getElementById(mpcp.pe.tbodyid),
document.getElementById(mpcp.libraryAlbums.tbodyid),
document.getElementById(mpcp.libraryArtists.tbodyid),
document.getElementById(mpcp.librarySongs.tbodyid)
], {
copy: function (el, source) {
// clone everything but the playlist and playlist editor
return (
source.parentElement.id !== mpcp.playlist.tableid &&
source.parentElement.id !== mpcp.pe.tableid
)
},
accepts: function (el, target, source, sibling) {
var tp = target.parentElement.id
var sp = source.parentElement.id
// only accept the playlist and playlist editor but
// make sure pl and pe don't accept eachother
return (
(
(tp !== mpcp.pe.tableid || sp !== mpcp.playlist.tableid) &&
(tp !== mpcp.playlist.tableid || sp !== mpcp.pe.tableid)
) &&
(
tp === mpcp.playlist.tableid ||
tp === mpcp.pe.tableid
)
)
}
})
function getIndex (el, target) {
// added at the end of the target
if (!el) return target.children.length - 1
return Array.prototype.indexOf.call(target.children, el) - 1
}
drake.on('drag', function (el, source) {
switch (source.parentElement.id) {
case mpcp.browser.tableid:
mpcp.utils.checkSelected(el, mpcp.browser)
break
case mpcp.libraryArtists.tableid:
mpcp.utils.checkSelected(el, mpcp.libraryArtists)
break
case mpcp.libraryAlbums.tableid:
mpcp.utils.checkSelected(el, mpcp.libraryAlbums)
break
case mpcp.librarySongs.tableid:
mpcp.utils.checkSelected(el, mpcp.librarySongs)
break
case mpcp.playlist.tableid:
mpcp.utils.checkSelected(el, mpcp.playlist)
break
case mpcp.pe.tableid:
mpcp.utils.checkSelected(el, mpcp.pe)
}
})
drake.on('drop', function (el, target, source, sibling) {
if (!source || !target) return console.log('null dnd')
var tp = target.parentElement.id
var sp = source.parentElement.id
var index = getIndex(sibling, target)
var pageIndex = 0
// TODO figure out how to make the pe and pl scroll?
if ((sp === mpcp.browser.tableid && tp === mpcp.playlist.tableid) ||
(sp === mpcp.libraryArtists.tableid && tp === mpcp.playlist.tableid) ||
(sp === mpcp.libraryAlbums.tableid && tp === mpcp.playlist.tableid) ||
(sp === mpcp.librarySongs.tableid && tp === mpcp.playlist.tableid)) {
// browser -> playlist
// library artists -> playlist
// library albums -> playlist
// library songs -> playlist
pageIndex = (mpcp.pages.currentPlaylist - 1) * mpcp.pages.maxPlaylist
mpcp.playlist.fromSortableSender(el, index + pageIndex)
} else if (sp === mpcp.playlist.tableid && tp === mpcp.playlist.tableid) {
// playlist -> playlist
pageIndex = (mpcp.pages.currentPlaylist - 1) * mpcp.pages.maxPlaylist
mpcp.playlist.fromSortableSelf(el, index + pageIndex)
} else if ((sp === mpcp.browser.tableid && tp === mpcp.pe.tableid) ||
(sp === mpcp.libraryArtists.tableid && tp === mpcp.pe.tableid) ||
(sp === mpcp.libraryAlbums.tableid && tp === mpcp.pe.tableid) ||
(sp === mpcp.librarySongs.tableid && tp === mpcp.pe.tableid)) {
// browser -> playlist editor
// library artists -> playlist editor
// library albums -> playlist editor
// library songs -> playlist editor
mpcp.pe.fromSortableSender(el, index)
} else if (sp === mpcp.pe.tableid && tp === mpcp.pe.tableid) {
// playlist editor -> playlist editor
mpcp.pe.fromSortableSelf(el)
}
})
}