2
0

pdf_layer_viewer.js 5.3 KB

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