UNPKG

4.06 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 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
31var _base_tree_viewer = require("./base_tree_viewer.js");
32
33var _event_utils = require("./event_utils.js");
34
35class 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
162exports.PDFAttachmentViewer = PDFAttachmentViewer;
\No newline at end of file