-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
332 lines (286 loc) · 14.1 KB
/
Copy pathscript.js
File metadata and controls
332 lines (286 loc) · 14.1 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
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
let contacts = [];
let currentEditId = null;
let currentAvatar = "";
let contactModal = null;
document.addEventListener("DOMContentLoaded", () => {
loadContactsFromStorage();
contactModal = new bootstrap.Modal(document.getElementById('contactModal'));
renderAll();
setupEventListeners();
});
function saveToStorage() {
localStorage.setItem("myContacts", JSON.stringify(contacts));
}
function loadContactsFromStorage() {
const stored = localStorage.getItem("myContacts");
if (stored) contacts = JSON.parse(stored);
}
function generateId() {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
function addContact(contactData) {
const newContact = {
id: generateId(),
...contactData,
createdAt: new Date().toISOString()
};
contacts.push(newContact);
saveToStorage();
renderAll();
}
function updateContact(id, updatedData) {
const index = contacts.findIndex(c => c.id === id);
if (index !== -1) {
contacts[index] = { ...contacts[index], ...updatedData };
saveToStorage();
renderAll();
}
}
function deleteContact(id) {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(result => {
if (result.isConfirmed) {
contacts = contacts.filter(c => c.id !== id);
saveToStorage();
renderAll();
Swal.fire('Deleted!', 'Contact has been deleted.', 'success');
}
});
}
function toggleFavorite(id) {
const contact = contacts.find(c => c.id === id);
if (contact) {
contact.isFavorite = !contact.isFavorite;
saveToStorage();
renderAll();
}
}
function toggleEmergency(id) {
const contact = contacts.find(c => c.id === id);
if (contact) {
contact.isEmergency = !contact.isEmergency;
saveToStorage();
renderAll();
}
}
function renderAll() {
const searchTerm = document.getElementById("searchInput").value.toLowerCase();
const filteredContacts = contacts.filter(c =>
c.name.toLowerCase().includes(searchTerm) ||
c.phone.includes(searchTerm) ||
(c.email && c.email.toLowerCase().includes(searchTerm))
);
renderContactsGrid(filteredContacts);
renderStats();
renderSidebars();
}
function renderContactsGrid(list) {
const grid = document.getElementById("contacts-grid");
if (list.length === 0) {
grid.innerHTML = `<div class="col-12 text-center text-muted py-5"><i class="fa-solid fa-box-open fs-1 mb-3"></i><p>No contacts found.</p></div>`;
return;
}
grid.innerHTML = list.map(contact => `
<div class="col-md-6">
<div class="card contact-card h-100 rounded-4 border-0 shadow-sm bg-white position-relative">
<div class="custom-badge-position p-3 d-flex flex-column gap-1">
${contact.isFavorite ? '<i class="fa-solid fa-star custom-yellow-icon rounded-circle"></i>' : ''}
${contact.isEmergency ? '<i class="fa-solid fa-heart-pulse custom-red-icon rounded-circle"></i>' : ''}
</div>
<div class="card-body">
<div class="d-flex align-items-center gap-3 mb-3">
${getAvatarHTML(contact, 'md')}
<div>
<h6 class="fw-bold mb-0 text-dark">${contact.name}</h6>
<small class="text-muted"><i class="fa-solid fa-phone me-1 custom-phone-icon"></i> ${contact.phone}</small>
</div>
</div>
<div class="small text-secondary mb-3">
${contact.email ? `<div class="mb-1"><i class="fa-solid fa-envelope me-2 custom-purple-icon"></i>${contact.email}</div>` : ''}
${contact.address ? `<div><i class="fa-solid fa-location-dot me-2 custom-green-icon"></i>${contact.address}</div>` : ''}
</div>
<div class="mb-3">
${contact.group ? `<span class="badge badge-${contact.group} rounded-1 fw-normal px-2 py-1 text-capitalize">${contact.group}</span>` : ''}
${contact.isEmergency ? `<span class="badge badge-danger rounded-1 fw-normal px-2 py-1">Emergency</span>` : ''}
</div>
</div>
<div class="card-footer bg-light border-0 d-flex justify-content-between py-2 rounded-bottom-4">
<div>
<a href="tel:${contact.phone}" class="custom-green-hover custom-green-icon"><i class="fa-solid fa-phone"></i></a>
${contact.email ? `<a href="mailto:${contact.email}" class="custom-yellow-hover custom-purple-icon"><i class="fa-solid fa-envelope"></i></a>` : ''}
</div>
<div>
<button onclick="toggleFavorite('${contact.id}')" class="btn btn-sm btn-light ${contact.isFavorite ? 'text-warning' : 'text-secondary'}"><i class="fa-${contact.isFavorite ? 'solid' : 'regular'} fa-star"></i></button>
<button onclick="toggleEmergency('${contact.id}')" class="btn btn-sm btn-light ${contact.isEmergency ? 'text-danger' : 'text-secondary'}"><i class="fa-solid fa-heart-pulse"></i></button>
<button onclick="openEditModal('${contact.id}')" class="btn btn-sm btn-light text-secondary"><i class="fa-solid fa-pen"></i></button>
<button onclick="deleteContact('${contact.id}')" class="btn btn-sm btn-light text-secondary"><i class="fa-solid fa-trash"></i></button>
</div>
</div>
</div>
</div>
`).join("");
document.getElementById("contacts-count").textContent = `Manage and organize your ${list.length} contacts`;
}
function renderStats() {
const total = contacts.length;
const favorites = contacts.filter(c => c.isFavorite).length;
const emergency = contacts.filter(c => c.isEmergency).length;
document.getElementById("stats-container").innerHTML = `
<div class="col-md-4">
<div class="card border-0 shadow-sm rounded-4 px-4 py-3 d-flex flex-row align-items-center gap-3 card-hover-shadow">
<div class="rounded-4 d-flex justify-content-center align-items-center"
style="width:46px; height:46px; background: #1e63ff; box-shadow: 0px 4px 4px rgba(30, 99, 255, 0.25);">
<i class="fa-solid fa-users text-white fs-6"></i>
</div>
<div>
<small class="text-muted text-uppercase fw-bold">Total</small>
<h3 class="fw-bold text-dark mb-0">${total}</h3>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card border-0 shadow-sm rounded-4 px-4 py-3 d-flex flex-row align-items-center gap-3 card-hover-shadow">
<div class="rounded-4 d-flex justify-content-center align-items-center"
style="width:46px; height:46px; background:#ff9800; box-shadow: 0px 4px 4px rgba(255, 152, 0, 0.25);">
<i class="fa-solid fa-star text-white fs-6"></i>
</div>
<div>
<small class="text-muted text-uppercase fw-bold">Favorites</small>
<h3 class="fw-bold text-dark mb-0">${favorites}</h3>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card border-0 shadow-sm rounded-4 px-4 py-3 d-flex flex-row align-items-center gap-3 card-hover-shadow">
<div class="rounded-4 d-flex justify-content-center align-items-center"
style="width:46px; height:46px; background:#ff1744; box-shadow: 0px 4px 4px rgba(255, 23, 68, 0.25);">
<i class="fa-solid fa-heart-pulse text-white fs-6"></i>
</div>
<div>
<small class="text-muted text-uppercase fw-bold">Emergency</small>
<h3 class="fw-bold text-dark mb-0">${emergency}</h3>
</div>
</div>
</div>
`;
}
function renderSidebars() {
const favList = document.getElementById("favorites-list");
const emergList = document.getElementById("emergency-list");
const favs = contacts.filter(c => c.isFavorite);
const emergs = contacts.filter(c => c.isEmergency);
const createMiniItem = (c, isEmergency = false) => `
<div class="d-flex align-items-center justify-content-between mb-2 p-2 rounded-4 ${isEmergency ? 'hover-danger' : 'hover-light'}">
<div class="d-flex align-items-center gap-2">
${c.avatar ? `<img src="${c.avatar}" class="mini-avatar-img rounded-3">`
: `<div class="mini-avatar d-flex justify-content-center align-items-center fw-bold text-white">${c.name.charAt(0)}</div>`}
<div class="text-truncate" style="max-width: 120px;">
<div class="fw-semibold text-dark small">${c.name}</div>
<div class="text-muted" style="font-size: 11px;">${c.phone}</div>
</div>
</div>
<a href="tel:${c.phone}"
class="mini-call-btn d-flex justify-content-center align-items-center hover-phone ${isEmergency ? 'dangerbg' : 'lightbg'}">
<i class="fa-solid fa-phone"></i>
</a>
</div>
`;
favList.innerHTML = favs.length
? favs.map(c => createMiniItem(c)).join("")
: '<div class="text-center text-muted small py-2">No favorites yet</div>';
emergList.innerHTML = emergs.length
? emergs.map(c => createMiniItem(c, true)).join("")
: '<div class="text-center text-muted small py-2">No emergency contacts</div>';
}
function getAvatarHTML(contact, size = 'md') {
if (contact.avatar) {
return `<img src="${contact.avatar}" class="avatar-${size} rounded-4">`;
}
const colors = ['#ef4444', '#4ff916ff', '#f59e0b', '#10b981', '#6366f1', '#8b5cf6', '#ec4899'];
const charCode = contact.name.charCodeAt(0) || 0;
const color = colors[charCode % colors.length];
const initial = contact.name.charAt(0).toUpperCase();
return `<div class="avatar-${size}" style="background-color: ${color};">${initial}</div>`;
}
window.openAddModal = function () {
currentEditId = null;
currentAvatar = "";
document.getElementById("contactForm").reset();
document.getElementById("modalTitle").textContent = "Add New Contact";
document.getElementById("avatarPreview").innerHTML = '<i class="fa-solid fa-user"></i>';
contactModal.show();
}
window.openEditModal = function (id) {
const contact = contacts.find(c => c.id === id);
if (!contact) return;
currentEditId = id;
currentAvatar = contact.avatar || "";
document.getElementById("contactName").value = contact.name;
document.getElementById("contactPhone").value = contact.phone;
document.getElementById("contactEmail").value = contact.email || "";
document.getElementById("contactAddress").value = contact.address || "";
document.getElementById("contactGroup").value = contact.group || "";
document.getElementById("contactFavorite").checked = contact.isFavorite;
document.getElementById("contactEmergency").checked = contact.isEmergency;
const preview = document.getElementById("avatarPreview");
preview.innerHTML = currentAvatar
? `<img src="${currentAvatar}" class="w-100 h-100 object-fit-cover">`
: contact.name.charAt(0).toUpperCase();
document.getElementById("modalTitle").textContent = "Edit Contact";
contactModal.show();
}
function setupEventListeners() {
document.getElementById("contactForm").addEventListener("submit", function (e) {
e.preventDefault();
const name = document.getElementById("contactName").value.trim();
const phone = document.getElementById("contactPhone").value.trim();
if (!name || !phone) {
Swal.fire("Error", "Name and Phone are required!", "error");
return;
}
const formData = {
name,
phone,
email: document.getElementById("contactEmail").value.trim(),
address: document.getElementById("contactAddress").value.trim(),
group: document.getElementById("contactGroup").value,
isFavorite: document.getElementById("contactFavorite").checked,
isEmergency: document.getElementById("contactEmergency").checked,
avatar: currentAvatar
};
if (currentEditId) {
updateContact(currentEditId, formData);
Swal.fire("Updated", "Contact updated successfully", "success");
} else {
if (contacts.some(c => c.phone === phone)) {
Swal.fire("Error", "Phone number already exists!", "error");
return;
}
addContact(formData);
Swal.fire("Saved", "Contact added successfully", "success");
}
contactModal.hide();
});
document.getElementById("avatarInput").addEventListener("change", function (e) {
const file = e.target.files[0];
if (!file) return;
if (!file.type.startsWith("image/")) {
Swal.fire("Error", "Please select a valid image file!", "error");
return;
}
const reader = new FileReader();
reader.onload = function (event) {
currentAvatar = event.target.result;
document.getElementById("avatarPreview").innerHTML =
`<img src="${currentAvatar}" class="w-100 h-100 object-fit-cover rounded-4">`;
};
reader.readAsDataURL(file);
});
document.getElementById("searchInput").addEventListener("input", renderAll);
}