-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (69 loc) · 2.86 KB
/
index.js
File metadata and controls
87 lines (69 loc) · 2.86 KB
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
const mainDiv = document.getElementById('main');
const baseRef = window.location.pathname.slice(0, -1);
const template = document.querySelector('template');
(async () => {
// Fetch content from repos.json file.
const requestInit = {
mode: `cors`,
method: `GET`,
credentials: `same-origin`,
headers: {
'Pragma': `no-cache`,
'Cache-Control': `no-store`
}
};
const response = await fetch(`${baseRef}/repos.json`, requestInit);
let structure = await response.text();
if (structure) {
structure = JSON.parse(structure);
} else {
structure = {
repos: []
};
}
// Listen for event where OS color scheme changes and re-render repos.
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', renderRepos);
renderRepos();
function renderRepos() {
mainDiv.innerHTML = '';
const templateSupported = !!template.content;
for (const repo of structure.repos) {
// console.log(repo);
// Repo docs path inferred from repo name.
const repoPath = `${baseRef}/docs/${repo.name}`;
const clonedNode = templateSupported ? template.content.cloneNode(true) : document.createElement('div');
if (!templateSupported) {
clonedNode.innerHTML = template.innerHTML;
}
const gitHubMarkPath = `github-mark${window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? `-white` : ''}.png`;
clonedNode.querySelector('a').href = repoPath;
clonedNode.querySelector('[data-logo]').src = repo.logo ?? gitHubMarkPath;
if (repo.version) {
clonedNode.querySelector('[data-version]').innerHTML = repo.version
} else {
clonedNode.querySelector('[data-version]').classList.remove('version');
}
// clonedNode.querySelector('[data-version]').innerHTML = repo.version ?? '';
clonedNode.querySelector('[data-name]').innerHTML = repo.display ?? repo.name;
clonedNode.querySelector('[data-desc]').innerHTML = repo.description ?? '';
if (templateSupported) {
mainDiv.appendChild(clonedNode);
} else {
moveElements(clonedNode, mainDiv);
}
}
if (!templateSupported) {
template.remove();
}
}
})();
function dragStartHandler(ev) {
const target = ev.target;
const boundingRect = target.getBoundingClientRect();
ev.dataTransfer.setDragImage(ev.target, (boundingRect.width ?? target.offsetWidth) / 2, (boundingRect.height ?? target.offsetHeight) / 2);
}
function moveElements(oldParent, newParent) {
while (oldParent.childNodes.length > 0) {
newParent.appendChild(oldParent.childNodes[0]);
}
}