UNPKG

5.92 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2019 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.PDFAttachmentViewer = void 0;
28
29var _pdf = require("../pdf");
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33function _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); } }
34
35function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
36
37var PDFAttachmentViewer =
38/*#__PURE__*/
39function () {
40 function PDFAttachmentViewer(_ref) {
41 var container = _ref.container,
42 eventBus = _ref.eventBus,
43 downloadManager = _ref.downloadManager;
44
45 _classCallCheck(this, PDFAttachmentViewer);
46
47 this.container = container;
48 this.eventBus = eventBus;
49 this.downloadManager = downloadManager;
50 this.reset();
51 this.eventBus.on('fileattachmentannotation', this._appendAttachment.bind(this));
52 }
53
54 _createClass(PDFAttachmentViewer, [{
55 key: "reset",
56 value: function reset() {
57 var keepRenderedCapability = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
58 this.attachments = null;
59 this.container.textContent = '';
60
61 if (!keepRenderedCapability) {
62 this._renderedCapability = (0, _pdf.createPromiseCapability)();
63 }
64 }
65 }, {
66 key: "_dispatchEvent",
67 value: function _dispatchEvent(attachmentsCount) {
68 this._renderedCapability.resolve();
69
70 this.eventBus.dispatch('attachmentsloaded', {
71 source: this,
72 attachmentsCount: attachmentsCount
73 });
74 }
75 }, {
76 key: "_bindPdfLink",
77 value: function _bindPdfLink(button, content, filename) {
78 if (this.downloadManager.disableCreateObjectURL) {
79 throw new Error('bindPdfLink: Unsupported "disableCreateObjectURL" value.');
80 }
81
82 var blobUrl;
83
84 button.onclick = function () {
85 if (!blobUrl) {
86 blobUrl = (0, _pdf.createObjectURL)(content, 'application/pdf');
87 }
88
89 var viewerUrl;
90 viewerUrl = '?file=' + encodeURIComponent(blobUrl + '#' + filename);
91 window.open(viewerUrl);
92 return false;
93 };
94 }
95 }, {
96 key: "_bindLink",
97 value: function _bindLink(button, content, filename) {
98 var _this = this;
99
100 button.onclick = function () {
101 _this.downloadManager.downloadData(content, filename, '');
102
103 return false;
104 };
105 }
106 }, {
107 key: "render",
108 value: function render(_ref2) {
109 var attachments = _ref2.attachments,
110 _ref2$keepRenderedCap = _ref2.keepRenderedCapability,
111 keepRenderedCapability = _ref2$keepRenderedCap === void 0 ? false : _ref2$keepRenderedCap;
112 var attachmentsCount = 0;
113
114 if (this.attachments) {
115 this.reset(keepRenderedCapability === true);
116 }
117
118 this.attachments = attachments || null;
119
120 if (!attachments) {
121 this._dispatchEvent(attachmentsCount);
122
123 return;
124 }
125
126 var names = Object.keys(attachments).sort(function (a, b) {
127 return a.toLowerCase().localeCompare(b.toLowerCase());
128 });
129 attachmentsCount = names.length;
130
131 for (var i = 0; i < attachmentsCount; i++) {
132 var item = attachments[names[i]];
133 var filename = (0, _pdf.removeNullCharacters)((0, _pdf.getFilenameFromUrl)(item.filename));
134 var div = document.createElement('div');
135 div.className = 'attachmentsItem';
136 var button = document.createElement('button');
137 button.textContent = filename;
138
139 if (/\.pdf$/i.test(filename) && !this.downloadManager.disableCreateObjectURL) {
140 this._bindPdfLink(button, item.content, filename);
141 } else {
142 this._bindLink(button, item.content, filename);
143 }
144
145 div.appendChild(button);
146 this.container.appendChild(div);
147 }
148
149 this._dispatchEvent(attachmentsCount);
150 }
151 }, {
152 key: "_appendAttachment",
153 value: function _appendAttachment(_ref3) {
154 var _this2 = this;
155
156 var id = _ref3.id,
157 filename = _ref3.filename,
158 content = _ref3.content;
159
160 this._renderedCapability.promise.then(function () {
161 var attachments = _this2.attachments;
162
163 if (!attachments) {
164 attachments = Object.create(null);
165 } else {
166 for (var name in attachments) {
167 if (id === name) {
168 return;
169 }
170 }
171 }
172
173 attachments[id] = {
174 filename: filename,
175 content: content
176 };
177
178 _this2.render({
179 attachments: attachments,
180 keepRenderedCapability: true
181 });
182 });
183 }
184 }]);
185
186 return PDFAttachmentViewer;
187}();
188
189exports.PDFAttachmentViewer = PDFAttachmentViewer;
\No newline at end of file