-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathindex.js
54 lines (48 loc) · 1.64 KB
/
index.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
const DEFAULT_QUERY_PREFIX = '';
const DEFAULT_TEMPLATE_PATH = './detail.html?path=./markdown/';
const VALID_TYPES = {
md: {
queryPrefix: DEFAULT_QUERY_PREFIX,
path: DEFAULT_TEMPLATE_PATH
},
html: {
queryPrefix: '?',
path: './html/'
}
};
const ajax = (path, callback) => {
let xhr = new XMLHttpRequest();
xhr.open('get', path, true);
xhr.send();
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
callback(xhr.responseText);
}
}
};
};
const init = menu => {
let result = '';
let data = JSON.parse(menu);
let [templatePath, queryPrefix] = [DEFAULT_TEMPLATE_PATH, DEFAULT_QUERY_PREFIX];
data.list.forEach(function (value) {
templatePath = VALID_TYPES[value.type] ? VALID_TYPES[value.type].path : DEFAULT_TEMPLATE_PATH;
queryPrefix = VALID_TYPES[value.type] ? VALID_TYPES[value.type].queryPrefix : DEFAULT_QUERY_PREFIX;
value.path = templatePath + value.path + queryPrefix;
let html = `
<li>
<a class="author" title="${value.author}" href="${value.homepage}"><span>${value.author.replace(/(.).+$/, '$1')}</span></a>
<a class="content" href="${value.path}&title=${value.title}"><div class="arrow"></div>${value.title}</a>
<div class="info-wrapper">
<a class="icon icon-github" href="${value.homepage}"></a>
<a class="icon icon-weibo" href="${value.weibo}"></a>
</div>
</li>`;
result += html;
});
document.getElementById('list').innerHTML = result;
};
document.addEventListener('DOMContentLoaded', () => {
ajax('./menu.json', init);
}, false);