pdf_outline_viewer.js 6.0 KB

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