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 | class PDFAttachmentViewer {
|
32 | constructor({
|
33 | container,
|
34 | eventBus,
|
35 | downloadManager
|
36 | }) {
|
37 | this.container = container;
|
38 | this.eventBus = eventBus;
|
39 | this.downloadManager = downloadManager;
|
40 | this.reset();
|
41 |
|
42 | this.eventBus._on("fileattachmentannotation", this._appendAttachment.bind(this));
|
43 | }
|
44 |
|
45 | reset(keepRenderedCapability = false) {
|
46 | this.attachments = null;
|
47 | this.container.textContent = "";
|
48 |
|
49 | if (!keepRenderedCapability) {
|
50 | this._renderedCapability = (0, _pdf.createPromiseCapability)();
|
51 | }
|
52 | }
|
53 |
|
54 | _dispatchEvent(attachmentsCount) {
|
55 | this._renderedCapability.resolve();
|
56 |
|
57 | this.eventBus.dispatch("attachmentsloaded", {
|
58 | source: this,
|
59 | attachmentsCount
|
60 | });
|
61 | }
|
62 |
|
63 | _bindPdfLink(button, content, filename) {
|
64 | if (this.downloadManager.disableCreateObjectURL) {
|
65 | throw new Error('bindPdfLink: Unsupported "disableCreateObjectURL" value.');
|
66 | }
|
67 |
|
68 | let blobUrl;
|
69 |
|
70 | button.onclick = function () {
|
71 | if (!blobUrl) {
|
72 | blobUrl = (0, _pdf.createObjectURL)(content, "application/pdf");
|
73 | }
|
74 |
|
75 | let viewerUrl;
|
76 | viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
|
77 | window.open(viewerUrl);
|
78 | return false;
|
79 | };
|
80 | }
|
81 |
|
82 | _bindLink(button, content, filename) {
|
83 | button.onclick = () => {
|
84 | this.downloadManager.downloadData(content, filename, "");
|
85 | return false;
|
86 | };
|
87 | }
|
88 |
|
89 | render({
|
90 | attachments,
|
91 | keepRenderedCapability = false
|
92 | }) {
|
93 | let attachmentsCount = 0;
|
94 |
|
95 | if (this.attachments) {
|
96 | this.reset(keepRenderedCapability === true);
|
97 | }
|
98 |
|
99 | this.attachments = attachments || null;
|
100 |
|
101 | if (!attachments) {
|
102 | this._dispatchEvent(attachmentsCount);
|
103 |
|
104 | return;
|
105 | }
|
106 |
|
107 | const names = Object.keys(attachments).sort(function (a, b) {
|
108 | return a.toLowerCase().localeCompare(b.toLowerCase());
|
109 | });
|
110 | attachmentsCount = names.length;
|
111 |
|
112 | for (let i = 0; i < attachmentsCount; i++) {
|
113 | const item = attachments[names[i]];
|
114 | const filename = (0, _pdf.removeNullCharacters)((0, _pdf.getFilenameFromUrl)(item.filename));
|
115 | const div = document.createElement("div");
|
116 | div.className = "attachmentsItem";
|
117 | const button = document.createElement("button");
|
118 | button.textContent = filename;
|
119 |
|
120 | if (/\.pdf$/i.test(filename) && !this.downloadManager.disableCreateObjectURL) {
|
121 | this._bindPdfLink(button, item.content, filename);
|
122 | } else {
|
123 | this._bindLink(button, item.content, filename);
|
124 | }
|
125 |
|
126 | div.appendChild(button);
|
127 | this.container.appendChild(div);
|
128 | }
|
129 |
|
130 | this._dispatchEvent(attachmentsCount);
|
131 | }
|
132 |
|
133 | _appendAttachment({
|
134 | id,
|
135 | filename,
|
136 | content
|
137 | }) {
|
138 | this._renderedCapability.promise.then(() => {
|
139 | let attachments = this.attachments;
|
140 |
|
141 | if (!attachments) {
|
142 | attachments = Object.create(null);
|
143 | } else {
|
144 | for (const name in attachments) {
|
145 | if (id === name) {
|
146 | return;
|
147 | }
|
148 | }
|
149 | }
|
150 |
|
151 | attachments[id] = {
|
152 | filename,
|
153 | content
|
154 | };
|
155 | this.render({
|
156 | attachments,
|
157 | keepRenderedCapability: true
|
158 | });
|
159 | });
|
160 | }
|
161 |
|
162 | }
|
163 |
|
164 | exports.PDFAttachmentViewer = PDFAttachmentViewer; |
\ | No newline at end of file |