-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
250 lines (208 loc) · 8.82 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
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
247
248
249
250
'use strict';
/**
* gh-index
* An directory index for gh-pages.
*/
window.addEventListener('DOMContentLoaded', function () {
var wrapper = document.getElementById('gh-index');
function insertStylesheet(url) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = url;
document.head.appendChild(link);
}
function insertStylesheetAsync(url) {
var raf = function () {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
}();
raf(function () {
insertStylesheet(url);
});
}
function insertScript(url) {
var script = document.createElement('script');
script.src = url;
document.head.appendChild(script);
}
var index = {
// RegExp for files to exclude
excludes: new RegExp(wrapper.getAttribute('excludes')),
/**
* Init gh-index
*/
init: function init() {
window.addEventListener('hashchange', index.hashRoute);
// insertStylesheet('http://amio.github.io/gh-index/index.css');
insertStylesheet('index.css')
//insertStylesheetAsync('https://octicons.github.com/components/octicons/octicons/octicons.css');
insertStylesheetAsync('https://cdnjs.cloudflare.com/ajax/libs/octicons/3.5.0/octicons.min.css');
window.fetch || insertScript('https://cdn.jsdelivr.net/fetch/0.9.0/fetch.min.js');
index.loadTrees();
},
getRepoInfo: function getRepoInfo() {
var config = wrapper.getAttribute('repo') || wrapper.getAttribute('data-repo');
if (!config) return null;
var repoInfo = config.split('/');
return {
owner: repoInfo[0],
name: repoInfo[1],
branch: 'gh-pages'
};
},
loadTrees: function loadTrees() {
var repo = index.getRepoInfo();
if (!repo) return window.alert('Repo config missing!');
var uri = 'https://api.github.com/repos/' + repo.owner + '/' + repo.name + ('/git/trees/' + repo.branch + '?recursive=1');
window.fetch(uri, { cache: 'force-cache' }).then(function (resp) {
return resp.json();
}).then(function (result) {
index.refresh(result.tree);
});
},
/**
* Everything after data loaded.
* @param resp
*/
refresh: function refresh(treeData) {
// build tree
index.tree = index.genTree(treeData);
// build html
index.hashRoute();
},
/**
* Route task depends on current hash
*/
hashRoute: function hashRoute() {
var path = window.location.hash.substr(1).split('/');
var sub = index.tree;
while (sub && path.length && path[0]) {
sub = sub[path.shift()];
}
index.updateIndexies(sub);
removeAllSvg();
},
/**
* Generate entire tree base on node list array return by Github API.
* @param {Array} treeArr
* @returns {Object}
*/
genTree: function genTree(treeArr) {
var root = {};
for (var i = treeArr.length; i--;) {
if (!this.excludes.test(treeArr[i].path)) {
this.addItem(root, treeArr[i]);
}
}
return root;
},
/**
* Add an item into tree.
* @param tree
* @param item
* @returns {*}
*/
addItem: function addItem(tree, item) {
var parent = tree;
item.path.replace(/[^/]+/g, function (seg, idx) {
parent[seg] || (parent[seg] = {});
parent = parent[seg];
return idx;
});
parent['/NODE/'] = item;
return item;
},
/**
* Update list base on tree
* @param tree
*/
updateIndexies: function updateIndexies(tree) {
// generate header html
var path = window.location.hash.replace('#', '');
var header = '<div class="header"><span>•_•</span></div>';
var parentLink = '#' + path.replace(/[^/]+\/$/, '');
if (path) {
header = '<div class="header"><a class="uplink" href="' + parentLink + '">' + (path + '<span>←_←</span></a></div>');
}
// generate list html
var repo = index.getRepoInfo();
var home = 'http://' + repo.owner + '.github.io/' + repo.name + '/';
var items = Object.keys(tree).map(function (key) {
if (key === '/NODE/') return '';
var node = tree[key]['/NODE/'];
var str = '';
switch (node.type) {
case 'blob':
str = '<li class="blob"><a class="lazy" href="' + (home + node.path) + '">' + ('<span class="octicon octicon-file-text"></span> ' + node.path + '</a></li>');
break;
case 'tree':
str = '<li class="tree"><a href="#' + node.path + '/">' + ('<span class="octicon octicon-file-directory"></span> ' + node.path + '/</a></li>');
break;
}
return str;
}).join('');
var list = '<ul>' + items + '</ul>';
// Generate footer html
var footer = '<div class="footer">' + '<a href="http://github.com/amio/gh-index">gh-index</a> ' + 'by <a href="http://github.com/amio">amio</a></div>';
// insert html
wrapper.innerHTML = header + list + footer;
}
};
index.init();
});
document.addEventListener('DOMContentLoaded', function () {
let active = false;
const lazyLoad = function () {
var lazyLinks = [].slice.call(document.querySelectorAll('a.lazy'));
if (active === false) {
active = true;
//console.log(lazyLinks);
setTimeout(function () {
lazyLinks.forEach(function (lazyLinks) {
if ((lazyLinks.getBoundingClientRect().top <= window.innerHeight && lazyLinks.getBoundingClientRect().bottom >= 0) && getComputedStyle(lazyLinks).display !== "none") {
//console.log(lazyLinks);
makeReportPreview(lazyLinks);
lazyLinks.classList.remove('lazy');
if (lazyLinks.length === 0) {
document.removeEventListener('scroll', lazyLoad);
window.removeEventListener('resize', lazyLoad);
window.removeEventListener('orientationchange', lazyLoad);
}
}
});
active = false;
}, 200);
}
};
document.addEventListener('scroll', lazyLoad);
window.addEventListener('resize', lazyLoad);
window.addEventListener('orientationchange', lazyLoad);
});
function makeReportPreview(linkObj) {
var pageAddress = linkObj.getAttribute('href'); // linklerdeki adresleri al
if (pageAddress != undefined) { // adres bilgisi boş değilse
pageAddress = pageAddress.replace('http://ysdede.github.io/benchmarks/', ''); //cross origin kısıtlamasına takılmamak için kök adresi sil kendi siten içindeki dosyayı okuduğun anlaşılsın
fetch(pageAddress)
.then(response => response.text())
.then(result => { // içerik çekme tamamlandığında
let parser = new DOMParser();
var doc = parser.parseFromString(result, 'text/html'); // içeriği html dom yapısına çevir
if (doc.getElementById('left')) {
var getContent = doc.getElementById('left').firstElementChild.innerHTML; // çekilen sayfada left IS'si altındaki ilk divi içini al
getContent = '<a class="svg" href="' + pageAddress + '" target="_blank">' + getContent + '</a>'; // sayfadan çektiğin içeriğe sayfanın linkini ekle
var body = document.getElementsByTagName('BODY')[0];
linkObj.closest('li').insertAdjacentHTML('beforeend', getContent); // bulunduğun sayfanın gövdesinde sona bas
linkObj.style.display = 'none'; // linki gizle
}
});
}
}
function removeAllSvg() {
var svgs = document.querySelectorAll('.svg');
for (let i = 0; i < svgs.length; i++) {
svgs[i].remove();
}
window.scrollTo(window.scrollX, window.scrollY + 1);
}