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