UNPKG

4.92 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.PDFLayerViewer = void 0;
28
29var _base_tree_viewer = require("./base_tree_viewer.js");
30
31class PDFLayerViewer extends _base_tree_viewer.BaseTreeViewer {
32 constructor(options) {
33 super(options);
34 this.l10n = options.l10n;
35
36 this.eventBus._on("resetlayers", this._resetLayers.bind(this));
37
38 this.eventBus._on("togglelayerstree", this._toggleAllTreeItems.bind(this));
39 }
40
41 reset() {
42 super.reset();
43 this._optionalContentConfig = null;
44 }
45
46 _dispatchEvent(layersCount) {
47 this.eventBus.dispatch("layersloaded", {
48 source: this,
49 layersCount
50 });
51 }
52
53 _bindLink(element, {
54 groupId,
55 input
56 }) {
57 const setVisibility = () => {
58 this._optionalContentConfig.setVisibility(groupId, input.checked);
59
60 this.eventBus.dispatch("optionalcontentconfig", {
61 source: this,
62 promise: Promise.resolve(this._optionalContentConfig)
63 });
64 };
65
66 element.onclick = evt => {
67 if (evt.target === input) {
68 setVisibility();
69 return true;
70 } else if (evt.target !== element) {
71 return true;
72 }
73
74 input.checked = !input.checked;
75 setVisibility();
76 return false;
77 };
78 }
79
80 async _setNestedName(element, {
81 name = null
82 }) {
83 if (typeof name === "string") {
84 element.textContent = this._normalizeTextContent(name);
85 return;
86 }
87
88 element.textContent = await this.l10n.get("additional_layers");
89 element.style.fontStyle = "italic";
90 }
91
92 _addToggleButton(div, {
93 name = null
94 }) {
95 super._addToggleButton(div, name === null);
96 }
97
98 _toggleAllTreeItems() {
99 if (!this._optionalContentConfig) {
100 return;
101 }
102
103 super._toggleAllTreeItems();
104 }
105
106 render({
107 optionalContentConfig,
108 pdfDocument
109 }) {
110 if (this._optionalContentConfig) {
111 this.reset();
112 }
113
114 this._optionalContentConfig = optionalContentConfig || null;
115 this._pdfDocument = pdfDocument || null;
116 const groups = optionalContentConfig?.getOrder();
117
118 if (!groups) {
119 this._dispatchEvent(0);
120
121 return;
122 }
123
124 const fragment = document.createDocumentFragment(),
125 queue = [{
126 parent: fragment,
127 groups
128 }];
129 let layersCount = 0,
130 hasAnyNesting = false;
131
132 while (queue.length > 0) {
133 const levelData = queue.shift();
134
135 for (const groupId of levelData.groups) {
136 const div = document.createElement("div");
137 div.className = "treeItem";
138 const element = document.createElement("a");
139 div.append(element);
140
141 if (typeof groupId === "object") {
142 hasAnyNesting = true;
143
144 this._addToggleButton(div, groupId);
145
146 this._setNestedName(element, groupId);
147
148 const itemsDiv = document.createElement("div");
149 itemsDiv.className = "treeItems";
150 div.append(itemsDiv);
151 queue.push({
152 parent: itemsDiv,
153 groups: groupId.order
154 });
155 } else {
156 const group = optionalContentConfig.getGroup(groupId);
157 const input = document.createElement("input");
158
159 this._bindLink(element, {
160 groupId,
161 input
162 });
163
164 input.type = "checkbox";
165 input.checked = group.visible;
166 const label = document.createElement("label");
167 label.textContent = this._normalizeTextContent(group.name);
168 label.append(input);
169 element.append(label);
170 layersCount++;
171 }
172
173 levelData.parent.append(div);
174 }
175 }
176
177 this._finishRendering(fragment, layersCount, hasAnyNesting);
178 }
179
180 async _resetLayers() {
181 if (!this._optionalContentConfig) {
182 return;
183 }
184
185 const optionalContentConfig = await this._pdfDocument.getOptionalContentConfig();
186 this.eventBus.dispatch("optionalcontentconfig", {
187 source: this,
188 promise: Promise.resolve(optionalContentConfig)
189 });
190 this.render({
191 optionalContentConfig,
192 pdfDocument: this._pdfDocument
193 });
194 }
195
196}
197
198exports.PDFLayerViewer = PDFLayerViewer;
\No newline at end of file