pdf_outline_viewer.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, _ref2) {
  61. var _this = this;
  62. var url = _ref2.url,
  63. newWindow = _ref2.newWindow,
  64. dest = _ref2.dest;
  65. var linkService = this.linkService;
  66. if (url) {
  67. (0, _pdf.addLinkAttributes)(element, {
  68. url: url,
  69. target: newWindow ? _pdf.LinkTarget.BLANK : linkService.externalLinkTarget,
  70. rel: linkService.externalLinkRel
  71. });
  72. return;
  73. }
  74. element.href = this.linkService.getDestinationHash(dest);
  75. element.onclick = function () {
  76. if (dest) {
  77. _this.linkService.navigateTo(dest);
  78. }
  79. return false;
  80. };
  81. }
  82. }, {
  83. key: '_setStyles',
  84. value: function _setStyles(element, _ref3) {
  85. var bold = _ref3.bold,
  86. italic = _ref3.italic;
  87. var styleStr = '';
  88. if (bold) {
  89. styleStr += 'font-weight: bold;';
  90. }
  91. if (italic) {
  92. styleStr += 'font-style: italic;';
  93. }
  94. if (styleStr) {
  95. element.setAttribute('style', styleStr);
  96. }
  97. }
  98. }, {
  99. key: '_addToggleButton',
  100. value: function _addToggleButton(div) {
  101. var _this2 = this;
  102. var toggler = document.createElement('div');
  103. toggler.className = 'outlineItemToggler';
  104. toggler.onclick = function (evt) {
  105. evt.stopPropagation();
  106. toggler.classList.toggle('outlineItemsHidden');
  107. if (evt.shiftKey) {
  108. var shouldShowAll = !toggler.classList.contains('outlineItemsHidden');
  109. _this2._toggleOutlineItem(div, shouldShowAll);
  110. }
  111. };
  112. div.insertBefore(toggler, div.firstChild);
  113. }
  114. }, {
  115. key: '_toggleOutlineItem',
  116. value: function _toggleOutlineItem(root, show) {
  117. this.lastToggleIsShow = show;
  118. var togglers = root.querySelectorAll('.outlineItemToggler');
  119. for (var i = 0, ii = togglers.length; i < ii; ++i) {
  120. togglers[i].classList[show ? 'remove' : 'add']('outlineItemsHidden');
  121. }
  122. }
  123. }, {
  124. key: 'toggleOutlineTree',
  125. value: function toggleOutlineTree() {
  126. if (!this.outline) {
  127. return;
  128. }
  129. this._toggleOutlineItem(this.container, !this.lastToggleIsShow);
  130. }
  131. }, {
  132. key: 'render',
  133. value: function render(_ref4) {
  134. var outline = _ref4.outline;
  135. var outlineCount = 0;
  136. if (this.outline) {
  137. this.reset();
  138. }
  139. this.outline = outline || null;
  140. if (!outline) {
  141. this._dispatchEvent(outlineCount);
  142. return;
  143. }
  144. var fragment = document.createDocumentFragment();
  145. var queue = [{
  146. parent: fragment,
  147. items: this.outline
  148. }];
  149. var hasAnyNesting = false;
  150. while (queue.length > 0) {
  151. var levelData = queue.shift();
  152. for (var i = 0, len = levelData.items.length; i < len; i++) {
  153. var item = levelData.items[i];
  154. var div = document.createElement('div');
  155. div.className = 'outlineItem';
  156. var element = document.createElement('a');
  157. this._bindLink(element, item);
  158. this._setStyles(element, item);
  159. element.textContent = (0, _pdf.removeNullCharacters)(item.title) || DEFAULT_TITLE;
  160. div.appendChild(element);
  161. if (item.items.length > 0) {
  162. hasAnyNesting = true;
  163. this._addToggleButton(div);
  164. var itemsDiv = document.createElement('div');
  165. itemsDiv.className = 'outlineItems';
  166. div.appendChild(itemsDiv);
  167. queue.push({
  168. parent: itemsDiv,
  169. items: item.items
  170. });
  171. }
  172. levelData.parent.appendChild(div);
  173. outlineCount++;
  174. }
  175. }
  176. if (hasAnyNesting) {
  177. this.container.classList.add('outlineWithDeepNesting');
  178. }
  179. this.container.appendChild(fragment);
  180. this._dispatchEvent(outlineCount);
  181. }
  182. }]);
  183. return PDFOutlineViewer;
  184. }();
  185. exports.PDFOutlineViewer = PDFOutlineViewer;