forked from gbtami/pychess-variants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.ts
71 lines (59 loc) · 2.43 KB
/
calendar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Calendar } from '@fullcalendar/core';
import interactionPlugin from '@fullcalendar/interaction';
import dayGridPlugin from '@fullcalendar/daygrid';
import timeGridPlugin from '@fullcalendar/timegrid';
import listPlugin from '@fullcalendar/list';
import allLocales from '@fullcalendar/core/locales-all';
import { h, VNode } from 'snabbdom';
import { VARIANTS } from './variants';
function buildCalendar() {
const xmlhttp = new XMLHttpRequest();
const url = "/api/calendar";
xmlhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
const response = JSON.parse(this.responseText);
if (!response.length) {
return;
}
for (const i in response) {
const variant = response[i].title.replace('960', '');
const chess960 = response[i].title.includes('960');
response[i].title = VARIANTS[variant].displayName(chess960);
}
// console.log(response);
let calendarEl: HTMLElement = document.getElementById('fullcalendar')!;
let calendar = new Calendar(calendarEl, {
plugins: [ interactionPlugin, dayGridPlugin, timeGridPlugin, listPlugin ],
locales: allLocales,
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay,listWeek'
},
firstDay: 1,
showNonCurrentDates: false,
fixedWeekCount: false,
navLinks: true, // can click day/week names to navigate views
dayMaxEvents: true, // allow "more" link when too many events
nextDayThreshold: '06:00:00', // events only considered to take up the next day if it ends after this time
events: response
});
let locale = 'en'
const el = document.getElementById('pychess-variants');
if (el) {
const lang = el.getAttribute("data-lang")!;
if (calendar.getAvailableLocaleCodes().includes(lang)) locale = lang;
if (lang === 'zh') locale = 'zh-cn';
}
calendar.setOption('locale', locale);
calendar.render();
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
export function calendarView(): VNode[] {
return [
h('div#fullcalendar.box', { hook: { insert: () => buildCalendar() } }),
];
}