Files
CT/src/View/Components/monthGrid.tsx
T

111 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-07-26 03:14:55 +02:00
import "./monthGrid.css";
import * as React from "bloatless-react";
import { MonthGrid } from "../../Model/Files/calendarModel";
import TaskViewModel from "../../ViewModel/Pages/taskViewModel";
2024-07-26 03:29:07 +02:00
import { translations } from "../translations";
2024-07-26 07:07:07 +02:00
import { allowDrop } from "../utility";
2024-07-26 16:49:13 +02:00
import { localeCompare } from "../../Model/Utility/utility";
2024-07-26 03:14:55 +02:00
2024-07-26 03:38:46 +02:00
export function MonthGrid<T>(
monthGrid: MonthGrid<React.MapState<T>>,
2024-07-26 07:07:07 +02:00
selectedDate: React.State<number>,
handleDrop: (date: string) => void
2024-07-26 03:38:46 +02:00
) {
2024-07-26 03:29:07 +02:00
const dayLabels: HTMLElement[] = [];
let currentWeekday = monthGrid.firstDayOfWeek;
while (dayLabels.length < 7) {
dayLabels.push(
<span class="secondary">
{translations.regional.weekdays.abbreviated[currentWeekday]}
</span>
);
currentWeekday++;
if (currentWeekday == 7) currentWeekday = 0;
}
2024-07-26 03:14:55 +02:00
const offsetElements: HTMLElement[] = [];
for (let i = 0; i < monthGrid.offset; i++) {
offsetElements.push(<div></div>);
}
const converter: React.StateItemConverter<TaskViewModel> = (
taskViewModel
) => {
2024-07-26 06:20:16 +02:00
const view = (
<span class="ellipsis secondary">{taskViewModel.task.name}</span>
);
taskViewModel.index.subscribe((newIndex) => {
view.style.order = newIndex;
});
return view;
2024-07-26 03:14:55 +02:00
};
return (
<div class="month-grid-wrapper">
2024-07-26 03:29:07 +02:00
<div class="day-labels">{...dayLabels}</div>
<div class="month-grid">
{...offsetElements}
2024-07-26 16:49:13 +02:00
{...Object.entries(monthGrid.days)
.sort((a, b) => localeCompare(a[0], b[0]))
.map((entry) => {
const [date, mapState] = entry;
2024-07-26 03:14:55 +02:00
2024-07-26 16:49:13 +02:00
const isSelected = React.createProxyState(
[selectedDate],
() => selectedDate.value == parseInt(date)
);
2024-07-26 03:38:46 +02:00
2024-07-26 16:49:13 +02:00
const isToday: boolean =
monthGrid.isCurrentMonth == true &&
parseInt(date) == new Date().getDate();
2024-07-26 05:32:07 +02:00
2024-08-27 09:58:28 -07:00
const eventCount: React.State<number> = React.createProxyState(
[mapState],
() => mapState.value.size
);
const hasEvents: React.State<boolean> = React.createProxyState(
[eventCount],
() => eventCount.value != 0
);
2024-07-26 16:49:13 +02:00
function select() {
selectedDate.value = parseInt(date);
}
2024-07-26 03:38:46 +02:00
2024-07-26 16:49:13 +02:00
function drop() {
handleDrop(date);
}
2024-07-26 07:07:07 +02:00
2024-07-26 16:49:13 +02:00
return (
<button
class="tile"
on:click={select}
toggle:selected={isSelected}
toggle:today={isToday}
on:dragover={allowDrop}
on:drop={drop}
>
<div>
<b>{date}</b>
2024-08-27 09:58:28 -07:00
<span
class="event-count"
toggle:has-events={hasEvents}
subscribe:innerText={eventCount}
></span>
2024-07-26 16:49:13 +02:00
<div
2024-08-27 09:58:28 -07:00
class="event-list"
2024-07-26 16:49:13 +02:00
children:append={[mapState, converter]}
></div>
</div>
</button>
);
})}
2024-07-26 03:29:07 +02:00
</div>
2024-07-26 03:14:55 +02:00
</div>
);
}