Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit 130a032

Browse files
Create functions.js
1 parent 0407484 commit 130a032

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

functions.js

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
let tasks = [];
2+
let subjects = ['Personal', 'School', 'Work'];
3+
let currentTab = 'personal';
4+
let userName = '';
5+
6+
function init() {
7+
loadData();
8+
if (!userName) {
9+
userName = prompt("Please enter your name:");
10+
if (userName) {
11+
localStorage.setItem('userName', userName);
12+
}
13+
}
14+
updateGreeting();
15+
updateSubjectSelect();
16+
renderTasks();
17+
updateCharts();
18+
}
19+
20+
function updateGreeting() {
21+
document.getElementById('greeting').textContent = `Hi, ${userName}!`;
22+
}
23+
24+
function switchTab(tab) {
25+
currentTab = tab;
26+
renderTasks();
27+
}
28+
29+
function addTask() {
30+
const taskText = document.getElementById('taskInput').value;
31+
const subject = document.getElementById('subjectSelect').value;
32+
const priority = document.getElementById('prioritySelect').value;
33+
const dueDate = document.getElementById('dueDateInput').value;
34+
if (taskText) {
35+
tasks.push({
36+
text: taskText,
37+
completed: false,
38+
subject: subject,
39+
priority: priority,
40+
dueDate: dueDate,
41+
subtasks: [],
42+
tab: currentTab
43+
});
44+
document.getElementById('taskInput').value = '';
45+
saveData();
46+
renderTasks();
47+
updateCharts();
48+
}
49+
}
50+
51+
function addSubject() {
52+
const subject = document.getElementById('subjectInput').value;
53+
if (subject && !subjects.includes(subject)) {
54+
subjects.push(subject);
55+
document.getElementById('subjectInput').value = '';
56+
updateSubjectSelect();
57+
saveData();
58+
}
59+
}
60+
61+
function updateSubjectSelect() {
62+
const select = document.getElementById('subjectSelect');
63+
select.innerHTML = '';
64+
subjects.forEach(subject => {
65+
const option = document.createElement('option');
66+
option.value = subject;
67+
option.textContent = subject;
68+
select.appendChild(option);
69+
});
70+
}
71+
72+
function addSubtask(taskIndex) {
73+
const subtaskText = prompt("Enter subtask:");
74+
if (subtaskText) {
75+
tasks[taskIndex].subtasks.push({ text: subtaskText, completed: false });
76+
saveData();
77+
renderTasks();
78+
}
79+
}
80+
81+
function toggleTaskCompletion(taskIndex) {
82+
tasks[taskIndex].completed = !tasks[taskIndex].completed;
83+
saveData();
84+
renderTasks();
85+
updateCharts();
86+
}
87+
88+
function toggleSubtaskCompletion(taskIndex, subtaskIndex) {
89+
tasks[taskIndex].subtasks[subtaskIndex].completed = !tasks[taskIndex].subtasks[subtaskIndex].completed;
90+
saveData();
91+
renderTasks();
92+
}
93+
94+
function deleteTask(taskIndex) {
95+
tasks.splice(taskIndex, 1);
96+
saveData();
97+
renderTasks();
98+
updateCharts();
99+
}
100+
101+
function editTask(taskIndex) {
102+
const newText = prompt("Edit task:", tasks[taskIndex].text);
103+
if (newText !== null) {
104+
tasks[taskIndex].text = newText;
105+
saveData();
106+
renderTasks();
107+
}
108+
}
109+
110+
function renderTasks() {
111+
const taskList = document.getElementById('taskList');
112+
taskList.innerHTML = '';
113+
tasks.filter(task => task.tab === currentTab).forEach((task, index) => {
114+
const li = document.createElement('li');
115+
li.innerHTML = `
116+
<input type="checkbox" ${task.completed ? 'checked' : ''} onchange="toggleTaskCompletion(${index})">
117+
<span class="${task.completed ? 'completed' : ''} priority-${task.priority}">${task.text}</span>
118+
<span>(${task.subject})</span>
119+
<span>Due: ${task.dueDate}</span>
120+
<button onclick="editTask(${index})">Edit</button>
121+
<button onclick="deleteTask(${index})">Delete</button>
122+
<button onclick="addSubtask(${index})">Add Subtask</button>
123+
`;
124+
const subtaskList = document.createElement('ul');
125+
task.subtasks.forEach((subtask, subtaskIndex) => {
126+
const subtaskLi = document.createElement('li');
127+
subtaskLi.className = 'subtask';
128+
subtaskLi.innerHTML = `
129+
<input type="checkbox" ${subtask.completed ? 'checked' : ''} onchange="toggleSubtaskCompletion(${index}, ${subtaskIndex})">
130+
<span class="${subtask.completed ? 'completed' : ''}">${subtask.text}</span>
131+
`;
132+
subtaskList.appendChild(subtaskLi);
133+
});
134+
li.appendChild(subtaskList);
135+
taskList.appendChild(li);
136+
});
137+
}
138+
139+
function updateCharts() {
140+
updatePriorityChart();
141+
updateSubjectChart();
142+
}
143+
144+
function updatePriorityChart() {
145+
const ctx = document.getElementById('priorityChart').getContext('2d');
146+
const priorityCounts = {
147+
low: tasks.filter(task => task.priority === 'low').length,
148+
medium: tasks.filter(task => task.priority === 'medium').length,
149+
high: tasks.filter(task => task.priority === 'high').length
150+
};
151+
new Chart(ctx, {
152+
type: 'pie',
153+
data: {
154+
labels: ['Low', 'Medium', 'High'],
155+
datasets: [{
156+
data: [priorityCounts.low, priorityCounts.medium, priorityCounts.high],
157+
backgroundColor: ['#00C851', '#ffbb33', '#ff4444']
158+
}]
159+
},
160+
options: {
161+
responsive: true,
162+
title: {
163+
display: true,
164+
text: 'Tasks by Priority'
165+
}
166+
}
167+
});
168+
}
169+
170+
function updateSubjectChart() {
171+
const ctx = document.getElementById('subjectChart').getContext('2d');
172+
const subjectCounts = {};
173+
tasks.forEach(task => {
174+
subjectCounts[task.subject] = (subjectCounts[task.subject] || 0) + 1;
175+
});
176+
new Chart(ctx, {
177+
type: 'bar',
178+
data: {
179+
labels: Object.keys(subjectCounts),
180+
datasets: [{
181+
label: 'Tasks per Subject',
182+
data: Object.values(subjectCounts),
183+
backgroundColor: '#4CAF50'
184+
}]
185+
},
186+
options: {
187+
responsive: true,
188+
scales: {
189+
y: {
190+
beginAtZero: true,
191+
ticks: {
192+
stepSize: 1
193+
}
194+
}
195+
}
196+
}
197+
});
198+
}
199+
200+
function saveData() {
201+
localStorage.setItem('tasks', JSON.stringify(tasks));
202+
localStorage.setItem('subjects', JSON.stringify(subjects));
203+
}
204+
205+
function loadData() {
206+
const savedTasks = localStorage.getItem('tasks');
207+
const savedSubjects = localStorage.getItem('subjects');
208+
userName = localStorage.getItem('userName');
209+
if (savedTasks) tasks = JSON.parse(savedTasks);
210+
if (savedSubjects) subjects = JSON.parse(savedSubjects);
211+
}
212+
213+
window.onload = init;

0 commit comments

Comments
 (0)