molstar
Version:
A comprehensive macromolecular library.
74 lines • 2.68 kB
JavaScript
/**
* Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.download = void 0;
function openUrl(url) {
var opened = window.open(url, '_blank');
if (!opened) {
window.location.href = url;
}
}
function click(node) {
try {
node.dispatchEvent(new MouseEvent('click'));
}
catch (e) {
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
node.dispatchEvent(evt);
}
}
function download(data, downloadName) {
// using ideas from https://github.com/eligrey/FileSaver.js/blob/master/FileSaver.js
if (downloadName === void 0) { downloadName = 'download'; }
if (!data)
return;
if ('download' in HTMLAnchorElement.prototype) {
var a_1 = document.createElement('a');
a_1.download = downloadName;
a_1.rel = 'noopener';
if (typeof data === 'string') {
a_1.href = data;
click(a_1);
}
else {
a_1.href = URL.createObjectURL(data);
setTimeout(function () { return URL.revokeObjectURL(a_1.href); }, 4E4); // 40s
setTimeout(function () { return click(a_1); });
}
}
else if (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob) {
// native saveAs in IE 10+
navigator.msSaveOrOpenBlob(data, downloadName);
}
else {
var ua = window.navigator.userAgent;
var isSafari = /Safari/i.test(ua);
var isChromeIos_1 = /CriOS\/[\d]+/.test(ua);
var open_1 = function (str) {
openUrl(isChromeIos_1 ? str : str.replace(/^data:[^;]*;/, 'data:attachment/file;'));
};
if ((isSafari || isChromeIos_1) && FileReader) {
if (data instanceof Blob) {
// no downloading of blob urls in Safari
var reader_1 = new FileReader();
reader_1.onloadend = function () { return open_1(reader_1.result); };
reader_1.readAsDataURL(data);
}
else {
open_1(data);
}
}
else {
var url_1 = URL.createObjectURL(data);
location.href = url_1;
setTimeout(function () { return URL.revokeObjectURL(url_1); }, 4E4); // 40s
}
}
}
exports.download = download;
//# sourceMappingURL=download.js.map
;