-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
1499 lines (1371 loc) · 69.7 KB
/
Copy pathapp.js
File metadata and controls
1499 lines (1371 loc) · 69.7 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
/**
* WeekWise 3.0 – Application Entry Point (ES Module Bridge)
*
* Imports all extracted modules, then defines the remaining
* UI-layer functions (renderer, modals, sidebar, drag-drop, context-menu).
*
* This is the active entry point for index.html via:
* <script type="module" src="app.js"></script>
*/
import { CONFIG, LOADING_LOGO_MIN_MS } from './js/core/constants.js';
import { stringToHash, rgbToHex, isLightColor, sanitizeColor, formatTime, timeToMinutes, escapeHtml, generateBookingId, ensureBookingId, snapTo15 } from './js/core/utils.js';
import { getState, onStateChange, getBookings, setBookings, isLoggedIn, setLoggedIn, getSelectedDay, setSelectedDay, getSelectedBookingIndex, setSelectedBookingIndex, getSelectedBookingIndices, clearMultiSelection, isSidebarOpen, setSidebarOpen, getMode, setMode, getEventStartDate, setEventStartDate, getEventDayCount, setEventDayCount, getDragBookingIndex, setDragBookingIndex, isDragCopy, setDragCopy, isResizing, setResizing, isJustResized, setJustResized, getIcsTokens, setIcsTokens, getUrlParams, setUrlParams, getLoadingStartTime, resetLoadingStartTime } from './js/core/state.js';
import { getStartHour, setStartHour, getEndHour, setEndHour, getHiddenDays, setHiddenDays, getHideEmptyDays, setHideEmptyDays, getCollapseEmptyHours, setCollapseEmptyHours, getBookingColors, setBookingColors, getLoginHash, setLoginHash, getHeaderColor, setHeaderColor, getSecondaryColor, setSecondaryColor, getCalendarTitle, setCalendarTitle } from './js/core/persist.js';
import { saveToServer, loadFromServer } from './js/services/server.js';
import { login as authLogin, logout as authLogout } from './js/services/auth.js';
import { generateToken, getIcsBaseUrl, buildIcsUrls, countFilteredIcsBookings } from './js/services/ics-service.js';
import { getDayCount, getDayLabel, getDayShortLabel, parseDayInput, parseDayList, migrateDay, migrateHiddenDays } from './js/models/day-model.js';
import { validateBooking, detectOverlaps, ensureDefaultCategory, getDefaultCategoryName, buildCategoryOptions } from './js/models/booking-model.js';
import { buildFullConfigData, renderColorOptions, initializeDefaults } from './js/models/settings-model.js';
import { parseUrlParams } from './js/features/url-params.js';
import { shouldCollapseEmpty, analyzeEmptyHours, buildYMapper } from './js/features/empty-collapse.js';
import { togglePrintPopup, executePrint } from './js/features/print.js';
import { exportBookings, importBookings } from './js/features/import-export.js';
// Re-expose to window for HTML onclick handlers
Object.assign(window, {
stringToHash, rgbToHex, isLightColor, sanitizeColor, formatTime, timeToMinutes,
escapeHtml, generateBookingId, ensureBookingId, snapTo15,
CONFIG, LOADING_LOGO_MIN_MS,
saveToServer, loadFromServer,
generateToken, getIcsBaseUrl, buildIcsUrls, countFilteredIcsBookings,
getDayCount, getDayLabel, getDayShortLabel, parseDayInput, parseDayList, migrateDay, migrateHiddenDays,
validateBooking, detectOverlaps, ensureDefaultCategory, getDefaultCategoryName, buildCategoryOptions,
buildFullConfigData, renderColorOptions, initializeDefaults,
parseUrlParams, shouldCollapseEmpty, analyzeEmptyHours, buildYMapper,
togglePrintPopup, executePrint, exportBookings, importBookings,
});
// ── State references for inline UI code ───────────────
const state = getState();
// Internal loading start time (used by showLoading/hideLoading)
let _loadingStartTime = Date.now();
function showLoading() {
const overlay = document.getElementById('loadingOverlay');
if (!overlay) return;
_loadingStartTime = Date.now();
}
function hideLoading() {
const overlay = document.getElementById('loadingOverlay');
if (!overlay) return;
const elapsed = _loadingStartTime ? Date.now() - _loadingStartTime : Infinity;
const delay = Math.max(0, LOADING_LOGO_MIN_MS - elapsed);
setTimeout(() => {
const el = document.getElementById('loadingOverlay');
if (el) el.classList.remove('active');
}, delay);
}
// ── Drag Ghost ───────────────────────────────────────
function updateDragGhost(isCopy, bookingIndex) {
if (isCopy && !state.dragGhost) {
const booking = getBookings()[bookingIndex];
if (!booking) return;
const ghost = document.createElement('div');
ghost.className = 'drag-ghost drag-ghost-floating';
ghost.textContent = '+ Kopie';
document.body.appendChild(ghost);
state.dragGhost = ghost;
} else if (!isCopy && state.dragGhost) {
state.dragGhost.remove();
state.dragGhost = null;
}
}
// ── Sidebar ──────────────────────────────────────────
function toggleSidebar(updateVisibility = false) {
if (isSidebarOpen()) closeSidebar(updateVisibility);
else openSidebar(updateVisibility);
}
function openSidebar(updateVisibility = false) {
setSidebarOpen(true);
const sidebar = document.getElementById('contentSidebar');
const main = document.getElementById('main');
const openBtn = document.querySelector('.openbtn');
const closeBtn = document.querySelector('.closebtn');
if (sidebar) sidebar.style.width = '20%';
if (main) main.style.marginRight = '20%';
if (openBtn) openBtn.style.display = 'none';
if (closeBtn) closeBtn.style.display = 'flex';
if (updateVisibility) {
const sidebarContainer = document.getElementById('sidebar');
const ablageTile = document.getElementById('ablage-tile');
if (sidebarContainer) sidebarContainer.style.display = 'block';
if (ablageTile) ablageTile.style.display = 'block';
}
}
function closeSidebar(updateVisibility = false) {
setSidebarOpen(false);
const sidebar = document.getElementById('contentSidebar');
const main = document.getElementById('main');
const openBtn = document.querySelector('.openbtn');
const closeBtn = document.querySelector('.closebtn');
if (sidebar) sidebar.style.width = '0';
if (main) main.style.marginRight = '0';
if (openBtn) openBtn.style.display = 'flex';
if (closeBtn) closeBtn.style.display = 'none';
if (updateVisibility) {
const sidebarContainer = document.getElementById('sidebar');
const ablageTile = document.getElementById('ablage-tile');
if (sidebarContainer) sidebarContainer.style.display = 'none';
if (ablageTile) ablageTile.style.display = 'none';
}
}
// ── Time Options ─────────────────────────────────────
function createTimeOptions() {
const startH = getStartHour();
const endH = getEndHour();
const startSelect = document.getElementById('bookingStart');
const endSelect = document.getElementById('bookingEnd');
if (!startSelect || !endSelect) return;
startSelect.innerHTML = '';
endSelect.innerHTML = '';
for (let hour = startH; hour <= endH; hour++) {
for (let minute = 0; minute < 60; minute += 15) {
const timeStr = formatTime(hour, minute);
startSelect.add(new Option(timeStr, timeStr));
endSelect.add(new Option(timeStr, timeStr));
}
}
}
function populateTimeOptions() {
const startSelect = document.getElementById('startHour');
const endSelect = document.getElementById('endHour');
if (!startSelect || !endSelect) return;
startSelect.innerHTML = '';
endSelect.innerHTML = '';
for (let i = 0; i < 24; i++) {
const hour = `${i.toString().padStart(2, '0')}:00`;
startSelect.add(new Option(hour, i));
endSelect.add(new Option(hour, i));
}
}
// ── Day Visibility ───────────────────────────────────
function getDaysToShow() {
const count = getDayCount();
const allDays = [];
for (let i = 1; i <= count; i++) allDays.push(i);
const hiddenDays = getHiddenDays();
const hideEmptyDays = getHideEmptyDays() || getUrlParams().hideempty;
const bookings = getBookings();
const daysWithBookings = new Set(bookings.map(b => b.day));
const allHiddenDays = new Set([
...(getMode() === 'week' ? hiddenDays : []),
...getUrlParams().hideDays
]);
return allDays.map(dayNum => {
const hiddenByDayParam = getUrlParams().days.length > 0 && !getUrlParams().days.includes(dayNum);
const hiddenBySetting = allHiddenDays.has(dayNum);
const isEmpty = !daysWithBookings.has(dayNum);
const hiddenByEmpty = hideEmptyDays && isEmpty;
const isHidden = hiddenByDayParam || hiddenBySetting || hiddenByEmpty;
return { day: dayNum, label: getDayLabel(dayNum), hidden: isHidden, empty: isEmpty };
});
}
// ── Day Columns ──────────────────────────────────────
function createDayColumns() {
const container = document.getElementById('dayColumns');
if (!container) return;
container.innerHTML = '';
const bookings = getBookings();
const startHour = getStartHour();
const endHour = getEndHour();
let yMapper = null;
const useCollapse = shouldCollapseEmpty();
if (useCollapse) {
const dayInfo = getDaysToShow();
const visibleDayNums = new Set(dayInfo.filter(d => !d.hidden || isLoggedIn()).map(d => d.day));
const emptyHours = analyzeEmptyHours(bookings, startHour, endHour, visibleDayNums);
yMapper = buildYMapper(emptyHours, startHour);
}
const totalHeight = useCollapse && yMapper ? yMapper.getTotalHeight() : (endHour - startHour) * 60;
const dayInfo = getDaysToShow();
const visibleDays = dayInfo.filter(d => !d.hidden || isLoggedIn());
const weekdaysContainer = document.querySelector('.weekdays');
if (weekdaysContainer) {
weekdaysContainer.innerHTML = visibleDays.map(d =>
`<div class="weekday${d.hidden ? ' weekday-hidden' : ''}">${escapeHtml(d.label)}</div>`
).join('');
weekdaysContainer.style.gridTemplateColumns = `repeat(${visibleDays.length}, 1fr)`;
container.style.gridTemplateColumns = `repeat(${visibleDays.length}, 1fr)`;
const needsScroll = visibleDays.length > 7;
const scrollWrapper = document.getElementById('desktopScrollWrapper');
weekdaysContainer.classList.toggle('event-scroll', needsScroll);
container.classList.toggle('event-scroll', needsScroll);
if (scrollWrapper) scrollWrapper.classList.toggle('event-scroll', needsScroll);
if (needsScroll) {
weekdaysContainer.style.gridTemplateColumns = `repeat(${visibleDays.length}, minmax(120px, 1fr))`;
container.style.gridTemplateColumns = `repeat(${visibleDays.length}, minmax(120px, 1fr))`;
}
}
const emptyHoursCache = useCollapse ? analyzeEmptyHours(bookings, startHour, endHour) : null;
const urlParams = getUrlParams();
visibleDays.forEach(({ day, label, hidden }) => {
const column = document.createElement('div');
column.className = 'day-column' + (hidden ? ' day-column-hidden' : '');
column.dataset.day = day;
column.style.height = `${totalHeight}px`;
if (useCollapse) column.classList.add('day-column-collapsed');
const timeMarkers = document.createElement('div');
timeMarkers.className = 'time-markers';
for (let hour = startHour; hour <= endHour; hour++) {
const marker = document.createElement('div');
marker.className = 'time-marker';
if (useCollapse && yMapper) {
let topPx;
if (hour === endHour) topPx = yMapper.getTotalHeight();
else topPx = yMapper.mapMinuteToPixel((hour - startHour) * 60);
marker.style.top = `${topPx}px`;
const hourInfo = emptyHoursCache ? emptyHoursCache.find(e => e.hour === hour) : null;
if (hourInfo && hourInfo.empty) marker.classList.add('time-marker-collapsed');
} else {
marker.style.top = `${(hour - startHour) * 60}px`;
}
marker.textContent = `${hour}:00`;
timeMarkers.appendChild(marker);
}
column.appendChild(timeMarkers);
if (!urlParams.readonly && !urlParams.embedded) {
column.addEventListener('click', (e) => {
if (isJustResized()) return;
if (isLoggedIn() && e.target.classList.contains('day-column')) {
setSelectedDay(day);
state.selectedColumn = column;
setSelectedBookingIndex(null);
document.getElementById('bookingForm')?.reset();
const rect = column.getBoundingClientRect();
const clickY = e.clientY - rect.top;
const hour = Math.floor(clickY / 60) + startHour;
const minute = Math.round((clickY % 60) / 15) * 15;
buildDaySelect();
document.getElementById('bookingDay').value = day;
document.getElementById('bookingStart').value = formatTime(hour, minute);
const endHourCalc = hour + 1;
if (endHourCalc <= endHour) {
document.getElementById('bookingEnd').value = formatTime(endHourCalc, minute);
}
buildCategoryOptions(document.getElementById('bookingCategory'));
document.getElementById('modalDeleteButton').style.display = 'none';
const dupBtn = document.getElementById('modalDuplicateButton');
if (dupBtn) dupBtn.style.display = 'none';
document.getElementById('bookingModal').style.display = 'flex';
}
});
}
// ── Drag & drop target (admin only, desktop) ──
if (!urlParams.readonly && !urlParams.embedded) {
column.addEventListener('dragover', (e) => {
if (getDragBookingIndex() === null) return;
e.preventDefault();
setDragCopy(e.shiftKey || e.ctrlKey);
e.dataTransfer.dropEffect = isDragCopy() ? 'copy' : 'move';
updateDragGhost(isDragCopy(), getDragBookingIndex());
column.classList.add('drag-over');
});
column.addEventListener('dragleave', () => {
column.classList.remove('drag-over');
});
column.addEventListener('drop', async (e) => {
e.preventDefault();
column.classList.remove('drag-over');
setDragCopy(e.shiftKey || e.ctrlKey);
const bookingIndex = getDragBookingIndex();
if (bookingIndex === null || bookingIndex < 0) return;
const targetDay = parseInt(column.dataset.day);
const sourceBooking = getBookings()[bookingIndex];
if (!sourceBooking) return;
// Calculate target time from drop position (15-min snap)
const rect = column.getBoundingClientRect();
const dropY = e.clientY - rect.top;
const dropMinutes = snapTo15(dropY);
const duration = timeToMinutes(sourceBooking.endTime) - timeToMinutes(sourceBooking.startTime);
const newStartMin = Math.max(0, dropMinutes);
const maxStart = (endHour - startHour) * 60 - duration;
const clampedStart = Math.min(newStartMin, Math.max(0, maxStart));
const newStartTotal = clampedStart + startHour * 60;
const newEndTotal = newStartTotal + duration;
const newStartTime = formatTime(Math.floor(newStartTotal / 60), newStartTotal % 60);
const newEndTime = formatTime(Math.floor(newEndTotal / 60), newEndTotal % 60);
const bookings = getBookings();
if (isDragCopy()) {
// Shift/Ctrl+Drag: duplicate to target day + time
const copy = JSON.parse(JSON.stringify(sourceBooking));
copy.id = generateBookingId();
copy.day = targetDay;
copy.startTime = newStartTime;
copy.endTime = newEndTime;
bookings.push(copy);
} else {
// Normal drag: move to target day + time
sourceBooking.day = targetDay;
sourceBooking.startTime = newStartTime;
sourceBooking.endTime = newEndTime;
}
await saveToServer('bookings.json', bookings);
createDayColumns();
});
}
container.appendChild(column);
});
renderBookings();
}
// ── Mobile Tiles ─────────────────────────────────────
function buildMobileTiles() {
const container = document.getElementById('dayTiles');
if (!container) return;
container.innerHTML = '';
const count = getDayCount();
for (let i = 1; i <= count; i++) {
const tile = document.createElement('div');
tile.className = 'day-tile';
tile.dataset.day = i;
tile.innerHTML = `<div class="day-name">${escapeHtml(getDayLabel(i))}</div><div class="day-bookings"></div>`;
container.appendChild(tile);
}
if (isLoggedIn()) {
const ablageTile = document.createElement('div');
ablageTile.className = 'day-tile ablage-tile';
ablageTile.id = 'ablage-tile';
ablageTile.dataset.day = '0';
ablageTile.innerHTML = '<div class="day-name filing-tile">Ablage</div><div class="day-bookings"></div>';
container.appendChild(ablageTile);
}
}
function buildDaySelect() {
const select = document.getElementById('bookingDay');
if (!select) return;
select.innerHTML = '';
const count = getDayCount();
for (let i = 1; i <= count; i++) {
const option = document.createElement('option');
option.value = i;
option.textContent = getDayLabel(i);
select.appendChild(option);
}
const ablageOption = document.createElement('option');
ablageOption.value = 0;
ablageOption.textContent = 'Ablage';
select.appendChild(ablageOption);
}
function buildHiddenDaysCheckboxes() {
const container = document.getElementById('hiddenDaysCheckboxes');
if (!container) return;
container.innerHTML = '';
const hiddenDays = getHiddenDays();
for (let i = 1; i <= 7; i++) {
const label = document.createElement('label');
label.className = 'day-chip';
const cb = document.createElement('input');
cb.type = 'checkbox';
cb.value = i;
cb.checked = !hiddenDays.includes(i);
label.appendChild(cb);
label.appendChild(document.createTextNode(getDayShortLabel(i)));
container.appendChild(label);
}
}
// ── Booking Element ──────────────────────────────────
function createBookingElement(booking, index, isMobile) {
const el = document.createElement('div');
el.className = isMobile ? 'mobile-booking' : 'booking';
el.dataset.index = index;
el.dataset.bookingIndex = index;
const bgColor = sanitizeColor(rgbToHex(booking.color) || booking.color || 'var(--secondary)');
el.style.backgroundColor = bgColor;
if (isLightColor(bgColor)) el.classList.add('light-bg');
else el.classList.add('dark-bg');
const titleClass = isMobile ? 'mobile-booking-title' : 'booking-title';
const timeClass = isMobile ? 'mobile-booking-time' : 'booking-time';
const actionsClass = isMobile ? 'mobile-booking-actions' : 'booking-actions';
let html = `<div class="${titleClass}">${escapeHtml(booking.title)}</div>
<div class="${timeClass}">${escapeHtml(booking.startTime)} - ${escapeHtml(booking.endTime)}</div>`;
if (isLoggedIn() && !getUrlParams().readonly) {
html += `<div class="${actionsClass}">
<button class="action-button edit-button" data-action="edit" data-index="${index}">Bearbeiten</button>
<button class="action-button delete-button" data-action="delete" data-index="${index}">Löschen</button>
</div>`;
}
if (!isMobile && isLoggedIn() && !getUrlParams().readonly && !getUrlParams().embedded) {
html += '<div class="resize-handle"></div>';
}
el.innerHTML = html;
// ── Desktop Drag & Drop (admin only) ──
if (!isMobile && isLoggedIn() && !getUrlParams().readonly && !getUrlParams().embedded) {
el.setAttribute('draggable', 'true');
el.addEventListener('dragstart', (e) => {
if (state.resizing) { e.preventDefault(); return; }
setDragBookingIndex(index);
setDragCopy(e.shiftKey || e.ctrlKey);
el.classList.add('dragging');
e.dataTransfer.effectAllowed = 'all';
e.dataTransfer.setData('text/plain', String(index));
});
el.addEventListener('dragend', () => {
el.classList.remove('dragging');
setDragBookingIndex(null);
setDragCopy(false);
if (state.dragGhost) {
state.dragGhost.remove();
state.dragGhost = null;
}
document.querySelectorAll('.day-column').forEach(c => c.classList.remove('drag-over'));
});
// ── Resize handle ──
const resizeHandle = el.querySelector('.resize-handle');
if (resizeHandle) {
resizeHandle.addEventListener('mousedown', (e) => {
e.preventDefault();
e.stopPropagation();
el.setAttribute('draggable', 'false');
setResizing(true);
const column = el.parentElement;
const startHourVal = getStartHour();
const endHourVal = getEndHour();
const colRect = column.getBoundingClientRect();
el.classList.add('resizing');
const onMouseMove = (moveE) => {
const relY = moveE.clientY - colRect.top;
const snappedMinutes = snapTo15(relY);
const bookingStartMin = timeToMinutes(booking.startTime) - startHourVal * 60;
const minHeight = 15;
const maxMinutes = (endHourVal - startHourVal) * 60;
const newHeight = Math.max(minHeight, Math.min(snappedMinutes - bookingStartMin, maxMinutes - bookingStartMin));
el.style.height = `${newHeight}px`;
};
const onMouseUp = async () => {
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
el.classList.remove('resizing');
setResizing(false);
setJustResized(true);
setTimeout(() => { setJustResized(false); }, 200);
el.setAttribute('draggable', 'true');
const newHeight = parseInt(el.style.height);
const bookingTopPx = parseInt(el.style.top);
const endHourVal = getEndHour();
const startHourVal = getStartHour();
const endTotalMin = startHourVal * 60 + bookingTopPx + newHeight;
const endH = Math.min(Math.floor(endTotalMin / 60), endHourVal);
const endM = endTotalMin % 60;
booking.endTime = formatTime(endH, endM);
await saveToServer('bookings.json', getBookings());
createDayColumns();
};
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
});
}
}
el.addEventListener('click', (e) => {
const action = e.target.dataset.action;
const targetIndex = e.target.dataset.index;
if (action === 'edit') {
editBooking(parseInt(targetIndex));
} else if (action === 'delete') {
deleteBooking(parseInt(targetIndex));
} else if (!isLoggedIn() && !getUrlParams().readonly) {
showBookingDetails(booking);
} else if (isLoggedIn()) {
// Multi-select with Ctrl/Cmd key
if (e.ctrlKey || e.metaKey) {
e.stopPropagation();
const sel = getSelectedBookingIndices();
const allBookings = document.querySelectorAll(isMobile ? '.mobile-booking' : '.booking');
// Toggle the clicked booking first
const wasSelected = sel.has(index);
if (wasSelected) {
sel.delete(index);
el.classList.remove('selected');
} else {
sel.add(index);
el.classList.add('selected');
}
// Collect any OTHER expanded bookings into the selection
allBookings.forEach(b => {
if (b === el) return; // already handled above
if (b.classList.contains('expanded')) {
const bIndex = parseInt(b.dataset.bookingIndex);
if (!isNaN(bIndex) && !sel.has(bIndex)) {
sel.add(bIndex);
b.classList.add('selected');
}
b.classList.remove('expanded');
}
});
return;
}
// Normal click (no Ctrl): clear selection, toggle expand
clearMultiSelection();
document.querySelectorAll(isMobile ? '.mobile-booking.selected' : '.booking.selected')
.forEach(b => b.classList.remove('selected'));
const allBookings = document.querySelectorAll(isMobile ? '.mobile-booking' : '.booking');
allBookings.forEach(b => {
if (b !== el) b.classList.remove('expanded');
});
el.classList.toggle('expanded');
}
});
// Right-click context menu (admin only)
if (isLoggedIn() && !getUrlParams().readonly) {
el.addEventListener('contextmenu', (e) => {
showBookingContextMenu(e, index);
});
}
return el;
}
// ── Render Bookings ──────────────────────────────────
function renderBookings() {
const bookingColors = getBookingColors();
const bookings = getBookings();
bookings.forEach(booking => {
if (!booking.categoryID) booking.categoryID = 'default';
const colorConfig = bookingColors.find(c => c.id === booking.categoryID);
booking.color = colorConfig ? colorConfig.color : 'var(--secondary)';
});
bookings.sort((a, b) => timeToMinutes(a.startTime) - timeToMinutes(b.startTime));
let filtered = bookings;
if (getUrlParams().category) {
const catFilter = getUrlParams().category.toLowerCase();
filtered = bookings.filter(b => {
const cfg = bookingColors.find(c => c.id === b.categoryID);
return cfg && cfg.name.toLowerCase() === catFilter;
});
}
renderMobileBookings(filtered);
renderDesktopBookings(filtered);
renderSidebarBookings(filtered);
}
function renderMobileBookings(bookings) {
const mobileColumns = document.querySelectorAll('.day-tile');
mobileColumns.forEach(col => col.querySelectorAll('.mobile-booking').forEach(b => b.remove()));
const dayInfo = getDaysToShow();
const tileMap = {};
mobileColumns.forEach(col => {
const dayNum = parseInt(col.dataset.day);
if (!isNaN(dayNum)) tileMap[dayNum] = col;
});
dayInfo.forEach(info => {
const tile = tileMap[info.day];
if (!tile) return;
if (info.hidden && !isLoggedIn()) tile.style.display = 'none';
else { tile.style.display = 'block'; tile.classList.toggle('day-tile-hidden', info.hidden && isLoggedIn()); }
});
bookings.forEach((booking, index) => {
if (booking.day === 0 && isLoggedIn()) {
const ablageTile = tileMap[0];
if (ablageTile) ablageTile.appendChild(createBookingElement(booking, index, true));
return;
}
const tile = tileMap[booking.day];
if (tile && tile.style.display !== 'none') {
tile.appendChild(createBookingElement(booking, index, true));
}
});
}
function renderDesktopBookings(bookings) {
const columns = document.querySelectorAll('.day-column');
columns.forEach(col => col.querySelectorAll('.booking').forEach(b => b.remove()));
const startHour = getStartHour();
const endHour = getEndHour();
let yMapper = null;
const useCollapse = shouldCollapseEmpty();
if (useCollapse) {
const emptyHours = analyzeEmptyHours(bookings, startHour, endHour);
yMapper = buildYMapper(emptyHours, startHour);
}
const dayColumns = {};
columns.forEach(col => { dayColumns[parseInt(col.dataset.day)] = col; });
const overlapInfoByDay = {};
Object.keys(dayColumns).forEach(key => {
overlapInfoByDay[parseInt(key)] = detectOverlaps(bookings, parseInt(key));
});
bookings.forEach((booking, index) => {
if (booking.day === 0) return;
const column = dayColumns[booking.day];
if (!column) return;
const bookingEl = createBookingElement(booking, index, false);
const [startH, startM] = booking.startTime.split(':').map(Number);
const [endH, endM] = booking.endTime.split(':').map(Number);
let top, height;
if (useCollapse && yMapper) {
top = yMapper.mapMinuteToPixel((startH - startHour) * 60 + startM);
height = yMapper.mapMinuteToPixel((endH - startHour) * 60 + endM) - top;
} else {
top = (startH - startHour) * 60 + startM;
height = (endH - startH) * 60 + (endM - startM);
}
bookingEl.style.top = `${top}px`;
bookingEl.style.height = `${height}px`;
const overlapInfo = overlapInfoByDay[booking.day];
if (overlapInfo && overlapInfo[index] && overlapInfo[index].count > 1) {
bookingEl.dataset.overlapCount = overlapInfo[index].count;
bookingEl.dataset.overlapIndex = overlapInfo[index].index;
}
column.appendChild(bookingEl);
});
}
function renderSidebarBookings(bookings) {
const sidebar = document.getElementById('Ablage');
if (!sidebar) return;
sidebar.querySelectorAll('.mobile-booking').forEach(b => b.remove());
const urlParams = getUrlParams();
bookings.filter(b => b.day === 0).forEach((booking, idx) => {
const originalIndex = getBookings().indexOf(booking);
const bookingEl = createBookingElement(booking, originalIndex, true);
// Make sidebar bookings draggable (admin only, desktop)
if (isLoggedIn() && !urlParams.readonly && !urlParams.embedded) {
bookingEl.setAttribute('draggable', 'true');
bookingEl.addEventListener('dragstart', (e) => {
if (state.resizing) { e.preventDefault(); return; }
setDragBookingIndex(originalIndex);
setDragCopy(e.shiftKey || e.ctrlKey);
bookingEl.classList.add('dragging');
e.dataTransfer.effectAllowed = 'all';
e.dataTransfer.setData('text/plain', String(originalIndex));
});
bookingEl.addEventListener('dragend', () => {
bookingEl.classList.remove('dragging');
setDragBookingIndex(null);
setDragCopy(false);
if (state.dragGhost) {
state.dragGhost.remove();
state.dragGhost = null;
}
document.querySelectorAll('.day-column').forEach(c => c.classList.remove('drag-over'));
});
}
sidebar.appendChild(bookingEl);
});
}
// ── Booking Details ──────────────────────────────────
function showBookingDetails(booking) {
const modal = document.getElementById('viewBookingModal');
if (!modal) return;
document.querySelector('.view-booking-header').style.backgroundColor = sanitizeColor(booking.color);
document.getElementById('viewBookingTitle').textContent = booking.title;
document.getElementById('viewBookingTime').textContent = `${booking.startTime} - ${booking.endTime}`;
document.getElementById('viewBookingLocation').textContent = booking.location || '';
const trainersDiv = document.getElementById('viewBookingTrainers');
if (booking.trainer) {
trainersDiv.innerHTML = booking.trainer.split('\n').filter(t => t.trim())
.map(t => `<span class="trainer-tag">${escapeHtml(t)}</span>`).join('');
} else trainersDiv.innerHTML = '';
document.getElementById('viewBookingContact').innerHTML = booking.contact ? `Kontakt: ${escapeHtml(booking.contact)}` : '';
const descDiv = document.getElementById('viewBookingDescription');
descDiv.innerHTML = booking.description ? escapeHtml(booking.description).replace(/\n/g, '<br>') : '';
descDiv.style.display = booking.description ? 'block' : 'none';
const linkDiv = document.getElementById('viewBookingLink');
if (booking.link) {
linkDiv.innerHTML = `<a href="${escapeHtml(booking.link)}" target="_blank" rel="noopener noreferrer" class="link-button"><span class="icon link"></span></a>`;
linkDiv.style.display = 'block';
} else { linkDiv.innerHTML = ''; linkDiv.style.display = 'none'; }
modal.style.display = 'flex';
}
function closeViewBookingModal() {
document.getElementById('viewBookingModal').style.display = 'none';
}
// ── Booking CRUD ─────────────────────────────────────
function editBooking(index) {
const booking = getBookings()[index];
if (!booking) return;
buildCategoryOptions(document.getElementById('bookingCategory'));
buildDaySelect();
document.getElementById('bookingTitle').value = booking.title;
document.getElementById('bookingDay').value = booking.day;
document.getElementById('bookingStart').value = booking.startTime;
document.getElementById('bookingEnd').value = booking.endTime;
document.getElementById('bookingLocation').value = booking.location || '';
document.getElementById('bookingTrainer').value = booking.trainer || '';
document.getElementById('bookingContact').value = booking.contact || '';
document.getElementById('bookingLink').value = booking.link || '';
document.getElementById('bookingDescription').value = booking.description || '';
document.getElementById('bookingCategory').value = booking.categoryID || 'default';
document.getElementById('modalDeleteButton').style.display = 'inline';
document.getElementById('modalDeleteButton').onclick = () => deleteBooking(index);
const dupBtn = document.getElementById('modalDuplicateButton');
if (dupBtn) { dupBtn.style.display = 'inline'; dupBtn.onclick = () => duplicateBooking(index); }
setSelectedBookingIndex(index);
document.getElementById('bookingModal').style.display = 'flex';
}
async function deleteBooking(index) {
if (confirm('Möchten Sie diesen Termin wirklich löschen?')) {
const bookings = getBookings();
bookings.splice(index, 1);
await saveToServer('bookings.json', bookings);
createDayColumns();
closeBookingModal();
}
}
async function deleteSelectedBookings() {
const indices = Array.from(getSelectedBookingIndices()).sort((a, b) => b - a);
if (indices.length === 0) return;
if (!confirm(`${indices.length} Termin${indices.length > 1 ? 'e' : ''} wirklich löschen?`)) return;
const bookings = getBookings();
indices.forEach(i => bookings.splice(i, 1));
clearMultiSelection();
await saveToServer('bookings.json', bookings);
buildMobileTiles();
createDayColumns();
}
async function duplicateBooking(index) {
const source = getBookings()[index];
if (!source) return;
const copy = JSON.parse(JSON.stringify(source));
copy.id = generateBookingId();
const bookings = getBookings();
bookings.push(copy);
await saveToServer('bookings.json', bookings);
closeBookingModal();
buildMobileTiles();
createDayColumns();
}
function closeBookingModal() {
document.getElementById('bookingModal').style.display = 'none';
document.getElementById('bookingForm').reset();
document.getElementById('modalDeleteButton').style.display = 'none';
const dupBtn = document.getElementById('modalDuplicateButton');
if (dupBtn) dupBtn.style.display = 'none';
setSelectedBookingIndex(null);
}
// ── Auth ─────────────────────────────────────────────
function login() {
const passwordInput = document.getElementById('password');
if (authLogin(passwordInput.value)) {
document.getElementById('editButton').innerHTML = '<span class="icon settings"></span>';
document.getElementById('newBookingButton').style.display = 'flex';
document.getElementById('printButton').style.display = 'flex';
document.getElementById('icsButton').style.display = 'flex';
document.getElementById('helpButton').style.display = 'flex';
closeLoginModal();
buildMobileTiles();
createDayColumns();
renderBookings();
toggleSidebar(true);
} else {
alert('Falsches Passwort!');
}
}
function logout() {
if (confirm('Möchten Sie sich wirklich abmelden?')) {
authLogout();
document.getElementById('editButton').innerHTML = '<span class="icon edit"></span>';
document.getElementById('newBookingButton').style.display = 'none';
document.getElementById('printButton').style.display = 'none';
document.getElementById('icsButton').style.display = 'none';
document.getElementById('helpButton').style.display = 'none';
document.getElementById('printPopup')?.classList.remove('visible');
closeOptionsModal();
if (isSidebarOpen()) toggleSidebar(true);
buildMobileTiles();
createDayColumns();
renderBookings();
}
}
function closeLoginModal() {
document.getElementById('loginModal').style.display = 'none';
document.getElementById('password').value = '';
}
// ── Settings Modal ───────────────────────────────────
function showOptionsModal() {
const modal = document.getElementById('optionsModal');
if (!modal) return;
modal.style.display = 'flex';
document.getElementById('calendarTitle').value = document.querySelector('.calendar-header h2').textContent;
const modeSelect = document.getElementById('calendarModeSelect');
if (modeSelect) modeSelect.value = getMode();
updateModeUI();
document.getElementById('eventStartDate').value = getEventStartDate() || '';
document.getElementById('eventDayCount').value = getEventDayCount();
updateEventPreview();
populateTimeOptions();
document.getElementById('startHour').value = getStartHour();
document.getElementById('endHour').value = getEndHour();
buildHiddenDaysCheckboxes();
document.getElementById('hideEmptyDays').checked = getHideEmptyDays();
document.getElementById('collapseEmptyHours').checked = getCollapseEmptyHours();
renderColorOptions(document.getElementById('bookingColors'));
// Populate header/secondary color swatches from current CSS values
const headerSwatch = document.querySelector('.header-color-edit .color-swatch');
const secondarySwatch = document.querySelector('.secondary-color-edit .color-swatch');
if (headerSwatch) {
headerSwatch.style.backgroundColor = getComputedStyle(document.documentElement).getPropertyValue('--header-color').trim();
}
if (secondarySwatch) {
secondarySwatch.style.backgroundColor = getComputedStyle(document.documentElement).getPropertyValue('--secondary').trim();
}
const icsPublicCb = document.getElementById('icsPublicCheckbox');
if (icsPublicCb) icsPublicCb.checked = !!state.icsPublic;
const icsDayFilterCb = document.getElementById('icsDayFilterCheckbox');
if (icsDayFilterCb) icsDayFilterCb.checked = !!state.icsDayFilter;
const icsAboCb = document.getElementById('icsAboCheckbox');
if (icsAboCb) icsAboCb.checked = !!state.icsAboEnabled;
updateIcsTokenDisplay();
}
function updateModeUI() {
const mode = document.getElementById('calendarModeSelect')?.value || 'week';
document.getElementById('weekModeSection').style.display = mode === 'week' ? '' : 'none';
document.getElementById('eventModeSection').style.display = mode === 'event' ? '' : 'none';
}
function updateEventPreview() {
const preview = document.getElementById('eventDayPreview');
if (!preview) return;
const startDate = document.getElementById('eventStartDate')?.value;
const dayCount = parseInt(document.getElementById('eventDayCount')?.value) || 3;
if (!startDate) { preview.textContent = ''; return; }
const labels = [];
for (let i = 0; i < Math.min(dayCount, 14); i++) {
const d = new Date(startDate + 'T00:00:00');
d.setDate(d.getDate() + i);
labels.push(`${d.toLocaleDateString('de-DE', { weekday: 'short' })} ${d.toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit' })}`);
}
preview.textContent = '→ ' + labels.join(' │ ');
}
async function saveSettings() {
document.querySelector('.calendar-header h2').textContent = document.getElementById('calendarTitle').value;
const newMode = document.getElementById('calendarModeSelect')?.value || 'week';
const eventStartDate = document.getElementById('eventStartDate')?.value || null;
const eventDayCount = parseInt(document.getElementById('eventDayCount')?.value) || 3;
if (newMode === 'event' && !eventStartDate) { alert('Bitte ein Startdatum für den Eventmodus angeben.'); return; }
if (newMode !== getMode() && getBookings().length > 0) {
if (newMode === 'event' && !confirm('Modus wird zu "Event" gewechselt.')) return;
if (newMode === 'week' && getBookings().some(b => b.day > 7) && !confirm('Modus wird zu "Woche" gewechselt.')) return;
}
setMode(newMode);
setEventStartDate(eventStartDate);
setEventDayCount(Math.max(1, Math.min(99, eventDayCount)));
const headerColor = rgbToHex(document.querySelector('.header-color-edit .color-swatch').style.backgroundColor);
const secondaryColor = rgbToHex(document.querySelector('.secondary-color-edit .color-swatch').style.backgroundColor);
document.documentElement.style.setProperty('--header-color', headerColor);
document.documentElement.style.setProperty('--secondary', secondaryColor);
setHeaderColor(headerColor);
setSecondaryColor(secondaryColor);
setStartHour(document.getElementById('startHour').value);
setEndHour(document.getElementById('endHour').value);
const oldPwdInput = document.getElementById('oldpwd');
const newPwdInput = document.getElementById('newpwd');
let newLoginhash = getLoginHash();
if (oldPwdInput.value) {
if (getLoginHash() !== stringToHash(oldPwdInput.value)) { alert('Das alte Passwort ist falsch'); return; }
if (!newPwdInput.value) { alert('Das neue Passwort darf nicht leer sein'); return; }
if (newPwdInput.value.length < 8 ||
!/[A-Z]/.test(newPwdInput.value) ||
!/[a-z]/.test(newPwdInput.value) ||
!/[0-9]/.test(newPwdInput.value)) {
alert('Das neue Passwort muss mindestens 8 Zeichen lang sein und Groß-/Kleinbuchstaben sowie Zahlen enthalten');
return;
}
if (!confirm('Möchten Sie das Passwort wirklich aktualisieren?')) {
alert('Das Passwort wurde nicht aktualisiert');
return;
}
newLoginhash = stringToHash(newPwdInput.value);
}
const hiddenDays = [];
if (newMode === 'week') {
document.querySelectorAll('#hiddenDaysCheckboxes input[type="checkbox"]').forEach(cb => {
if (!cb.checked) hiddenDays.push(parseInt(cb.value));
});
}
setHiddenDays(hiddenDays);
setHideEmptyDays(document.getElementById('hideEmptyDays').checked);
setCollapseEmptyHours(document.getElementById('collapseEmptyHours').checked);
const bookingColors = Array.from(document.querySelectorAll('#bookingColors .color-edit')).map(el => ({
name: el.querySelector('input').value,
color: el.querySelector('.color-swatch').style.backgroundColor,
id: el.id
}));
setBookingColors(bookingColors);
setLoginHash(newLoginhash);
const icsAboEnabled = document.getElementById('icsAboCheckbox')?.checked || false;
const configData = {
title: document.querySelector('.calendar-header h2').textContent,
headerColor, secondaryColor,
startHour: document.getElementById('startHour').value,
endHour: document.getElementById('endHour').value,
bookingColors, hiddenDays,
hideEmptyDays: getHideEmptyDays(),
collapseEmptyHours: getCollapseEmptyHours(),
loginhash: newLoginhash,
mode: newMode, eventStartDate, eventDayCount,
icsTokens: getIcsTokens(),
icsPublic: document.getElementById('icsPublicCheckbox')?.checked || false,
icsDayFilter: document.getElementById('icsDayFilterCheckbox')?.checked || false,
icsAboEnabled
};
state.icsPublic = configData.icsPublic;
state.icsDayFilter = configData.icsDayFilter;
state.icsAboEnabled = icsAboEnabled;
await saveToServer('settings.json', configData);
await saveToServer('bookings.json', getBookings());
oldPwdInput.value = ''; newPwdInput.value = '';
createTimeOptions(); buildMobileTiles(); createDayColumns(); closeOptionsModal();
}
function closeOptionsModal() {
document.getElementById('optionsModal').style.display = 'none';
}
// ── ICS Modal ────────────────────────────────────────
function updateIcsTokenDisplay() {
const display = document.getElementById('icsTokenDisplay');
const hint = document.getElementById('icsTokenHint');
if (!display || !hint) return;
const tokens = getIcsTokens();
if (!Array.isArray(tokens) || tokens.length === 0) {
display.textContent = 'Kein Token vorhanden';
hint.textContent = 'Ein Token wird für Kalender-Abonnements benötigt.';
} else {
const active = tokens[tokens.length - 1];
const created = new Date(active.created);
display.textContent = `Aktiver Token: ${active.token}`;
hint.textContent = `Generiert am ${created.toLocaleDateString('de-DE')}`;
}
}
async function generateIcsToken() {
if (!confirm('Neuen Token generieren?')) return;
const token = generateToken();
const tokens = getIcsTokens();
tokens.push({ token, created: new Date().toISOString() });
const configData = buildFullConfigData();
await saveToServer('settings.json', configData);
updateIcsTokenDisplay();