pdf_thumbnail_viewer.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 uiUtils = require('./ui_utils.js');
  17. var pdfThumbnailView = require('./pdf_thumbnail_view.js');
  18. var watchScroll = uiUtils.watchScroll;
  19. var getVisibleElements = uiUtils.getVisibleElements;
  20. var scrollIntoView = uiUtils.scrollIntoView;
  21. var PDFThumbnailView = pdfThumbnailView.PDFThumbnailView;
  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 = 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 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. 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 = PDFThumbnailView.tempImageCache;
  72. if (tempCanvas) {
  73. tempCanvas.width = 0;
  74. tempCanvas.height = 0;
  75. }
  76. 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. if (this.pdfDocument) {
  87. this._cancelRendering();
  88. this._resetView();
  89. }
  90. this.pdfDocument = pdfDocument;
  91. if (!pdfDocument) {
  92. return Promise.resolve();
  93. }
  94. return pdfDocument.getPage(1).then(function (firstPage) {
  95. var pagesCount = pdfDocument.numPages;
  96. var viewport = firstPage.getViewport(1.0);
  97. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  98. var thumbnail = new PDFThumbnailView({
  99. container: this.container,
  100. id: pageNum,
  101. defaultViewport: viewport.clone(),
  102. linkService: this.linkService,
  103. renderingQueue: this.renderingQueue,
  104. disableCanvasToImageConversion: false
  105. });
  106. this.thumbnails.push(thumbnail);
  107. }
  108. }.bind(this));
  109. },
  110. _cancelRendering: function PDFThumbnailViewer_cancelRendering() {
  111. for (var i = 0, ii = this.thumbnails.length; i < ii; i++) {
  112. if (this.thumbnails[i]) {
  113. this.thumbnails[i].cancelRendering();
  114. }
  115. }
  116. },
  117. setPageLabels: function PDFThumbnailViewer_setPageLabels(labels) {
  118. if (!this.pdfDocument) {
  119. return;
  120. }
  121. if (!labels) {
  122. this._pageLabels = null;
  123. } else if (!(labels instanceof Array && this.pdfDocument.numPages === labels.length)) {
  124. this._pageLabels = null;
  125. console.error('PDFThumbnailViewer_setPageLabels: Invalid page labels.');
  126. } else {
  127. this._pageLabels = labels;
  128. }
  129. for (var i = 0, ii = this.thumbnails.length; i < ii; i++) {
  130. var thumbnailView = this.thumbnails[i];
  131. var label = this._pageLabels && this._pageLabels[i];
  132. thumbnailView.setPageLabel(label);
  133. }
  134. },
  135. _ensurePdfPageLoaded: function PDFThumbnailViewer_ensurePdfPageLoaded(thumbView) {
  136. if (thumbView.pdfPage) {
  137. return Promise.resolve(thumbView.pdfPage);
  138. }
  139. var pageNumber = thumbView.id;
  140. if (this._pagesRequests[pageNumber]) {
  141. return this._pagesRequests[pageNumber];
  142. }
  143. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  144. thumbView.setPdfPage(pdfPage);
  145. this._pagesRequests[pageNumber] = null;
  146. return pdfPage;
  147. }.bind(this));
  148. this._pagesRequests[pageNumber] = promise;
  149. return promise;
  150. },
  151. forceRendering: function () {
  152. var visibleThumbs = this._getVisibleThumbs();
  153. var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this.thumbnails, this.scroll.down);
  154. if (thumbView) {
  155. this._ensurePdfPageLoaded(thumbView).then(function () {
  156. this.renderingQueue.renderView(thumbView);
  157. }.bind(this));
  158. return true;
  159. }
  160. return false;
  161. }
  162. };
  163. return PDFThumbnailViewer;
  164. }();
  165. exports.PDFThumbnailViewer = PDFThumbnailViewer;