-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
110 lines (90 loc) · 3.86 KB
/
app.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
(function () {
const statusList = document.getElementById('status-list');
const newsList = document.getElementById('news-list');
const loadMore = document.getElementById('load-more');
const formatTime = function(timeStr) {
return new Date(timeStr).toLocaleString('en-US');
};
const render = function(res) {
const sEls = [], nEls = [];
if (res.status) {
res.status.forEach(s => {
const regionBlock = document.createElement('div');
const blockHeader = document.createElement('div');
const region = document.createElement('div');
const status = document.createElement('div');
const message = document.createElement('div');
regionBlock.append(blockHeader, message);
blockHeader.append(region, status);
regionBlock.classList.add('region-block');
blockHeader.classList.add('block-header');
region.classList.add('region', `status-${s.status}`);
status.classList.add('status', `status-${s.status}`);
message.classList.add('message');
const statusText = s.status.charAt(0).toUpperCase() + s.status.slice(1);
region.innerText = s.region;
status.innerText = statusText;
if (s.message) {
const p = document.createElement('p');
p.innerText = s.message;
message.append(p);
} else if (s.messages) {
s.messages.forEach(m => {
const p = document.createElement('p');
p.innerText = m;
message.append(p);
});
}
sEls.push(regionBlock);
});
}
if (res.news) {
res.news.forEach(n => {
const newsBlock = document.createElement('div');
const blockHeader = document.createElement('div');
const region = document.createElement('span');
const time = document.createElement('span');
const message = document.createElement('div');
newsBlock.append(blockHeader, message);
blockHeader.append(region, time);
newsBlock.classList.add('news-block');
blockHeader.classList.add('block-header', `type-${n.type}`);
region.classList.add('region');
time.classList.add('time');
message.classList.add('message');
region.innerText = n.region;
time.innerText = formatTime(n.time);
if (n.message) {
const p = document.createElement('p');
p.innerText = n.message;
message.append(p);
} else if (n.messages) {
n.messages.forEach(m => {
const p = document.createElement('p');
p.innerText = m;
message.append(p);
});
}
nEls.push(newsBlock);
});
}
statusList.append(...sEls);
const loadHandler = function() {
const queue = nEls.splice(0, 5);
newsList.append(...queue);
if (nEls.length <= 0) {
loadMore.remove();
}
};
loadMore.onclick = loadHandler;
loadHandler();
};
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', function (ev) {
const res = JSON.parse(this.responseText);
render(res);
});
xhr.open('GET', `https://status-api.apernet.io/status.json?v=${Date.now()}`);
// xhr.open('GET', `/status.json?v=${Date.now()}`);
xhr.send();
})();