-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddress.js
More file actions
207 lines (151 loc) · 6.04 KB
/
address.js
File metadata and controls
207 lines (151 loc) · 6.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
var quickAddBtn = document.getElementById("QuickAdd");
var AddBtn = document.getElementById("Add");
var cancelBtn = document.getElementById("Cancel");
var quickAddFormDiv = document.querySelector(".quickaddForm");
var editFormDiv = document.querySelector(".editForm");
var fullname = document.getElementById("fullname");
var phone = document.getElementById("phone");
var address = document.getElementById("address");
var city = document.getElementById("city");
var email = document.getElementById("email");
var addBookDiv = document.querySelector(".addbook");
var addressBook = [];
quickAddBtn.addEventListener("click", function(){
quickAddFormDiv.style.display = "block";
editFormDiv.style.display = "none";
});
cancelBtn.addEventListener("click", function(){
quickAddFormDiv.style.display = "none";
});
AddBtn.addEventListener("click", addToBook);
addBookDiv.addEventListener("click", removeEntry);
function jsonStructure(fullname, phone, address, city, email){
this.fullname = fullname;
this.phone = phone;
this.address = address;
this.city = city;
this.email = email;
}
// Add into the address book function
function addToBook(){
if (fullname.value!=="" && phone.value!=="" && address.value!=="" && city.value!=="" && email.value!=="")
{
var newObj = new jsonStructure(fullname.value, phone.value, address.value, city.value, email.value) ;
addressBook.push(newObj);
localStorage['addbook'] = JSON.stringify(addressBook);
quickAddFormDiv.style.display = "none";
showAddressBook();
clearForm();
}
}
// Remove and Edit function
function removeEntry(e){
if(e.target.classList.contains("delbutton")){
var affirm = confirm("Delete contact?");
if(affirm){
var remID =e.target.getAttribute("value");
remID = remID - 1;
addressBook.splice(remID, 1);
localStorage['addbook'] = JSON.stringify(addressBook);
showAddressBook();
}
}else if(e.target.classList.contains("edtbutton")){
var remID =e.target.getAttribute("value");
remID = remID - 1;
var edtID = e.target.getAttribute("data.id");
editFormDiv.style.display = "block";
quickAddFormDiv.style.display = "none";
var AddEdt = document.getElementById("addEdt");
var cancelEdt = document.getElementById("cancelEdt");
var edtname = document.getElementById("edtName");
var edtphone = document.getElementById("edtPhone");
var edtaddress = document.getElementById("edtAddress");
var edtcity = document.getElementById("edtCity");
var edtemail = document.getElementById("edtEmail");
edtname.value = addressBook[remID].fullname;
edtphone.value = addressBook[remID].phone;
edtaddress.value= addressBook[remID].address;
edtcity.value = addressBook[remID].city;
edtemail.value = addressBook[remID].email;
function jsonStructure(fullname, phone, address, city, email){
this.fullname = fullname;
this.phone = phone;
this.address = address;
this.city = city;
this.email = email;
}
function addToBook(){
edtname = edtname.value;
fullname = edtname.trim();
edtphone = edtphone.value;
phone = edtphone.trim();
edtaddress = edtaddress.value;
address = edtaddress.trim();
edtcity = edtcity.value;
city = edtcity.trim();
edtemail = edtemail.value;
email = edtemail.trim();
if (fullname=="" || phone=="" || address=="" || city=="" || email =="")
{
var error = document.getElementById("editErr");
error.innerHTML = "*Some input spaces are empty*"
}
else{
var edtObj = new jsonStructure(fullname, phone, address, city, email);
addressBook.splice(remID, 1, edtObj);
// addressBook.push();
localStorage['addbook'] = JSON.stringify(addressBook);
editFormDiv.style.display = "none";
function clearEdtForm()
{
edtname = "";
edtphone = "";
edtaddress = "";
edtcity = "";
edtemail = "";
}
showAddressBook();
}
}
cancelEdt.addEventListener("click", function(){
editFormDiv.style.display = "none";
});
AddEdt.addEventListener("click", addToBook);
}
}
function clearForm(){
var fullname = document.getElementById("fullname");
var phone = document.getElementById("phone");
var address = document.getElementById("address");
var city = document.getElementById("city");
var email = document.getElementById("email");
fullname.value = "";
phone.value = "";
address.value = "";
city.value = "";
email.value = "";
}
function showAddressBook(){
if(localStorage['addbook'] === undefined){
localStorage['addbook'] = "[]";
}else
{
addressBook = JSON.parse(localStorage['addbook']);
addBookDiv.innerHTML = '<span class="header"><span class="head">ID</span><span class="head">Name</span><span class="head">Email</span><span class="head">Phone</span><span class="head">Address</span><span class="head">City</span><span class="head">Actions</span></span>';
for(var n in addressBook){
var no = Number(n)+1;
var str = '<div class="entry">';
str += '<span class="spa name" id="no">' + no + '</span>';
str += '<span class="spa name">' + addressBook[n].fullname + '</span>';
str += '<span class="spa email">' + addressBook[n].email + '</span>';
str += '<span class="spa phone">' + addressBook[n].phone + '</span>';
str += '<span class="spa address">' + addressBook[n].address + '</span>';
str += '<span class="spa city">'+ addressBook[n].city + '</span>';
str += '<span class="del"><button href="#" id="no" value="'+no+'"class="delbutton">Delete</button></span>';
str += '<span class="edt"><button href="#" id="no" value="'+no+'"class="edtbutton">Edit</button></span>';
str += '</div>';
addBookDiv.innerHTML += str;
}
}
}
showAddressBook();