-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
195 lines (159 loc) · 6.65 KB
/
index.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
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
const cardContainer = document.querySelector(".card__container");
// global array for local storage
let globalStore = [];
const newCard = ({ id, imgUrl, taskTitle, taskType, taskDescription }) => {
return `<div class="col-lg-3 col-md-4 col-sm-6 mb-3" id=${id}>
<div class="card">
<div class="card-header d-flex justify-content-end gap-1">
<button class="btn id=${id} btn-outline-success btn-sm" onclick="editCard.apply(this, arguments)">
<i class="far fa-edit" id=${id}></i>
</button>
<button class="btn btn-outline-danger btn-sm" id=${id} onclick="deleteCard.apply(this, arguments)">
<i class="fas fa-trash" onclick="deleteCard.apply(this, arguments)"></i>
</button>
</div>
<div class="card-body">
<img src=${imgUrl} alt="" class="card-img-top mb-2">
<h4 class="card-title">${taskTitle}</h4>
<p class="card-text">${taskDescription}</p>
<h5>
<span class="badge bg-success">${taskType}</span>
</h5>
</div>
<div class="card-footer d-flex justify-content-end">
<button class="btn btn-primary" id=${id} type="button" data-bs-toggle="modal" data-bs-target="#openTask" onclick="openTask.apply(this, arguments)">
Open task
</button>
</div>
</div>
</div>`;
}
const loadInitialTaskCards = () => {
// accessing local storage
const getInitialData = localStorage.getItem("taskApp");
if (!getInitialData) return; // if on new system we won't have tasky so to handle it
const { cards } = JSON.parse(getInitialData);
cards.forEach((cardObject) => {
const createNewCard = newCard(cardObject);
cardContainer.insertAdjacentHTML("beforeend", createNewCard);
globalStore.push(cardObject);
});
};
// function called when we click the save button in add task
const saveChanges = () => {
const taskData = {
id: `${Date.now()}`,
imgUrl: document.getElementById("imageUrl").value,
taskTitle: document.getElementById("taskTitle").value,
taskType: document.getElementById("taskType").value,
taskDescription: document.getElementById("taskDescription").value
};
const createNewCard = newCard(taskData);
cardContainer.insertAdjacentHTML("beforeend", createNewCard);
globalStore.push(taskData);
// local storage API
localStorage.setItem("taskApp", JSON.stringify({ cards: globalStore }));
};
const deleteCard = (e) => {
e = window.event;
const targetID = e.target.id;
const tagname = e.target.tagName;
globalStore = globalStore.filter((cardObject) => cardObject.id !== targetID);
localStorage.setItem("taskApp", JSON.stringify({ cards: globalStore }));
if (tagname === "BUTTON") {
return cardContainer.removeChild(
e.target.parentNode.parentNode.parentNode
);
}
else {
return cardContainer.removeChild(
e.target.parentNode.parentNode.parentNode.parentNode
);
}
};
const editCard = (e) => {
e = window.event;
const targetId = e.target.id;
const tagName = e.target.tagName;
let parentElement = e.target.parentNode.parentNode
if (tagName === "I")
parentElement = parentElement.parentNode;
let taskTitle = parentElement.childNodes[3].childNodes[3];
taskTitle.setAttribute("contenteditable", "true");
let taskDescription = parentElement.childNodes[3].childNodes[5];
taskDescription.setAttribute("contenteditable", "true");
let taskType = parentElement.childNodes[3].childNodes[7];
taskType.setAttribute("contenteditable", "true");
let saveButton = parentElement.childNodes[5].childNodes[1];
saveButton.removeAttribute("type");
saveButton.removeAttribute("data-bs-toggle");
saveButton.removeAttribute("data-bs-target");
saveButton.setAttribute("onclick", "saveEditChanges.apply(this, arguments)")
saveButton.innerText = "Save Changes";
}
const saveEditChanges = (e) => {
e = window.event;
const targetId = e.target.id;
const tagName = e.target.tagName;
let parentElement = e.target.parentNode.parentNode
if (tagName === "I")
parentElement = parentElement.parentNode;
let taskTitle = parentElement.childNodes[3].childNodes[3];
taskTitle.setAttribute("contenteditable", "false");
let taskDescription = parentElement.childNodes[3].childNodes[5];
taskDescription.setAttribute("contenteditable", "false");
let taskType = parentElement.childNodes[3].childNodes[7];
taskType.setAttribute("contenteditable", "false");
let saveButton = parentElement.childNodes[5].childNodes[1];
const updatedData = {
taskTitle: taskTitle.innerText,
taskType: taskType.innerText,
taskDescription: taskDescription.innerText
}
globalStore = globalStore.map((task) => {
if (task.id === targetId) {
return {
id: task.id,
imgUrl: task.imgUrl,
taskTitle: updatedData.taskTitle,
taskType: updatedData.taskType,
taskDescription: updatedData.taskDescription
};
}
return task;
});
localStorage.setItem("taskApp", JSON.stringify({ cards: globalStore }));
saveButton.innerText = "Open Task";
saveButton.setAttribute("onclick", "openTask.apply(this, arguments)");
saveButton.setAttribute("type", "button");
saveButton.setAttribute("data-bs-toggle", "modal");
saveButton.setAttribute("data-bs-target", "#openTask");
}
const openTaskTemplate = ({ id, imgUrl, taskTitle, taskType, taskDescription }) => {
return `<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">${taskTitle}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="imageBlock">
<img src=${imgUrl} alt="Task image" class="img-fluid">
</div>
<p>${taskDescription}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>`;
};
const openTask = (e) => {
e = window.event;
const targetId = e.target.id;
const openTaskData = globalStore.filter((data) => {
return data.id === targetId;
});
const openTaskDiv = document.querySelector("#openTask");
openTaskDiv.innerHTML = openTaskTemplate(openTaskData[0]);
};