UNPKG

3.64 kBJavaScriptView Raw
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
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.DownloadManager = undefined;
28
29var _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; }; }();
30
31var _pdf = require('../pdf');
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35;
36var DISABLE_CREATE_OBJECT_URL = _pdf.apiCompatibilityParams.disableCreateObjectURL || false;
37function _download(blobUrl, filename) {
38 var a = document.createElement('a');
39 if (!a.click) {
40 throw new Error('DownloadManager: "a.click()" is not supported.');
41 }
42 a.href = blobUrl;
43 a.target = '_parent';
44 if ('download' in a) {
45 a.download = filename;
46 }
47 (document.body || document.documentElement).appendChild(a);
48 a.click();
49 a.remove();
50}
51
52var DownloadManager = function () {
53 function DownloadManager(_ref) {
54 var _ref$disableCreateObj = _ref.disableCreateObjectURL,
55 disableCreateObjectURL = _ref$disableCreateObj === undefined ? DISABLE_CREATE_OBJECT_URL : _ref$disableCreateObj;
56
57 _classCallCheck(this, DownloadManager);
58
59 this.disableCreateObjectURL = disableCreateObjectURL;
60 }
61
62 _createClass(DownloadManager, [{
63 key: 'downloadUrl',
64 value: function downloadUrl(url, filename) {
65 if (!(0, _pdf.createValidAbsoluteUrl)(url, 'http://example.com')) {
66 return;
67 }
68 _download(url + '#pdfjs.action=download', filename);
69 }
70 }, {
71 key: 'downloadData',
72 value: function downloadData(data, filename, contentType) {
73 if (navigator.msSaveBlob) {
74 return navigator.msSaveBlob(new Blob([data], { type: contentType }), filename);
75 }
76 var blobUrl = (0, _pdf.createObjectURL)(data, contentType, this.disableCreateObjectURL);
77 _download(blobUrl, filename);
78 }
79 }, {
80 key: 'download',
81 value: function download(blob, url, filename) {
82 if (navigator.msSaveBlob) {
83 if (!navigator.msSaveBlob(blob, filename)) {
84 this.downloadUrl(url, filename);
85 }
86 return;
87 }
88 if (this.disableCreateObjectURL) {
89 this.downloadUrl(url, filename);
90 return;
91 }
92 var blobUrl = URL.createObjectURL(blob);
93 _download(blobUrl, filename);
94 }
95 }]);
96
97 return DownloadManager;
98}();
99
100exports.DownloadManager = DownloadManager;
\No newline at end of file