Commit 65080051b1ab47f0135c174c5a4759ba447ccce0

Golmote 2014-12-20T19:21:48

Add error message in red above the code if a file fails to download

diff --git a/download.html b/download.html
index 29ea909..7efa7de 100644
--- a/download.html
+++ b/download.html
@@ -87,6 +87,10 @@ section.download {
 	padding: .3em;
 }
 
+	#download .error {
+		color: #B61500;
+		display: none;
+	}
 	#download pre {
 		height: 20em;
 		word-wrap: break-word;
@@ -133,6 +137,7 @@ section.download {
 		<p><strong>Note:</strong> The filesizes displayed refer to non-gizipped files and include any CSS code required. The CSS code is not minified.</p>
 		
 		<section id="download">
+			<div class="error"></div>
 			<section id="download-js" class="download">
 				<pre><code class="language-javascript"></code></pre>
 				<a href="#" class="download-button" download="prism.js" target="_blank">Download JS</a>
diff --git a/download.js b/download.js
index 44a7299..7b4d99e 100644
--- a/download.js
+++ b/download.js
@@ -33,7 +33,7 @@ if (qstr) {
 				if (components[category][id].option) {
 					delete components[category][id].option;
 				}
-			};
+			}
 			ids.forEach(function(id) {
 				if (id !== 'meta') {
 					if (components[category][id]) {
@@ -187,7 +187,7 @@ form.elements.compression[1].onclick = function() {
 	minified = !!+this.value;
 	
 	getFilesSizes();
-}
+};
 
 function getFileSize(filepath) {
 	return treePromise.then(function(tree) {
@@ -242,7 +242,7 @@ function getFileContents(filepath) {
 		$u.xhr({
 			url: filepath,
 			callback: function(xhr) {
-				if (xhr.status < 400) {
+				if (xhr.status < 400 && xhr.responseText) {
 					resolve(xhr.responseText);
 				} else {
 					reject();
@@ -361,7 +361,19 @@ function generateCode(){
 		}
 	}
 
-	buildCode(promises).then(function(code) {
+	// Hide error message if visible
+	var error = $('#download .error');
+	error.style.display = '';
+
+	buildCode(promises).then(function(res) {
+		var code = res.code;
+		var errors = res.errors;
+
+		if(errors.length) {
+			error.style.display = 'block';
+			error.innerHTML = '';
+			$u.element.contents(error, errors);
+		}
 	
 		var redownloadUrl = window.location.href.split("?")[0] + "?";
 		for (var category in redownload) {
@@ -384,6 +396,8 @@ function buildCode(promises) {
 	var i = 0,
 	    l = promises.length;
 	var code = {js: '', css: ''};
+	var errors = [];
+
 	var f = function(resolve) {
 		if(i < l) {
 			var p = promises[i];
@@ -393,12 +407,17 @@ function buildCode(promises) {
 				f(resolve);
 			});
 			p.contentsPromise['catch'](function() {
-				code[p.type] += '/* Error downloading file '+p.path+' */' + '\n';
+				errors.push($u.element.create({
+					tag: 'p',
+					prop: {
+						textContent: 'An error occurred while fetching the file "' + p.path + '".'
+					}
+				}));
 				i++;
 				f(resolve);
 			});
 		} else {
-			resolve(code);
+			resolve({code: code, errors: errors});
 		}
 	};