pdf_outline_viewer.js 6.2 KB

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