-
Notifications
You must be signed in to change notification settings - Fork 0
/
initDND.js
77 lines (61 loc) · 3.04 KB
/
initDND.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
const initDND = data => {
$('.url-cont').sortable('destroy');
$('.url-cont').sortable({
// SortableJS options go here
// See: (https://github.com/SortableJS/Sortable#options)
group: 'shared',
animation: 150,
draggable: '.url-buttons',
onEnd: event => {
(async () => {
let data = await storageGet();
const oldGroupId = $(event.from).prop('id').slice(9);
const newGroupId = $(event.to).prop('id').slice(9);
let obj;
let tmpUrlDataObj = {};
data[oldGroupId].data.forEach(urlData => tmpUrlDataObj[urlData.urlId] = urlData);
data[oldGroupId].data = [];
$(`#url-cont-${oldGroupId}`).find('.url-buttons').each(function() {
const urlId = parseInt($(this).attr('id').slice(9));
data[oldGroupId].data.push(tmpUrlDataObj[urlId]);
});
if (oldGroupId === newGroupId) {
obj = await updateOrRemainGroupBookmarksNUrls(data,oldGroupId,data[oldGroupId].data,data[oldGroupId].groupName);
if (obj) {
data = obj.storageData;
}
} else {
data[newGroupId].data.forEach(urlData => tmpUrlDataObj[urlData.urlId] = urlData);
data[newGroupId].data = [];
$(`#url-cont-${newGroupId}`).find('.url-buttons').each(function() {
const urlId = parseInt($(this).attr('id').slice(9));
data[newGroupId].data.push(tmpUrlDataObj[urlId]);
});
obj = await updateOrRemainGroupBookmarksNUrls(data,oldGroupId,data[oldGroupId].data,data[oldGroupId].groupName);
if (obj) {
data = obj.storageData;
}
obj = await updateOrRemainGroupBookmarksNUrls(data,newGroupId,data[newGroupId].data,data[newGroupId].groupName);
if (obj) {
data = obj.storageData;
}
// color change
const rgbColor = data[newGroupId].color;
$(`#${event.to.id}`).find('.url-text > p').css('color',rgbColor);
}
await storageSet(data);
})();
/*
evt.item; // dragged HTMLElement
evt.to; // target list
evt.from; // previous list
evt.oldIndex; // element's old index within old parent
evt.newIndex; // element's new index within new parent
evt.oldDraggableIndex; // element's old index within old parent, only counting draggable elements
evt.newDraggableIndex; // element's new index within new parent, only counting draggable elements
evt.clone // the clone element
evt.pullMode; // when item is in another sortable: `"clone"` if cloning, `true` if moving
*/
}
});
};