Improve date handling for the calendar widget
This commit is contained in:
@@ -51,19 +51,33 @@
|
||||
|
||||
function getCalendarStateKey(calendar) {
|
||||
var baseNs = calendar.getAttribute('data-base-ns') || 'chronological';
|
||||
return 'luxtools.calendar.month.' + baseNs;
|
||||
return 'luxtools_calendar_month_' + baseNs.replace(/[^a-zA-Z0-9]/g, '_');
|
||||
}
|
||||
|
||||
function shouldPersistCalendarMonth(calendar) {
|
||||
return (calendar.getAttribute('data-luxtools-size') || 'large') === 'small';
|
||||
}
|
||||
|
||||
function getCookieValue(name) {
|
||||
var match = document.cookie.match('(^|;)\\s*' + name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\s*=\\s*([^;]+)');
|
||||
return match ? decodeURIComponent(match[2]) : null;
|
||||
}
|
||||
|
||||
function setCookie(name, value) {
|
||||
var date = new Date();
|
||||
date.setFullYear(date.getFullYear() + 1);
|
||||
document.cookie = name + '=' + encodeURIComponent(value) + '; expires=' + date.toUTCString() + '; path=/; SameSite=Lax';
|
||||
}
|
||||
|
||||
function deleteCookie(name) {
|
||||
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; SameSite=Lax';
|
||||
}
|
||||
|
||||
function readSavedCalendarMonth(calendar) {
|
||||
if (!shouldPersistCalendarMonth(calendar)) return null;
|
||||
if (!window.localStorage) return null;
|
||||
|
||||
try {
|
||||
var raw = window.localStorage.getItem(getCalendarStateKey(calendar));
|
||||
var raw = getCookieValue(getCalendarStateKey(calendar));
|
||||
if (!raw) return null;
|
||||
|
||||
var parsed = JSON.parse(raw);
|
||||
@@ -79,31 +93,17 @@
|
||||
|
||||
function saveCalendarMonth(calendar) {
|
||||
if (!shouldPersistCalendarMonth(calendar)) return;
|
||||
if (!window.localStorage) return;
|
||||
|
||||
var year = parseInt(calendar.getAttribute('data-current-year') || '', 10);
|
||||
var month = parseInt(calendar.getAttribute('data-current-month') || '', 10);
|
||||
if (!year || month < 1 || month > 12) return;
|
||||
|
||||
try {
|
||||
window.localStorage.setItem(getCalendarStateKey(calendar), JSON.stringify({
|
||||
year: year,
|
||||
month: month
|
||||
}));
|
||||
} catch (e) {
|
||||
// ignore storage failures
|
||||
}
|
||||
setCookie(getCalendarStateKey(calendar), JSON.stringify({ year: year, month: month }));
|
||||
}
|
||||
|
||||
function clearSavedCalendarMonth(calendar) {
|
||||
if (!shouldPersistCalendarMonth(calendar)) return;
|
||||
if (!window.localStorage) return;
|
||||
|
||||
try {
|
||||
window.localStorage.removeItem(getCalendarStateKey(calendar));
|
||||
} catch (e) {
|
||||
// ignore storage failures
|
||||
}
|
||||
deleteCookie(getCalendarStateKey(calendar));
|
||||
}
|
||||
|
||||
function fetchCalendarMonth(calendar, year, month) {
|
||||
@@ -227,11 +227,22 @@
|
||||
|
||||
function restoreCalendarMonth(calendar) {
|
||||
if (!shouldPersistCalendarMonth(calendar)) return;
|
||||
var saved = readSavedCalendarMonth(calendar);
|
||||
if (!saved) return;
|
||||
|
||||
var year = parseInt(calendar.getAttribute('data-current-year') || '', 10);
|
||||
var month = parseInt(calendar.getAttribute('data-current-month') || '', 10);
|
||||
if (!year || !month) return;
|
||||
|
||||
var saved = readSavedCalendarMonth(calendar);
|
||||
if (!saved) {
|
||||
var now = new Date();
|
||||
var todayYear = now.getFullYear();
|
||||
var todayMonth = now.getMonth() + 1;
|
||||
if (year !== todayYear || month !== todayMonth) {
|
||||
loadCalendarMonth(calendar, todayYear, todayMonth, false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (saved.year === year && saved.month === month) return;
|
||||
|
||||
loadCalendarMonth(calendar, saved.year, saved.month, true);
|
||||
@@ -258,7 +269,17 @@
|
||||
navigateCalendarMonth(calendar, direction, true);
|
||||
}
|
||||
|
||||
function updateClientDateCookie() {
|
||||
var now = new Date();
|
||||
setCookie('luxtools_client_month', JSON.stringify({
|
||||
year: now.getFullYear(),
|
||||
month: now.getMonth() + 1
|
||||
}));
|
||||
}
|
||||
|
||||
function initCalendarWidgets() {
|
||||
updateClientDateCookie();
|
||||
|
||||
var calendars = document.querySelectorAll('div.luxtools-calendar[data-luxtools-calendar="1"]');
|
||||
for (var i = 0; i < calendars.length; i++) {
|
||||
syncCalendarToday(calendars[i]);
|
||||
|
||||
Reference in New Issue
Block a user