-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
225 lines (205 loc) · 7.04 KB
/
Copy pathscript.js
File metadata and controls
225 lines (205 loc) · 7.04 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const addBtns = document.querySelectorAll('.add-btn:not(.solid)');
const saveItemBtns = document.querySelectorAll('.solid');
const addItemContainers = document.querySelectorAll('.add-container');
const addItems = document.querySelectorAll('.add-item');
// Item Lists
const listColumns = document.querySelectorAll('.drag-item-list');
const ideasList = document.getElementById('ideas-list');
const workingList = document.getElementById('working-list');
const completeList = document.getElementById('complete-list');
const onHoldList = document.getElementById('on-hold-list');
// Initialize Arrays
let ideasListArray = [];
let workingListArray = [];
let completeListArray = [];
let onHoldListArray = [];
let listArrays = [];
// Drag Functionality
let draggedItem;
let currentColumn;
let dragging = false;
// Get Arrays from localStorage if available, set default values if not
function getSavedColumns() {
if (localStorage.getItem('ideasItems')) {
ideasListArray = JSON.parse(localStorage.ideasItems);
workingListArray = JSON.parse(localStorage.workingItems);
completeListArray = JSON.parse(localStorage.completeItems);
onHoldListArray = JSON.parse(localStorage.onHoldItems);
} else {
ideasListArray = ['Release the course', 'Sit back and relax'];
workingListArray = ['Work on projects', 'Listen to music'];
completeListArray = ['Being cool', 'Getting stuff done'];
onHoldListArray = ['Being uncool'];
}
}
// Set localStorage Arrays
function updateSavedColumns() {
listArrays = [ideasListArray, workingListArray, completeListArray, onHoldListArray];
const arrayNames = ['ideas', 'working', 'complete', 'onHold'];
arrayNames.forEach((arrayName, index) => {
localStorage.setItem(`${arrayName}Items`, JSON.stringify(listArrays[index]));
});
}
// filter arrays to remove empty items
function filterArray(array) {
const filteredArray = array.filter(item => item !== null )
return filteredArray;
}
// Create DOM Elements for each list item
function createItemEl(parentEl, column, item, index) {
// List Item
const listEl = document.createElement('li');
listEl.classList.add('drag-item');
listEl.textContent = item;
listEl.draggable = true;
listEl.contentEditable = true;
listEl.addEventListener('dragstart', drag);
listEl.addEventListener('keydown', inputExit);
listEl.dataset.id = index;
listEl.dataset.col = column;
// add class instead id!!!
listEl.addEventListener('focusout', () => updateItem(index, column));
parentEl.appendChild(listEl);
}
function inputExit(e) {
const eId = e.target.dataset.id;
const eCol = e.target.dataset.col;
const selectedArray = listArrays[eCol];
if (e.keyCode === 13) {
console.log(e.target.dataset.id, e.target.dataset.col);
updateItem(e.target.dataset.id, e.target.dataset.col);
return;
}
if (e.keyCode === 46) {
console.log(selectedArray[eId]);
e.target.textContent = '';
delete selectedArray[eId];
console.log(listArrays[eCol][eId]);
updateDOM();
}
}
// Update Columns in DOM - Reset HTML, Filter Array, Update localStorage
function updateDOM() {
ideasList.textContent = '';
ideasListArray.forEach((ideasItem, index) => {
createItemEl(ideasList, 0, ideasItem, index);
});
ideasListArray = filterArray(ideasListArray);
// working Column
workingList.textContent = '';
workingListArray.forEach((workingItem, index) => {
createItemEl(workingList, 1, workingItem, index);
});
workingListArray = filterArray(workingListArray);
// Complete Column
completeList.textContent = '';
completeListArray.forEach((completeItem, index) => {
createItemEl(completeList, 2, completeItem, index);
});
completeListArray = filterArray(completeListArray);
// On Hold Column
onHoldList.textContent = '';
onHoldListArray.forEach((onHoldItem, index) => {
createItemEl(onHoldList, 3, onHoldItem, index);
});
onHoldListArray = filterArray(onHoldListArray);
// Run getSavedColumns only once, Update Local Storage
updateSavedColumns();
}
// update item or delete, update array
function updateItem(id, column) {
const selectedArray = listArrays[column];
const selectedColumnEl = listColumns[column].children;
if (!dragging) {
if (!selectedColumnEl[id].textContent) {
console.log(selectedArray[id]);
delete selectedArray[id];
console.log(listArrays[column]);
} else {
selectedArray[id] = selectedColumnEl[id].textContent;
}
updateDOM();
}
}
// add to column and reset input
function addToColumn(column) {
const itemText = addItems[column].textContent;
const selectedArray = listArrays[column];
selectedArray.push(itemText);
updateDOM();
addItems[column].textContent = '';
}
// show add item box
function showInputBox(column) {
addBtns[column].style.visibility = 'hidden';
saveItemBtns[column].style.display = 'flex';
addItemContainers[column].style.display = 'flex';
addItemContainers[column].firstElementChild.focus();
console.log(addItemContainers);
}
// hide add item box
function hideInputBox(column) {
addBtns[column].style.visibility = 'visible';
saveItemBtns[column].style.display = 'none';
addItemContainers[column].style.display = 'none';
if (addItems[column].textContent) {
addToColumn(column);
}
}
// allow arrays reflect drag and drop
function rebuildArrays() {
ideasListArray = Array.from(ideasList.children).map(item => item.textContent);
workingListArray = Array.from(workingList.children).map(item => item.textContent);
completeListArray = Array.from(completeList.children).map(item => item.textContent);
onHoldListArray = Array.from(onHoldList.children).map(item => item.textContent);
updateDOM();
}
// when start dragging item
function drag(e) {
draggedItem = e.target;
dragging = true;
}
//allow item to drop
function allowDrop(e) {
e.preventDefault();
}
//dropping item
function drop(e) {
e.preventDefault();
// remove background color/padding
listColumns.forEach((column) => {
column.classList.remove('over');
});
// add item to column
const parent = listColumns[currentColumn];
parent.appendChild(draggedItem);
dragging = false;
rebuildArrays();
}
// when item enters column area
function dragEnter(column) {
listColumns[column].classList.add('over');
currentColumn = column;
}
// function hideInputOnEnter(e) {
// console.log(e.target.parentNode, e);
// if (e.keyCode === 13) {
// console.log(e.target);
// hideInputBox(index);
// }
// }
listColumns.forEach((item, index) => {
item.addEventListener('dragover', allowDrop);
item.addEventListener('drop', drop);
item.addEventListener('dragenter', () => dragEnter(index));
});
addItems.forEach(item => item.contentEditable = true);
addBtns.forEach((item, index) => item.addEventListener('click', () => showInputBox(index)));
saveItemBtns.forEach((item, index) => item.addEventListener('click', () => hideInputBox(index)));
addItemContainers.forEach((item, index) => {
item.firstElementChild.addEventListener('focusout', () => hideInputBox(index));
// item.firstElementChild.addEventListener('keydown', hideInputOnEnter);
});
getSavedColumns();
updateSavedColumns();
updateDOM();