Changed save.js, fdc.js, and diskdump.js to use a shared download function

This commit is contained in:
Jeff Parsons 2016-02-23 00:06:38 -08:00
commit 255700f88a
7 changed files with 1332 additions and 1337 deletions

View file

@ -198,45 +198,18 @@ function downloadPC(sURL, sCSS, nErrorCode, aMachineInfo)
if (sXMLFile && sXSLFile) {
var sResources = JSON.stringify(resNew);
sScript += ".js";
sPCJS = matchScript[1] + "var resources=" + sResources + ";" + matchScript[2] + matchScript[3];
Component.log("saving machine: '" + idMachine + "' (" + sPCJS.length + " bytes)");
var sURI, sAlert;
var link = document.createElement('a');
var fDownload = (!DEBUG && typeof link.download == 'string');
sPCJS = sPCJS.replace(/\u00A9/g, "©");
if (MAXDEBUG) {
sPCJS = sPCJS.replace(/[\u00A0-\u2666]/g, function(c) {
return '&#' + c.charCodeAt(0) + ';';
});
sURI = "data:application/javascript;base64," + btoa(encodeURI(sPCJS));
} else {
sPCJS = sPCJS.replace(/\u00A9/g, "©");
sURI = "data:application/javascript,";
if (!web.isUserAgent("Firefox")) {
fDownload = false;
sURI += encodeURI(sPCJS);
} else {
sURI += encodeURIComponent(sPCJS);
}
}
var sAlert = web.downloadFile(sPCJS, "javascript", false, sScript);
if (fDownload) {
link.href = sURI;
link.download = sScript + ".js";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
sAlert = 'Check your Downloads folder for "' + link.download + '", ';
} else {
window.open(sURI);
sAlert = 'Check your browser for a new window/tab containing the PCjs script, ';
}
sAlert += 'copy it to your web server as "' + sScript + '.js", and then add the following to your web page:\n\n';
sAlert += ', copy it to your web server as "' + sScript + '", and then add the following to your web page:\n\n';
sAlert += '<div id="' + idMachine + '"></div>\n';
sAlert += '...\n';
sAlert += '<script type="text/javascript" src="' + sScript + '.js"></script>\n';
sAlert += '<script type="text/javascript" src="' + sScript + '"></script>\n';
sAlert += '<script type="text/javascript">embedPC("' + idMachine + '","' + sXMLFile + '","' + sXSLFile + '");</script>\n\n';
sAlert += 'The machine should appear where the <div> is located.';
web.alertUser(sAlert);

View file

@ -239,7 +239,7 @@ web.getResource = function(sURL, dataPost, fAsync, done)
};
}
if (typeof dataPost == "object") {
if (dataPost && typeof dataPost == "object") {
var sDataPost = "";
for (var p in dataPost) {
if (!dataPost.hasOwnProperty(p)) continue;
@ -601,44 +601,41 @@ web.getURLParameters = function(sParms)
};
/**
* downloadJSON()
* downloadFile(sData, sType, fBase64, sFileName)
*
* @param {string} sJSON
* @param {string} sData
* @param {string} sType
* @param {boolean} [fBase64]
* @param {string} [sFileName]
*/
web.downloadJSON = function(sJSON, sFileName)
web.downloadFile = function(sData, sType, fBase64, sFileName)
{
var link, sAlert;
var fDownload = !!sFileName;
var sURI = "data:application/json,";
var link = null, sAlert;
var sURI = "data:application/" + sType + (fBase64? ";base64" : "") + ",";
if (!web.isUserAgent("Firefox")) {
fDownload = false;
sURI += encodeURI(sJSON);
sURI += (fBase64? sData : encodeURI(sData));
} else {
sURI += encodeURIComponent(sJSON);
if (fDownload) {
sURI += (fBase64? sData : encodeURIComponent(sData));
if (sFileName) {
link = document.createElement('a');
if (typeof link.download != 'string') fDownload = false;
if (typeof link.download != 'string') link = null;
}
}
if (fDownload) {
if (link) {
link.href = sURI;
link.download = sFileName;
document.body.appendChild(link);
document.body.appendChild(link); // Firefox requires the link to be in the body
link.click();
document.body.removeChild(link);
sAlert = 'Check your Downloads folder for "' + sFileName + '".';
sAlert = 'Check your Downloads folder for "' + sFileName + '"';
} else {
window.open(sURI);
sAlert = 'Check your browser for a new window/tab containing the requested data.';
sAlert = 'Check your browser for a new window/tab containing the requested data';
}
web.alertUser(sAlert);
return sAlert;
};
/**
* onCountRepeat(n, fnRepeat, fnComplete, msDelay)
*