Files
CT/src/Views/Objects/calendarView.tsx
T

122 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-07-18 14:54:11 +02:00
import * as React from "bloatless-react";
import { Chat, MessageObject } from "../../Model/chatModel";
2024-07-18 17:42:41 +02:00
import { DayView } from "./dayView";
2024-07-18 22:35:25 +02:00
import { MiniatureDayView } from "./miniatureDayView";
2024-07-18 17:42:41 +02:00
import { dayInCalendar } from "../../Model/model";
2024-07-18 23:25:37 +02:00
import { translation } from "../../translations";
2024-07-18 14:54:11 +02:00
export function CalendarView(
chat: Chat,
messageObjects: React.MapState<MessageObject>,
selectedObject: React.State<MessageObject | undefined>,
isShowingObjectModal: React.State<boolean>
) {
2024-07-19 00:33:55 +02:00
let selectedDate = new Date(dayInCalendar.value);
const selectedMonth = new React.State(selectedDate.getUTCMonth() + 1);
const selectedYear = new React.State(selectedDate.getUTCFullYear());
React.bulkSubscribe([selectedMonth, selectedYear], () =>
updateSelectedDate()
);
dayInCalendar.subscribeSilent(() => {
selectedDate = new Date(dayInCalendar.value);
})
2024-07-18 23:36:47 +02:00
updateSelectedDate();
2024-07-18 23:25:37 +02:00
2024-07-18 23:36:47 +02:00
function updateSelectedDate() {
2024-07-19 00:33:55 +02:00
selectedDate.setUTCMonth(selectedMonth.value - 1);
selectedDate.setUTCFullYear(selectedYear.value);
2024-07-18 23:25:37 +02:00
dayInCalendar.value = selectedDate.toISOString().split("T")[0];
2024-07-18 23:36:47 +02:00
}
2024-07-18 23:25:37 +02:00
function previousMonth() {
if (selectedMonth.value <= 1) {
selectedMonth.value = 12;
selectedYear.value -= 1;
return;
}
selectedMonth.value -= 1;
}
function nextMonth() {
if (selectedMonth.value >= 12) {
selectedMonth.value = 1;
selectedYear.value += 1;
return;
}
selectedMonth.value += 1;
}
2024-07-18 22:35:25 +02:00
2024-07-18 23:25:37 +02:00
let monthGridCells = new React.State<HTMLElement[]>([]);
2024-07-18 22:35:25 +02:00
dayInCalendar.subscribe((newValue) => {
2024-07-18 23:25:37 +02:00
const newSelectedDate = new Date(newValue);
2024-07-18 22:35:25 +02:00
const currentDate = new Date();
monthGridCells.value = [];
// initialize to first day of month
2024-07-19 00:33:55 +02:00
currentDate.setUTCDate(1);
currentDate.setUTCMonth(newSelectedDate.getMonth());
2024-07-18 23:25:37 +02:00
// labels
for (let i = 0; i < 7; i++) {
monthGridCells.value.push(
<b class="secondary ellipsis width-100">{translation.weekdays[i]}</b>
);
}
2024-07-18 22:35:25 +02:00
// create blank dates starting sunday
2024-07-19 13:11:56 +02:00
const monthOffset = currentDate.getDay();
2024-07-18 22:35:25 +02:00
for (let i = 0; i < monthOffset; i++) {
2024-07-18 23:25:37 +02:00
monthGridCells.value.push(<div></div>);
2024-07-18 22:35:25 +02:00
}
// add day views
2024-07-19 00:33:55 +02:00
while (currentDate.getUTCMonth() == newSelectedDate.getUTCMonth()) {
2024-07-18 22:35:25 +02:00
monthGridCells.value.push(
2024-07-19 00:33:55 +02:00
MiniatureDayView(chat, messageObjects, currentDate)
2024-07-18 22:35:25 +02:00
);
currentDate.setDate(currentDate.getDate() + 1);
}
2024-07-18 23:25:37 +02:00
monthGridCells.callSubscriptions();
2024-07-18 22:35:25 +02:00
});
return (
<div class="calendar-wrapper">
<div class="month-grid padding scroll-v">
2024-07-18 23:25:37 +02:00
<div class="flex-row justify-center gap">
<button
on:click={previousMonth}
aria-label={translation.previousMonth}
>
<span class="icon">arrow_back</span>
</button>
<input
style="width: 90px"
type="number"
bind:value={selectedMonth}
></input>
<input
style="width: 110px"
type="number"
bind:value={selectedYear}
></input>
<button on:click={nextMonth} aria-label={translation.nextMonth}>
<span class="icon">arrow_forward</span>
</button>
</div>
<hr></hr>
2024-07-18 22:35:25 +02:00
<div
class="grid gap"
style="grid-template-columns: repeat(7, 1fr)"
children:set={monthGridCells}
></div>
2024-07-18 17:42:41 +02:00
</div>
{DayView(chat, messageObjects, selectedObject, isShowingObjectModal)}
</div>
2024-07-18 22:35:25 +02:00
);
2024-07-18 14:54:11 +02:00
}