pdf_outline_viewer.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.PDFOutlineViewer = void 0;
  27. var _pdf = require("../pdf");
  28. var _base_tree_viewer = require("./base_tree_viewer.js");
  29. class PDFOutlineViewer extends _base_tree_viewer.BaseTreeViewer {
  30. constructor(options) {
  31. super(options);
  32. this.linkService = options.linkService;
  33. this.eventBus._on("toggleoutlinetree", this._toggleAllTreeItems.bind(this));
  34. }
  35. reset() {
  36. super.reset();
  37. this._outline = null;
  38. }
  39. _dispatchEvent(outlineCount) {
  40. this.eventBus.dispatch("outlineloaded", {
  41. source: this,
  42. outlineCount
  43. });
  44. }
  45. _bindLink(element, {
  46. url,
  47. newWindow,
  48. dest
  49. }) {
  50. const {
  51. linkService
  52. } = this;
  53. if (url) {
  54. (0, _pdf.addLinkAttributes)(element, {
  55. url,
  56. target: newWindow ? _pdf.LinkTarget.BLANK : linkService.externalLinkTarget,
  57. rel: linkService.externalLinkRel,
  58. enabled: linkService.externalLinkEnabled
  59. });
  60. return;
  61. }
  62. element.href = linkService.getDestinationHash(dest);
  63. element.onclick = () => {
  64. if (dest) {
  65. linkService.navigateTo(dest);
  66. }
  67. return false;
  68. };
  69. }
  70. _setStyles(element, {
  71. bold,
  72. italic
  73. }) {
  74. if (bold) {
  75. element.style.fontWeight = "bold";
  76. }
  77. if (italic) {
  78. element.style.fontStyle = "italic";
  79. }
  80. }
  81. _addToggleButton(div, {
  82. count,
  83. items
  84. }) {
  85. const hidden = count < 0 && Math.abs(count) === items.length;
  86. super._addToggleButton(div, hidden);
  87. }
  88. _toggleAllTreeItems() {
  89. if (!this._outline) {
  90. return;
  91. }
  92. super._toggleAllTreeItems();
  93. }
  94. render({
  95. outline
  96. }) {
  97. if (this._outline) {
  98. this.reset();
  99. }
  100. this._outline = outline || null;
  101. if (!outline) {
  102. this._dispatchEvent(0);
  103. return;
  104. }
  105. const fragment = document.createDocumentFragment();
  106. const queue = [{
  107. parent: fragment,
  108. items: outline
  109. }];
  110. let outlineCount = 0,
  111. hasAnyNesting = false;
  112. while (queue.length > 0) {
  113. const levelData = queue.shift();
  114. for (const item of levelData.items) {
  115. const div = document.createElement("div");
  116. div.className = "treeItem";
  117. const element = document.createElement("a");
  118. this._bindLink(element, item);
  119. this._setStyles(element, item);
  120. element.textContent = this._normalizeTextContent(item.title);
  121. div.appendChild(element);
  122. if (item.items.length > 0) {
  123. hasAnyNesting = true;
  124. this._addToggleButton(div, item);
  125. const itemsDiv = document.createElement("div");
  126. itemsDiv.className = "treeItems";
  127. div.appendChild(itemsDiv);
  128. queue.push({
  129. parent: itemsDiv,
  130. items: item.items
  131. });
  132. }
  133. levelData.parent.appendChild(div);
  134. outlineCount++;
  135. }
  136. }
  137. if (hasAnyNesting) {
  138. this.container.classList.add("treeWithDeepNesting");
  139. this._lastToggleIsShow = fragment.querySelectorAll(".treeItemsHidden").length === 0;
  140. }
  141. this.container.appendChild(fragment);
  142. this._dispatchEvent(outlineCount);
  143. }
  144. }
  145. exports.PDFOutlineViewer = PDFOutlineViewer;