2024-07-31 10:46:31 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2020-04-26 09:40:02 +02:00
|
|
|
import utils from "../../services/utils.js";
|
|
|
|
import dateNoteService from "../../services/date_notes.js";
|
|
|
|
import server from "../../services/server.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../../components/app_context.js";
|
2021-10-06 22:25:33 +02:00
|
|
|
import RightDropdownButtonWidget from "./right_dropdown_button.js";
|
2022-08-24 23:20:05 +02:00
|
|
|
import toastService from "../../services/toast.js";
|
2024-08-31 16:50:23 +03:00
|
|
|
import options from "../../services/options.js";
|
2025-02-21 20:41:00 +01:00
|
|
|
import { Dropdown } from "bootstrap";
|
2025-02-28 14:41:30 +01:00
|
|
|
import type { EventData } from "../../components/app_context.js";
|
2025-03-31 22:26:28 +02:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
import utc from "dayjs/plugin/utc.js";
|
2025-03-28 17:08:34 +01:00
|
|
|
import "../../../stylesheets/calendar.css";
|
2021-10-06 22:25:33 +02:00
|
|
|
|
2025-03-31 22:26:28 +02:00
|
|
|
dayjs.extend(utc);
|
|
|
|
|
2024-08-31 14:42:21 +03:00
|
|
|
const MONTHS = [
|
|
|
|
t("calendar.january"),
|
|
|
|
t("calendar.febuary"),
|
|
|
|
t("calendar.march"),
|
|
|
|
t("calendar.april"),
|
|
|
|
t("calendar.may"),
|
|
|
|
t("calendar.june"),
|
|
|
|
t("calendar.july"),
|
|
|
|
t("calendar.august"),
|
|
|
|
t("calendar.september"),
|
|
|
|
t("calendar.october"),
|
|
|
|
t("calendar.november"),
|
|
|
|
t("calendar.december")
|
|
|
|
];
|
|
|
|
|
2021-10-06 22:25:33 +02:00
|
|
|
const DROPDOWN_TPL = `
|
2021-10-07 21:57:20 +02:00
|
|
|
<div class="calendar-dropdown-widget">
|
2024-08-31 14:32:25 +03:00
|
|
|
<style>
|
|
|
|
.calendar-dropdown-widget {
|
2025-03-31 02:44:30 +02:00
|
|
|
width: 400px;
|
2024-08-31 14:32:25 +03:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<div class="calendar-header">
|
|
|
|
<div class="calendar-month-selector">
|
2025-02-06 05:25:18 +02:00
|
|
|
<button class="calendar-btn tn-tool-button bx bx-chevron-left" data-calendar-toggle="previous"></button>
|
2024-08-31 14:32:25 +03:00
|
|
|
|
2025-02-06 05:20:22 +02:00
|
|
|
<button class="btn dropdown-toggle select-button" type="button"
|
2025-02-28 15:09:28 +01:00
|
|
|
data-bs-toggle="dropdown" data-bs-auto-close="true"
|
2024-12-14 03:50:42 +02:00
|
|
|
aria-expanded="false"
|
|
|
|
data-calendar-input="month"></button>
|
|
|
|
<ul class="dropdown-menu" data-calendar-input="month-list">
|
2025-01-09 18:07:02 +02:00
|
|
|
${Object.entries(MONTHS)
|
2025-03-03 21:02:18 +01:00
|
|
|
.map(([i, month]) => `<li><button class="dropdown-item" data-value=${i}>${month}</button></li>`)
|
|
|
|
.join("")}
|
2024-12-14 03:50:42 +02:00
|
|
|
</ul>
|
2024-08-31 14:32:25 +03:00
|
|
|
|
2025-02-06 05:25:18 +02:00
|
|
|
<button class="calendar-btn tn-tool-button bx bx-chevron-right" data-calendar-toggle="next"></button>
|
2024-08-31 14:32:25 +03:00
|
|
|
</div>
|
2021-10-06 22:25:33 +02:00
|
|
|
|
2024-08-31 14:32:25 +03:00
|
|
|
<div class="calendar-year-selector">
|
2025-02-06 05:25:18 +02:00
|
|
|
<button class="calendar-btn tn-tool-button bx bx-chevron-left" data-calendar-toggle="previousYear"></button>
|
2021-10-06 22:25:33 +02:00
|
|
|
|
2024-08-31 14:49:01 +03:00
|
|
|
<input type="number" min="1900" max="2999" step="1" data-calendar-input="year" />
|
2021-10-06 22:25:33 +02:00
|
|
|
|
2025-02-06 05:25:18 +02:00
|
|
|
<button class="calendar-btn tn-tool-button bx bx-chevron-right" data-calendar-toggle="nextYear"></button>
|
2024-08-31 14:32:25 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-06 22:25:33 +02:00
|
|
|
|
2025-03-31 02:44:30 +02:00
|
|
|
<div class="calendar-week"></div>
|
|
|
|
<div class="calendar-body" data-calendar-area="month"></div>
|
2021-10-06 22:25:33 +02:00
|
|
|
</div>`;
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const DAYS_OF_WEEK = [t("calendar.sun"), t("calendar.mon"), t("calendar.tue"), t("calendar.wed"), t("calendar.thu"), t("calendar.fri"), t("calendar.sat")];
|
2024-08-31 16:50:23 +03:00
|
|
|
|
2025-02-28 14:41:30 +01:00
|
|
|
interface DateNotesForMonth {
|
|
|
|
[date: string]: string;
|
|
|
|
}
|
|
|
|
|
2025-03-31 01:09:57 +02:00
|
|
|
interface WeekCalculationOptions {
|
|
|
|
firstWeekType: number;
|
|
|
|
minDaysInFirstWeek: number;
|
|
|
|
}
|
|
|
|
|
2021-10-07 21:57:20 +02:00
|
|
|
export default class CalendarWidget extends RightDropdownButtonWidget {
|
2025-02-28 14:41:30 +01:00
|
|
|
private $month!: JQuery<HTMLElement>;
|
|
|
|
private $weekHeader!: JQuery<HTMLElement>;
|
|
|
|
private $monthSelect!: JQuery<HTMLElement>;
|
|
|
|
private $yearSelect!: JQuery<HTMLElement>;
|
|
|
|
private $next!: JQuery<HTMLElement>;
|
|
|
|
private $previous!: JQuery<HTMLElement>;
|
|
|
|
private $nextYear!: JQuery<HTMLElement>;
|
|
|
|
private $previousYear!: JQuery<HTMLElement>;
|
|
|
|
private monthDropdown!: Dropdown;
|
|
|
|
private firstDayOfWeek!: number;
|
2025-03-31 01:09:57 +02:00
|
|
|
private weekCalculationOptions!: WeekCalculationOptions;
|
2025-02-28 14:41:30 +01:00
|
|
|
private activeDate: Date | null = null;
|
|
|
|
private todaysDate!: Date;
|
|
|
|
private date!: Date;
|
|
|
|
|
|
|
|
constructor(title: string = "", icon: string = "") {
|
2022-08-05 16:44:26 +02:00
|
|
|
super(title, icon, DROPDOWN_TPL);
|
2021-07-05 14:46:20 +02:00
|
|
|
}
|
2019-09-08 16:06:42 +02:00
|
|
|
|
2021-10-06 22:25:33 +02:00
|
|
|
doRender() {
|
|
|
|
super.doRender();
|
2024-09-12 13:55:07 +02:00
|
|
|
|
2021-10-06 22:25:33 +02:00
|
|
|
this.$month = this.$dropdownContent.find('[data-calendar-area="month"]');
|
2024-08-31 16:50:23 +03:00
|
|
|
this.$weekHeader = this.$dropdownContent.find(".calendar-week");
|
2024-09-12 13:55:07 +02:00
|
|
|
|
2024-08-31 16:50:23 +03:00
|
|
|
this.manageFirstDayOfWeek();
|
2025-03-31 01:09:57 +02:00
|
|
|
this.initWeekCalculation();
|
2024-09-12 13:55:07 +02:00
|
|
|
|
2024-08-31 14:32:25 +03:00
|
|
|
// Month navigation
|
2024-08-31 14:42:21 +03:00
|
|
|
this.$monthSelect = this.$dropdownContent.find('[data-calendar-input="month"]');
|
2024-12-14 03:58:11 +02:00
|
|
|
this.$monthSelect.on("show.bs.dropdown", (e) => {
|
|
|
|
// Don't trigger dropdownShown() at widget level when the month selection dropdown is shown, since it would cause a redundant refresh.
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2025-02-28 14:41:30 +01:00
|
|
|
this.monthDropdown = Dropdown.getOrCreateInstance(this.$monthSelect[0]);
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$dropdownContent.find('[data-calendar-input="month-list"] button').on("click", (e) => {
|
2025-02-28 14:41:30 +01:00
|
|
|
const target = e.target as HTMLElement;
|
|
|
|
const value = target.dataset.value;
|
|
|
|
if (value) {
|
2025-03-31 22:26:28 +02:00
|
|
|
this.date = dayjs(this.date).month(parseInt(value)).toDate();
|
2025-02-28 14:41:30 +01:00
|
|
|
this.createMonth();
|
|
|
|
}
|
2024-08-31 14:42:21 +03:00
|
|
|
});
|
2024-08-31 14:49:01 +03:00
|
|
|
this.$next = this.$dropdownContent.find('[data-calendar-toggle="next"]');
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$next.on("click", () => {
|
2025-03-31 22:26:28 +02:00
|
|
|
this.date = dayjs(this.date).add(1, 'month').toDate();
|
2019-09-08 16:06:42 +02:00
|
|
|
this.createMonth();
|
|
|
|
});
|
2024-09-12 13:55:07 +02:00
|
|
|
this.$previous = this.$dropdownContent.find('[data-calendar-toggle="previous"]');
|
2025-02-28 14:41:30 +01:00
|
|
|
this.$previous.on("click", () => {
|
2025-03-31 22:26:28 +02:00
|
|
|
this.date = dayjs(this.date).subtract(1, 'month').toDate();
|
2019-09-08 16:06:42 +02:00
|
|
|
this.createMonth();
|
|
|
|
});
|
|
|
|
|
2024-08-31 14:32:25 +03:00
|
|
|
// Year navigation
|
2024-08-31 14:49:01 +03:00
|
|
|
this.$yearSelect = this.$dropdownContent.find('[data-calendar-input="year"]');
|
|
|
|
this.$yearSelect.on("input", (e) => {
|
2025-02-28 14:41:30 +01:00
|
|
|
const target = e.target as HTMLInputElement;
|
2025-03-31 22:26:28 +02:00
|
|
|
this.date = dayjs(this.date).year(parseInt(target.value)).toDate();
|
2024-08-31 14:49:01 +03:00
|
|
|
this.createMonth();
|
|
|
|
});
|
2024-08-31 14:32:25 +03:00
|
|
|
this.$nextYear = this.$dropdownContent.find('[data-calendar-toggle="nextYear"]');
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$nextYear.on("click", () => {
|
2025-03-31 22:26:28 +02:00
|
|
|
this.date = dayjs(this.date).add(1, 'year').toDate();
|
2024-08-31 14:32:25 +03:00
|
|
|
this.createMonth();
|
|
|
|
});
|
2024-09-12 13:55:07 +02:00
|
|
|
this.$previousYear = this.$dropdownContent.find('[data-calendar-toggle="previousYear"]');
|
2025-02-28 14:41:30 +01:00
|
|
|
this.$previousYear.on("click", () => {
|
2025-03-31 22:26:28 +02:00
|
|
|
this.date = dayjs(this.date).subtract(1, 'year').toDate();
|
2024-08-31 14:32:25 +03:00
|
|
|
this.createMonth();
|
|
|
|
});
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$dropdownContent.on("click", ".calendar-date", async (ev) => {
|
|
|
|
const date = $(ev.target).closest(".calendar-date").attr("data-calendar-date");
|
2019-09-08 16:06:42 +02:00
|
|
|
|
2025-02-28 14:41:30 +01:00
|
|
|
if (date) {
|
|
|
|
const note = await dateNoteService.getDayNote(date);
|
2019-09-08 16:06:42 +02:00
|
|
|
|
2025-02-28 14:41:30 +01:00
|
|
|
if (note) {
|
2025-03-03 21:02:18 +01:00
|
|
|
appContext.tabManager.getActiveContext()?.setNote(note.noteId);
|
2025-02-28 14:41:30 +01:00
|
|
|
this.dropdown?.hide();
|
|
|
|
} else {
|
|
|
|
toastService.showError(t("calendar.cannot_find_day_note"));
|
|
|
|
}
|
2019-09-08 16:06:42 +02:00
|
|
|
}
|
2024-09-12 13:55:07 +02:00
|
|
|
|
2024-09-12 19:37:15 +03:00
|
|
|
ev.stopPropagation();
|
2025-01-09 18:07:02 +02:00
|
|
|
});
|
|
|
|
|
2025-02-28 15:09:28 +01:00
|
|
|
// Handle click events for the entire calendar widget
|
|
|
|
this.$dropdownContent.on("click", (e) => {
|
|
|
|
const $target = $(e.target);
|
|
|
|
|
|
|
|
// Keep dropdown open when clicking on month select button or year selector area
|
|
|
|
if ($target.closest('.btn.dropdown-toggle.select-button').length ||
|
|
|
|
$target.closest('.calendar-year-selector').length) {
|
|
|
|
e.stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide dropdown for all other cases
|
|
|
|
this.monthDropdown.hide();
|
|
|
|
// Prevent dismissing the calendar popup by clicking on an empty space inside it.
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2024-08-31 16:50:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
manageFirstDayOfWeek() {
|
2025-02-28 14:41:30 +01:00
|
|
|
this.firstDayOfWeek = options.getInt("firstDayOfWeek") || 0;
|
2024-08-31 16:50:23 +03:00
|
|
|
|
|
|
|
// Generate the list of days of the week taking into consideration the user's selected first day of week.
|
2024-09-12 13:55:07 +02:00
|
|
|
let localeDaysOfWeek = [...DAYS_OF_WEEK];
|
2024-08-31 16:50:23 +03:00
|
|
|
const daysToBeAddedAtEnd = localeDaysOfWeek.splice(0, this.firstDayOfWeek);
|
2025-03-31 02:44:30 +02:00
|
|
|
localeDaysOfWeek = ['', ...localeDaysOfWeek, ...daysToBeAddedAtEnd];
|
2025-02-28 14:41:30 +01:00
|
|
|
this.$weekHeader.html(localeDaysOfWeek.map((el) => `<span>${el}</span>`).join(''));
|
2021-10-06 22:25:33 +02:00
|
|
|
}
|
2019-09-08 16:06:42 +02:00
|
|
|
|
2025-03-31 01:09:57 +02:00
|
|
|
initWeekCalculation() {
|
|
|
|
this.weekCalculationOptions = {
|
|
|
|
firstWeekType: options.getInt("firstWeekOfYear") || 0,
|
|
|
|
minDaysInFirstWeek: options.getInt("minDaysInFirstWeek") || 4
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getWeekNumber(date: Date): number {
|
2025-03-31 23:24:11 +02:00
|
|
|
const year = dayjs(date).year();
|
|
|
|
const jan1 = dayjs().year(year).month(0).date(1);
|
|
|
|
const jan1Day = jan1.day();
|
2025-03-31 01:09:57 +02:00
|
|
|
|
2025-03-31 23:24:11 +02:00
|
|
|
let firstWeekStart = jan1.clone();
|
2025-03-31 01:09:57 +02:00
|
|
|
|
2025-03-31 23:24:11 +02:00
|
|
|
let dayOffset;
|
|
|
|
if (jan1Day < this.firstDayOfWeek) {
|
|
|
|
dayOffset = jan1Day + (7 - this.firstDayOfWeek);
|
|
|
|
} else {
|
|
|
|
dayOffset = jan1Day - this.firstDayOfWeek;
|
|
|
|
}
|
|
|
|
firstWeekStart = firstWeekStart.subtract(dayOffset, 'day');
|
2025-03-31 01:09:57 +02:00
|
|
|
|
|
|
|
switch (this.weekCalculationOptions.firstWeekType) {
|
2025-03-31 23:24:11 +02:00
|
|
|
// case 0 is default: week containing Jan 1
|
2025-03-31 01:09:57 +02:00
|
|
|
case 1: {
|
2025-03-31 23:24:11 +02:00
|
|
|
let thursday = firstWeekStart.clone();
|
|
|
|
const day = thursday.day();
|
2025-03-31 01:09:57 +02:00
|
|
|
const offset = (4 - day + 7) % 7;
|
2025-03-31 23:24:11 +02:00
|
|
|
thursday = thursday.add(offset, 'day');
|
|
|
|
if (thursday.year() < year) {
|
|
|
|
firstWeekStart = firstWeekStart.add(7, 'day');
|
2025-03-31 01:09:57 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
const daysInFirstWeek = 7 - dayOffset;
|
|
|
|
if (daysInFirstWeek < this.weekCalculationOptions.minDaysInFirstWeek) {
|
2025-03-31 23:24:11 +02:00
|
|
|
firstWeekStart = firstWeekStart.add(7, 'day');
|
2025-03-31 01:09:57 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-31 23:24:11 +02:00
|
|
|
const diffDays = dayjs(date).diff(firstWeekStart, 'day') + 1;
|
|
|
|
const weekNumber = Math.floor(diffDays / 7) + 1;
|
|
|
|
|
|
|
|
// Check if the week number is less than 0, which means the date is in the previous year
|
|
|
|
if (weekNumber <= 0) {
|
|
|
|
const prevYearLastWeek = this.getWeekNumber(dayjs(date).subtract(1, 'year').endOf('year').toDate());
|
|
|
|
return prevYearLastWeek;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it's the last week of December
|
|
|
|
if (dayjs(date).month() === 11) { // December
|
|
|
|
const lastDayOfYear = dayjs().year(year).month(11).date(31);
|
|
|
|
const lastWeekStart = lastDayOfYear.subtract((lastDayOfYear.day() - this.firstDayOfWeek + 7) % 7, 'day');
|
|
|
|
|
|
|
|
if (this.isEqual(date, lastWeekStart.toDate())) {
|
|
|
|
const nextYearFirstWeek = this.getWeekNumber(lastDayOfYear.add(1, 'day').toDate());
|
|
|
|
return nextYearFirstWeek;
|
|
|
|
}
|
|
|
|
}
|
2025-03-31 01:09:57 +02:00
|
|
|
|
2025-03-31 23:24:11 +02:00
|
|
|
return weekNumber;
|
2025-03-31 01:09:57 +02:00
|
|
|
}
|
|
|
|
|
2021-10-07 21:57:20 +02:00
|
|
|
async dropdownShown() {
|
2025-03-03 21:02:18 +01:00
|
|
|
this.init(appContext.tabManager.getActiveContextNote()?.getOwnedLabelValue("dateNote") ?? null);
|
2020-02-02 20:02:08 +01:00
|
|
|
}
|
|
|
|
|
2025-02-28 14:41:30 +01:00
|
|
|
init(activeDate: string | null) {
|
2021-07-05 15:50:21 +02:00
|
|
|
// attaching time fixes local timezone handling
|
2025-03-31 22:26:28 +02:00
|
|
|
this.activeDate = activeDate ? dayjs(`${activeDate}T12:00:00`).toDate() : null;
|
|
|
|
this.todaysDate = dayjs().toDate();
|
|
|
|
this.date = dayjs(this.activeDate || this.todaysDate).startOf('month').toDate();
|
2020-02-02 20:02:08 +01:00
|
|
|
|
|
|
|
this.createMonth();
|
|
|
|
}
|
|
|
|
|
2025-03-31 19:40:50 +02:00
|
|
|
createDay(dateNotesForMonth: DateNotesForMonth, num: number) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const $newDay = $("<a>").addClass("calendar-date").attr("data-calendar-date", utils.formatDateISO(this.date));
|
2025-02-28 14:41:30 +01:00
|
|
|
const $date = $("<span>").html(String(num));
|
2019-09-08 16:06:42 +02:00
|
|
|
|
2019-09-08 16:30:33 +02:00
|
|
|
const dateNoteId = dateNotesForMonth[utils.formatDateISO(this.date)];
|
|
|
|
|
|
|
|
if (dateNoteId) {
|
2025-01-09 18:07:02 +02:00
|
|
|
$newDay.addClass("calendar-date-exists");
|
2024-09-12 20:53:31 +03:00
|
|
|
$newDay.attr("data-href", `#root/${dateNoteId}`);
|
2019-09-08 16:30:33 +02:00
|
|
|
}
|
|
|
|
|
2019-09-08 16:06:42 +02:00
|
|
|
if (this.isEqual(this.date, this.activeDate)) {
|
2025-01-09 18:07:02 +02:00
|
|
|
$newDay.addClass("calendar-date-active");
|
2019-09-08 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isEqual(this.date, this.todaysDate)) {
|
2025-01-09 18:07:02 +02:00
|
|
|
$newDay.addClass("calendar-date-today");
|
2019-09-08 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$newDay.append($date);
|
2020-02-02 20:02:08 +01:00
|
|
|
return $newDay;
|
2019-09-08 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2025-03-31 02:47:16 +02:00
|
|
|
createWeekNumber(weekNumber: number) {
|
2025-03-31 18:53:17 +02:00
|
|
|
const weekNumberText = String(weekNumber);
|
|
|
|
const $newWeekNumber = $("<a>").addClass("calendar-date calendar-week-number").attr("data-calendar-week-number", 'W' + weekNumberText.padStart(2, '0'));
|
2025-03-31 02:47:16 +02:00
|
|
|
const $weekNumber = $("<span>").html(weekNumberText);
|
|
|
|
|
|
|
|
$newWeekNumber.append($weekNumber);
|
|
|
|
return $newWeekNumber;
|
|
|
|
}
|
|
|
|
|
2025-02-28 14:41:30 +01:00
|
|
|
isEqual(a: Date, b: Date | null) {
|
2025-01-09 18:07:02 +02:00
|
|
|
if ((!a && b) || (a && !b)) {
|
2021-07-05 15:50:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-02-28 14:41:30 +01:00
|
|
|
if (!b) return false;
|
|
|
|
|
2025-03-31 23:24:11 +02:00
|
|
|
return dayjs(a).isSame(dayjs(b), 'day');
|
2019-09-08 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2025-03-31 19:19:59 +02:00
|
|
|
private getPrevMonthDays(firstDayOfWeek: number): { weekNumber: number, dates: Date[] } {
|
|
|
|
const prevMonthLastDay = new Date(this.date.getFullYear(), this.date.getMonth(), 0);
|
|
|
|
const daysToAdd = (firstDayOfWeek - this.firstDayOfWeek + 7) % 7;
|
|
|
|
const dates = [];
|
|
|
|
|
|
|
|
const firstDay = new Date(this.date.getFullYear(), this.date.getMonth(), 1);
|
|
|
|
const weekNumber = this.getWeekNumber(firstDay);
|
|
|
|
|
|
|
|
// Get dates from previous month
|
|
|
|
for (let i = daysToAdd - 1; i >= 0; i--) {
|
|
|
|
dates.push(new Date(prevMonthLastDay.getFullYear(), prevMonthLastDay.getMonth(), prevMonthLastDay.getDate() - i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return { weekNumber, dates };
|
|
|
|
}
|
|
|
|
|
|
|
|
private getNextMonthDays(lastDayOfWeek: number): Date[] {
|
|
|
|
const nextMonthFirstDay = new Date(this.date.getFullYear(), this.date.getMonth() + 1, 1);
|
2025-03-31 19:30:10 +02:00
|
|
|
const dates = [];
|
|
|
|
|
2025-03-31 19:36:57 +02:00
|
|
|
const lastDayOfUserWeek = (this.firstDayOfWeek + 6) % 7;
|
|
|
|
const daysToAdd = (lastDayOfUserWeek - lastDayOfWeek + 7) % 7;
|
|
|
|
|
2025-03-31 19:30:10 +02:00
|
|
|
// Get dates from next month
|
2025-03-31 19:36:57 +02:00
|
|
|
for (let i = 0; i < daysToAdd; i++) {
|
2025-03-31 19:30:10 +02:00
|
|
|
dates.push(new Date(nextMonthFirstDay.getFullYear(), nextMonthFirstDay.getMonth(), i + 1));
|
2025-03-31 19:19:59 +02:00
|
|
|
}
|
2025-03-31 19:30:10 +02:00
|
|
|
|
|
|
|
return dates;
|
2025-03-31 19:19:59 +02:00
|
|
|
}
|
|
|
|
|
2019-09-08 16:30:33 +02:00
|
|
|
async createMonth() {
|
2025-03-31 22:26:28 +02:00
|
|
|
const month = dayjs(this.date).format('YYYY-MM');
|
2025-02-28 14:41:30 +01:00
|
|
|
const dateNotesForMonth: DateNotesForMonth = await server.get(`special-notes/notes-for-month/${month}`);
|
2019-09-08 16:30:33 +02:00
|
|
|
|
2020-02-02 20:02:08 +01:00
|
|
|
this.$month.empty();
|
|
|
|
|
2025-03-31 22:26:28 +02:00
|
|
|
const firstDay = dayjs(this.date).startOf('month');
|
|
|
|
const firstDayOfWeek = firstDay.day();
|
2025-03-31 19:19:59 +02:00
|
|
|
|
|
|
|
// Add dates from previous month
|
|
|
|
if (firstDayOfWeek !== this.firstDayOfWeek) {
|
|
|
|
const { weekNumber, dates } = this.getPrevMonthDays(firstDayOfWeek);
|
|
|
|
|
2025-03-31 22:26:28 +02:00
|
|
|
const prevMonth = dayjs(this.date).subtract(1, 'month').format('YYYY-MM');
|
|
|
|
const dateNotesForPrevMonth: DateNotesForMonth = await server.get(`special-notes/notes-for-month/${prevMonth}`);
|
2025-03-31 19:19:59 +02:00
|
|
|
|
|
|
|
const $weekNumber = this.createWeekNumber(weekNumber);
|
|
|
|
this.$month.append($weekNumber);
|
|
|
|
|
|
|
|
dates.forEach(date => {
|
2025-03-31 19:40:10 +02:00
|
|
|
const tempDate = this.date;
|
|
|
|
this.date = date;
|
2025-03-31 22:26:28 +02:00
|
|
|
const $day = this.createDay(dateNotesForPrevMonth, dayjs(date).date());
|
2025-03-31 19:19:59 +02:00
|
|
|
$day.addClass('calendar-date-prev-month');
|
|
|
|
this.$month.append($day);
|
2025-03-31 19:40:10 +02:00
|
|
|
this.date = tempDate;
|
2025-03-31 19:19:59 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-08 16:06:42 +02:00
|
|
|
const currentMonth = this.date.getMonth();
|
2025-03-31 01:09:57 +02:00
|
|
|
|
2019-09-08 16:06:42 +02:00
|
|
|
while (this.date.getMonth() === currentMonth) {
|
2025-03-31 22:26:28 +02:00
|
|
|
// Using UTC to avoid issues with summer/winter time
|
|
|
|
const weekNumber = this.getWeekNumber(dayjs(this.date).utc().toDate());
|
2025-03-31 02:44:30 +02:00
|
|
|
|
2025-03-31 19:30:10 +02:00
|
|
|
// Add week number if it's first day of week
|
2025-03-31 19:19:59 +02:00
|
|
|
if (this.date.getDay() === this.firstDayOfWeek) {
|
2025-03-31 02:47:16 +02:00
|
|
|
const $weekNumber = this.createWeekNumber(weekNumber);
|
2025-03-31 02:44:30 +02:00
|
|
|
this.$month.append($weekNumber);
|
|
|
|
}
|
2025-03-31 01:09:57 +02:00
|
|
|
|
2025-03-31 19:40:50 +02:00
|
|
|
const $day = this.createDay(dateNotesForMonth, this.date.getDate());
|
2020-02-02 20:02:08 +01:00
|
|
|
this.$month.append($day);
|
|
|
|
|
2025-03-31 22:26:28 +02:00
|
|
|
this.date = dayjs(this.date).add(1, 'day').toDate();
|
2019-09-08 16:06:42 +02:00
|
|
|
}
|
2025-03-31 22:26:28 +02:00
|
|
|
// while loop trips over and day is at 30/31, bring it back
|
|
|
|
this.date = dayjs(this.date).startOf('month').subtract(1, 'month').toDate();
|
2025-03-31 19:30:10 +02:00
|
|
|
|
|
|
|
// Add dates from next month
|
2025-03-31 22:26:28 +02:00
|
|
|
const lastDayOfMonth = dayjs(this.date).endOf('month').toDate();
|
|
|
|
const lastDayOfWeek = lastDayOfMonth.getDay();
|
2025-03-31 19:36:57 +02:00
|
|
|
const lastDayOfUserWeek = (this.firstDayOfWeek + 6) % 7;
|
|
|
|
if (lastDayOfWeek !== lastDayOfUserWeek) {
|
2025-03-31 19:30:10 +02:00
|
|
|
const dates = this.getNextMonthDays(lastDayOfWeek);
|
|
|
|
|
2025-03-31 22:26:28 +02:00
|
|
|
const nextMonth = dayjs(this.date).add(1, 'month').format('YYYY-MM');
|
|
|
|
const dateNotesForNextMonth: DateNotesForMonth = await server.get(`special-notes/notes-for-month/${nextMonth}`);
|
2025-03-31 19:30:10 +02:00
|
|
|
|
|
|
|
dates.forEach(date => {
|
2025-03-31 19:40:10 +02:00
|
|
|
const tempDate = this.date;
|
|
|
|
this.date = date;
|
2025-03-31 22:26:28 +02:00
|
|
|
const $day = this.createDay(dateNotesForNextMonth, dayjs(date).date());
|
2025-03-31 19:30:10 +02:00
|
|
|
$day.addClass('calendar-date-next-month');
|
|
|
|
this.$month.append($day);
|
2025-03-31 19:40:10 +02:00
|
|
|
this.date = tempDate;
|
2025-03-31 19:30:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-14 03:50:42 +02:00
|
|
|
this.$monthSelect.text(MONTHS[this.date.getMonth()]);
|
2024-08-31 14:49:01 +03:00
|
|
|
this.$yearSelect.val(this.date.getFullYear());
|
2019-09-08 16:06:42 +02:00
|
|
|
}
|
2024-08-31 16:24:09 +03:00
|
|
|
|
2025-02-28 14:41:30 +01:00
|
|
|
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
|
2025-03-31 01:09:57 +02:00
|
|
|
if (!loadResults.getOptionNames().includes("firstDayOfWeek") &&
|
|
|
|
!loadResults.getOptionNames().includes("firstWeekOfYear") &&
|
|
|
|
!loadResults.getOptionNames().includes("minDaysInFirstWeek")) {
|
2024-08-31 17:08:55 +03:00
|
|
|
return;
|
2024-08-31 16:24:09 +03:00
|
|
|
}
|
2024-09-12 13:55:07 +02:00
|
|
|
|
2024-08-31 17:08:55 +03:00
|
|
|
this.manageFirstDayOfWeek();
|
2025-03-31 01:09:57 +02:00
|
|
|
this.initWeekCalculation();
|
2024-08-31 17:08:55 +03:00
|
|
|
this.createMonth();
|
2024-08-31 16:24:09 +03:00
|
|
|
}
|
2020-08-12 00:02:19 +02:00
|
|
|
}
|