pdf_thumbnail_viewer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _ui_utils = require('./ui_utils');
  22. var _pdf_thumbnail_view = require('./pdf_thumbnail_view');
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. var THUMBNAIL_SCROLL_MARGIN = -19;
  25. var PDFThumbnailViewer = function () {
  26. function PDFThumbnailViewer(_ref) {
  27. var container = _ref.container,
  28. linkService = _ref.linkService,
  29. renderingQueue = _ref.renderingQueue,
  30. _ref$l10n = _ref.l10n,
  31. l10n = _ref$l10n === undefined ? _ui_utils.NullL10n : _ref$l10n;
  32. _classCallCheck(this, PDFThumbnailViewer);
  33. this.container = container;
  34. this.linkService = linkService;
  35. this.renderingQueue = renderingQueue;
  36. this.l10n = l10n;
  37. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdated.bind(this));
  38. this._resetView();
  39. }
  40. _createClass(PDFThumbnailViewer, [{
  41. key: '_scrollUpdated',
  42. value: function _scrollUpdated() {
  43. this.renderingQueue.renderHighestPriority();
  44. }
  45. }, {
  46. key: 'getThumbnail',
  47. value: function getThumbnail(index) {
  48. return this._thumbnails[index];
  49. }
  50. }, {
  51. key: '_getVisibleThumbs',
  52. value: function _getVisibleThumbs() {
  53. return (0, _ui_utils.getVisibleElements)(this.container, this._thumbnails);
  54. }
  55. }, {
  56. key: 'scrollThumbnailIntoView',
  57. value: function scrollThumbnailIntoView(page) {
  58. var selected = document.querySelector('.thumbnail.selected');
  59. if (selected) {
  60. selected.classList.remove('selected');
  61. }
  62. var thumbnail = document.querySelector('div.thumbnail[data-page-number="' + page + '"]');
  63. if (thumbnail) {
  64. thumbnail.classList.add('selected');
  65. }
  66. var visibleThumbs = this._getVisibleThumbs();
  67. var numVisibleThumbs = visibleThumbs.views.length;
  68. if (numVisibleThumbs > 0) {
  69. var first = visibleThumbs.first.id;
  70. var last = numVisibleThumbs > 1 ? visibleThumbs.last.id : first;
  71. if (page <= first || page >= last) {
  72. (0, _ui_utils.scrollIntoView)(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN });
  73. }
  74. }
  75. }
  76. }, {
  77. key: 'cleanup',
  78. value: function cleanup() {
  79. _pdf_thumbnail_view.PDFThumbnailView.cleanup();
  80. }
  81. }, {
  82. key: '_resetView',
  83. value: function _resetView() {
  84. this._thumbnails = [];
  85. this._pageLabels = null;
  86. this._pagesRotation = 0;
  87. this._pagesRequests = [];
  88. this.container.textContent = '';
  89. }
  90. }, {
  91. key: 'setDocument',
  92. value: function setDocument(pdfDocument) {
  93. var _this = this;
  94. if (this.pdfDocument) {
  95. this._cancelRendering();
  96. this._resetView();
  97. }
  98. this.pdfDocument = pdfDocument;
  99. if (!pdfDocument) {
  100. return;
  101. }
  102. pdfDocument.getPage(1).then(function (firstPage) {
  103. var pagesCount = pdfDocument.numPages;
  104. var viewport = firstPage.getViewport(1.0);
  105. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  106. var thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  107. container: _this.container,
  108. id: pageNum,
  109. defaultViewport: viewport.clone(),
  110. linkService: _this.linkService,
  111. renderingQueue: _this.renderingQueue,
  112. disableCanvasToImageConversion: false,
  113. l10n: _this.l10n
  114. });
  115. _this._thumbnails.push(thumbnail);
  116. }
  117. }).catch(function (reason) {
  118. console.error('Unable to initialize thumbnail viewer', reason);
  119. });
  120. }
  121. }, {
  122. key: '_cancelRendering',
  123. value: function _cancelRendering() {
  124. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  125. if (this._thumbnails[i]) {
  126. this._thumbnails[i].cancelRendering();
  127. }
  128. }
  129. }
  130. }, {
  131. key: 'setPageLabels',
  132. value: function setPageLabels(labels) {
  133. if (!this.pdfDocument) {
  134. return;
  135. }
  136. if (!labels) {
  137. this._pageLabels = null;
  138. } else if (!(labels instanceof Array && this.pdfDocument.numPages === labels.length)) {
  139. this._pageLabels = null;
  140. console.error('PDFThumbnailViewer_setPageLabels: Invalid page labels.');
  141. } else {
  142. this._pageLabels = labels;
  143. }
  144. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  145. var label = this._pageLabels && this._pageLabels[i];
  146. this._thumbnails[i].setPageLabel(label);
  147. }
  148. }
  149. }, {
  150. key: '_ensurePdfPageLoaded',
  151. value: function _ensurePdfPageLoaded(thumbView) {
  152. var _this2 = this;
  153. if (thumbView.pdfPage) {
  154. return Promise.resolve(thumbView.pdfPage);
  155. }
  156. var pageNumber = thumbView.id;
  157. if (this._pagesRequests[pageNumber]) {
  158. return this._pagesRequests[pageNumber];
  159. }
  160. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  161. thumbView.setPdfPage(pdfPage);
  162. _this2._pagesRequests[pageNumber] = null;
  163. return pdfPage;
  164. }).catch(function (reason) {
  165. console.error('Unable to get page for thumb view', reason);
  166. _this2._pagesRequests[pageNumber] = null;
  167. });
  168. this._pagesRequests[pageNumber] = promise;
  169. return promise;
  170. }
  171. }, {
  172. key: 'forceRendering',
  173. value: function forceRendering() {
  174. var _this3 = this;
  175. var visibleThumbs = this._getVisibleThumbs();
  176. var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, this.scroll.down);
  177. if (thumbView) {
  178. this._ensurePdfPageLoaded(thumbView).then(function () {
  179. _this3.renderingQueue.renderView(thumbView);
  180. });
  181. return true;
  182. }
  183. return false;
  184. }
  185. }, {
  186. key: 'pagesRotation',
  187. get: function get() {
  188. return this._pagesRotation;
  189. },
  190. set: function set(rotation) {
  191. if (!(typeof rotation === 'number' && rotation % 90 === 0)) {
  192. throw new Error('Invalid thumbnails rotation angle.');
  193. }
  194. if (!this.pdfDocument) {
  195. return;
  196. }
  197. this._pagesRotation = rotation;
  198. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  199. this._thumbnails[i].update(rotation);
  200. }
  201. }
  202. }]);
  203. return PDFThumbnailViewer;
  204. }();
  205. exports.PDFThumbnailViewer = PDFThumbnailViewer;