forked from tuna/mirror-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.es6
246 lines (236 loc) · 6.45 KB
/
index.es6
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
---
---
$(document).ready(() => {
$('.selectpicker').selectpicker()
var global_options = {% include options.json %};
var label_map = global_options.options.label_map;
var help_url = {};
global_options.helps.forEach((h) => help_url[h.mirrorid] = h.url);
var new_mirrors = {};
global_options.options.new_mirrors.forEach((m) => new_mirrors[m] = true);
var unlisted = global_options.options.unlisted_mirrors;
var options = {};
global_options.options.force_redirect_help_mirrors.forEach((m) => options[m] = {'url': "/help/" + m + "/"})
var descriptions = {};
global_options.options.mirror_desc.forEach((m) => descriptions[m.name] = m.desc);
new Vue({
el: "#upgrade-mask",
});
const vmMirList = new Vue({
el: "#mirror-list",
data: {
test: "hello",
mirrorList: [],
filter: "",
rawMirrorList: [],
dateTooltip: localStorage.getItem('DateTooltip') !== 'false',
haveSearchBox: false,
},
created() {
this.refreshMirrorList();
},
mounted() {
if (this.$refs.search){
this.haveSearchBox = true;
}
if (this.haveSearchBox){
window.addEventListener("keypress", this.onKeyPress);
}
window.addEventListener("visibilitychange", this.onVisibilityChange);
},
beforeDestroy() {
if (this.haveSearchBox){
window.removeEventListener("keypress", this.onKeyPress);
}
},
updated() {
$('.mirror-item-label').popover();
},
computed: {
nowBrowsingMirror: function() {
var mirrorName = location.pathname.split('/')[1];
if (!mirrorName){
return false;
}
mirrorName = mirrorName.toLowerCase();
const result = this.mirrorList.filter((m) => {
return m.name.toLowerCase() === mirrorName;
})[0];
if (!result){
return false;
}
return result;
},
filteredMirrorList: function() {
var filter = this.filter.toLowerCase();
return this.mirrorList.filter((m) => {
return m.is_master && m.name.toLowerCase().indexOf(filter) !== -1;
});
},
},
methods: {
getURL(mir) {
if (mir.url !== undefined) {
return mir.url
}
return `/${mir.name}/`
},
refreshMirrorList() {
// do nothing if the tab is not visible
if (document.hidden === true) {
return;
}
var self = this;
$.getJSON("/static/tunasync.json", (status_data) => {
const unlisted_mir = unlisted.map(d => processMirrorItem(d))
status_data = status_data.map(d => processMirrorItem(d));
var mir_data = $.merge(unlisted_mir, status_data);
mir_data = processLinkItem(mir_data);
status_data = sortAndUniqMirrors(status_data);
mir_data = sortAndUniqMirrors(mir_data).filter(d => !(d.status == "disabled"));
self.mirrorList = mir_data;
self.rawMirrorList = status_data;
self.refreshTimer = setTimeout(() => {self.refreshMirrorList()}, 10000);
});
},
onKeyPress(event) {
if (event.key === '/' && document.activeElement !== this.$refs.search) {
event.preventDefault();
this.$refs.search.focus();
}
},
onVisibilityChange() {
// disable the timer anyway
clearTimeout(this.refreshTimer);
if (document.visibilityState === 'visible') {
this.refreshMirrorList();
}
}
}
})
var stringifyTime = (ts) => {
const date = new Date(ts * 1000);
var str = "";
var ago = "";
if (date.getFullYear() > 2000) {
str = `${('000'+date.getFullYear()).substr(-4)}-${('0'+(date.getMonth()+1)).substr(-2)}-${('0'+date.getDate()).substr(-2)}` +
` ${('0'+date.getHours()).substr(-2)}:${('0'+date.getMinutes()).substr(-2)}`;
ago = timeago.format(date);
} else {
str = "0000-00-00 00:00";
ago = "Never";
}
return [str, ago];
}
var sortAndUniqMirrors = (mirs) => {
mirs.sort((a, b) => { return a.name < b.name ? -1: 1 });
return mirs.reduce((acc, cur)=>{
if(acc.length > 1 && acc[acc.length - 1].name == cur.name){
if(acc[acc.length - 1].last_update_ts && cur.last_update_ts){
if(acc[acc.length - 1].last_update_ts < cur.last_update_ts){
acc[acc.length - 1] = cur;
}
} else if(cur.last_update_ts){
acc[acc.length - 1] = cur;
}
}else{
acc.push(cur);
}
return acc;
}, []);
}
const processLinkItem = (mirrors) => {
var processed = [];
for (let d of mirrors) {
if (d.link_to === undefined) {
processed.push(d);
continue;
}
for (const target of mirrors) {
if (d.link_to === target.name) {
d.status = target.status;
d.label = target.label;
d.upstream = target.upstream;
d.show_status = target.show_status;
d.last_update = target.last_update;
d.last_update_ago = target.last_update_ago;
d.last_ended = target.last_ended;
d.last_ended_ago = target.last_ended_ago;
d.last_schedule = target.last_schedule;
d.last_schedule_ago = target.last_schedule_ago;
processed.push(d);
break;
}
}
}
return processed;
};
const processMirrorItem = (d) => {
if (options[d.name] != undefined ) {
d = $.extend(d, options[d.name]);
}
d.help_url = help_url[d.name];
d.is_new = Boolean(new_mirrors[d.name]);
d.description = descriptions[d.name];
d.github_release = d.url && d.url.startsWith('/github-release/');
if (d.is_master === undefined) {
d.is_master = true;
}
if (d.link_to !== undefined) {
return d;
}
d.label = label_map[d.status];
d.show_status = (d.status != "success");
// Strip the second component of last_update
[d.last_update, d.last_update_ago] = stringifyTime(d.last_update_ts);
[d.last_ended, d.last_ended_ago] = stringifyTime(d.last_ended_ts);
[d.last_started, d.last_started_ago] = stringifyTime(d.last_started_ts);
[d.next_schedule, d.next_schedule_ago] = stringifyTime(d.next_schedule_ts);
return d;
}
var vmIso = new Vue({
el: "#isoModal",
data: {
distroList: [],
selected: {},
curCategory: "",
knownCategories: {
os: "操作系统",
app: "应用软件",
font: "字体",
},
availableCategories: []
},
created: function() {
var self = this;
$.getJSON("/static/status/isoinfo.json", (isoinfo) => {
self.distroList = isoinfo;
self.availableCategories = [... new Set(isoinfo.map((x) => x.category))]
self.curCategory = self.availableCategories[0];
self.selected = self.curDistroList[0];
if (window.location.hash.match(/#iso-download(\?.*)?/)) {
$('#isoModal').modal();
}
});
},
computed: {
curDistroList() {
return this.distroList
.filter((x) => x.category === this.curCategory)
.sort(function (a, b) {
return a.distro.localeCompare(b.distro);
});
},
},
methods: {
switchDistro (distro) {
this.selected = distro;
},
switchCategory (category) {
this.curCategory = category;
this.selected = this.curDistroList[0];
}
}
});
});
// vim: ts=2 sts=2 sw=2 noexpandtab