From cbc411a2c7a0f2c195157beebd7ef1c8b737ffdf Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 6 Aug 2017 14:15:18 +0200 Subject: [PATCH] Convert `DownloadManager` to an ES6 class --- web/download_manager.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/web/download_manager.js b/web/download_manager.js index ebbed2e18..c6f7da862 100644 --- a/web/download_manager.js +++ b/web/download_manager.js @@ -21,7 +21,7 @@ if (typeof PDFJSDev !== 'undefined' && !PDFJSDev.test('CHROME || GENERIC')) { } function download(blobUrl, filename) { - var a = document.createElement('a'); + let a = document.createElement('a'); if (a.click) { // Use a.click() if available. Otherwise, Chrome might show // "Unsafe JavaScript attempt to initiate a navigation change @@ -49,36 +49,32 @@ function download(blobUrl, filename) { blobUrl.split('#')[0] === window.location.href.split('#')[0]) { // If _parent == self, then opening an identical URL with different // location hash will only cause a navigation, not a download. - var padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&'; + let padCharacter = blobUrl.indexOf('?') === -1 ? '?' : '&'; blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&'); } window.open(blobUrl, '_parent'); } } -function DownloadManager() {} - -DownloadManager.prototype = { - downloadUrl: function DownloadManager_downloadUrl(url, filename) { +class DownloadManager { + downloadUrl(url, filename) { if (!createValidAbsoluteUrl(url, 'http://example.com')) { return; // restricted/invalid URL } download(url + '#pdfjs.action=download', filename); - }, + } - downloadData: function DownloadManager_downloadData(data, filename, - contentType) { + downloadData(data, filename, contentType) { if (navigator.msSaveBlob) { // IE10 and above return navigator.msSaveBlob(new Blob([data], { type: contentType, }), filename); } - - var blobUrl = createObjectURL(data, contentType, + let blobUrl = createObjectURL(data, contentType, PDFJS.disableCreateObjectURL); download(blobUrl, filename); - }, + } - download: function DownloadManager_download(blob, url, filename) { + download(blob, url, filename) { if (navigator.msSaveBlob) { // IE10 / IE11 if (!navigator.msSaveBlob(blob, filename)) { @@ -93,10 +89,10 @@ DownloadManager.prototype = { return; } - var blobUrl = URL.createObjectURL(blob); + let blobUrl = URL.createObjectURL(blob); download(blobUrl, filename); - }, -}; + } +} export { DownloadManager,