-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
149 lines (131 loc) · 5.79 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
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
const nextButton = document.getElementById('nextButton');
const taskInputPage = document.getElementById('taskInputPage');
const studentInfoPage = document.getElementById('studentInfoPage');
const addTaskBtn = document.getElementById('addTaskBtn');
const taskList = document.getElementById('taskList');
const progressFill = document.getElementById('progressFill');
const progressPercentage = document.getElementById('progressPercentage');
const copyButton = document.getElementById('copyButton');
const emailButton = document.getElementById('emailButton');
const shareButton = document.getElementById('shareButton');
let tasks = [];
// Request notification permission
if (Notification.permission === 'default' || Notification.permission === 'denied') {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
console.log('Notification permission granted.');
} else {
console.log('Notification permission denied.');
}
});
}
// Go to task input page
nextButton.addEventListener('click', () => {
studentInfoPage.style.display = 'none';
taskInputPage.style.display = 'block';
});
// Add task
addTaskBtn.addEventListener('click', () => {
const subjectInput = document.getElementById('subject').value;
const taskInput = document.getElementById('task').value;
const deadlineInput = document.getElementById('deadline').value;
const resourceInput = document.getElementById('resource').value;
if (subjectInput && taskInput && deadlineInput) {
const task = { subject: subjectInput, name: taskInput, deadline: deadlineInput, resource: resourceInput, completed: false };
tasks.push(task);
displayTasks();
updateProgress();
scheduleNotifications(); // Schedule notification for the new task
document.getElementById('subject').value = '';
document.getElementById('task').value = '';
document.getElementById('deadline').value = '';
document.getElementById('resource').value = '';
}
});
// Schedule notifications
function scheduleNotifications() {
tasks.forEach(task => {
const taskDeadline = new Date(task.deadline).getTime();
const now = Date.now();
const oneDayBefore = taskDeadline - (24 * 60 * 60 * 1000); // 1 day in milliseconds
if (oneDayBefore > now) {
const timeUntilNotification = oneDayBefore - now;
setTimeout(() => {
showNotification(task.subject, task.name, task.deadline);
}, timeUntilNotification);
}
});
}
// Show notification
function showNotification(subject, taskName, deadline) {
if (Notification.permission === 'granted') {
new Notification(`Reminder: ${taskName}`, {
body: `The deadline for ${subject} is tomorrow! (${deadline})`,
});
}
}
// Display tasks
function displayTasks() {
taskList.innerHTML = ''; // Clear the previous list
document.getElementById('roadmap').innerHTML = ''; // Clear the roadmap div
tasks.sort((a, b) => new Date(a.deadline) - new Date(b.deadline)); // Sort by deadline
tasks.forEach((task, index) => {
const taskDiv = document.createElement('div');
taskDiv.classList.add('task-item'); // Apply the pink box styling
taskDiv.innerHTML = `
<h3>${task.subject}</h3>
<p><strong>Task:</strong> ${task.name}</p>
<p><strong>Deadline:</strong> ${task.deadline}</p>
${task.resource ? `<a href="${task.resource}" target="_blank">Resource Link</a>` : ''}
<label><input type="checkbox" class="task-complete" data-index="${index}"> Mark as complete</label>
`;
document.getElementById('roadmap').appendChild(taskDiv);
});
// Add event listeners for the checkboxes
document.querySelectorAll('.task-complete').forEach(checkbox => {
checkbox.addEventListener('change', function () {
const index = this.dataset.index;
tasks[index].completed = this.checked;
updateProgress();
});
});
}
// Update progress
function updateProgress() {
const completedTasks = tasks.filter(task => task.completed).length;
const totalTasks = tasks.length;
const progress = totalTasks > 0 ? (completedTasks / totalTasks) * 100 : 0;
progressFill.style.width = `${progress}%`;
progressPercentage.textContent = `${Math.round(progress)}%`;
}
// Copy to Clipboard
copyButton.addEventListener('click', () => {
const textToCopy = tasks.map(task => `${task.subject}: ${task.name} - Deadline: ${task.deadline}`).join('\n');
navigator.clipboard.writeText(textToCopy).then(() => {
alert('Study plan copied to clipboard!');
}).catch(err => {
console.error('Failed to copy: ', err);
});
});
// Email Sharing
emailButton.addEventListener('click', () => {
const subject = "My Study Plan";
const body = encodeURIComponent(tasks.map(task => `${task.subject}: ${task.name} - Deadline: ${task.deadline} - Resource: ${task.resource}`).join('\n'));
const mailtoLink = `mailto:?subject=${subject}&body=${body}`;
window.location.href = mailtoLink;
});
// Web Share API
if (navigator.share) {
shareButton.addEventListener('click', () => {
const shareText = tasks.map(task => `${task.subject}: ${task.name} - Deadline: ${task.deadline} - Resource: ${task.resource}`).join('\n');
navigator.share({
title: 'My Study Plan',
text: shareText,
url: window.location.href,
})
.then(() => console.log('Successful share'))
.catch((error) => console.log('Error sharing:', error));
});
} else {
console.log('Web Share API not supported.');
}