2
0

pdf_outline_viewer.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFOutlineViewer = undefined;
  20. 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; }; }();
  21. var _pdfjs = require('./pdfjs');
  22. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  23. var DEFAULT_TITLE = '\u2013';
  24. var PDFOutlineViewer = function () {
  25. function PDFOutlineViewer(options) {
  26. _classCallCheck(this, PDFOutlineViewer);
  27. this.outline = null;
  28. this.lastToggleIsShow = true;
  29. this.container = options.container;
  30. this.linkService = options.linkService;
  31. this.eventBus = options.eventBus;
  32. }
  33. _createClass(PDFOutlineViewer, [{
  34. key: 'reset',
  35. value: function reset() {
  36. this.outline = null;
  37. this.lastToggleIsShow = true;
  38. this.container.textContent = '';
  39. this.container.classList.remove('outlineWithDeepNesting');
  40. }
  41. }, {
  42. key: '_dispatchEvent',
  43. value: function _dispatchEvent(outlineCount) {
  44. this.eventBus.dispatch('outlineloaded', {
  45. source: this,
  46. outlineCount: outlineCount
  47. });
  48. }
  49. }, {
  50. key: '_bindLink',
  51. value: function _bindLink(element, item) {
  52. var _this = this;
  53. if (item.url) {
  54. (0, _pdfjs.addLinkAttributes)(element, {
  55. url: item.url,
  56. target: item.newWindow ? _pdfjs.PDFJS.LinkTarget.BLANK : undefined
  57. });
  58. return;
  59. }
  60. var destination = item.dest;
  61. element.href = this.linkService.getDestinationHash(destination);
  62. element.onclick = function () {
  63. if (destination) {
  64. _this.linkService.navigateTo(destination);
  65. }
  66. return false;
  67. };
  68. }
  69. }, {
  70. key: '_setStyles',
  71. value: function _setStyles(element, item) {
  72. var styleStr = '';
  73. if (item.bold) {
  74. styleStr += 'font-weight: bold;';
  75. }
  76. if (item.italic) {
  77. styleStr += 'font-style: italic;';
  78. }
  79. if (styleStr) {
  80. element.setAttribute('style', styleStr);
  81. }
  82. }
  83. }, {
  84. key: '_addToggleButton',
  85. value: function _addToggleButton(div) {
  86. var _this2 = this;
  87. var toggler = document.createElement('div');
  88. toggler.className = 'outlineItemToggler';
  89. toggler.onclick = function (evt) {
  90. evt.stopPropagation();
  91. toggler.classList.toggle('outlineItemsHidden');
  92. if (evt.shiftKey) {
  93. var shouldShowAll = !toggler.classList.contains('outlineItemsHidden');
  94. _this2._toggleOutlineItem(div, shouldShowAll);
  95. }
  96. };
  97. div.insertBefore(toggler, div.firstChild);
  98. }
  99. }, {
  100. key: '_toggleOutlineItem',
  101. value: function _toggleOutlineItem(root, show) {
  102. this.lastToggleIsShow = show;
  103. var togglers = root.querySelectorAll('.outlineItemToggler');
  104. for (var i = 0, ii = togglers.length; i < ii; ++i) {
  105. togglers[i].classList[show ? 'remove' : 'add']('outlineItemsHidden');
  106. }
  107. }
  108. }, {
  109. key: 'toggleOutlineTree',
  110. value: function toggleOutlineTree() {
  111. if (!this.outline) {
  112. return;
  113. }
  114. this._toggleOutlineItem(this.container, !this.lastToggleIsShow);
  115. }
  116. }, {
  117. key: 'render',
  118. value: function render() {
  119. var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  120. var outline = params.outline || null;
  121. var outlineCount = 0;
  122. if (this.outline) {
  123. this.reset();
  124. }
  125. this.outline = outline;
  126. if (!outline) {
  127. this._dispatchEvent(outlineCount);
  128. return;
  129. }
  130. var fragment = document.createDocumentFragment();
  131. var queue = [{
  132. parent: fragment,
  133. items: this.outline
  134. }];
  135. var hasAnyNesting = false;
  136. while (queue.length > 0) {
  137. var levelData = queue.shift();
  138. for (var i = 0, len = levelData.items.length; i < len; i++) {
  139. var item = levelData.items[i];
  140. var div = document.createElement('div');
  141. div.className = 'outlineItem';
  142. var element = document.createElement('a');
  143. this._bindLink(element, item);
  144. this._setStyles(element, item);
  145. element.textContent = (0, _pdfjs.removeNullCharacters)(item.title) || DEFAULT_TITLE;
  146. div.appendChild(element);
  147. if (item.items.length > 0) {
  148. hasAnyNesting = true;
  149. this._addToggleButton(div);
  150. var itemsDiv = document.createElement('div');
  151. itemsDiv.className = 'outlineItems';
  152. div.appendChild(itemsDiv);
  153. queue.push({
  154. parent: itemsDiv,
  155. items: item.items
  156. });
  157. }
  158. levelData.parent.appendChild(div);
  159. outlineCount++;
  160. }
  161. }
  162. if (hasAnyNesting) {
  163. this.container.classList.add('outlineWithDeepNesting');
  164. }
  165. this.container.appendChild(fragment);
  166. this._dispatchEvent(outlineCount);
  167. }
  168. }]);
  169. return PDFOutlineViewer;
  170. }();
  171. exports.PDFOutlineViewer = PDFOutlineViewer;