1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.PDFAttachmentViewer = void 0;
|
28 |
|
29 | var _pdf = require("../pdf");
|
30 |
|
31 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
32 |
|
33 | 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); } }
|
34 |
|
35 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
36 |
|
37 | var PDFAttachmentViewer =
|
38 |
|
39 | function () {
|
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 |
|
189 | exports.PDFAttachmentViewer = PDFAttachmentViewer; |
\ | No newline at end of file |