-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
338 lines (285 loc) · 9.97 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// var state = {
// taskList: [
// {
// imageUrl : "",
// taskTitle : "",
// taskType : "",
// taskDesciption : "",
// },
// {
// imageUrl : "",
// taskTitle : "",
// taskType : "",
// taskDesciption : "",
// },
// {
// imageUrl : "",
// taskTitle : "",
// taskType : "",
// taskDesciption : "",
// },
// {
// imageUrl : "",
// taskTitle : "",
// taskType : "",
// taskDesciption : "",
// },
// {
// imageUrl : "",
// taskTitle : "",
// taskType : "",
// taskDesciption : "",
// },
// ]
// };
// backup storage
const state = {
taskList: [],
};
// DOM Operations
const taskModal = document.querySelector(".task__modal__body");
const taskContents = document.querySelector(".task__contents");
// console.log(taskContents);
// console.log(taskModal);
// Template for the card on screen
// elem identifier key=${id} is been misssing on line 50th
const htmlTaskContent = ({ id, title, description, type, url }) => `
<div class="col-md-6 col-lg-4 mt-3" id=${id} key=${id}>
<div class='card shadow-sm task__card'>
<div class='card-header d-flex justify-content-end task__card__header'>
<button type='button' class='btn btn-outline-primary mr-2' name=${id} onclick="editTask.apply(this, arguments)">
<i class='fas fa-pencil-alt name=${id}'></i>
</button>
<button type='button' class='btn btn-outline-danger mr-2' name=${id} onclick="deleteTask.apply(this, arguments)">
<i class='fas fa-trash-alt name=${id}' ></i>
</button>
</div>
<div class='card-body'>
${
// url &&
// `<img width='100%' src=${url} alt='Card Image' class='card-img-top md-3 rounded-lg' />`
url
? `<img width='100%' src=${url} alt='Card Image' class='card-img-top md-3 rounded-lg' />`
: `<img width='100%' src="https://tse1.mm.bing.net/th?id=OIP.F00dCf4bXxX0J-qEEf4qIQHaD6&pid=Api&rs=1&c=1&qlt=95&w=223&h=117" alt='Card Image' class='card-img-top md-3 rounded-lg' />`
}
<h4 class='card-title task__card__title'>${title}</h4>
<p class='description trim-3-lines text-muted'>${description}</p>
<div class='tags text-white d-flex flex-wrap'>
<span class='badge bg-primary m-1'>${type}</span>
</div>
</div>
<div class='card-footer'>
<button type='button' class='btn btn-outline-primary float-right' data-bs-toggle="modal" data-bs-target="#showTask" onclick='openTask.apply(this, arguments)' id=${id}>Open Task</button>
</div>
</div>
</div>
`;
// Modal Body on >> Clk of Open Task
const htmlModalContent = ({ id, title, description, url }) => {
const date = new Date(parseInt(id));
return `
<div id=${id}>
${
// url &&
// // `<img width='100%' src=${url} alt='Card Image' class='img-fluid place__holder__image mb-3' />`
// `<img width='100%' src=${url} alt='Card Image' class='img-fluid place__holder__image mb-3' />`
url
? `<img width='100%' src=${url} alt='Card Image' class='card-img-top md-3 rounded-lg' />`
: `<img width='100%' src="https://tse1.mm.bing.net/th?id=OIP.F00dCf4bXxX0J-qEEf4qIQHaD6&pid=Api&rs=1&c=1&qlt=95&w=223&h=117" alt='Card Image' class='card-img-top md-3 rounded-lg' />`
}
<strong class='text-muted text-sm'>Created on: ${date.toDateString()}</strong>
<h2 class='my-3'>${title}</h2>
<p class='text-muted'>${description}</p>
</div>
`;
};
// where we convert json > str (i.e., for local storage)
const updateLocalStorage = () => {
localStorage.setItem(
"task",
JSON.stringify({
tasks: state.taskList,
})
);
};
// where we convert str > json (i.e., for rendering the cards on the screen)
// const loadInitialData = () => {
// const localStorageCopy = JSON.parse(localStorage.task);
// if (localStorageCopy) state.taskList = localStorageCopy.tasks;
// state.taskList.map((cardDate) => {
// // taskContents.innerAdjacentHTML("beforeend", htmlTaskContent(cardDate));
// taskContents.insertAdjacentHTML("beforeend", htmlTaskContent(cardDate));
// });
// };
const loadInitialData = () => {
const localStorageCopy = JSON.parse(localStorage.task);
if (localStorageCopy) state.taskList = localStorageCopy.tasks;
state.taskList.map((cardDate) => {
taskContents.insertAdjacentHTML("beforeend", htmlTaskContent(cardDate));
});
};
// Spread Operator
/**
const obj = {
name: "rohan",
age: 2
}
console.log(obj);
{name: 'rohan', age: 2}
console.log({obj});
{obj: {…}}obj: {name: 'rohan', age: 2}[[Prototype]]: Object
console.log({...obj});
{name: 'rohan', age: 2}
// appending or adding a new key into obj:
console.log({...obj, designation: "mentor"});
{name: 'rohan', age: 2, designation: 'mentor'}
*/
/**
*
// updating key value using spread operator
const obj={
name: "rohan"
}
console.log(obj)
{name: 'rohan'}
console.log({...obj, age : 2});
{name: 'rohan', age: 2}
console.log({...obj, age :4});
{name: 'rohan', age: 4}
*/
/*
var date = new Date();
console.log(Date.now());
1677511569666
*/
// when we update or when we edit ..we need to save
const handleSubmit = (event) => {
// console.log("event triggerd");
const id = `${Date.now()}`;
const input = {
url: document.getElementById("imageUrl").value,
title: document.getElementById("taskTitle").value,
type: document.getElementById("tags").value,
description: document.getElementById("taskDescription").value,
};
if (input.title === "" || input.type === "" || input.description === "") {
return alert("Please fill all the necessary fiels :-)");
}
// taskContents.innerAdjacentHTML(
taskContents.insertAdjacentHTML(
"beforeend",
htmlTaskContent({ ...input, id })
);
state.taskList.push({ ...input, id });
updateLocalStorage();
};
//open task
const openTask = (e) => {
if (!e) e = window.event;
const getTask = state.taskList.find(({ id }) => id === e.target.id);
taskModal.innerHTML = htmlModalContent(getTask);
};
// delete task
const deleteTask = (e) => {
if (!e) e = window.event;
const targetId = e.target.getAttribute("name");
// console.log(targetId);
const type = e.target.tagName;
// console.log(type);
const removeTask = state.taskList.filter(({ id }) => id !== targetId);
// console.log(removeTask);
updateLocalStorage();
if (type === "BUTTON") {
// console.log(e.target.parentNode.parentNode.parentNode.parentNode);
return e.target.parentNode.parentNode.parentNode.parentNode.removeChild(
e.target.parentNode.parentNode.parentNode
);
} else if (type === "I") {
return e.target.parentNode.parentNode.parentNode.parentNode.parentNode.removeChild(
e.target.parentNode.parentNode.parentNode.parentNode
);
}
};
// edit task
const editTask = (e) => {
if (!e) e = window.event;
const targetId = e.target.id;
const type = e.target.tagName;
let parentNode;
let taskTitle;
let taskDescription;
let taskType;
let submitButton;
if (type === "BUTTON") {
parentNode = e.target.parentNode.parentNode;
} else {
parentNode = e.target.parentNode.parentNode.parentNode;
}
// taskTitle = parentNode.childNodes[3].childNodes[7].childNodes;
// console.log(taskTitle);
taskTitle = parentNode.childNodes[3].childNodes[3];
taskDescription = parentNode.childNodes[3].childNodes[5];
taskType = parentNode.childNodes[3].childNodes[7].childNodes[1];
submitButton = parentNode.childNodes[5].childNodes[1];
// console.log(taskTitle, taskDescription, taskType, submitButton);
taskTitle.setAttribute("contenteditable", "true");
taskDescription.setAttribute("contenteditable", "true");
taskType.setAttribute("contenteditable", "true");
submitButton.setAttribute("onclick", "saveEdit.apply(this, arguments)");
submitButton.removeAttribute("data-bs-toggle");
submitButton.removeAttribute("data-bs-target");
submitButton.innerHTML = "Save Changes";
};
// save edit
const saveEdit = (e) => {
if (!e) e = window.event;
const targetId = e.target.id;
const parentNode = e.target.parentNode.parentNode;
// console.log(parentNode.childNodes)
const taskTitle = parentNode.childNodes[3].childNodes[3];
const taskDescription = parentNode.childNodes[3].childNodes[5];
const taskType = parentNode.childNodes[3].childNodes[7].childNodes[1];
const submitButton = parentNode.childNodes[5].childNodes[1];
const updateData = {
taskTitle: taskTitle.innerHTML,
taskDescription: taskDescription.innerHTML,
taskType: taskType.innerHTML,
};
let stateCopy = state.taskList;
stateCopy = stateCopy.map((task) =>
task.id === targetId
? {
id: task.id,
title: updateData.taskTitle,
description: updateData.taskDescription,
type: updateData.taskType,
url: task.url,
}
: task
);
state.taskList = stateCopy;
updateLocalStorage();
taskTitle.setAttribute("contenteditable", "false");
taskDescription.setAttribute("contenteditable", "false");
taskType.setAttribute("contenteditable", "false");
submitButton.setAttribute("onclick", "openTask.apply(this, arguments)");
submitButton.setAttribute("data-bs-toggle", "modal");
submitButton.setAttribute("data-bs-target", "#showTask");
submitButton.innerHTML = "Open Task";
};
// search
const searchTask = (e) => {
if (!e) e = window.event;
while (taskContents.firstChild) {
taskContents.removeChild(taskContents.firstChild);
}
const resultData = state.taskList.filter(({ title }) =>
title.toLowerCase().includes(e.target.value.toLowerCase())
);
// console.log(resultData);
resultData.map(
(cardData) =>
taskContents.insertAdjacentHTML("beforeend", htmlTaskContent(cardData))
// taskContents.insertAdjacentHTML("beforeend", htmlModalContent(cardData))
);
};