-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappes6-with-module.js
153 lines (130 loc) · 4.59 KB
/
appes6-with-module.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
// UI Controler
const UIcontroler = (function() {
const DOMStrings = {
form: '#task-form',
taskInput: '#task',
filter: '#filter',
collection: '.collection',
clearTask: '.clear-task'
};
return {
getDOMStrings: () => {
return DOMStrings;
},
addTask: inputvalue =>{
// create element
let li = document.createElement('li');
// add class name
li.className = 'collection-item';
// Add text
li.appendChild(document.createTextNode(inputvalue));
// Craete link button
const link = document.createElement('a');
// Add class name
link.className = 'delete-item secondary-content';
// Add font awesome cross icon
link.innerHTML = '<i class="fa fa-remove"></i>';
// Append link to li
li.appendChild(link);
// Select collection list
const collection = document.querySelector(DOMStrings.collection);
collection.appendChild(li);
},
clearInput: () => {
document.querySelector(DOMStrings.taskInput).value = '';
},
deleteitem: (e) => {
if(e.target.className === 'fa fa-remove') {
if(confirm('Are u sure?'))
e.target.parentNode.parentNode.remove();
}
},
clearTask: () => {
const collection = document.querySelector(DOMStrings.collection);
// collection.innerHTML = '';
// OR
if(collection.children.length !== 0) {
if(confirm('Are u sure?'))
while(collection.firstChild) {
collection.removeChild(collection.firstChild);
}
}
// Clear From ls
localStorage.clear();
},
// get task From LS
getTask: () => {
let tasks;
if(localStorage.getItem('tasks') === null) {
tasks = [];
} else {
tasks = JSON.parse(localStorage.getItem('tasks'));
}
return tasks;
},
displayTask: () => {
const collection = document.querySelector(DOMStrings.collection);
const tasks = this.getTask();
let self = this;
tasks.forEach((el) => {
self.addTask(el);
});
},
additemToLS: inputvalue => {
const tasks = this.getTask();
tasks.push(inputvalue);
localStorage.setItem('tasks', JSON.stringify(tasks));
},
removeTaskFromLs: e => {
const tasks = this.getTask();
tasks.forEach((el, index) => {
if(e.target.parentNode.parentNode.textContent === el) {
tasks.splice(index, 1);
}
});
localStorage.setItem('tasks', JSON.stringify(tasks));
}
};
})();
// APP Controler
const controler = (function(UICtrl){
let DOMCIN = UICtrl.getDOMStrings();
// Setup all event listener
const allEventListener = () => {
document.querySelector(DOMCIN.form).addEventListener('submit', additem);
document.querySelector(DOMCIN.collection).addEventListener('click', UICtrl.deleteitem);
document.querySelector(DOMCIN.clearTask).addEventListener('click', UICtrl.clearTask);
document.querySelector(DOMCIN.collection).addEventListener('click', removeTaskFromLs);
};
// Add item
const additem = (e) => {
let inputvalue;
// get input value
inputvalue = document.querySelector(DOMCIN.taskInput).value;
//validation
if(inputvalue === '') {
alert("Please Input value!");
} else {
// Add single task
UICtrl.addTask(inputvalue);
// Add to LS
UICtrl.additemToLS(inputvalue);
// Clear Input
UICtrl.clearInput();
}
e.preventDefault();
};
// Remove task list from Ls
const removeTaskFromLs = e => {
let target = e;
UICtrl.removeTaskFromLs(target);
};
return {
init: () => {
allEventListener();
UICtrl.displayTask();
console.log("Programe Started");
}
}
})(UIcontroler);
controler.init();