-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendar.min.js
81 lines (75 loc) · 2.6 KB
/
calendar.min.js
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
72
73
74
75
76
77
78
79
80
81
/**
* Copyright 2017-2021 Christoph M. Becker
*
* This file is part of Calendar_XH.
*
* Calendar_XH is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calendar_XH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Calendar_XH. If not, see <http://www.gnu.org/licenses/>.
*/
// @ts-check
class CalendarWidget {
/**
* @param {Element} element
*/
constructor (element) {
this.element = element;
let anchors = /** @type {NodeListOf<HTMLAnchorElement>} */
(element.querySelectorAll(".calendar_monthyear a"));
anchors.forEach(anchor => {
anchor.onclick = event => {
this.retrieveCalendar(anchor.href, false);
event.preventDefault();
};
});
history.replaceState({calendar_url: location.href}, document.title, location.href);
window.addEventListener("popstate", event => this.onPopState(event));
}
/**
* @param {string} url
* @param {boolean} isPop
*/
retrieveCalendar(url, isPop) {
var request = new XMLHttpRequest();
this.element.classList.add("calendar_loading");
request.open("GET", url, true);
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.onload = () => {
if (request.status >= 200 && request.status < 300) {
this.replaceCalendar(request.response, url, isPop);
}
this.element.classList.remove("calendar_loading");
};
request.send();
}
/**
* @param {string} response
* @param {string} url
* @param {boolean} isPop
*/
replaceCalendar(response, url, isPop) {
this.element.innerHTML = response;
new CalendarWidget(this.element);
if (!isPop) {
history.pushState({calendar_url: url}, document.title, url);
}
}
/**
* @param {PopStateEvent} event
*/
onPopState(event) {
if (event.state && "calendar_url" in event.state) {
this.retrieveCalendar(event.state.calendar_url, true);
}
}
}
document.querySelectorAll(".calendar_calendar").forEach(element => new CalendarWidget(element));