download_manager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DownloadManager = undefined;
  27. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  28. var _pdf = require('../pdf');
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. ;
  31. function _download(blobUrl, filename) {
  32. var a = document.createElement('a');
  33. if (a.click) {
  34. a.href = blobUrl;
  35. a.target = '_parent';
  36. if ('download' in a) {
  37. a.download = filename;
  38. }
  39. (document.body || document.documentElement).appendChild(a);
  40. a.click();
  41. a.remove();
  42. } else {
  43. if (window.top === window && blobUrl.split('#')[0] === window.location.href.split('#')[0]) {
  44. var padCharacter = blobUrl.includes('?') ? '&' : '?';
  45. blobUrl = blobUrl.replace(/#|$/, padCharacter + '$&');
  46. }
  47. window.open(blobUrl, '_parent');
  48. }
  49. }
  50. var DownloadManager = function () {
  51. function DownloadManager() {
  52. _classCallCheck(this, DownloadManager);
  53. }
  54. _createClass(DownloadManager, [{
  55. key: 'downloadUrl',
  56. value: function downloadUrl(url, filename) {
  57. if (!(0, _pdf.createValidAbsoluteUrl)(url, 'http://example.com')) {
  58. return;
  59. }
  60. _download(url + '#pdfjs.action=download', filename);
  61. }
  62. }, {
  63. key: 'downloadData',
  64. value: function downloadData(data, filename, contentType) {
  65. if (navigator.msSaveBlob) {
  66. return navigator.msSaveBlob(new Blob([data], { type: contentType }), filename);
  67. }
  68. var blobUrl = (0, _pdf.createObjectURL)(data, contentType, _pdf.PDFJS.disableCreateObjectURL);
  69. _download(blobUrl, filename);
  70. }
  71. }, {
  72. key: 'download',
  73. value: function download(blob, url, filename) {
  74. if (navigator.msSaveBlob) {
  75. if (!navigator.msSaveBlob(blob, filename)) {
  76. this.downloadUrl(url, filename);
  77. }
  78. return;
  79. }
  80. if (_pdf.PDFJS.disableCreateObjectURL) {
  81. this.downloadUrl(url, filename);
  82. return;
  83. }
  84. var blobUrl = URL.createObjectURL(blob);
  85. _download(blobUrl, filename);
  86. }
  87. }]);
  88. return DownloadManager;
  89. }();
  90. exports.DownloadManager = DownloadManager;