Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override Views, HeaderToolbar, and ValidRange #466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 50 additions & 31 deletions src/ui/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EventClickArg,
EventHoveringArg,
EventSourceInput,
ToolbarInput,
} from "@fullcalendar/core";
import dayGridPlugin from "@fullcalendar/daygrid";
import timeGridPlugin from "@fullcalendar/timegrid";
Expand Down Expand Up @@ -54,6 +55,12 @@ interface ExtraRenderProps {
) => Promise<void>;
toggleTask?: (event: EventApi, isComplete: boolean) => Promise<boolean>;
forceNarrow?: boolean;
validRange?: { start: string; end: string };
headerToolbar?: false | ToolbarInput;
views?: {
timeGridDay: { left: string; right: string; center: string };
timeGrid3Days: { left: string; right: string; center: string };
};
}

export function renderCalendar(
Expand All @@ -71,6 +78,7 @@ export function renderCalendar(
openContextMenuForEvent,
toggleTask,
} = settings || {};

const modifyEventCallback =
modifyEvent &&
(async ({
Expand All @@ -88,6 +96,45 @@ export function renderCalendar(
}
});

const buildHeaderToolbar = (settings?: ExtraRenderProps) => {
if (settings?.headerToolbar) {
return settings?.headerToolbar;
}
if (isNarrow) {
return {
left: "prev,next today",
center: "title",
right: "dayGridMonth,timeGridWeek,timeGridDay,listWeek",
};
}
if (isMobile) {
return {
left: "prev,next today",
center: "title",
right: "dayGridMonth,timeGridWeek,timeGridDay,listWeek",
};
}
return false;
};

const buildViews = (settings?: ExtraRenderProps) => {
if (settings?.views) {
return settings?.views;
}
return {
timeGridDay: {
type: "timeGrid",
duration: { days: 1 },
buttonText: isNarrow ? "1" : "day",
},
timeGrid3Days: {
type: "timeGrid",
duration: { days: 3 },
buttonText: "3",
},
};
};

const cal = new Calendar(containerEl, {
plugins: [
// View plugins
Expand All @@ -105,41 +152,13 @@ export function renderCalendar(
initialView:
settings?.initialView?.[isNarrow ? "mobile" : "desktop"] ||
(isNarrow ? "timeGrid3Days" : "timeGridWeek"),
validRange: settings?.validRange,
nowIndicator: true,
scrollTimeReset: false,
dayMaxEvents: true,

headerToolbar: !isNarrow
? {
left: "prev,next today",
center: "title",
right: "dayGridMonth,timeGridWeek,timeGridDay,listWeek",
}
: !isMobile
? {
right: "today,prev,next",
left: "timeGrid3Days,timeGridDay,listWeek",
}
: false,
footerToolbar: isMobile
? {
right: "today,prev,next",
left: "timeGrid3Days,timeGridDay,listWeek",
}
: false,

views: {
timeGridDay: {
type: "timeGrid",
duration: { days: 1 },
buttonText: isNarrow ? "1" : "day",
},
timeGrid3Days: {
type: "timeGrid",
duration: { days: 3 },
buttonText: "3",
},
},
headerToolbar: buildHeaderToolbar(settings),
views: buildViews(settings),
firstDay: settings?.firstDay,
...(settings?.timeFormat24h && {
eventTimeFormat: {
Expand Down