Improvements to the calendar widget
This commit is contained in:
@@ -77,6 +77,34 @@
|
||||
});
|
||||
}
|
||||
|
||||
function pad2(num) {
|
||||
return num < 10 ? '0' + num : String(num);
|
||||
}
|
||||
|
||||
function syncCalendarToday(calendar) {
|
||||
if (!calendar) return;
|
||||
|
||||
var todayCells = calendar.querySelectorAll('td.luxtools-calendar-day-today');
|
||||
for (var i = 0; i < todayCells.length; i++) {
|
||||
todayCells[i].classList.remove('luxtools-calendar-day-today');
|
||||
}
|
||||
|
||||
var now = new Date();
|
||||
var year = parseInt(calendar.getAttribute('data-current-year') || '', 10);
|
||||
var month = parseInt(calendar.getAttribute('data-current-month') || '', 10);
|
||||
if (!year || !month) return;
|
||||
|
||||
var currentYear = now.getFullYear();
|
||||
var currentMonth = now.getMonth() + 1;
|
||||
if (year !== currentYear || month !== currentMonth) return;
|
||||
|
||||
var dateIso = String(currentYear) + '-' + pad2(currentMonth) + '-' + pad2(now.getDate());
|
||||
var todayCell = calendar.querySelector('td.luxtools-calendar-day[data-luxtools-date="' + dateIso + '"]');
|
||||
if (todayCell) {
|
||||
todayCell.classList.add('luxtools-calendar-day-today');
|
||||
}
|
||||
}
|
||||
|
||||
function navigateCalendarMonth(calendar, direction) {
|
||||
var year = parseInt(calendar.getAttribute('data-current-year') || '', 10);
|
||||
var month = parseInt(calendar.getAttribute('data-current-month') || '', 10);
|
||||
@@ -92,6 +120,7 @@
|
||||
throw new Error('Calendar markup missing in response');
|
||||
}
|
||||
|
||||
syncCalendarToday(replacement);
|
||||
calendar.replaceWith(replacement);
|
||||
})
|
||||
.catch(function () {
|
||||
@@ -120,6 +149,11 @@
|
||||
}
|
||||
|
||||
function initCalendarWidgets() {
|
||||
var calendars = document.querySelectorAll('div.luxtools-calendar[data-luxtools-calendar="1"]');
|
||||
for (var i = 0; i < calendars.length; i++) {
|
||||
syncCalendarToday(calendars[i]);
|
||||
}
|
||||
|
||||
document.addEventListener('click', onCalendarClick, false);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user