-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
99 lines (85 loc) · 3.53 KB
/
script.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
// Function to save checklist data to local storage
function saveChecklist() {
const form = document.getElementById('checklistForm');
const formData = new FormData(form);
const checklistData = {};
for (const [name, value] of formData) {
checklistData[name] = value;
}
localStorage.setItem('travelChecklist', JSON.stringify(checklistData));
}
// Function to load checklist data from local storage
function loadChecklist() {
const savedData = localStorage.getItem('travelChecklist');
if (savedData) {
const checklistData = JSON.parse(savedData);
for (const key in checklistData) {
const checkbox = document.querySelector(`[name="${key}"]`);
if (checkbox) {
checkbox.checked = checklistData[key];
}
}
updateCounts(); // Update counts after loading checklist data
}
}
// Function to calculate and display total count of items, checked items, and remaining items
function updateCounts() {
const form = document.getElementById('checklistForm');
const checkboxes = form.querySelectorAll('input[type="checkbox"]');
const totalCount = checkboxes.length;
let checkedCount = 0;
checkboxes.forEach(checkbox => {
if (checkbox.checked) {
checkedCount++;
}
});
const remainingCount = totalCount - checkedCount;
document.getElementById('totalCount').textContent = `Total items: ${totalCount}`;
document.getElementById('checkedCount').textContent = `Checked items: ${checkedCount}`;
document.getElementById('remainingCount').textContent = `Remaining items: ${remainingCount}`;
}
// Function to send notifications until all checkboxes are checked
function sendNotifications() {
const form = document.getElementById('checklistForm');
const checkboxes = form.querySelectorAll('input[type="checkbox"]');
let uncheckedCount = 0;
checkboxes.forEach(checkbox => {
if (!checkbox.checked) {
uncheckedCount++;
}
});
if (uncheckedCount > 0) {
const notification = new Notification('Travel Checklist Reminder', {
body: `You still have ${uncheckedCount} item(s) unchecked on your travel checklist. Don't forget to pack them!`
});
notification.onclick = function () {
window.focus();
};
setTimeout(sendNotifications, 60000); // Send notification every minute
} else {
const notification = new Notification('Travel Checklist Complete', {
body: 'Congratulations! You have checked all items on your travel checklist. You are ready to go!'
});
notification.onclick = function () {
window.focus();
};
}
}
// Function to request permission for notifications and start sending notifications
function requestNotificationPermission() {
if (Notification.permission !== 'granted') {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
loadChecklist(); // Load checklist data
sendNotifications(); // Start sending notifications
}
});
} else {
loadChecklist(); // Load checklist data
sendNotifications(); // Start sending notifications
}
}
// Load checklist data when the page loads
window.onload = loadChecklist;
// Update counts when a checkbox is clicked
document.getElementById('checklistForm').addEventListener('change', updateCounts);