-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.js
100 lines (80 loc) · 2.69 KB
/
List.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
let draggedListpad = null;
class List {
constructor(listId, listpadType, save) {
this.list = document.getElementById(listId);
this.listpadType = listpadType;
this.save = save;
}
newListpadButton() {
const className = 'new-listpad-btn';
return this.list.getElementsByClassName(className)[0];
}
createNewListpadButton(title, onclick) {
const className = 'new-listpad-btn'
if (this.list.getElementsByClassName(className)[0]) {
return;
}
let newBtn = document.createElement('button');
newBtn.className = className;
newBtn.innerText = title;
newBtn.onclick = onclick;
this.list.prepend(newBtn);
}
createListpad(uid) {
let listpad = new Listpad(uid, this.listpadType, this.save);
this.addDragFunctionality(listpad.element());
return listpad;
}
getListpad(uid) {
let listpads = this.getListpads();
for (let i = 0; i < listpads.length; i++) {
if (listpads[i].getAttribute('uid') == uid) {
return listpads[i];
}
}
}
getListpads() {
return [...document.getElementsByClassName(this.listpadType)];
}
addDragFunctionality(listpad) {
listpad.setAttribute('draggable', true);
let save = this.save;
let listpadType = this.listpadType;
listpad.addEventListener('dragstart', dragStart);
listpad.addEventListener('dragenter', dragEnter);
listpad.addEventListener('dragend', dragEnd);
function dragStart(e) {
if (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA') {
draggedListpad = null;
e.preventDefault();
e.stopPropagation();
return;
}
draggedListpad = listpad;
}
function dragEnter() {
let listpads = [...document.getElementsByClassName(listpadType)];
if (!draggedListpad) {
return;
}
if (draggedListpad.className != listpadType) {
return;
}
if (listpads.indexOf(draggedListpad) < listpads.indexOf(listpad)) {
listpad.after(draggedListpad);
} else {
listpad.before(draggedListpad);
}
}
function dragEnd() {
if (!draggedListpad) {
return;
}
save();
setTimeout(setDraggedNull, 0.05);
function setDraggedNull() {
draggedListpad = null;
}
}
}
}