matrix-public-archive/shared/CalendarView.js

108 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-02-15 20:33:31 -07:00
'use strict';
2022-02-14 20:11:55 -07:00
const { TemplateView } = require('hydrogen-view-sdk');
// Month in JavaScript is 0-indexed (January is 0, February is 1, etc),
// but by using 0 as the day it will give us the last day of the prior
// month.
//
// via https://stackoverflow.com/a/1184359/796832
function numDaysInMonthForDate(date) {
return new Date(date.getYear(), date.getMonth() + 1, 0).getDate();
}
// Map from day of week to the localized name of the day
const DAYS_OF_WEEK = {
Sun: null,
Mon: null,
Tue: null,
Wed: null,
Thu: null,
Fri: null,
Sat: null,
};
// Generate the localized days of the week names
const today = new Date();
for (let i = 0; i < 7; i++) {
const lookupDate = new Date(today);
lookupDate.setDate(i + 1);
const lookup = lookupDate.toLocaleString('en-US', { weekday: 'short' });
const localized = lookupDate.toLocaleString('default', { weekday: 'short' });
DAYS_OF_WEEK[lookup] = localized;
}
class CalendarView extends TemplateView {
render(t, vm) {
return t.div({ className: { CalendarView: true } }, [
t.div({ className: { CalendarView_heading: true } }, [
t.button(
{
className: { CalendarView_heading_prevButton: true },
onClick: () => vm.prevMonth(),
},
['\u276E']
),
t.map(
(vm) => vm.date,
(date, t) => {
return t.h4({ className: { CalendarView_heading_text: true } }, [
date.toLocaleString('default', { year: 'numeric', month: 'long' }),
]);
}
),
2022-02-14 20:11:55 -07:00
t.button(
{
className: { CalendarView_heading_nextButton: true },
onClick: () => vm.nextMonth(),
},
['\u276F']
),
]),
2022-02-15 01:30:26 -07:00
t.map(
(vm) => vm.date,
(date, t) => {
return t.ol(
{ className: { CalendarView_calendar: true } },
[].concat(
Object.keys(DAYS_OF_WEEK).map((dayKey) => {
return t.li({ className: { CalendarView_dayName: true } }, [DAYS_OF_WEEK[dayKey]]);
}),
(() => {
let dayNodes = [];
for (let i = 0; i < numDaysInMonthForDate(date); i++) {
2022-02-16 18:58:32 -07:00
const dayNumberDate = new Date(date);
dayNumberDate.setDate(i);
// day number from 0 (monday) to 6 (sunday)
const dayNumber = dayNumberDate.getDay();
// +1 because we're going from 0-based day to 1-based `grid-column-start`
// +1 because we actually start the week on Sunday(6) instead of Monday(0)
const gridColumnStart = dayNumber + 1 + 1;
2022-02-14 20:11:55 -07:00
2022-02-15 01:30:26 -07:00
dayNodes.push(
t.li(
{
className: { CalendarView_day: true },
2022-02-16 18:58:32 -07:00
style: i === 0 ? `grid-column-start: ${gridColumnStart};` : null,
2022-02-15 01:30:26 -07:00
},
2022-02-16 18:58:32 -07:00
[t.a({ href: vm.linkForDate(dayNumberDate) }, [String(i + 1)])]
2022-02-15 01:30:26 -07:00
)
);
}
2022-02-14 20:11:55 -07:00
2022-02-15 01:30:26 -07:00
return dayNodes;
})()
)
);
}
2022-02-14 20:11:55 -07:00
),
]);
}
}
module.exports = CalendarView;