-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
156 lines (120 loc) · 3.21 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
/**
* Manage downloads
*/
(function() {
var form = document.getElementsByTagName('form')[0],
p = form.getElementsByTagName('p')[1],
a = form.getElementsByTagName('a')[0],
code;
form.elements.compression[0].onclick =
form.elements.compression[1].onclick = function() {
var minified = +this.value;
p.innerHTML = p.firstChild.textContent + '<progress></progress>';
form.className = 'open';
// Download chainvas.js through XHR
var xhr = new XMLHttpRequest();
code = {};
xhr.open('GET', 'chainvas' + (minified? '.min' : '') + '.js', true);
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
if(xhr.status === 200 || xhr.status === 304) {
p.innerHTML = p.firstChild.textContent;
window.chainvasjs = xhr.responseText;
// Split into modules
var moduleHeader = /(\/\*\*\s+\* Chainvas module: ([\w -]+?)\s+\*\/)/gim,
parts = chainvasjs.split(moduleHeader);
moduleHeader = RegExp(moduleHeader.source, 'i');
for(var i=0; i<parts.length; i++) {
var part = parts[i];
if(moduleHeader.test(part)) {
var id = parts[i+1];
code[id] = {
code: (minified? '' : part) + parts[i+2]
}
}
else if (i === 0) {
var id = 'Core';
code[id] = {
code: parts[i]
}
}
else {
continue;
}
if(window.ByteSize) {
code[id].bytes = ByteSize.count(code[id].code);
code[id].byteSize = ByteSize.format(code[id].bytes);
}
// Add checkboxes
var checkbox = code[id].checkbox = document.createElement('input').prop({
type: 'checkbox',
value: id
}),
label = document.createElement('label');
if(i === 0) {
checkbox.checked = checkbox.disabled = true;
label.textContent = 'Chainvas Core';
}
else {
label.textContent = id + ' module';
checkbox.onclick = updateSize;
}
label.innerHTML += ' (' + code[id].byteSize + ')';
label.insertBefore(checkbox, label.firstChild);
p.appendChild(label);
}
updateSize();
}
else {
// TODO show error
console.log(xhr.status, xhr.statusText);
}
}
};
xhr.send(null);
form.onsubmit = function() {
evt.preventDefault();
a.onclick();
a.click();
return false;
};
a.onclick = function(evt) {
var codeParts = [];
for(var id in code) {
if(code[id].checkbox.checked) {
codeParts.push(code[id].code);
}
}
var blob = new Blob(
[codeParts.join('\r\n')],
{type : 'text/javascript'}
);
this.href = URL.createObjectURL(blob);
this.download = 'chainvas.js';
};
};
function updateSize() {
var totalSize = 0;
for (var id in code) {
if(code[id].checkbox.checked) {
totalSize += code[id].bytes;
}
}
document.getElementById('filesize').innerHTML = ByteSize.format(totalSize);
}
})();
/**
* Table of contents
*/
(function(){
var toc = document.querySelector('#toc ol'),
headings = document.querySelectorAll('body > h2');
for(var i=0; i<headings.length; i++) {
var li = document.createElement('li');
li.appendChild(document.createElement('a').prop({
textContent: headings[i].textContent,
href: '#' + headings[i].id
}));
toc.appendChild(li);
}
})();