-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesidebar.js
199 lines (154 loc) · 5.85 KB
/
filesidebar.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
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
//-*- coding: utf-8 -*-
function ajax(opt) {
opt = opt || {};
opt.method = opt.method.toUpperCase() || 'POST';
opt.url = opt.url || '';
opt.async = opt.async || true;
opt.data = opt.data || null;
opt.success = opt.success || function () {};
var xmlHttp = null;
if (XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}var params = [];
for (var key in opt.data){
params.push(key + '=' + opt.data[key]);
}
var postData = params.join('&');
if (opt.method.toUpperCase() === 'POST') {
xmlHttp.open(opt.method, opt.url, opt.async);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
xmlHttp.send(postData);
}
else if (opt.method.toUpperCase() === 'GET') {
xmlHttp.open(opt.method, opt.url + '?' + postData, opt.async);
xmlHttp.send(null);
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
opt.success(xmlHttp.responseText);
}
};
// https://blog.csdn.net/Lpandeng/article/details/72399221
}
function create_contents(div_nav) {
var div_l = document.createElement('div');
div_l.innerText = 'contents';
div_nav.appendChild(div_l);
document.querySelectorAll('h2').forEach(function (item) {
var ul = document.createElement('ul');
div_l.appendChild(ul);
var li = document.createElement('li');
ul.appendChild(li);
var a = document.createElement('a');
li.appendChild(a);
a.setAttribute("href",document.location.href.split('?')[0]+'?id='+item.id);
a.innerText = item.innerText;
});
}
function github_filetree (getree, div_nav, filetypes) {
var z_da_t = getree.tree;
for (var k = 0; k <= z_da_t.length - 1; k++) {
var pa = z_da_t[k]['path'];
var type_ = pa.split('.');
if (filetypes.indexOf(type_[type_.length-1]) > -1) {
var fles = pa.split('/');
var this_node = div_nav;
for (var i = 0; i <= fles.length - 1; i++) {
var fod_i = this_node.querySelector('ul[folder="'+fles[i]+'"]');
if (!fod_i) {
if (i == fles.length - 1) {
var ul = document.createElement('ul');
var li = document.createElement('li');
ul.appendChild(li);
var a = document.createElement('a');
a.innerText = fles[i];
a.setAttribute('href','#/'+pa);
li.appendChild(a);
this_node.appendChild(ul);
}else {
var ul = document.createElement('ul');
ul.setAttribute('folder', fles[i]);
ul.innerHTML= '<a>'+fles[i]+'</a>';
this_node.appendChild(ul);
this_node = ul;
}
}else{
this_node = fod_i;
};
};
};
};
}
function fucos() {
var div_nav = document.querySelector('div.sidebar-nav')
/*active current site*/
var site = document.location.href.split('?')[0].split('#');
site = site[site.length-1];
if (site==='') {
var active = div_nav.querySelector('#addsidebarok a');
}else {
var active = div_nav.querySelector('a[href*="'+decodeURI(site)+'"]');
};
active.parentElement.setAttribute('class', 'active');
active.focus();
}
function addsidebar(getree,sidebar_css,filetypes){
var div_sidebar = document.querySelector(sidebar_css)
//div_nav.innerText = '';
//create_contents(div_nav); // static/sidebar.js
var div_nav = document.createElement('div');
div_sidebar.appendChild(div_nav);
div_nav.setAttribute('class','githubtree');
var d_ul = document.createElement('ul');
div_nav.appendChild(d_ul);
github_filetree(getree, div_nav, filetypes);
d_ul.setAttribute('id','addsidebarok');
var li = document.createElement('li');
d_ul.appendChild(li);
var a = document.createElement('a');
li.appendChild(a);
a.innerHTML = '<strong>home</strong>';
a.setAttribute('href', document.location.href.split('#')[0].split('?')[0]+'#');
}
function make_filenav(filenav_param){
if (!filenav_param.file_tree_url && !filenav_param.gtihub_repo) {
console.log("file_tree_url and gtihub_repo is null")
return;
};
if (filenav_param.file_tree_url) {
var file_tree = filenav_param.file_tree_url
}else{
var file_tree = "https://api.github.com/repos/" +
filenav_param.gtihub_repo +
"/git/trees/master?recursive=1" ;//&_="+
//Date.parse(new Date());
};
var filetypes = filenav_param.filetypes || ['md']; //
var sidebar_css = filenav_param.sidebar_css || 'div.sidebar-nav'; //
var getree;
ajax({
method: 'get',
url: file_tree,
success: function (response) { // 此处为执行成功后的代码
getree = JSON.parse(response);
//github_filetree(getree, div_nav, filetypes) // static/sidebar.js
},
fail:function (status) {
console.log('状态码为'+status);
}
});
var x = document.location.href.split('?')[0];
setInterval(function(){
if (!document.querySelector('#addsidebarok')) {
addsidebar(getree, sidebar_css,filetypes);
console.log('sidebar 1');
}
if (document.location.href.split('?')[0]!=x) {
x = document.location.href.split('?')[0];
fucos();
}
},3000);
}