2
0

pdf_thumbnail_viewer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFThumbnailViewer = undefined;
  20. var _ui_utils = require('./ui_utils');
  21. var _pdf_thumbnail_view = require('./pdf_thumbnail_view');
  22. var THUMBNAIL_SCROLL_MARGIN = -19;
  23. var PDFThumbnailViewer = function PDFThumbnailViewerClosure() {
  24. function PDFThumbnailViewer(options) {
  25. this.container = options.container;
  26. this.renderingQueue = options.renderingQueue;
  27. this.linkService = options.linkService;
  28. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdated.bind(this));
  29. this._resetView();
  30. }
  31. PDFThumbnailViewer.prototype = {
  32. _scrollUpdated: function PDFThumbnailViewer_scrollUpdated() {
  33. this.renderingQueue.renderHighestPriority();
  34. },
  35. getThumbnail: function PDFThumbnailViewer_getThumbnail(index) {
  36. return this.thumbnails[index];
  37. },
  38. _getVisibleThumbs: function PDFThumbnailViewer_getVisibleThumbs() {
  39. return (0, _ui_utils.getVisibleElements)(this.container, this.thumbnails);
  40. },
  41. scrollThumbnailIntoView: function PDFThumbnailViewer_scrollThumbnailIntoView(page) {
  42. var selected = document.querySelector('.thumbnail.selected');
  43. if (selected) {
  44. selected.classList.remove('selected');
  45. }
  46. var thumbnail = document.querySelector('div.thumbnail[data-page-number="' + page + '"]');
  47. if (thumbnail) {
  48. thumbnail.classList.add('selected');
  49. }
  50. var visibleThumbs = this._getVisibleThumbs();
  51. var numVisibleThumbs = visibleThumbs.views.length;
  52. if (numVisibleThumbs > 0) {
  53. var first = visibleThumbs.first.id;
  54. var last = numVisibleThumbs > 1 ? visibleThumbs.last.id : first;
  55. if (page <= first || page >= last) {
  56. (0, _ui_utils.scrollIntoView)(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN });
  57. }
  58. }
  59. },
  60. get pagesRotation() {
  61. return this._pagesRotation;
  62. },
  63. set pagesRotation(rotation) {
  64. this._pagesRotation = rotation;
  65. for (var i = 0, l = this.thumbnails.length; i < l; i++) {
  66. var thumb = this.thumbnails[i];
  67. thumb.update(rotation);
  68. }
  69. },
  70. cleanup: function PDFThumbnailViewer_cleanup() {
  71. var tempCanvas = _pdf_thumbnail_view.PDFThumbnailView.tempImageCache;
  72. if (tempCanvas) {
  73. tempCanvas.width = 0;
  74. tempCanvas.height = 0;
  75. }
  76. _pdf_thumbnail_view.PDFThumbnailView.tempImageCache = null;
  77. },
  78. _resetView: function PDFThumbnailViewer_resetView() {
  79. this.thumbnails = [];
  80. this._pageLabels = null;
  81. this._pagesRotation = 0;
  82. this._pagesRequests = [];
  83. this.container.textContent = '';
  84. },
  85. setDocument: function PDFThumbnailViewer_setDocument(pdfDocument) {
  86. var _this = this;
  87. if (this.pdfDocument) {
  88. this._cancelRendering();
  89. this._resetView();
  90. }
  91. this.pdfDocument = pdfDocument;
  92. if (!pdfDocument) {
  93. return Promise.resolve();
  94. }
  95. return pdfDocument.getPage(1).then(function (firstPage) {
  96. var pagesCount = pdfDocument.numPages;
  97. var viewport = firstPage.getViewport(1.0);
  98. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  99. var thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  100. container: _this.container,
  101. id: pageNum,
  102. defaultViewport: viewport.clone(),
  103. linkService: _this.linkService,
  104. renderingQueue: _this.renderingQueue,
  105. disableCanvasToImageConversion: false
  106. });
  107. _this.thumbnails.push(thumbnail);
  108. }
  109. });
  110. },
  111. _cancelRendering: function PDFThumbnailViewer_cancelRendering() {
  112. for (var i = 0, ii = this.thumbnails.length; i < ii; i++) {
  113. if (this.thumbnails[i]) {
  114. this.thumbnails[i].cancelRendering();
  115. }
  116. }
  117. },
  118. setPageLabels: function PDFThumbnailViewer_setPageLabels(labels) {
  119. if (!this.pdfDocument) {
  120. return;
  121. }
  122. if (!labels) {
  123. this._pageLabels = null;
  124. } else if (!(labels instanceof Array && this.pdfDocument.numPages === labels.length)) {
  125. this._pageLabels = null;
  126. console.error('PDFThumbnailViewer_setPageLabels: Invalid page labels.');
  127. } else {
  128. this._pageLabels = labels;
  129. }
  130. for (var i = 0, ii = this.thumbnails.length; i < ii; i++) {
  131. var thumbnailView = this.thumbnails[i];
  132. var label = this._pageLabels && this._pageLabels[i];
  133. thumbnailView.setPageLabel(label);
  134. }
  135. },
  136. _ensurePdfPageLoaded: function _ensurePdfPageLoaded(thumbView) {
  137. var _this2 = this;
  138. if (thumbView.pdfPage) {
  139. return Promise.resolve(thumbView.pdfPage);
  140. }
  141. var pageNumber = thumbView.id;
  142. if (this._pagesRequests[pageNumber]) {
  143. return this._pagesRequests[pageNumber];
  144. }
  145. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  146. thumbView.setPdfPage(pdfPage);
  147. _this2._pagesRequests[pageNumber] = null;
  148. return pdfPage;
  149. });
  150. this._pagesRequests[pageNumber] = promise;
  151. return promise;
  152. },
  153. forceRendering: function forceRendering() {
  154. var _this3 = this;
  155. var visibleThumbs = this._getVisibleThumbs();
  156. var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this.thumbnails, this.scroll.down);
  157. if (thumbView) {
  158. this._ensurePdfPageLoaded(thumbView).then(function () {
  159. _this3.renderingQueue.renderView(thumbView);
  160. });
  161. return true;
  162. }
  163. return false;
  164. }
  165. };
  166. return PDFThumbnailViewer;
  167. }();
  168. exports.PDFThumbnailViewer = PDFThumbnailViewer;