-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
91 lines (79 loc) · 2.37 KB
/
main.js
File metadata and controls
91 lines (79 loc) · 2.37 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
87
88
89
90
91
const d = document;
const root = d.documentElement;
var lights = matchMedia("(prefers-color-scheme: light)").matches;
const btnLights = d.getElementById("lights");
function commitLights() {
if (lights) {
btnLights.innerText = "🌙";
btnLights.title = "Turn off the lights."
d.documentElement.className = "light";
} else {
btnLights.innerText = "☀️";
btnLights.title = "Turn on the lights."
d.documentElement.className = "";
}
}
commitLights();
function toggleLights() {
lights ^= 1;
commitLights();
}
btnLights.addEventListener("click", toggleLights);
const reqLanguagesYml = new XMLHttpRequest();
const yamlfile = "https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml"
reqLanguagesYml.open("GET", yamlfile, false);
reqLanguagesYml.send(null);
if (reqLanguagesYml.status !== 200) {
throw request.status;
}
const langData =
Object
.entries(jsyaml.load(reqLanguagesYml.responseText))
.map(([name, {color}]) => ({name, color}));
langData.sort((a, b) => a.name.toLowerCase() > b.name.toLowerCase());
class LangInfo extends HTMLElement {
constructor(name, hex) {
super();
this.className = "lang-info";
if (hex == null) {
LangInfo.noHex.push(this);
}
const title = d.createElement("span");
title.innerText = name;
const color = d.createElement("div");
color.classList.add("color");
color.style.backgroundColor = hex;
this.appendChild(title);
this.appendChild(color);
}
}
LangInfo.noHex = [];
customElements.define("lang-info", LangInfo);
const langInfos = langData.map(({name, color}) => new LangInfo(name, color));
var hidden = true;
const bnHidden = d.getElementById("hidden")
function commitHidden() {
if (hidden) {
bnHidden.innerText = "📃";
bnHidden.title = "Display colorless languages."
for (const info of LangInfo.noHex) {
info.style.display = "none";
}
} else {
bnHidden.innerText = "🎨";
bnHidden.title = "Only show colored languages."
for (const info of LangInfo.noHex) {
info.style.display = "";
}
}
}
commitHidden();
function toggleHidden() {
hidden ^= 1;
commitHidden();
}
bnHidden.addEventListener("click", toggleHidden);
const divLangs = d.getElementById("langs")
for (const info of langInfos) {
divLangs.appendChild(info);
}