-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.html
More file actions
222 lines (194 loc) · 8.61 KB
/
task.html
File metadata and controls
222 lines (194 loc) · 8.61 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tasks</title>
<link rel="stylesheet" href="task.css">
</head>
<body>
<div class="container">
<div class="header">
<h1 id="todayDate"></h1>
</div>
<div class="form-section">
<form id="taskForm" class="task-form">
<div style="display: flex; gap: 20px;">
<div class="form-group" style="flex:1;">
<label for="startTime">Start</label>
<input type="time" id="startTime" required inputmode="numeric" pattern="[0-9]{2}:[0-9]{2}">
</div>
<div class="form-group" style="flex:1;">
<label for="endTime">End</label>
<input type="time" id="endTime" required inputmode="numeric" pattern="[0-9]{2}:[0-9]{2}">
</div>
</div>
<div class="form-group">
<input type="text" id="description" placeholder="Task..." required>
</div>
<button type="submit" class="submit-btn">Add Task</button>
</form>
</div>
<div class="tasks-section">
<div id="tasksContainer">
<!-- Tasks will be populated here -->
</div>
<div class="clear-all">
<button class="delete-btn" id="clearAllBtn" type="button">Clear All</button>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, '0');
const dd = String(today.getDate()).padStart(2, '0');
document.getElementById('todayDate').textContent = `${mm}/${dd}`;
});
class TaskManager {
constructor() {
this.tasks = this.loadTasks();
this.initializeEventListeners();
this.renderTasks();
this.setDefaultTime();
document.getElementById('clearAllBtn').addEventListener('click', () => this.clearAllTasks());
}
clearAllTasks() {
this.tasks = [];
this.saveTasks();
this.renderTasks();
}
loadTasks() {
const storedTasks = localStorage.getItem('tasks');
return storedTasks ? JSON.parse(storedTasks) : [];
}
saveTasks() {
localStorage.setItem('tasks', JSON.stringify(this.tasks));
}
setDefaultTime() {
const now = new Date();
const startInput = document.getElementById('startTime');
const endInput = document.getElementById('endTime');
// Set start time to current time
startInput.value = this.formatTimeForInput(now);
// Set end time to 1 hour from now
const oneHourLater = new Date(now.getTime() + 60 * 60 * 1000);
endInput.value = this.formatTimeForInput(oneHourLater);
}
formatTimeForInput(date) {
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
return `${hours}:${minutes}`;
}
initializeEventListeners() {
const form = document.getElementById('taskForm');
form.addEventListener('submit', (e) => this.handleSubmit(e));
}
handleSubmit(e) {
e.preventDefault();
const startTime = document.getElementById('startTime').value;
const endTime = document.getElementById('endTime').value;
const description = document.getElementById('description').value.trim();
if (!startTime || !endTime || !description) {
alert('Please fill in all fields');
return;
}
if (startTime >= endTime) {
alert('End time must be after start time');
return;
}
// Use today's date for all tasks
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, '0');
const dd = String(today.getDate()).padStart(2, '0');
const startDateTime = `${yyyy}-${mm}-${dd}T${startTime}`;
const endDateTime = `${yyyy}-${mm}-${dd}T${endTime}`;
const task = {
id: Date.now().toString(),
startDateTime: startDateTime,
endDateTime: endDateTime,
description: description,
createdAt: new Date().toISOString()
};
this.tasks.unshift(task); // Add to beginning of array
this.saveTasks();
this.renderTasks();
this.resetForm();
}
resetForm() {
document.getElementById('description').value = '';
this.setDefaultTime();
}
deleteTask(taskId) {
this.tasks = this.tasks.filter(task => task.id !== taskId);
this.saveTasks();
this.renderTasks();
}
formatDateTime(dateTimeString) {
const date = new Date(dateTimeString);
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
return `${hours}:${minutes}`;
}
calculateDuration(start, end) {
const startDate = new Date(start);
const endDate = new Date(end);
const diffMs = endDate - startDate;
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
const diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));
if (diffHours > 0) {
return `${diffHours}h ${diffMinutes}m`;
} else {
return `${diffMinutes}m`;
}
}
renderTasks() {
const container = document.getElementById('tasksContainer');
if (this.tasks.length === 0) {
container.innerHTML = `
<div class="empty-state">
<div style="font-size: 4rem; margin-bottom: 10px;">📝</div>
<h4>No tasks recorded yet</h4>
</div>
`;
return;
}
const tableHTML = `
<div style="overflow-x: auto;">
<table class="tasks-table">
<thead>
<tr>
<th>Start</th>
<th>End</th>
<th>Duration</th>
<th>Task</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
${this.tasks.map(task => `
<tr>
<td><span class="datetime">${this.formatDateTime(task.startDateTime)}</span></td>
<td><span class="datetime">${this.formatDateTime(task.endDateTime)}</span></td>
<td><strong>${this.calculateDuration(task.startDateTime, task.endDateTime)}</strong></td>
<td><div class="description">${task.description}</div></td>
<td>
<button class="delete-btn" onclick="taskManager.deleteTask('${task.id}')">
X
</button>
</td>
</tr>
`).join('')}
</tbody>
</table>
</div>
`;
container.innerHTML = tableHTML;
}
}
const taskManager = new TaskManager();
</script>
</body>
</html>