-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
241 lines (205 loc) · 7.41 KB
/
main.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
let allchecked = true; /* indicates if all contacts are selected */
let allNotChecked = true; /* indicates if no contacts are selected */
let onlyOneChecked = false; /* indicates if only one contact contact is selected */
let subADDRESSES = []; /* subgroup of ADDRESSES array */
/********************************************************************/
/* Show contact form when new contact button clicked */
function newContact() {
document.getElementById('txtName').value = "";
document.getElementById('txtSurname').value = "";
document.getElementById('txtPhone').value = "";
document.getElementById('txtAddress').value = "";
let title = document.getElementById('titleContactForm');
title.innerHTML = '<h2 id="titleContactForm">Add a new contact</h2>';
$('#contactForm').show();
}
/********************************************************************/
/* Show contact form with the info of the selected contact */
function modifyContact() {
for (i=0; i<ADDRESSES.length; i++) {
if (ADDRESSES[i].checked) {
document.getElementById('txtName').value = ADDRESSES[i].name;
document.getElementById('txtSurname').value = ADDRESSES[i].surname;
document.getElementById('txtPhone').value = ADDRESSES[i].phone;
document.getElementById('txtAddress').value = ADDRESSES[i].address;
}
}
let title = document.getElementById('titleContactForm');
title.innerHTML = '<h2 id="titleContactForm">Modify contact</h2>';
$('#contactForm').show();
}
/********************************************************************/
/* Exit contact form when exit button clicked */
function exitForm() {
$('#contactForm').hide();
}
/********************************************************************/
/* Save contact when save button clicked in the contact form */
function saveContact() {
let name = document.getElementById('txtName').value;
let surname = document.getElementById('txtSurname').value;
let phone = document.getElementById('txtPhone').value;
let address = document.getElementById('txtAddress').value;
onlyOneChecked = false;
/* If a contact has been selected for modification, modify ADDRESSES array*/
for (i=0; i<ADDRESSES.length; i++) {
if (ADDRESSES[i].checked) {
ADDRESSES[i].checked = false;
if (document.getElementById('txtName').value!=='' &&
document.getElementById('txtSurname').value!=='' &&
document.getElementById('txtPhone').value!=='' &&
document.getElementById('txtAddress').value!=='') {
ADDRESSES[i].name = document.getElementById('txtName').value;
ADDRESSES[i].surname = document.getElementById('txtSurname').value;
ADDRESSES[i].phone = document.getElementById('txtPhone').value;
ADDRESSES[i].address = document.getElementById('txtAddress').value;
}
onlyOneChecked = true;
}
};
/* If no contact has been selected for modification, add new contact */
if (!onlyOneChecked) {
if (name!=='' && surname!=='' && phone!=='' && address!=='') {
ADDRESSES.push({
checked: false,
name: name,
surname: surname,
phone: phone,
address: address
});
}
};
displayTbl(ADDRESSES);
}
/********************************************************************/
/* If check button click, change its value*/
function checkFunc(idxCheck) {
ADDRESSES[idxCheck].checked = !ADDRESSES[idxCheck].checked;
displayTbl(ADDRESSES);
}
/********************************************************************/
/* If allcheck button clicked, modify all the contact check buttons */
function checkAll() {
allchecked = !allchecked;
for (i=0; i<ADDRESSES.length; i++) {
ADDRESSES[i].checked = allchecked;
}
displayTbl(ADDRESSES);
}
/********************************************************************/
/* Delete contact(s) when delete button clicked */
function deleteContact() {
let index = 0;
let lengthAddresses = ADDRESSES.length;
for (i=0; i<lengthAddresses; i++) {
if (ADDRESSES[index].checked) {
ADDRESSES.splice(index,1);
}
else {
index++;
}
}
if (allchecked) {
allchecked = false;
}
displayTbl(ADDRESSES);
};
/********************************************************************/
/* Search for an input string within all contacts */
function searchContact() {
let searchVal = document.getElementById('txtFilter').value.toLowerCase();
subADDRESSES = [];
for (i=0; i<ADDRESSES.length; i++) {
let name = ADDRESSES[i].name.toLowerCase();
let surname = ADDRESSES[i].surname.toLowerCase();
let phone = ADDRESSES[i].phone.toLowerCase();
let address = ADDRESSES[i].address.toLowerCase();
if (name.includes(searchVal) || surname.includes(searchVal) ||
phone.includes(searchVal) || address.includes(searchVal)) {
subADDRESSES.push(ADDRESSES[i]);
}
};
displayTbl(subADDRESSES);
}
/********************************************************************/
/* Display contact table and buttons format based on contact checked buttons */
function displayTbl(addressArr) {
allNotChecked = true;
onlyOneChecked = false;
let countChecks = 0;
let tblAddress = document.getElementById('addressTblId');
/* Delete content of tblAddress */
while (tblAddress.rows.length>1) {
for (i=1; i<tblAddress.rows.length+1; i++) {
tblAddress.deleteRow(1);
}
}
/* Print tblAddress based on array ADDRESSES */
for (i=0; i<addressArr.length; i++) {
let row = tblAddress.insertRow(i+1);
let checkbox = row.insertCell(0);
let name = row.insertCell(1);
let surname = row.insertCell(2);
let phone = row.insertCell(3);
let address = row.insertCell(4);
if (addressArr[i].checked) {
checkbox.innerHTML ='<img src="img/checkImg.png" onclick="checkFunc('+i+')" id="chBox"/>';
allNotChecked = false;
countChecks++;
}
else {
checkbox.innerHTML ='<img src="img/uncheckImg.png" onclick="checkFunc('+i+')" id="chBox"/>';
allchecked = false;
}
name.innerHTML = addressArr[i].name;
surname.innerHTML = addressArr[i].surname;
phone.innerHTML = addressArr[i].phone;
address.innerHTML = addressArr[i].address;
}
if (countChecks!==1) {
onlyOneChecked = true;
}
if (!allchecked) {
tblAddress.rows[0].cells[0].innerHTML = '<img src="img/uncheckImg.png" id="chBoxAll" onclick="checkAll()"/>';
} else {
tblAddress.rows[0].cells[0].innerHTML = '<img src="img/checkImg.png" id="chBoxAll" onclick="checkAll()"/>';
}
/* Modify button only active if one contact is checked */
if (onlyOneChecked) {
$('#modifyBtn').css({
backgroundColor: '#C0C0C0',
opacity: '0.7',
pointerEvents: 'none',
});
document.getElementById("modifyBtn").disabled = true;
}
else {
$('#modifyBtn').css({
backgroundColor: '#ffa05a',
opacity: '1',
pointerEvents: 'auto',
});
document.getElementById("modifyBtn").disabled = false;
}
/* Delete button active if at least contact is checked */
if (allNotChecked) {
$('#deleteBtn').css({
backgroundColor: '#C0C0C0',
opacity: '0.7',
pointerEvents: 'none',
});
document.getElementById("deleteBtn").disabled = true;
}
else {
$('#deleteBtn').css({
backgroundColor: '#ffa05a',
opacity: '1',
pointerEvents: 'auto',
});
document.getElementById("deleteBtn").disabled = false;
}
};
/********************************************************************/
$(document).ready(() => {
displayTbl(ADDRESSES);
});