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 | var _base_tree_viewer = require("./base_tree_viewer.js");
|
32 |
|
33 | var _event_utils = require("./event_utils.js");
|
34 |
|
35 | class PDFAttachmentViewer extends _base_tree_viewer.BaseTreeViewer {
|
36 | constructor(options) {
|
37 | super(options);
|
38 | this.downloadManager = options.downloadManager;
|
39 |
|
40 | this.eventBus._on("fileattachmentannotation", this.#appendAttachment.bind(this));
|
41 | }
|
42 |
|
43 | reset(keepRenderedCapability = false) {
|
44 | super.reset();
|
45 | this._attachments = null;
|
46 |
|
47 | if (!keepRenderedCapability) {
|
48 | this._renderedCapability = (0, _pdf.createPromiseCapability)();
|
49 | }
|
50 |
|
51 | this._pendingDispatchEvent = false;
|
52 | }
|
53 |
|
54 | async _dispatchEvent(attachmentsCount) {
|
55 | this._renderedCapability.resolve();
|
56 |
|
57 | if (attachmentsCount === 0 && !this._pendingDispatchEvent) {
|
58 | this._pendingDispatchEvent = true;
|
59 | await (0, _event_utils.waitOnEventOrTimeout)({
|
60 | target: this.eventBus,
|
61 | name: "annotationlayerrendered",
|
62 | delay: 1000
|
63 | });
|
64 |
|
65 | if (!this._pendingDispatchEvent) {
|
66 | return;
|
67 | }
|
68 | }
|
69 |
|
70 | this._pendingDispatchEvent = false;
|
71 | this.eventBus.dispatch("attachmentsloaded", {
|
72 | source: this,
|
73 | attachmentsCount
|
74 | });
|
75 | }
|
76 |
|
77 | _bindLink(element, {
|
78 | content,
|
79 | filename
|
80 | }) {
|
81 | element.onclick = () => {
|
82 | this.downloadManager.openOrDownloadData(element, content, filename);
|
83 | return false;
|
84 | };
|
85 | }
|
86 |
|
87 | render({
|
88 | attachments,
|
89 | keepRenderedCapability = false
|
90 | }) {
|
91 | if (this._attachments) {
|
92 | this.reset(keepRenderedCapability);
|
93 | }
|
94 |
|
95 | this._attachments = attachments || null;
|
96 |
|
97 | if (!attachments) {
|
98 | this._dispatchEvent(0);
|
99 |
|
100 | return;
|
101 | }
|
102 |
|
103 | const names = Object.keys(attachments).sort(function (a, b) {
|
104 | return a.toLowerCase().localeCompare(b.toLowerCase());
|
105 | });
|
106 | const fragment = document.createDocumentFragment();
|
107 | let attachmentsCount = 0;
|
108 |
|
109 | for (const name of names) {
|
110 | const item = attachments[name];
|
111 | const content = item.content,
|
112 | filename = (0, _pdf.getFilenameFromUrl)(item.filename);
|
113 | const div = document.createElement("div");
|
114 | div.className = "treeItem";
|
115 | const element = document.createElement("a");
|
116 |
|
117 | this._bindLink(element, {
|
118 | content,
|
119 | filename
|
120 | });
|
121 |
|
122 | element.textContent = this._normalizeTextContent(filename);
|
123 | div.append(element);
|
124 | fragment.append(div);
|
125 | attachmentsCount++;
|
126 | }
|
127 |
|
128 | this._finishRendering(fragment, attachmentsCount);
|
129 | }
|
130 |
|
131 | #appendAttachment({
|
132 | filename,
|
133 | content
|
134 | }) {
|
135 | const renderedPromise = this._renderedCapability.promise;
|
136 | renderedPromise.then(() => {
|
137 | if (renderedPromise !== this._renderedCapability.promise) {
|
138 | return;
|
139 | }
|
140 |
|
141 | const attachments = this._attachments || Object.create(null);
|
142 |
|
143 | for (const name in attachments) {
|
144 | if (filename === name) {
|
145 | return;
|
146 | }
|
147 | }
|
148 |
|
149 | attachments[filename] = {
|
150 | filename,
|
151 | content
|
152 | };
|
153 | this.render({
|
154 | attachments,
|
155 | keepRenderedCapability: true
|
156 | });
|
157 | });
|
158 | }
|
159 |
|
160 | }
|
161 |
|
162 | exports.PDFAttachmentViewer = PDFAttachmentViewer; |
\ | No newline at end of file |