2
0

pdf_outline_viewer.js 6.3 KB

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