Merge pull request #8764 from Snuffleupagus/es6-DownloadManager-class

Convert `DownloadManager` to an ES6 class
This commit is contained in:
Tim van der Meij 2017-08-09 22:26:25 +02:00 committed by GitHub
commit 21cc2c0200

View File

@ -21,7 +21,7 @@ if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('CHROME || GENERIC')) {
} }
function download(blobUrl, filename) { function download(blobUrl, filename) {
var a = document.createElement('a'); let a = document.createElement('a');
if (a.click) { if (a.click) {
// Use a.click() if available. Otherwise, Chrome might show // Use a.click() if available. Otherwise, Chrome might show
// "Unsafe JavaScript attempt to initiate a navigation change // "Unsafe JavaScript attempt to initiate a navigation change
@ -49,36 +49,32 @@ function download(blobUrl, filename) {
blobUrl.split('#')[0] === window.location.href.split('#')[0]) { blobUrl.split('#')[0] === window.location.href.split('#')[0]) {
// If _parent == self, then opening an identical URL with different // If _parent == self, then opening an identical URL with different
// location hash will only cause a navigation, not a download. // location hash will only cause a navigation, not a download.
var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&'; let padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&';
blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&'); blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');
} }
window.open(blobUrl, '_parent'); window.open(blobUrl, '_parent');
} }
} }
function DownloadManager() {} class DownloadManager {
downloadUrl(url, filename) {
DownloadManager.prototype = {
downloadUrl: function DownloadManager_downloadUrl(url, filename) {
if (!createValidAbsoluteUrl(url, 'http://example.com')) { if (!createValidAbsoluteUrl(url, 'http://example.com')) {
return; // restricted/invalid URL return; // restricted/invalid URL
} }
download(url + '#pdfjs.action=download', filename); download(url + '#pdfjs.action=download', filename);
}, }
downloadData: function DownloadManager_downloadData(data, filename, downloadData(data, filename, contentType) {
contentType) {
if (navigator.msSaveBlob) { // IE10 and above if (navigator.msSaveBlob) { // IE10 and above
return navigator.msSaveBlob(new Blob([data], { type: contentType, }), return navigator.msSaveBlob(new Blob([data], { type: contentType, }),
filename); filename);
} }
let blobUrl = createObjectURL(data, contentType,
var blobUrl = createObjectURL(data, contentType,
PDFJS.disableCreateObjectURL); PDFJS.disableCreateObjectURL);
download(blobUrl, filename); download(blobUrl, filename);
}, }
download: function DownloadManager_download(blob, url, filename) { download(blob, url, filename) {
if (navigator.msSaveBlob) { if (navigator.msSaveBlob) {
// IE10 / IE11 // IE10 / IE11
if (!navigator.msSaveBlob(blob, filename)) { if (!navigator.msSaveBlob(blob, filename)) {
@ -93,10 +89,10 @@ DownloadManager.prototype = {
return; return;
} }
var blobUrl = URL.createObjectURL(blob); let blobUrl = URL.createObjectURL(blob);
download(blobUrl, filename); download(blobUrl, filename);
}, }
}; }
export { export {
DownloadManager, DownloadManager,