2014-09-18 12:04:20 -04:00
|
|
|
define(function(require) {
|
2013-10-23 11:17:36 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-09-18 12:04:20 -04:00
|
|
|
var util = require('js/crypto/util');
|
|
|
|
|
2013-10-23 11:17:36 -04:00
|
|
|
var dl = {};
|
|
|
|
|
2014-09-24 06:46:19 -04:00
|
|
|
dl.createDownload = function(options) {
|
2013-11-04 08:20:14 -05:00
|
|
|
var contentType = options.contentType || 'application/octet-stream';
|
2014-09-18 12:04:20 -04:00
|
|
|
var filename = options.filename || 'file';
|
|
|
|
var content = options.content;
|
|
|
|
var a = document.createElement('a');
|
|
|
|
var supportsBlob;
|
2013-11-04 08:20:14 -05:00
|
|
|
|
2014-09-18 12:04:20 -04:00
|
|
|
try {
|
|
|
|
supportsBlob = !!new Blob();
|
|
|
|
} catch (e) {}
|
2013-11-04 08:20:14 -05:00
|
|
|
|
2014-09-24 06:46:19 -04:00
|
|
|
if (typeof a.download !== "undefined" && supportsBlob) {
|
2014-09-18 12:04:20 -04:00
|
|
|
// ff 30+, chrome 27+ (android: 37+)
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.style = "display: none";
|
|
|
|
a.href = window.URL.createObjectURL(new Blob([content], {
|
2013-11-04 08:20:14 -05:00
|
|
|
type: contentType
|
|
|
|
}));
|
2014-09-18 12:04:20 -04:00
|
|
|
a.download = filename;
|
|
|
|
a.click();
|
|
|
|
setTimeout(function() {
|
|
|
|
window.URL.revokeObjectURL(a.href);
|
|
|
|
document.body.removeChild(a);
|
|
|
|
}, 10); // arbitrary, just get it off the main thread
|
|
|
|
} else if (window.navigator.msSaveBlob) {
|
|
|
|
// ie 10+
|
|
|
|
window.navigator.msSaveBlob(new Blob([content], {
|
|
|
|
type: contentType
|
|
|
|
}), filename);
|
|
|
|
} else if (supportsBlob) {
|
|
|
|
// safari actually makes no sense:
|
|
|
|
// - you can't open a new window
|
|
|
|
// - the file system api is dead
|
|
|
|
// - download attribute doesn't work
|
|
|
|
// - behaves randomly (opens a new tab or doesn't, downloads stuff or doesn't, ...)
|
|
|
|
var url = window.URL.createObjectURL(new Blob([content], {
|
|
|
|
type: contentType
|
|
|
|
}));
|
|
|
|
var newTab = window.open(url, "_blank");
|
|
|
|
if (!newTab) {
|
|
|
|
window.location.href = url;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// anything else, where anything at all is better than nothing
|
|
|
|
if (typeof content !== "string" && content.buffer) {
|
|
|
|
content = util.arrBuf2BinStr(content.buffer);
|
|
|
|
}
|
|
|
|
window.open('data:' + contentType + ';base64,' + btoa(content), "_blank");
|
2013-11-04 08:20:14 -05:00
|
|
|
}
|
2013-10-23 11:17:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return dl;
|
|
|
|
});
|