-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
407 lines (337 loc) · 11.2 KB
/
Copy pathrenderer.js
File metadata and controls
407 lines (337 loc) · 11.2 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Application state
let timeEntries = [];
let editingEntryId = null;
// Timer state
let timerRunning = false;
let timerStartTime = null;
let timerInterval = null;
// DOM elements
const entryForm = document.getElementById('entry-form');
const formTitle = document.getElementById('form-title');
const submitBtn = document.getElementById('submit-btn');
const cancelBtn = document.getElementById('cancel-btn');
const entriesTbody = document.getElementById('entries-tbody');
const totalEntriesSpan = document.getElementById('total-entries');
const totalHoursSpan = document.getElementById('total-hours');
const dataFilePathSpan = document.getElementById('data-file-path');
// Timer DOM elements
const timerBtn = document.getElementById('timer-btn');
const timerDisplay = document.getElementById('timer-display');
const timerStatus = document.getElementById('timer-status');
// Form inputs
const dateInput = document.getElementById('entry-date');
const startTimeInput = document.getElementById('entry-start-time');
const endTimeInput = document.getElementById('entry-end-time');
const durationInput = document.getElementById('entry-duration');
const descriptionInput = document.getElementById('entry-description');
// Initialize the app
async function init() {
// Set default date to today
dateInput.valueAsDate = new Date();
// Load existing entries
await loadEntries();
// Load and restore timer state
await loadTimerState();
// Display data file path
const dataPath = await window.electronAPI.getDataFilePath();
dataFilePathSpan.textContent = dataPath;
// Set up event listeners
setupEventListeners();
}
// Set up all event listeners
function setupEventListeners() {
entryForm.addEventListener('submit', handleFormSubmit);
cancelBtn.addEventListener('click', cancelEdit);
timerBtn.addEventListener('click', toggleTimer);
// Auto-calculate duration when start/end times change
startTimeInput.addEventListener('change', calculateDuration);
endTimeInput.addEventListener('change', calculateDuration);
// Clear auto-calculated duration if user manually enters one
durationInput.addEventListener('input', () => {
if (durationInput.value) {
startTimeInput.removeEventListener('change', calculateDuration);
endTimeInput.removeEventListener('change', calculateDuration);
}
});
}
// Calculate duration from start and end times
function calculateDuration() {
const startTime = startTimeInput.value;
const endTime = endTimeInput.value;
if (startTime && endTime) {
const start = new Date(`2000-01-01T${startTime}`);
const end = new Date(`2000-01-01T${endTime}`);
let diffMs = end - start;
// Handle case where end time is before start time (crossing midnight)
if (diffMs < 0) {
diffMs += 24 * 60 * 60 * 1000; // Add 24 hours
}
const diffHours = diffMs / (1000 * 60 * 60);
durationInput.value = diffHours.toFixed(2);
}
}
// Load entries from file
async function loadEntries() {
try {
timeEntries = await window.electronAPI.loadEntries();
renderEntries();
updateStats();
} catch (error) {
console.error('Error loading entries:', error);
alert('Failed to load time entries: ' + error.message);
}
}
// Save entries to file
async function saveEntries() {
try {
const result = await window.electronAPI.saveEntries(timeEntries);
if (!result.success) {
throw new Error(result.error);
}
} catch (error) {
console.error('Error saving entries:', error);
alert('Failed to save time entries: ' + error.message);
}
}
// Handle form submission (create or update)
async function handleFormSubmit(e) {
e.preventDefault();
const entry = {
id: editingEntryId || Date.now().toString(),
date: dateInput.value,
startTime: startTimeInput.value || null,
endTime: endTimeInput.value || null,
duration: parseFloat(durationInput.value) || 0,
description: descriptionInput.value.trim()
};
if (editingEntryId) {
// Update existing entry
const index = timeEntries.findIndex(e => e.id === editingEntryId);
if (index !== -1) {
timeEntries[index] = entry;
}
} else {
// Add new entry
timeEntries.push(entry);
}
// Sort entries by date (newest first)
timeEntries.sort((a, b) => new Date(b.date) - new Date(a.date));
await saveEntries();
renderEntries();
updateStats();
resetForm();
}
// Render all entries in the table
function renderEntries() {
if (timeEntries.length === 0) {
entriesTbody.innerHTML = `
<tr class="empty-state">
<td colspan="6">No time entries yet. Add your first entry above!</td>
</tr>
`;
return;
}
entriesTbody.innerHTML = timeEntries.map(entry => `
<tr>
<td>${formatDate(entry.date)}</td>
<td>${entry.startTime || '-'}</td>
<td>${entry.endTime || '-'}</td>
<td>${entry.duration.toFixed(2)}</td>
<td>${escapeHtml(entry.description)}</td>
<td>
<div class="action-buttons">
<button class="btn btn-edit" onclick="editEntry('${entry.id}')">Edit</button>
<button class="btn btn-delete" onclick="deleteEntry('${entry.id}')">Delete</button>
</div>
</td>
</tr>
`).join('');
}
// Edit an entry
function editEntry(id) {
const entry = timeEntries.find(e => e.id === id);
if (!entry) return;
editingEntryId = id;
// Populate form
dateInput.value = entry.date;
startTimeInput.value = entry.startTime || '';
endTimeInput.value = entry.endTime || '';
durationInput.value = entry.duration;
descriptionInput.value = entry.description;
// Update UI
formTitle.textContent = 'Edit Time Entry';
submitBtn.textContent = 'Update Entry';
cancelBtn.style.display = 'inline-block';
// Scroll to form
window.scrollTo({ top: 0, behavior: 'smooth' });
}
// Delete an entry
async function deleteEntry(id) {
if (!confirm('Are you sure you want to delete this entry?')) {
return;
}
timeEntries = timeEntries.filter(e => e.id !== id);
await saveEntries();
renderEntries();
updateStats();
}
// Cancel editing
function cancelEdit() {
resetForm();
}
// Reset the form
function resetForm() {
editingEntryId = null;
entryForm.reset();
dateInput.valueAsDate = new Date();
formTitle.textContent = 'Add Time Entry';
submitBtn.textContent = 'Add Entry';
cancelBtn.style.display = 'none';
// Re-attach duration calculation listeners
startTimeInput.addEventListener('change', calculateDuration);
endTimeInput.addEventListener('change', calculateDuration);
}
// Update statistics
function updateStats() {
const totalHours = timeEntries.reduce((sum, entry) => sum + entry.duration, 0);
totalEntriesSpan.textContent = `${timeEntries.length} ${timeEntries.length === 1 ? 'entry' : 'entries'}`;
totalHoursSpan.textContent = `${totalHours.toFixed(2)} hours`;
}
// Utility functions
function formatDate(dateString) {
const date = new Date(dateString + 'T00:00:00');
return date.toLocaleDateString('en-US', {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric'
});
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// ===== TIMER FUNCTIONS =====
// Toggle timer on/off
function toggleTimer() {
if (timerRunning) {
stopTimer();
} else {
startTimer();
}
}
// Start the timer
function startTimer() {
timerRunning = true;
timerStartTime = Date.now();
// Update UI
timerBtn.classList.add('running');
timerBtn.querySelector('.timer-btn-icon').textContent = '■';
timerBtn.querySelector('.timer-btn-text').textContent = 'Stop Timer';
timerStatus.textContent = 'Timer running...';
// Start updating display
updateTimerDisplay();
timerInterval = setInterval(updateTimerDisplay, 1000);
// Save timer state
saveTimerState();
}
// Stop the timer
function stopTimer() {
if (!timerRunning) return;
const endTime = Date.now();
const elapsedMs = endTime - timerStartTime;
const elapsedHours = elapsedMs / (1000 * 60 * 60);
// Stop timer
timerRunning = false;
clearInterval(timerInterval);
timerInterval = null;
// Update UI
timerBtn.classList.remove('running');
timerBtn.querySelector('.timer-btn-icon').textContent = '▶';
timerBtn.querySelector('.timer-btn-text').textContent = 'Start Timer';
timerStatus.textContent = 'Ready to start';
timerDisplay.textContent = '00:00:00';
// Populate form with timer data
populateFormFromTimer(timerStartTime, endTime, elapsedHours);
// Clear timer state
timerStartTime = null;
saveTimerState();
// Scroll to form
window.scrollTo({ top: document.querySelector('.form-section').offsetTop - 20, behavior: 'smooth' });
}
// Update timer display
function updateTimerDisplay() {
if (!timerRunning || !timerStartTime) return;
const elapsedMs = Date.now() - timerStartTime;
const hours = Math.floor(elapsedMs / (1000 * 60 * 60));
const minutes = Math.floor((elapsedMs % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((elapsedMs % (1000 * 60)) / 1000);
timerDisplay.textContent =
`${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
// Update status with start time
const startTimeFormatted = new Date(timerStartTime).toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit'
});
timerStatus.textContent = `Started at ${startTimeFormatted}`;
}
// Populate form with timer data
function populateFormFromTimer(startTimeMs, endTimeMs, elapsedHours) {
const startDate = new Date(startTimeMs);
const endDate = new Date(endTimeMs);
// Set date to the start date
dateInput.value = startDate.toISOString().split('T')[0];
// Set start and end times
startTimeInput.value = startDate.toTimeString().slice(0, 5);
endTimeInput.value = endDate.toTimeString().slice(0, 5);
// Set duration
durationInput.value = elapsedHours.toFixed(2);
// Focus on description field
descriptionInput.focus();
}
// Save timer state to localStorage
async function saveTimerState() {
const timerState = {
running: timerRunning,
startTime: timerStartTime
};
try {
// Use localStorage for timer state (persists across app restarts)
localStorage.setItem('timerState', JSON.stringify(timerState));
} catch (error) {
console.error('Error saving timer state:', error);
}
}
// Load timer state from localStorage
async function loadTimerState() {
try {
const savedState = localStorage.getItem('timerState');
if (savedState) {
const timerState = JSON.parse(savedState);
if (timerState.running && timerState.startTime) {
// Restore running timer
timerRunning = true;
timerStartTime = timerState.startTime;
// Update UI
timerBtn.classList.add('running');
timerBtn.querySelector('.timer-btn-icon').textContent = '■';
timerBtn.querySelector('.timer-btn-text').textContent = 'Stop Timer';
// Start updating display
updateTimerDisplay();
timerInterval = setInterval(updateTimerDisplay, 1000);
}
}
} catch (error) {
console.error('Error loading timer state:', error);
}
}
// Make functions globally available for onclick handlers
window.editEntry = editEntry;
window.deleteEntry = deleteEntry;
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}