-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1133 lines (991 loc) · 38.1 KB
/
script.js
File metadata and controls
1133 lines (991 loc) · 38.1 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Global state
const appState = {
currentStep: 1,
days: ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'],
dailyHours: {},
restPeriods: [],
subjects: [],
schedule: null,
scheduleStats: null,
conflicts: [],
warnings: []
};
// Initialize the application
document.addEventListener('DOMContentLoaded', function() {
initializeApp();
setupEventListeners();
});
function initializeApp() {
updateProgressIndicator();
initializeDailyHours();
loadSampleData();
}
function setupEventListeners() {
// Day selection
document.querySelectorAll('input[name="days"]').forEach(checkbox => {
checkbox.addEventListener('change', function() {
updateSelectedDays();
updateDailyHoursVisibility();
});
});
}
function updateSelectedDays() {
appState.days = Array.from(document.querySelectorAll('input[name="days"]:checked'))
.map(checkbox => checkbox.value);
}
function initializeDailyHours() {
const dayNames = {
monday: 'Monday', tuesday: 'Tuesday', wednesday: 'Wednesday',
thursday: 'Thursday', friday: 'Friday', saturday: 'Saturday', sunday: 'Sunday'
};
Object.keys(dayNames).forEach(day => {
appState.dailyHours[day] = {
enabled: false,
startTime: '09:00',
endTime: '17:00'
};
});
renderDailyHours();
}
function renderDailyHours() {
const container = document.getElementById('dailyHoursContainer');
const dayNames = {
monday: 'Monday', tuesday: 'Tuesday', wednesday: 'Wednesday',
thursday: 'Thursday', friday: 'Friday', saturday: 'Saturday', sunday: 'Sunday'
};
container.innerHTML = appState.days.map(day => `
<div class="daily-hour-item" data-day="${day}">
<div class="daily-hour-header">
<h4>${dayNames[day]}</h4>
<label class="toggle-switch">
<input type="checkbox" ${appState.dailyHours[day].enabled ? 'checked' : ''}
onchange="toggleDailyHours('${day}', this.checked)">
<span class="toggle-slider"></span>
</label>
</div>
<div class="daily-hour-fields" ${!appState.dailyHours[day].enabled ? 'style="display: none;"' : ''}>
<div class="form-group">
<label>Start Time</label>
<input type="time" value="${appState.dailyHours[day].startTime}"
onchange="updateDailyHours('${day}', 'startTime', this.value)">
</div>
<div class="form-group">
<label>End Time</label>
<input type="time" value="${appState.dailyHours[day].endTime}"
onchange="updateDailyHours('${day}', 'endTime', this.value)">
</div>
</div>
</div>
`).join('');
}
function updateDailyHoursVisibility() {
setTimeout(renderDailyHours, 100);
}
function toggleDailyHours(day, enabled) {
appState.dailyHours[day].enabled = enabled;
const fields = document.querySelector(`[data-day="${day}"] .daily-hour-fields`);
if (fields) {
fields.style.display = enabled ? 'grid' : 'none';
}
}
function updateDailyHours(day, field, value) {
if (appState.dailyHours[day]) {
appState.dailyHours[day][field] = value;
}
}
// Rest Periods Management
function addRestPeriod() {
const restPeriodId = Date.now();
const newRestPeriod = {
id: restPeriodId,
name: 'Break',
startTime: '12:00',
endTime: '13:00',
days: [...appState.days],
applyToAll: true
};
appState.restPeriods.push(newRestPeriod);
renderRestPeriods();
}
function removeRestPeriod(id) {
appState.restPeriods = appState.restPeriods.filter(period => period.id !== id);
renderRestPeriods();
}
function renderRestPeriods() {
const container = document.getElementById('restPeriodsContainer');
if (appState.restPeriods.length === 0) {
container.innerHTML = `
<div class="empty-state">
<p>No rest periods added yet. Add breaks like lunch or personal time.</p>
</div>
`;
return;
}
container.innerHTML = appState.restPeriods.map(period => `
<div class="rest-period" data-rest-period="${period.id}">
<div class="item-header">
<h3>${period.name}</h3>
<button class="remove-btn" onclick="removeRestPeriod(${period.id})">
Remove
</button>
</div>
<div class="form-row">
<div class="form-group">
<label>Name</label>
<input type="text" value="${period.name}"
onchange="updateRestPeriod(${period.id}, 'name', this.value)">
</div>
<div class="form-group">
<label>Start Time</label>
<input type="time" value="${period.startTime}"
onchange="updateRestPeriod(${period.id}, 'startTime', this.value)">
</div>
<div class="form-group">
<label>End Time</label>
<input type="time" value="${period.endTime}"
onchange="updateRestPeriod(${period.id}, 'endTime', this.value)">
</div>
</div>
<div class="form-group">
<label>
<input type="checkbox" ${period.applyToAll ? 'checked' : ''}
onchange="updateRestPeriod(${period.id}, 'applyToAll', this.checked)">
Apply to all selected days
</label>
</div>
</div>
`).join('');
}
function updateRestPeriod(id, field, value) {
const period = appState.restPeriods.find(p => p.id === id);
if (period) {
period[field] = value;
}
}
// Subjects Management
function addSubject() {
const subjectId = Date.now();
const newSubject = {
id: subjectId,
name: 'New Task',
duration: 5,
priority: 'medium',
availableDays: [...appState.days],
unavailableTimes: []
};
appState.subjects.push(newSubject);
renderSubjects();
}
function removeSubject(id) {
appState.subjects = appState.subjects.filter(subject => subject.id !== id);
renderSubjects();
}
function renderSubjects() {
const container = document.getElementById('subjectsContainer');
if (appState.subjects.length === 0) {
container.innerHTML = `
<div class="empty-state">
<p>No subjects added yet. Add tasks like work, study, or exercise.</p>
</div>
`;
return;
}
container.innerHTML = appState.subjects.map(subject => `
<div class="subject-item" data-subject="${subject.id}">
<div class="item-header">
<h3>${subject.name}</h3>
<button class="remove-btn" onclick="removeSubject(${subject.id})">
Remove
</button>
</div>
<div class="form-row">
<div class="form-group">
<label>Task Name</label>
<input type="text" value="${subject.name}"
onchange="updateSubject(${subject.id}, 'name', this.value)">
</div>
<div class="form-group">
<label>Weekly Duration (hours)</label>
<input type="number" min="1" step="0.5" value="${subject.duration}"
onchange="updateSubject(${subject.id}, 'duration', parseFloat(this.value))">
</div>
<div class="form-group">
<label>Priority</label>
<select onchange="updateSubject(${subject.id}, 'priority', this.value)">
<option value="low" ${subject.priority === 'low' ? 'selected' : ''}>Low</option>
<option value="medium" ${subject.priority === 'medium' ? 'selected' : ''}>Medium</option>
<option value="high" ${subject.priority === 'high' ? 'selected' : ''}>High</option>
</select>
</div>
</div>
<div class="availability-section">
<div class="availability-header">
<h4>Available Days</h4>
</div>
<div class="availability-days">
${appState.days.map(day => `
<div class="availability-day">
<input type="checkbox" id="subject-${subject.id}-${day}"
value="${day}" ${subject.availableDays.includes(day) ? 'checked' : ''}
onchange="updateSubjectDays(${subject.id}, '${day}', this.checked)">
<label for="subject-${subject.id}-${day}">${day.charAt(0).toUpperCase() + day.slice(1)}</label>
</div>
`).join('')}
</div>
<div class="unavailable-times">
<div class="availability-header">
<h4>Unavailable Time Blocks</h4>
</div>
<div class="unavailable-times-list" id="unavailable-times-${subject.id}">
${renderUnavailableTimes(subject)}
</div>
<button type="button" class="add-unavailable-btn" onclick="addUnavailableTime(${subject.id})">
+ Add Unavailable Time Block
</button>
</div>
</div>
</div>
`).join('');
}
function renderUnavailableTimes(subject) {
if (!subject.unavailableTimes || subject.unavailableTimes.length === 0) {
return '<p class="text-small text-center">No unavailable times set</p>';
}
return subject.unavailableTimes.map((time, index) => `
<div class="unavailable-time-item">
<select onchange="updateUnavailableTime(${subject.id}, ${index}, 'days', this.value)">
${appState.days.map(day => `
<option value="${day}" ${time.days.includes(day) ? 'selected' : ''}>
${day.charAt(0).toUpperCase() + day.slice(1)}
</option>
`).join('')}
</select>
<input type="time" value="${time.startTime}"
onchange="updateUnavailableTime(${subject.id}, ${index}, 'startTime', this.value)">
<input type="time" value="${time.endTime}"
onchange="updateUnavailableTime(${subject.id}, ${index}, 'endTime', this.value)">
<button class="remove-time-btn" onclick="removeUnavailableTime(${subject.id}, ${index})">
Remove
</button>
</div>
`).join('');
}
function addUnavailableTime(subjectId) {
const subject = appState.subjects.find(s => s.id === subjectId);
if (subject) {
if (!subject.unavailableTimes) {
subject.unavailableTimes = [];
}
subject.unavailableTimes.push({
days: [appState.days[0]],
startTime: '14:00',
endTime: '15:00'
});
renderSubjects();
}
}
function removeUnavailableTime(subjectId, index) {
const subject = appState.subjects.find(s => s.id === subjectId);
if (subject && subject.unavailableTimes) {
subject.unavailableTimes.splice(index, 1);
renderSubjects();
}
}
function updateUnavailableTime(subjectId, index, field, value) {
const subject = appState.subjects.find(s => s.id === subjectId);
if (subject && subject.unavailableTimes && subject.unavailableTimes[index]) {
subject.unavailableTimes[index][field] = value;
}
}
function updateSubject(id, field, value) {
const subject = appState.subjects.find(s => s.id === id);
if (subject) {
subject[field] = value;
}
}
function updateSubjectDays(subjectId, day, checked) {
const subject = appState.subjects.find(s => s.id === subjectId);
if (subject) {
if (checked) {
if (!subject.availableDays.includes(day)) {
subject.availableDays.push(day);
}
} else {
subject.availableDays = subject.availableDays.filter(d => d !== day);
}
}
}
// Simple Conflict Detection
function detectConflicts() {
const conflicts = [];
const warnings = [];
// 1. Check total time
const totalRequired = appState.subjects.reduce((total, subject) => total + subject.duration, 0);
const totalAvailable = appState.days.length * 8; // Rough estimate
if (totalRequired > totalAvailable) {
conflicts.push({
type: 'critical',
title: 'Not Enough Time',
message: `You need ${totalRequired}h but only have ~${totalAvailable}h available`,
details: ['Reduce task durations or add more days']
});
}
// 2. Check individual tasks
appState.subjects.forEach(subject => {
if (subject.availableDays.length === 0) {
conflicts.push({
type: 'critical',
title: `"${subject.name}" Has No Available Days`,
message: 'Task cannot be scheduled without available days',
details: ['Select at least one available day for this task']
});
}
const hoursPerDay = subject.duration / subject.availableDays.length;
if (hoursPerDay > 8) {
warnings.push({
type: 'warning',
title: `"${subject.name}" May Be Too Intensive`,
message: `Task requires ${hoursPerDay.toFixed(1)}h per day on average`,
details: ['Consider spreading across more days or reducing duration']
});
}
});
return { conflicts, warnings, hasCritical: conflicts.length > 0 };
}
// Schedule Generation
async function generateSchedule() {
if (!validateCurrentStep()) return;
// Check for conflicts
const conflictCheck = detectConflicts();
if (conflictCheck.hasCritical) {
showConflictsDialog(conflictCheck.conflicts);
return;
}
if (conflictCheck.warnings.length > 0) {
const proceed = await showWarningsDialog(conflictCheck.warnings);
if (!proceed) return;
}
showLoading(true);
try {
// Generate sample schedule
setTimeout(() => {
generateSampleSchedule();
nextStep(4);
showNotification('Schedule generated successfully!', 'success');
showLoading(false);
}, 1500);
} catch (error) {
console.error('Schedule generation error:', error);
showNotification('Error generating schedule', 'error');
showLoading(false);
}
}
// Simple conflict dialogs
function showConflictsDialog(conflicts) {
const dialog = document.createElement('div');
dialog.className = 'conflict-dialog';
dialog.innerHTML = `
<div class="conflict-dialog-content">
<div class="conflict-header">
<h3>🚫 Schedule Conflicts</h3>
<p>Please fix these issues to continue:</p>
</div>
<div class="conflicts-list">
${conflicts.map(conflict => `
<div class="conflict-item critical">
<div class="conflict-title">${conflict.title}</div>
<div class="conflict-message">${conflict.message}</div>
<div class="conflict-details">
${conflict.details.map(detail => `<div>• ${detail}</div>`).join('')}
</div>
</div>
`).join('')}
</div>
<div class="conflict-actions">
<button class="btn-prev" onclick="this.closest('.conflict-dialog').remove()">Go Back & Fix</button>
</div>
</div>
`;
document.body.appendChild(dialog);
}
function showWarningsDialog(warnings) {
return new Promise((resolve) => {
const dialog = document.createElement('div');
dialog.className = 'conflict-dialog';
dialog.innerHTML = `
<div class="conflict-dialog-content">
<div class="conflict-header">
<h3>⚠️ Schedule Warnings</h3>
<p>You can proceed, but consider these suggestions:</p>
</div>
<div class="conflicts-list">
${warnings.map(warning => `
<div class="conflict-item warning">
<div class="conflict-title">${warning.title}</div>
<div class="conflict-message">${warning.message}</div>
<div class="conflict-details">
${warning.details.map(detail => `<div>• ${detail}</div>`).join('')}
</div>
</div>
`).join('')}
</div>
<div class="conflict-actions">
<button class="btn-prev" onclick="closeDialog(false)">Go Back</button>
<button class="btn-next" onclick="closeDialog(true)">Proceed Anyway</button>
</div>
</div>
`;
window.closeDialog = (proceed) => {
dialog.remove();
resolve(proceed);
};
document.body.appendChild(dialog);
});
}
// Sample schedule generation
function generateSampleSchedule() {
const timeSlots = generateTimeSlots();
const schedule = {};
// Initialize schedule
appState.days.forEach(day => {
schedule[day] = {};
timeSlots.forEach(time => {
schedule[day][time] = [];
});
});
// Add rest periods
appState.restPeriods.forEach(period => {
const periodDays = period.applyToAll ? appState.days : period.days;
periodDays.forEach(day => {
const restSlots = getTimeSlotsBetween(period.startTime, period.endTime, timeSlots);
restSlots.forEach(slot => {
schedule[day][slot].push({
type: 'rest',
name: period.name,
color: '#f39c12'
});
});
});
});
// Add tasks
appState.subjects.forEach((subject, index) => {
const colors = ['#3498db', '#2ecc71', '#9b59b6'];
const color = colors[index % colors.length];
subject.availableDays.forEach(day => {
if (schedule[day]) {
// Simple scheduling - just add to morning hours
const morningSlots = getTimeSlotsBetween('09:00', '12:00', timeSlots);
const slotsToUse = morningSlots.slice(0, Math.min(4, subject.duration * 2));
slotsToUse.forEach(slot => {
if (isSlotAvailable(schedule[day][slot])) {
schedule[day][slot].push({
type: 'task',
name: subject.name,
color: color
});
}
});
}
});
});
appState.schedule = schedule;
appState.scheduleStats = {
totalScheduledHours: appState.subjects.reduce((sum, subject) => sum + subject.duration, 0),
tasksScheduled: appState.subjects.length,
efficiency: 85
};
}
// Utility functions
function generateTimeSlots() {
const slots = [];
for (let hour = 8; hour <= 20; hour++) {
for (let minute = 0; minute < 60; minute += 30) {
slots.push(`${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`);
}
}
return slots;
}
function getTimeSlotsBetween(start, end, timeSlots) {
return timeSlots.filter(slot => slot >= start && slot < end);
}
function isSlotAvailable(slotItems) {
return slotItems.length === 0;
}
// Schedule Display
function renderSchedule() {
const container = document.getElementById('scheduleDisplay');
if (!appState.schedule) {
container.innerHTML = '<div class="empty-state"><p>No schedule generated yet.</p></div>';
return;
}
updateScheduleSummary();
const timeSlots = generateTimeSlots();
const dayNames = {
monday: 'Mon', tuesday: 'Tue', wednesday: 'Wed',
thursday: 'Thu', friday: 'Fri', saturday: 'Sat', sunday: 'Sun'
};
let html = `
<div class="schedule-legend">
<div class="legend-item">
<div class="legend-color" style="background: #3498db;"></div>
<span>Tasks</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background: #f39c12;"></div>
<span>Rest Periods</span>
</div>
</div>
<table class="schedule-table">
<thead>
<tr>
<th class="time-column">Time</th>
${appState.days.map(day => `<th>${dayNames[day]}</th>`).join('')}
</tr>
</thead>
<tbody>
`;
timeSlots.forEach(slot => {
html += `<tr><td class="time-column">${formatTimeDisplay(slot)}</td>`;
appState.days.forEach(day => {
const items = appState.schedule[day][slot] || [];
html += `<td class="time-slot">`;
items.forEach(item => {
const className = item.type === 'rest' ? 'schedule-rest' : 'schedule-task';
html += `<div class="${className}" style="background: ${item.color}">${item.name}</div>`;
});
if (items.length === 0) {
html += '<div class="empty-slot">Free</div>';
}
html += `</td>`;
});
html += `</tr>`;
});
html += `</tbody></table>`;
container.innerHTML = html;
}
function updateScheduleSummary() {
if (!appState.scheduleStats) return;
const stats = appState.scheduleStats;
document.getElementById('totalHours').textContent = `${stats.totalScheduledHours}h`;
document.getElementById('tasksScheduled').textContent = stats.tasksScheduled;
document.getElementById('efficiencyScore').textContent = `${stats.efficiency}%`;
}
function formatTimeDisplay(time) {
const [hours, minutes] = time.split(':');
const hour = parseInt(hours);
const ampm = hour >= 12 ? 'PM' : 'AM';
const displayHour = hour % 12 || 12;
return `${displayHour}:${minutes} ${ampm}`;
}
// Mobile-Compatible Export Functionality
async function exportSchedule(format) {
showLoading(true);
try {
if (format === 'png') {
await exportAsPNG();
} else if (format === 'pdf') {
await exportAsPDF();
}
} catch (error) {
console.error('Export error:', error);
showNotification(`Export failed: ${error.message}`, 'error');
// Fallback to basic export
try {
await fallbackExport(format);
} catch (fallbackError) {
console.error('Fallback export failed:', fallbackError);
showNotification('Export not supported on this device', 'error');
}
} finally {
showLoading(false);
}
}
// Mobile-compatible PNG Export
async function exportAsPNG() {
showNotification('Generating image...', 'info');
// Check if we're on mobile
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
if (isMobile) {
// Use simple canvas method for mobile
await createMobilePNG();
} else {
// Try advanced method for desktop
try {
if (typeof html2canvas === 'undefined') {
await loadScript('https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js');
}
await createAdvancedPNG();
} catch (error) {
// Fallback to mobile method
await createMobilePNG();
}
}
}
// Simple mobile PNG creation
async function createMobilePNG() {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set canvas size for mobile
canvas.width = 800;
canvas.height = 1000;
// Draw background
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw header
ctx.fillStyle = '#000000';
ctx.font = 'bold 28px Arial';
ctx.textAlign = 'center';
ctx.fillText('SchedualMe Schedule', canvas.width / 2, 50);
ctx.font = '18px Arial';
ctx.fillText(`Generated: ${new Date().toLocaleDateString()}`, canvas.width / 2, 85);
// Draw summary
let yPos = 130;
if (appState.scheduleStats) {
const stats = appState.scheduleStats;
ctx.font = 'bold 22px Arial';
ctx.textAlign = 'left';
ctx.fillText('Summary:', 50, yPos);
yPos += 35;
ctx.font = '18px Arial';
ctx.fillText(`• Total Hours: ${stats.totalScheduledHours}h`, 70, yPos);
yPos += 30;
ctx.fillText(`• Tasks Scheduled: ${stats.tasksScheduled}`, 70, yPos);
yPos += 30;
ctx.fillText(`• Efficiency: ${stats.efficiency}%`, 70, yPos);
yPos += 50;
}
// Draw schedule
ctx.font = 'bold 22px Arial';
ctx.fillText('Weekly Schedule:', 50, yPos);
yPos += 40;
const dayNames = {
monday: 'Monday', tuesday: 'Tuesday', wednesday: 'Wednesday',
thursday: 'Thursday', friday: 'Friday', saturday: 'Saturday', sunday: 'Sunday'
};
appState.days.forEach(day => {
ctx.font = 'bold 18px Arial';
ctx.fillText(`${dayNames[day]}:`, 70, yPos);
yPos += 25;
ctx.font = '16px Arial';
let tasksAdded = 0;
// Show up to 3 tasks per day to fit on screen
const timeSlots = generateTimeSlots();
for (const slot of timeSlots) {
if (tasksAdded >= 3) break;
const items = appState.schedule[day]?.[slot] || [];
if (items.length > 0) {
items.forEach(item => {
if (tasksAdded < 3) {
ctx.fillText(` ${formatTimeDisplay(slot)} - ${item.name}`, 90, yPos);
yPos += 20;
tasksAdded++;
}
});
}
}
if (tasksAdded === 0) {
ctx.fillStyle = '#666666';
ctx.fillText(` No tasks scheduled`, 90, yPos);
ctx.fillStyle = '#000000';
yPos += 20;
}
yPos += 15; // Spacing between days
});
// Convert to data URL
const dataUrl = canvas.toDataURL('image/png');
// Mobile-friendly download
if (isIOS() || isAndroid()) {
// For mobile, open in new tab or share
openImageForMobile(dataUrl, 'schedule.png');
} else {
// For desktop, download normally
downloadImage(dataUrl, `schedule_${getFormattedTimestamp()}.png`);
}
showNotification('Image ready!', 'success');
}
// Mobile-compatible PDF Export
async function exportAsPDF() {
showNotification('Creating document...', 'info');
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
if (isMobile) {
// Use text-based export for mobile
createMobilePDF();
} else {
// Try advanced PDF for desktop
try {
if (typeof jsPDF === 'undefined') {
await loadScript('https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js');
}
await createAdvancedPDF();
} catch (error) {
createMobilePDF();
}
}
}
// Simple text-based PDF for mobile
function createMobilePDF() {
let content = `SchedualMe Schedule\n`;
content += `===================\n\n`;
content += `Generated: ${new Date().toLocaleString()}\n\n`;
if (appState.scheduleStats) {
content += `SUMMARY:\n`;
content += `• Total Hours: ${appState.scheduleStats.totalScheduledHours}h\n`;
content += `• Tasks Scheduled: ${appState.scheduleStats.tasksScheduled}\n`;
content += `• Efficiency: ${appState.scheduleStats.efficiency}%\n\n`;
}
content += `SCHEDULE:\n`;
content += `=========\n\n`;
const dayNames = {
monday: 'Monday', tuesday: 'Tuesday', wednesday: 'Wednesday',
thursday: 'Thursday', friday: 'Friday', saturday: 'Saturday', sunday: 'Sunday'
};
const timeSlots = generateTimeSlots();
appState.days.forEach(day => {
content += `${dayNames[day].toUpperCase()}\n`;
content += `${'-'.repeat(dayNames[day].length)}\n`;
let hasTasks = false;
timeSlots.forEach(slot => {
const items = appState.schedule[day]?.[slot] || [];
if (items.length > 0) {
content += `${formatTimeDisplay(slot).padEnd(8)} : `;
items.forEach((item, index) => {
if (index > 0) content += ', ';
content += item.name;
});
content += '\n';
hasTasks = true;
}
});
if (!hasTasks) {
content += `No tasks scheduled\n`;
}
content += '\n';
});
// Create and download text file
const blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
const url = URL.createObjectURL(blob);
if (isIOS() || isAndroid()) {
// For mobile, open in new tab
window.open(url, '_blank');
} else {
// For desktop, download
const link = document.createElement('a');
link.download = `schedule_${getFormattedTimestamp()}.txt`;
link.href = url;
link.click();
}
setTimeout(() => URL.revokeObjectURL(url), 1000);
showNotification('Document ready!', 'success');
}
// Utility functions for mobile export
function isIOS() {
return /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
}
function isAndroid() {
return /Android/.test(navigator.userAgent);
}
function openImageForMobile(dataUrl, filename) {
// Open image in new tab for mobile users to save manually
const newWindow = window.open();
newWindow.document.write(`
<html>
<head><title>${filename}</title></head>
<body style="margin: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; background: #f0f0f0;">
<img src="${dataUrl}" style="max-width: 100%; height: auto;" alt="Schedule">
<p style="position: fixed; bottom: 10px; text-align: center; width: 100%;">Long press to save image</p>
</body>
</html>
`);
}
function downloadImage(dataUrl, filename) {
const link = document.createElement('a');
link.download = filename;
link.href = dataUrl;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function getFormattedTimestamp() {
return new Date().toISOString().slice(0, 10).replace(/-/g, '');
}
// Fallback export
async function fallbackExport(format) {
showNotification(`Creating ${format.toUpperCase()}...`, 'info');
if (format === 'png') {
await createMobilePNG();
} else {
createMobilePDF();
}
}
// Load external scripts
function loadScript(src) {
return new Promise((resolve, reject) => {
if (document.querySelector(`script[src="${src}"]`)) {
resolve();
return;
}
const script = document.createElement('script');
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
// Advanced export methods (for desktop)
async function createAdvancedPNG() {
const scheduleElement = document.getElementById('scheduleDisplay');
const canvas = await html2canvas(scheduleElement, {
scale: 2,
useCORS: true,
backgroundColor: '#ffffff'
});
const dataUrl = canvas.toDataURL('image/png');
downloadImage(dataUrl, `schedule_${getFormattedTimestamp()}.png`);
showNotification('PNG exported successfully!', 'success');
}
async function createAdvancedPDF() {
const scheduleElement = document.getElementById('scheduleDisplay');
const canvas = await html2canvas(scheduleElement, {
scale: 2,
useCORS: true,
backgroundColor: '#ffffff'
});
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF.jsPDF({
orientation: 'landscape',
unit: 'mm',
format: 'a4'
});
const pageWidth = pdf.internal.pageSize.getWidth();
const pageHeight = pdf.internal.pageSize.getHeight();
const imgWidth = canvas.width;
const imgHeight = canvas.height;
const ratio = imgHeight / imgWidth;
let pdfWidth = pageWidth - 20;
let pdfHeight = pdfWidth * ratio;
if (pdfHeight > pageHeight - 20) {
pdfHeight = pageHeight - 20;
pdfWidth = pdfHeight / ratio;
}
pdf.setFontSize(16);
pdf.text('SchedualMe Schedule', 10, 15);
pdf.setFontSize(10);
pdf.text(`Generated: ${new Date().toLocaleDateString()}`, 10, 22);