-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
114 lines (114 loc) · 3.47 KB
/
script.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function WordNumberCase(number) {
const num = number >= 0 ? number : -number;
let m = num % 10;
let result;
switch (m) {
case 1:
result = "год";
break;
case 2:
case 3:
case 4:
result = "года";
break;
default:
result = "лет";
break;
}
if (m >= 1 && m <= 4) {
m = num % 100;
if (m >= 11 && m <= 14)
result = "лет";
}
return result;
}
function createCORSRequest(method, url) {
let xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
function loadReq(URL, goFunc) {
let request = createCORSRequest('GET', URL);
request.responseType = 'json';
request.send();
request.onload = function() {
const elmToShowInfo = document.getElementById("sp");
goFunc(request.response, elmToShowInfo);
};
}
function showAPOD(res, q) {
let media = "";
if (res.media_type === "video") {
media = '<iframe width="560" height="315" src="' + res.url + '" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
} else {
media = '<a href="' + res.hdurl + '"><img alt="" src="' + res.url + '"></a>';
}
q.innerHTML = '<br><b>' + res.date + ', ' + res.title + '</b><br>' + media + '<br>' + res.explanation;
}
function mkReq(reqName) {
const reqNames = {
apod: {
URL: 'https://api.nasa.gov/planetary/apod?api_key=eZgjB1gdPqQDKOz5gh4hCvJy6A1oq22reiKFD3GI',
func: showAPOD
}
};
loadReq(reqNames[reqName].URL, reqNames[reqName].func);
}
function getDates(dateObj) {
const day = ("0" + dateObj.getDate()).slice(-2);
const month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
return { daymon: day + "." + month, month: month, year: dateObj.getFullYear() };
}
function appendContent(daymon, deltaYr, datada, div) {
const h2 = document.createElement('h2');
h2.innerText = daymon + "." + datada.year + ": " + datada.slug;
div.appendChild(h2);
const img = document.createElement('img');
img.src = datada.img;
div.appendChild(img);
const desc = document.createElement('div');
if (!datada.desc)
datada.desc = "назад";
desc.innerText = deltaYr + " " + WordNumberCase(deltaYr) + " " + datada.desc;
div.appendChild(desc);
}
function showDates(res, q) {
let deltaDays = document.getElementById("days").value;
let neg = 1;
if (deltaDays < 0) {
deltaDays = -deltaDays;
neg = -1;
}
q.innerHTML = "Годовщины на ближайшие " + deltaDays + " дней:";
const div = document.createElement('div');
q.appendChild(div);
const currentDate = new Date();
const tag = document.getElementById("tag").value;
const isTag = Boolean(tag);
for (let i = 0; i < deltaDays; i += 1) {
const curDate = new Date(currentDate);
curDate.setDate(curDate.getDate() + neg * i);
const data = getDates(curDate);
if (data.daymon in res) {
for (let datada of res[data.daymon]) {
if (!isTag || (isTag && datada.hasOwnProperty('tags') && datada.tags.includes(tag)) ) {
let deltaYr = currentDate.getFullYear() - datada.year;
appendContent(data.daymon, deltaYr, datada, div);
}
}
}
}
}
function loadDates(listName) {
let URL = 'https://gvard.github.io/dates/';
if (listName == "AC") URL += 'astrocosm.json';
else if (listName == "IT") URL += 'it.json';
loadReq(URL, showDates);
}