2016-10-17 22:06:48 -07:00
|
|
|
define([], function () {
|
|
|
|
'use strict';
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
function fallback(urls) {
|
|
|
|
var i = 0;
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
(function createIframe() {
|
|
|
|
var frame = document.createElement('iframe');
|
|
|
|
frame.style.display = 'none';
|
|
|
|
frame.src = urls[i++];
|
|
|
|
document.documentElement.appendChild(frame);
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
// the download init has to be sequential otherwise IE only use the first
|
|
|
|
var interval = setInterval(function () {
|
|
|
|
if (frame.contentWindow.document.readyState === 'complete') {
|
|
|
|
clearInterval(interval);
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
// Safari needs a timeout
|
|
|
|
setTimeout(function () {
|
|
|
|
frame.parentNode.removeChild(frame);
|
|
|
|
}, 1000);
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
if (i < urls.length) {
|
|
|
|
createIframe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
})();
|
|
|
|
}
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
function isFirefox() {
|
|
|
|
// sad panda :(
|
|
|
|
return /Firefox\//i.test(navigator.userAgent);
|
|
|
|
}
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
function sameDomain(url) {
|
|
|
|
var a = document.createElement('a');
|
|
|
|
a.href = url;
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
return location.hostname === a.hostname && location.protocol === a.protocol;
|
|
|
|
}
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
function download(url) {
|
|
|
|
var a = document.createElement('a');
|
|
|
|
a.download = '';
|
|
|
|
a.href = url;
|
|
|
|
// firefox doesn't support `a.click()`...
|
|
|
|
a.dispatchEvent(new MouseEvent('click'));
|
|
|
|
}
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
return function (urls) {
|
|
|
|
if (!urls) {
|
|
|
|
throw new Error('`urls` required');
|
|
|
|
}
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
if (typeof document.createElement('a').download === 'undefined') {
|
|
|
|
return fallback(urls);
|
|
|
|
}
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
var delay = 0;
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
urls.forEach(function (url) {
|
|
|
|
// the download init has to be sequential for firefox if the urls are not on the same domain
|
|
|
|
if (isFirefox() && !sameDomain(url)) {
|
|
|
|
return setTimeout(download.bind(null, url), 100 * ++delay);
|
|
|
|
}
|
2016-04-17 22:58:08 -07:00
|
|
|
|
2016-10-17 22:06:48 -07:00
|
|
|
download(url);
|
|
|
|
});
|
|
|
|
};
|
2016-04-17 22:58:08 -07:00
|
|
|
});
|