-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
110 lines (100 loc) · 3.65 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
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
const sidebarMenu = document.querySelector('.sidebar-menu');
const selectedID = location.hash.slice(2);
let selectedItem = null;
async function initDemos(container, collection) {
if(!collection) {
const url = new URL(location.href);
let search = url.search;
const matched = url.search.match(/\?([_\w][_-\w]*)/);
if(matched) {
search = matched[1];
}
const dataPath = search ? `./collections/${search}.jcoderc.js` : './jcoderc.js';
try {
const demos = (await import(dataPath)).default;
collection = demos.collection;
const titleEl = document.getElementById('title');
const title = demos.name || '码上掘金-精选';
document.title = title;
titleEl.textContent = title;
if(demos.url) {
titleEl.href = demos.url;
}
} catch (ex) {
location.replace(`//${url.host}`);
return;
}
}
collection.forEach((demo) => {
if(demo.type === 'folder') {
const el = document.createElement('div');
el.className = demo.folded ? 'sidebar-menuFolder folded' : 'sidebar-menuFolder';
const p = document.createElement('p');
p.className = 'title';
p.textContent = demo.name;
el.appendChild(p);
const c = document.createElement('div');
c.className = 'content';
el.appendChild(c);
container.appendChild(el);
initDemos(c, demo.collection);
} else {
const el = document.createElement('div');
const a = document.createElement('a');
a.id = `code_${demo.id}`;
a.href = `#/${demo.id}`;
a.className = 'sidebar-menuItem';
a.textContent = demo.name;
el.appendChild(a);
container.appendChild(el);
if(demo.id === selectedID) selectedItem = a;
if(demo.default && !selectedItem) selectedItem = a;
}
});
}
initDemos(sidebarMenu).then(() => {
if(selectedItem) {
const url = selectedItem.getAttribute('href');
selectedItem.className = 'sidebar-menuItem active';
loadCodeFrame(url, false);
}
function loadCodeFrame(url, pushState = true) {
const src = url.replace(/^#/, '//code.juejin.cn/pen');
window.frames[0].location.replace(src);
if(pushState) history.pushState({url}, '', url);
else history.replaceState({url}, '', url);
}
const container = document.querySelector('main');
document.querySelector('.handler').addEventListener('click', () => {
container.className = container.className === 'max' ? '' : 'max';
});
const menu = document.querySelector('.sidebar-menu');
menu.addEventListener('click', (evt) => {
const {target} = evt;
const activeEl = menu.querySelector('a.sidebar-menuItem.active');
if(target !== activeEl && /^sidebar-menuItem/.test(target.className)) {
if(activeEl) activeEl.className = 'sidebar-menuItem';
target.className = 'sidebar-menuItem active';
loadCodeFrame(target.getAttribute('href'));
}
const parent = target.parentElement;
if(parent && /^sidebar-menuFolder/.test(parent.className)) {
parent.className = parent.className === 'sidebar-menuFolder' ? 'sidebar-menuFolder folded' : 'sidebar-menuFolder';
}
evt.preventDefault();
});
const sidebar = document.getElementById('sidebar');
const sidebarMore = document.querySelector('.sidebar-more');
sidebarMore.addEventListener('click', () => {
sidebar.className = sidebar.className === 'show' ? '' : 'show';
});
window.onpopstate = function (e) {
const items = document.querySelectorAll('.sidebar-menuItem');
items.forEach((item) => {
item.className = 'sidebar-menuItem';
if(item.getAttribute('href') === e.state.url) {
item.className = 'sidebar-menuItem active';
}
});
};
});