Skip to content

Commit

Permalink
Merge pull request #3 from ekir/master
Browse files Browse the repository at this point in the history
downloadFile and Import/Export
  • Loading branch information
scottfr authored Oct 1, 2016
2 parents e44d77e + 91ab29c commit 757ac0d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion examples/full.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
<script type="text/javascript" src="../js/Localization.js"></script>
<script type="text/javascript" src="../js/Variables.js"></script>
<script type="text/javascript" src="../js/SharedJS.js"></script>
<script type="text/javascript" src="../js/ModelImporter.js"></script>
<script type="text/javascript" src="../js/FileManager.js"></script>
<script type="text/javascript">
mxBasePath = 'https://jgraph.github.io/mxgraph/javascript/src/';
Expand All @@ -140,7 +141,6 @@
<script type="text/javascript" src="../js/UnitsEditor.js"></script>
<script type="text/javascript" src="../js/Sensitivity.js"></script>
<script type="text/javascript" src="../js/Optimizer.js"></script>
<script type="text/javascript" src="../js/ModelImporter.js"></script>
<script type="text/javascript" src="../js/Sanitize.js"></script>
<script type="text/javascript" src="../js/Results.js"></script>
<script type="text/javascript" src="../resources/jquery.js"></script>
Expand Down
21 changes: 15 additions & 6 deletions js/API/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,25 @@ data - The data to download.
*/

function downloadFile(fileName, data) {
var a = document.createElement("a");
document.body.appendChild(a);

// Create Blob and attach it to ObjectURL
var blob = new Blob([data], {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
url = window.URL.createObjectURL(blob);

// Create download link and click it
var a = document.createElement("a");
a.style.display="none";
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();

// The setTimeout is a fix to make it work in Firefox
// Without it, the objectURL is removed before the click-event is triggered
// And the download does not work
setTimeout(function() {
window.URL.revokeObjectURL(url);
a.remove();
},1);
};

/*
Expand Down
12 changes: 1 addition & 11 deletions js/FileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ function newModel() {
clearModel();
}

function downloadWebFile(filename, content) {
// There is already a downloadFile in API.js
// But it does not appear to work in Firefox
var downloadlink = document.body.appendChild(document.createElement("a"));
downloadlink.download = filename;
downloadlink.href = "data:text/plain;base64," + btoa(content);
downloadlink.click();
downloadlink.parentElement.removeChild(downloadlink);
};

// High-level File manager. Does save and load of models
var FileManagerWeb = new function() {
var self = this;
Expand All @@ -64,7 +54,7 @@ var FileManagerWeb = new function() {
var xml_data = getModelXML2();
model_name=appendFileExtension(model_name,InsightMakerFileExtension);
self.set_filename(model_name);
downloadWebFile(model_name,xml_data);
downloadFile(model_name,xml_data);
}
});

Expand Down
5 changes: 3 additions & 2 deletions js/RibbonPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@ var RibbonPanel = function(graph, mainPanel, configPanel) {
});
}
}, {
hidden: (!viewConfig.showImportExport),
text: getText("Insight Maker File..."),
handler: importInsightMaker
},
Expand All @@ -1513,10 +1514,10 @@ var RibbonPanel = function(graph, mainPanel, configPanel) {
text: "Export",
glyph: 0xf019,
menu: [{
hidden: (!viewConfig.showImportExport),
text: getText("Download Insight Maker File"),
handler: function(){
/*downloadFile("Model.InsightMaker", getGraphXml(graph).replace(/mxGraphModel/g,"InsightMakerModel"));*/
new mxXmlRequest(builder_path + "/download.php", $.param({name: "Model", format: "InsightMaker", "data": getGraphXml(graph).replace(/mxGraphModel/g,"InsightMakerModel")})).simulate(document, "_blank");
downloadFile("Model.InsightMaker", getGraphXml(graph).replace(/mxGraphModel/g,"InsightMakerModel"));
}
},'-',
{
Expand Down
39 changes: 29 additions & 10 deletions js/SharedJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ terms of the Insight Maker Public License (https://InsightMaker.com/impl).
*/

var environment = {
AutoDetect: 0,
InsightMakerOnline: 1,
WebOffline: 2,
NodeWebKit: 3 /* Not yet implmented. Suggestion for future */
}

function environmentAutoDetect() {
if(location.hostname.match("insightmaker.com")!=null) {
return environment.InsightMakerOnline;
} else {
return environment.WebOffline;
}
}

var viewConfig = {
environment: environmentAutoDetect(),
environment: environment.AutoDetect,
showTopLinks: true,
sideBarWidth: 330,
referenceBarWidth: 240,
Expand All @@ -40,8 +33,34 @@ var viewConfig = {
toolsGroup: true,
exploreGroup: false,
fullScreenResults: false,
showResultsEdit: true
showResultsEdit: true,
showImportExport: true
};
environmentConfig();

function environmentAutoDetect() {
if(location.hostname.match("insightmaker.com")!=null) {
return environment.InsightMakerOnline;
} else {
return environment.WebOffline;
}
}

function environmentConfig() {
if(viewConfig.environment==environment.AutoDetect) {
viewConfig.environment = environmentAutoDetect();
}
switch(viewConfig.environment) {
case environment.InsightMakerOnline:

break;
case environment.WebOffline:
viewConfig.saveEnabled=false;
viewConfig.showTopLinks=false;
viewConfig.showImportExport=false;
break;
}
}

if(is_ebook){
viewConfig.showTopLinks = false;
Expand Down
14 changes: 2 additions & 12 deletions js/Utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -1241,13 +1241,7 @@ var downloadButton = function(name){
res += "\r\n" + cells.join(",");
});

/*downloadFile(name, res);*/

new mxXmlRequest(builder_path + "/download.php", $.param({
name: name,
"format": "csv",
"data": res
})).simulate(document, "_blank");
downloadFile(name+".csv", res);
}
};
}
Expand Down Expand Up @@ -1341,9 +1335,5 @@ var downloadButton = function(name){

var xml = (mxUtils.getXml(root));

new mxXmlRequest(builder_path + "/download.php", $.param({
name: "Insight Maker Diagram",
"format": "svg",
"data": xml
})).simulate(document, "_blank");
downloadFile("Insight Maker Diagram.svg", xml);
};

0 comments on commit 757ac0d

Please sign in to comment.