244 lines
8.9 KiB
JavaScript
Raw Normal View History

2024-07-31 10:46:31 +08:00
import { t } from "../../services/i18n.js";
2020-04-26 09:40:02 +02:00
import libraryLoader from "../../services/library_loader.js";
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";
import RightDropdownButtonWidget from "./right_dropdown_button.js";
2022-08-24 23:20:05 +02:00
import toastService from "../../services/toast.js";
import options from "../../services/options.js";
import { Dropdown } from "bootstrap";
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")
];
const DROPDOWN_TPL = `
2021-10-07 21:57:20 +02:00
<div class="calendar-dropdown-widget">
<style>
.calendar-dropdown-widget {
width: 350px;
}
</style>
<div class="calendar-header">
<div class="calendar-month-selector">
<button class="calendar-btn tn-tool-button bx bx-chevron-left" data-calendar-toggle="previous"></button>
<button class="btn dropdown-toggle select-button" type="button"
data-bs-toggle="dropdown" data-bs-auto-close="true"
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)
.map(([i, month]) => `<li><button class="dropdown-item" data-value=${i}>${month}</button></li>`)
.join("")}
</ul>
<button class="calendar-btn tn-tool-button bx bx-chevron-right" data-calendar-toggle="next"></button>
</div>
<div class="calendar-year-selector">
<button class="calendar-btn tn-tool-button bx bx-chevron-left" data-calendar-toggle="previousYear"></button>
<input type="number" min="1900" max="2999" step="1" data-calendar-input="year" />
<button class="calendar-btn tn-tool-button bx bx-chevron-right" data-calendar-toggle="nextYear"></button>
</div>
</div>
<div class="calendar-week">
</div>
<div class="calendar-body" data-calendar-area="month"></div>
</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")];
2021-10-07 21:57:20 +02:00
export default class CalendarWidget extends RightDropdownButtonWidget {
2022-08-05 16:44:26 +02:00
constructor(title, icon) {
super(title, icon, DROPDOWN_TPL);
2021-07-05 14:46:20 +02:00
}
2019-09-08 16:06:42 +02:00
doRender() {
super.doRender();
2024-09-12 13:55:07 +02:00
this.$month = this.$dropdownContent.find('[data-calendar-area="month"]');
this.$weekHeader = this.$dropdownContent.find(".calendar-week");
2024-09-12 13:55:07 +02:00
this.manageFirstDayOfWeek();
2024-09-12 13:55:07 +02:00
// Month navigation
this.$monthSelect = this.$dropdownContent.find('[data-calendar-input="month"]');
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();
});
this.monthDropdown = Dropdown.getOrCreateInstance(this.$monthSelect);
2025-01-09 18:07:02 +02:00
this.$dropdownContent.find('[data-calendar-input="month-list"] button').on("click", (e) => {
this.date.setMonth(e.target.dataset.value);
this.createMonth();
this.monthDropdown.hide();
});
this.$next = this.$dropdownContent.find('[data-calendar-toggle="next"]');
2025-01-09 18:07:02 +02:00
this.$next.on("click", () => {
2024-09-12 13:55:07 +02:00
this.date.setMonth(this.date.getMonth() + 1);
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-01-09 18:07:02 +02:00
this.$previous.on("click", (e) => {
2019-09-08 16:06:42 +02:00
this.date.setMonth(this.date.getMonth() - 1);
this.createMonth();
});
// Year navigation
this.$yearSelect = this.$dropdownContent.find('[data-calendar-input="year"]');
this.$yearSelect.on("input", (e) => {
this.date.setFullYear(e.target.value);
this.createMonth();
});
this.$nextYear = this.$dropdownContent.find('[data-calendar-toggle="nextYear"]');
2025-01-09 18:07:02 +02:00
this.$nextYear.on("click", () => {
this.date.setFullYear(this.date.getFullYear() + 1);
this.createMonth();
});
2024-09-12 13:55:07 +02:00
this.$previousYear = this.$dropdownContent.find('[data-calendar-toggle="previousYear"]');
2025-01-09 18:07:02 +02:00
this.$previousYear.on("click", (e) => {
this.date.setFullYear(this.date.getFullYear() - 1);
this.createMonth();
});
2025-01-09 18:07:02 +02:00
this.$dropdownContent.find(".calendar-header").on("click", (e) => e.stopPropagation());
2021-07-05 14:46:20 +02:00
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
2022-01-10 17:09:20 +01:00
const note = await dateNoteService.getDayNote(date);
2019-09-08 16:06:42 +02:00
if (note) {
2021-05-22 12:35:41 +02:00
appContext.tabManager.getActiveContext().setNote(note.noteId);
2024-09-12 13:55:07 +02:00
this.dropdown.hide();
2025-01-09 18:07:02 +02:00
} else {
2024-07-31 10:46:31 +08:00
toastService.showError(t("calendar.cannot_find_day_note"));
2019-09-08 16:06:42 +02:00
}
2024-09-12 13:55:07 +02:00
ev.stopPropagation();
2025-01-09 18:07:02 +02:00
});
// Prevent dismissing the calendar popup by clicking on an empty space inside it.
this.$dropdownContent.on("click", (e) => e.stopPropagation());
}
manageFirstDayOfWeek() {
this.firstDayOfWeek = options.getInt("firstDayOfWeek");
// 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];
const daysToBeAddedAtEnd = localeDaysOfWeek.splice(0, this.firstDayOfWeek);
2024-09-12 13:55:07 +02:00
localeDaysOfWeek = [...localeDaysOfWeek, ...daysToBeAddedAtEnd];
this.$weekHeader.html(localeDaysOfWeek.map((el) => `<span>${el}</span>`));
}
2019-09-08 16:06:42 +02:00
2021-10-07 21:57:20 +02:00
async dropdownShown() {
await libraryLoader.requireLibrary(libraryLoader.CALENDAR_WIDGET);
2021-07-05 14:46:20 +02:00
const activeNote = appContext.tabManager.getActiveContextNote();
2021-07-05 14:46:20 +02:00
this.init(activeNote?.getOwnedLabelValue("dateNote"));
2020-02-02 20:02:08 +01:00
}
init(activeDate) {
// attaching time fixes local timezone handling
this.activeDate = activeDate ? new Date(`${activeDate}T12:00:00`) : null;
2020-02-02 20:02:08 +01:00
this.todaysDate = new Date();
this.date = new Date((this.activeDate || this.todaysDate).getTime());
2020-02-02 20:02:08 +01:00
this.date.setDate(1);
this.createMonth();
}
createDay(dateNotesForMonth, num, day) {
2025-01-09 18:07:02 +02:00
const $newDay = $("<a>").addClass("calendar-date").attr("data-calendar-date", utils.formatDateISO(this.date));
const $date = $("<span>").html(num);
2019-09-08 16:06:42 +02:00
// if it's the first day of the month
if (num === 1) {
// 0 1 2 3 4 5 6
// Su Mo Tu We Th Fr Sa
// 1 2 3 4 5 6 0
// Mo Tu We Th Fr Sa Su
let dayOffset = day - this.firstDayOfWeek;
2025-01-09 18:07:02 +02:00
if (dayOffset < 0) dayOffset = 7 + dayOffset;
$newDay.css("marginLeft", dayOffset * 14.28 + "%");
2019-09-08 16:06:42 +02:00
}
const dateNoteId = dateNotesForMonth[utils.formatDateISO(this.date)];
if (dateNoteId) {
2025-01-09 18:07:02 +02:00
$newDay.addClass("calendar-date-exists");
$newDay.attr("data-href", `#root/${dateNoteId}`);
}
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
}
isEqual(a, b) {
2025-01-09 18:07:02 +02:00
if ((!a && b) || (a && !b)) {
return false;
}
2025-01-09 18:07:02 +02:00
return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
2019-09-08 16:06:42 +02:00
}
async createMonth() {
const month = utils.formatDateISO(this.date).substr(0, 7);
const dateNotesForMonth = await server.get(`special-notes/notes-for-month/${month}`);
2020-02-02 20:02:08 +01:00
this.$month.empty();
2019-09-08 16:06:42 +02:00
const currentMonth = this.date.getMonth();
while (this.date.getMonth() === currentMonth) {
2025-01-09 18:07:02 +02:00
const $day = this.createDay(dateNotesForMonth, this.date.getDate(), this.date.getDay(), this.date.getFullYear());
2020-02-02 20:02:08 +01:00
this.$month.append($day);
2019-09-08 16:06:42 +02:00
this.date.setDate(this.date.getDate() + 1);
}
// while loop trips over and day is at 30/31, bring it back
this.date.setDate(1);
this.date.setMonth(this.date.getMonth() - 1);
this.$monthSelect.text(MONTHS[this.date.getMonth()]);
this.$yearSelect.val(this.date.getFullYear());
2019-09-08 16:06:42 +02:00
}
2024-09-12 13:55:07 +02:00
async entitiesReloadedEvent({ loadResults }) {
2024-08-31 17:08:55 +03:00
if (!loadResults.getOptionNames().includes("firstDayOfWeek")) {
return;
}
2024-09-12 13:55:07 +02:00
2024-08-31 17:08:55 +03:00
this.manageFirstDayOfWeek();
this.createMonth();
}
}