New download page with dynamic loading of the scripts
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
diff --git a/download.html b/download.html
index 8b366a8..29ea909 100644
--- a/download.html
+++ b/download.html
@@ -154,6 +154,7 @@ section.download {
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
+<script src="vendor/promise.js"></script>
<script src="download.js"></script>
</body>
diff --git a/download.js b/download.js
index d6f106d..b56b28b 100644
--- a/download.js
+++ b/download.js
@@ -10,6 +10,18 @@ var minified = true;
var dependencies = {};
+var treeURL = 'https://api.github.com/repos/LeaVerou/prism/git/trees/gh-pages?recursive=1';
+var treePromise = new Promise(function(resolve) {
+ $u.xhr({
+ url: treeURL,
+ callback: function(xhr) {
+ if (xhr.status < 400) {
+ resolve(JSON.parse(xhr.responseText).tree);
+ }
+ }
+ });
+});
+
var qstr = window.location.search.match(/(?:languages|plugins)=[-+\w]+|themes=[-\w]+/g);
if (qstr) {
qstr.forEach(function(str) {
@@ -174,10 +186,20 @@ form.elements.compression[0].onclick =
form.elements.compression[1].onclick = function() {
minified = !!+this.value;
- fetchFiles();
+ getFilesSizes();
+}
+
+function getFileSize(filepath) {
+ return treePromise.then(function(tree) {
+ for(var i=0, l=tree.length; i<l; i++) {
+ if(tree[i].path === filepath) {
+ return tree[i].size;
+ }
+ }
+ });
}
-function fetchFiles() {
+function getFilesSizes() {
for (var category in components) {
var all = components[category];
@@ -192,26 +214,18 @@ function fetchFiles() {
files.forEach(function (filepath) {
var file = cache[filepath] = cache[filepath] || {};
- if (!file.contents) {
-
- (function(category, id, file, filepath, distro){
-
- $u.xhr({
- url: filepath,
- callback: function(xhr) {
- if (xhr.status < 400) {
-
- file.contents = xhr.responseText;
-
- file.size = +xhr.getResponseHeader('Content-Length') || file.contents.length;
-
- distro.size += file.size;
-
- update(category, id);
- }
+ if(!file.size) {
+
+ (function(category, id) {
+ getFileSize(filepath).then(function(size) {
+ if(size) {
+ file.size = size;
+ distro.size += file.size;
+
+ update(category, id);
}
});
- })(category, id, file, filepath, distro);
+ }(category, id));
}
else {
update(category, id);
@@ -221,7 +235,20 @@ function fetchFiles() {
}
}
-fetchFiles();
+getFilesSizes();
+
+function getFileContents(filepath) {
+ return new Promise(function(resolve) {
+ $u.xhr({
+ url: filepath,
+ callback: function(xhr) {
+ if (xhr.status < 400) {
+ resolve(xhr.responseText);
+ }
+ }
+ });
+ });
+}
function prettySize(size) {
return Math.round(100 * size / 1024)/100 + 'KB';
@@ -242,10 +269,17 @@ function update(updatedCategory, updatedId){
distro.paths.forEach(function(path) {
if (cache[path]) {
+ var file = cache[path];
+
var type = path.match(/\.(\w+)$/)[1],
- size = cache[path].size || 0;
+ size = file.size || 0;
if (info.enabled) {
+
+ if (!file.contentsPromise) {
+ file.contentsPromise = getFileContents(path);
+ }
+
total[type] += size;
}
@@ -291,8 +325,9 @@ function delayedGenerateCode(){
}
timerId = setTimeout(generateCode, 500);
}
+
function generateCode(){
- var code = {js: '', css: ''};
+ var promises = [];
var redownload = {};
for (var category in components) {
@@ -313,27 +348,53 @@ function generateCode(){
if (cache[path]) {
var type = path.match(/\.(\w+)$/)[1];
- code[type] += cache[path].contents + (type === 'js'? ';' : '') + '\n';
+ promises.push({
+ contentsPromise: cache[path].contentsPromise,
+ type: type
+ });
}
});
}
}
}
+
+ buildCode(promises).then(function(code) {
- var redownloadUrl = window.location.href.split("?")[0] + "?";
- for (var category in redownload) {
- redownloadUrl += category + "=" + redownload[category].join('+') + "&";
- }
- redownloadUrl = "/* " + redownloadUrl.replace(/&$/,"") + " */";
+ var redownloadUrl = window.location.href.split("?")[0] + "?";
+ for (var category in redownload) {
+ redownloadUrl += category + "=" + redownload[category].join('+') + "&";
+ }
+ redownloadUrl = "/* " + redownloadUrl.replace(/&$/,"") + " */";
- for (var type in code) {
- var codeElement = $('#download-' + type + ' code');
-
- codeElement.textContent = redownloadUrl + "\n" + code[type];
- Prism.highlightElement(codeElement, true);
-
- $('#download-' + type + ' .download-button').href = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(redownloadUrl + "\n" + code[type]);
- }
+ for (var type in code) {
+ var codeElement = $('#download-' + type + ' code');
+
+ codeElement.textContent = redownloadUrl + "\n" + code[type];
+ Prism.highlightElement(codeElement, true);
+
+ $('#download-' + type + ' .download-button').href = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(redownloadUrl + "\n" + code[type]);
+ }
+ });
+}
+
+function buildCode(promises) {
+ var i = 0,
+ l = promises.length;
+ var code = {js: '', css: ''};
+ var f = function(resolve) {
+ if(i < l) {
+ var p = promises[i];
+ p.contentsPromise.then(function(contents) {
+ code[p.type] += contents + (p.type === 'js'? ';' : '') + '\n';
+ i++;
+ f(resolve);
+ });
+ } else {
+ resolve(code);
+ }
+ };
+
+ return new Promise(f);
}
})();
\ No newline at end of file