pdf_layer_viewer.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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("resetlayers", this._resetLayers.bind(this));
  33. this.eventBus._on("togglelayerstree", this._toggleAllTreeItems.bind(this));
  34. }
  35. reset() {
  36. super.reset();
  37. this._optionalContentConfig = null;
  38. }
  39. _dispatchEvent(layersCount) {
  40. this.eventBus.dispatch("layersloaded", {
  41. source: this,
  42. layersCount
  43. });
  44. }
  45. _bindLink(element, {
  46. groupId,
  47. input
  48. }) {
  49. const setVisibility = () => {
  50. this._optionalContentConfig.setVisibility(groupId, input.checked);
  51. this.eventBus.dispatch("optionalcontentconfig", {
  52. source: this,
  53. promise: Promise.resolve(this._optionalContentConfig)
  54. });
  55. };
  56. element.onclick = evt => {
  57. if (evt.target === input) {
  58. setVisibility();
  59. return true;
  60. } else if (evt.target !== element) {
  61. return true;
  62. }
  63. input.checked = !input.checked;
  64. setVisibility();
  65. return false;
  66. };
  67. }
  68. async _setNestedName(element, {
  69. name = null
  70. }) {
  71. if (typeof name === "string") {
  72. element.textContent = this._normalizeTextContent(name);
  73. return;
  74. }
  75. element.textContent = await this.l10n.get("additional_layers");
  76. element.style.fontStyle = "italic";
  77. }
  78. _addToggleButton(div, {
  79. name = null
  80. }) {
  81. super._addToggleButton(div, name === null);
  82. }
  83. _toggleAllTreeItems() {
  84. if (!this._optionalContentConfig) {
  85. return;
  86. }
  87. super._toggleAllTreeItems();
  88. }
  89. render({
  90. optionalContentConfig,
  91. pdfDocument
  92. }) {
  93. if (this._optionalContentConfig) {
  94. this.reset();
  95. }
  96. this._optionalContentConfig = optionalContentConfig || null;
  97. this._pdfDocument = pdfDocument || null;
  98. const groups = optionalContentConfig?.getOrder();
  99. if (!groups) {
  100. this._dispatchEvent(0);
  101. return;
  102. }
  103. const fragment = document.createDocumentFragment(),
  104. queue = [{
  105. parent: fragment,
  106. groups
  107. }];
  108. let layersCount = 0,
  109. hasAnyNesting = false;
  110. while (queue.length > 0) {
  111. const levelData = queue.shift();
  112. for (const groupId of levelData.groups) {
  113. const div = document.createElement("div");
  114. div.className = "treeItem";
  115. const element = document.createElement("a");
  116. div.appendChild(element);
  117. if (typeof groupId === "object") {
  118. hasAnyNesting = true;
  119. this._addToggleButton(div, groupId);
  120. this._setNestedName(element, groupId);
  121. const itemsDiv = document.createElement("div");
  122. itemsDiv.className = "treeItems";
  123. div.appendChild(itemsDiv);
  124. queue.push({
  125. parent: itemsDiv,
  126. groups: groupId.order
  127. });
  128. } else {
  129. const group = optionalContentConfig.getGroup(groupId);
  130. const input = document.createElement("input");
  131. this._bindLink(element, {
  132. groupId,
  133. input
  134. });
  135. input.type = "checkbox";
  136. input.id = groupId;
  137. input.checked = group.visible;
  138. const label = document.createElement("label");
  139. label.setAttribute("for", groupId);
  140. label.textContent = this._normalizeTextContent(group.name);
  141. element.appendChild(input);
  142. element.appendChild(label);
  143. layersCount++;
  144. }
  145. levelData.parent.appendChild(div);
  146. }
  147. }
  148. this._finishRendering(fragment, layersCount, hasAnyNesting);
  149. }
  150. async _resetLayers() {
  151. if (!this._optionalContentConfig) {
  152. return;
  153. }
  154. const optionalContentConfig = await this._pdfDocument.getOptionalContentConfig();
  155. this.eventBus.dispatch("optionalcontentconfig", {
  156. source: this,
  157. promise: Promise.resolve(optionalContentConfig)
  158. });
  159. this.render({
  160. optionalContentConfig,
  161. pdfDocument: this._pdfDocument
  162. });
  163. }
  164. }
  165. exports.PDFLayerViewer = PDFLayerViewer;