pdf_outline_viewer.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. var pdfjsLib = require('./pdfjs.js');
  17. var PDFJS = pdfjsLib.PDFJS;
  18. var DEFAULT_TITLE = '\u2013';
  19. var PDFOutlineViewer = function PDFOutlineViewerClosure() {
  20. function PDFOutlineViewer(options) {
  21. this.outline = null;
  22. this.lastToggleIsShow = true;
  23. this.container = options.container;
  24. this.linkService = options.linkService;
  25. this.eventBus = options.eventBus;
  26. }
  27. PDFOutlineViewer.prototype = {
  28. reset: function PDFOutlineViewer_reset() {
  29. this.outline = null;
  30. this.lastToggleIsShow = true;
  31. this.container.textContent = '';
  32. this.container.classList.remove('outlineWithDeepNesting');
  33. },
  34. _dispatchEvent: function PDFOutlineViewer_dispatchEvent(outlineCount) {
  35. this.eventBus.dispatch('outlineloaded', {
  36. source: this,
  37. outlineCount: outlineCount
  38. });
  39. },
  40. _bindLink: function PDFOutlineViewer_bindLink(element, item) {
  41. if (item.url) {
  42. pdfjsLib.addLinkAttributes(element, {
  43. url: item.url,
  44. target: item.newWindow ? PDFJS.LinkTarget.BLANK : undefined
  45. });
  46. return;
  47. }
  48. var self = this,
  49. destination = item.dest;
  50. element.href = self.linkService.getDestinationHash(destination);
  51. element.onclick = function () {
  52. if (destination) {
  53. self.linkService.navigateTo(destination);
  54. }
  55. return false;
  56. };
  57. },
  58. _setStyles: function PDFOutlineViewer_setStyles(element, item) {
  59. var styleStr = '';
  60. if (item.bold) {
  61. styleStr += 'font-weight: bold;';
  62. }
  63. if (item.italic) {
  64. styleStr += 'font-style: italic;';
  65. }
  66. if (styleStr) {
  67. element.setAttribute('style', styleStr);
  68. }
  69. },
  70. _addToggleButton: function PDFOutlineViewer_addToggleButton(div) {
  71. var toggler = document.createElement('div');
  72. toggler.className = 'outlineItemToggler';
  73. toggler.onclick = function (event) {
  74. event.stopPropagation();
  75. toggler.classList.toggle('outlineItemsHidden');
  76. if (event.shiftKey) {
  77. var shouldShowAll = !toggler.classList.contains('outlineItemsHidden');
  78. this._toggleOutlineItem(div, shouldShowAll);
  79. }
  80. }.bind(this);
  81. div.insertBefore(toggler, div.firstChild);
  82. },
  83. _toggleOutlineItem: function PDFOutlineViewer_toggleOutlineItem(root, show) {
  84. this.lastToggleIsShow = show;
  85. var togglers = root.querySelectorAll('.outlineItemToggler');
  86. for (var i = 0, ii = togglers.length; i < ii; ++i) {
  87. togglers[i].classList[show ? 'remove' : 'add']('outlineItemsHidden');
  88. }
  89. },
  90. toggleOutlineTree: function PDFOutlineViewer_toggleOutlineTree() {
  91. if (!this.outline) {
  92. return;
  93. }
  94. this._toggleOutlineItem(this.container, !this.lastToggleIsShow);
  95. },
  96. render: function PDFOutlineViewer_render(params) {
  97. var outline = params && params.outline || null;
  98. var outlineCount = 0;
  99. if (this.outline) {
  100. this.reset();
  101. }
  102. this.outline = outline;
  103. if (!outline) {
  104. this._dispatchEvent(outlineCount);
  105. return;
  106. }
  107. var fragment = document.createDocumentFragment();
  108. var queue = [{
  109. parent: fragment,
  110. items: this.outline
  111. }];
  112. var hasAnyNesting = false;
  113. while (queue.length > 0) {
  114. var levelData = queue.shift();
  115. for (var i = 0, len = levelData.items.length; i < len; i++) {
  116. var item = levelData.items[i];
  117. var div = document.createElement('div');
  118. div.className = 'outlineItem';
  119. var element = document.createElement('a');
  120. this._bindLink(element, item);
  121. this._setStyles(element, item);
  122. element.textContent = pdfjsLib.removeNullCharacters(item.title) || DEFAULT_TITLE;
  123. div.appendChild(element);
  124. if (item.items.length > 0) {
  125. hasAnyNesting = true;
  126. this._addToggleButton(div);
  127. var itemsDiv = document.createElement('div');
  128. itemsDiv.className = 'outlineItems';
  129. div.appendChild(itemsDiv);
  130. queue.push({
  131. parent: itemsDiv,
  132. items: item.items
  133. });
  134. }
  135. levelData.parent.appendChild(div);
  136. outlineCount++;
  137. }
  138. }
  139. if (hasAnyNesting) {
  140. this.container.classList.add('outlineWithDeepNesting');
  141. }
  142. this.container.appendChild(fragment);
  143. this._dispatchEvent(outlineCount);
  144. }
  145. };
  146. return PDFOutlineViewer;
  147. }();
  148. exports.PDFOutlineViewer = PDFOutlineViewer;