pdf_outline_viewer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, destination = item.dest;
  49. element.href = self.linkService.getDestinationHash(destination);
  50. element.onclick = function () {
  51. if (destination) {
  52. self.linkService.navigateTo(destination);
  53. }
  54. return false;
  55. };
  56. },
  57. _setStyles: function PDFOutlineViewer_setStyles(element, item) {
  58. var styleStr = '';
  59. if (item.bold) {
  60. styleStr += 'font-weight: bold;';
  61. }
  62. if (item.italic) {
  63. styleStr += 'font-style: italic;';
  64. }
  65. if (styleStr) {
  66. element.setAttribute('style', styleStr);
  67. }
  68. },
  69. _addToggleButton: function PDFOutlineViewer_addToggleButton(div) {
  70. var toggler = document.createElement('div');
  71. toggler.className = 'outlineItemToggler';
  72. toggler.onclick = function (event) {
  73. event.stopPropagation();
  74. toggler.classList.toggle('outlineItemsHidden');
  75. if (event.shiftKey) {
  76. var shouldShowAll = !toggler.classList.contains('outlineItemsHidden');
  77. this._toggleOutlineItem(div, shouldShowAll);
  78. }
  79. }.bind(this);
  80. div.insertBefore(toggler, div.firstChild);
  81. },
  82. _toggleOutlineItem: function PDFOutlineViewer_toggleOutlineItem(root, show) {
  83. this.lastToggleIsShow = show;
  84. var togglers = root.querySelectorAll('.outlineItemToggler');
  85. for (var i = 0, ii = togglers.length; i < ii; ++i) {
  86. togglers[i].classList[show ? 'remove' : 'add']('outlineItemsHidden');
  87. }
  88. },
  89. toggleOutlineTree: function PDFOutlineViewer_toggleOutlineTree() {
  90. if (!this.outline) {
  91. return;
  92. }
  93. this._toggleOutlineItem(this.container, !this.lastToggleIsShow);
  94. },
  95. render: function PDFOutlineViewer_render(params) {
  96. var outline = params && params.outline || null;
  97. var outlineCount = 0;
  98. if (this.outline) {
  99. this.reset();
  100. }
  101. this.outline = outline;
  102. if (!outline) {
  103. this._dispatchEvent(outlineCount);
  104. return;
  105. }
  106. var fragment = document.createDocumentFragment();
  107. var queue = [{
  108. parent: fragment,
  109. items: this.outline
  110. }];
  111. var hasAnyNesting = false;
  112. while (queue.length > 0) {
  113. var levelData = queue.shift();
  114. for (var i = 0, len = levelData.items.length; i < len; i++) {
  115. var item = levelData.items[i];
  116. var div = document.createElement('div');
  117. div.className = 'outlineItem';
  118. var element = document.createElement('a');
  119. this._bindLink(element, item);
  120. this._setStyles(element, item);
  121. element.textContent = pdfjsLib.removeNullCharacters(item.title) || DEFAULT_TITLE;
  122. div.appendChild(element);
  123. if (item.items.length > 0) {
  124. hasAnyNesting = true;
  125. this._addToggleButton(div);
  126. var itemsDiv = document.createElement('div');
  127. itemsDiv.className = 'outlineItems';
  128. div.appendChild(itemsDiv);
  129. queue.push({
  130. parent: itemsDiv,
  131. items: item.items
  132. });
  133. }
  134. levelData.parent.appendChild(div);
  135. outlineCount++;
  136. }
  137. }
  138. if (hasAnyNesting) {
  139. this.container.classList.add('outlineWithDeepNesting');
  140. }
  141. this.container.appendChild(fragment);
  142. this._dispatchEvent(outlineCount);
  143. }
  144. };
  145. return PDFOutlineViewer;
  146. }();
  147. exports.PDFOutlineViewer = PDFOutlineViewer;