2
0

pdf_layer_viewer.js 5.2 KB

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