pdf_outline_viewer.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  29. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  30. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  31. var DEFAULT_TITLE = "\u2013";
  32. var PDFOutlineViewer =
  33. /*#__PURE__*/
  34. function () {
  35. function PDFOutlineViewer(_ref) {
  36. var container = _ref.container,
  37. linkService = _ref.linkService,
  38. eventBus = _ref.eventBus;
  39. _classCallCheck(this, PDFOutlineViewer);
  40. this.container = container;
  41. this.linkService = linkService;
  42. this.eventBus = eventBus;
  43. this.reset();
  44. eventBus.on('toggleoutlinetree', this.toggleOutlineTree.bind(this));
  45. }
  46. _createClass(PDFOutlineViewer, [{
  47. key: "reset",
  48. value: function reset() {
  49. this.outline = null;
  50. this.lastToggleIsShow = true;
  51. this.container.textContent = '';
  52. this.container.classList.remove('outlineWithDeepNesting');
  53. }
  54. }, {
  55. key: "_dispatchEvent",
  56. value: function _dispatchEvent(outlineCount) {
  57. this.eventBus.dispatch('outlineloaded', {
  58. source: this,
  59. outlineCount: outlineCount
  60. });
  61. }
  62. }, {
  63. key: "_bindLink",
  64. value: function _bindLink(element, _ref2) {
  65. var url = _ref2.url,
  66. newWindow = _ref2.newWindow,
  67. dest = _ref2.dest;
  68. var linkService = this.linkService;
  69. if (url) {
  70. (0, _pdf.addLinkAttributes)(element, {
  71. url: url,
  72. target: newWindow ? _pdf.LinkTarget.BLANK : linkService.externalLinkTarget,
  73. rel: linkService.externalLinkRel
  74. });
  75. return;
  76. }
  77. element.href = linkService.getDestinationHash(dest);
  78. element.onclick = function () {
  79. if (dest) {
  80. linkService.navigateTo(dest);
  81. }
  82. return false;
  83. };
  84. }
  85. }, {
  86. key: "_setStyles",
  87. value: function _setStyles(element, _ref3) {
  88. var bold = _ref3.bold,
  89. italic = _ref3.italic;
  90. var styleStr = '';
  91. if (bold) {
  92. styleStr += 'font-weight: bold;';
  93. }
  94. if (italic) {
  95. styleStr += 'font-style: italic;';
  96. }
  97. if (styleStr) {
  98. element.setAttribute('style', styleStr);
  99. }
  100. }
  101. }, {
  102. key: "_addToggleButton",
  103. value: function _addToggleButton(div) {
  104. var _this = this;
  105. var toggler = document.createElement('div');
  106. toggler.className = 'outlineItemToggler';
  107. toggler.onclick = function (evt) {
  108. evt.stopPropagation();
  109. toggler.classList.toggle('outlineItemsHidden');
  110. if (evt.shiftKey) {
  111. var shouldShowAll = !toggler.classList.contains('outlineItemsHidden');
  112. _this._toggleOutlineItem(div, shouldShowAll);
  113. }
  114. };
  115. div.insertBefore(toggler, div.firstChild);
  116. }
  117. }, {
  118. key: "_toggleOutlineItem",
  119. value: function _toggleOutlineItem(root) {
  120. var show = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  121. this.lastToggleIsShow = show;
  122. var _iteratorNormalCompletion = true;
  123. var _didIteratorError = false;
  124. var _iteratorError = undefined;
  125. try {
  126. for (var _iterator = root.querySelectorAll('.outlineItemToggler')[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  127. var toggler = _step.value;
  128. toggler.classList.toggle('outlineItemsHidden', !show);
  129. }
  130. } catch (err) {
  131. _didIteratorError = true;
  132. _iteratorError = err;
  133. } finally {
  134. try {
  135. if (!_iteratorNormalCompletion && _iterator.return != null) {
  136. _iterator.return();
  137. }
  138. } finally {
  139. if (_didIteratorError) {
  140. throw _iteratorError;
  141. }
  142. }
  143. }
  144. }
  145. }, {
  146. key: "toggleOutlineTree",
  147. value: function toggleOutlineTree() {
  148. if (!this.outline) {
  149. return;
  150. }
  151. this._toggleOutlineItem(this.container, !this.lastToggleIsShow);
  152. }
  153. }, {
  154. key: "render",
  155. value: function render(_ref4) {
  156. var outline = _ref4.outline;
  157. var outlineCount = 0;
  158. if (this.outline) {
  159. this.reset();
  160. }
  161. this.outline = outline || null;
  162. if (!outline) {
  163. this._dispatchEvent(outlineCount);
  164. return;
  165. }
  166. var fragment = document.createDocumentFragment();
  167. var queue = [{
  168. parent: fragment,
  169. items: this.outline
  170. }];
  171. var hasAnyNesting = false;
  172. while (queue.length > 0) {
  173. var levelData = queue.shift();
  174. for (var i = 0, len = levelData.items.length; i < len; i++) {
  175. var item = levelData.items[i];
  176. var div = document.createElement('div');
  177. div.className = 'outlineItem';
  178. var element = document.createElement('a');
  179. this._bindLink(element, item);
  180. this._setStyles(element, item);
  181. element.textContent = (0, _pdf.removeNullCharacters)(item.title) || DEFAULT_TITLE;
  182. div.appendChild(element);
  183. if (item.items.length > 0) {
  184. hasAnyNesting = true;
  185. this._addToggleButton(div);
  186. var itemsDiv = document.createElement('div');
  187. itemsDiv.className = 'outlineItems';
  188. div.appendChild(itemsDiv);
  189. queue.push({
  190. parent: itemsDiv,
  191. items: item.items
  192. });
  193. }
  194. levelData.parent.appendChild(div);
  195. outlineCount++;
  196. }
  197. }
  198. if (hasAnyNesting) {
  199. this.container.classList.add('outlineWithDeepNesting');
  200. }
  201. this.container.appendChild(fragment);
  202. this._dispatchEvent(outlineCount);
  203. }
  204. }]);
  205. return PDFOutlineViewer;
  206. }();
  207. exports.PDFOutlineViewer = PDFOutlineViewer;