pdf_thumbnail_viewer.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. var shouldScroll = false;
  72. if (page <= first || page >= last) {
  73. shouldScroll = true;
  74. } else {
  75. visibleThumbs.views.some(function (view) {
  76. if (view.id !== page) {
  77. return false;
  78. }
  79. shouldScroll = view.percent < 100;
  80. return true;
  81. });
  82. }
  83. if (shouldScroll) {
  84. (0, _ui_utils.scrollIntoView)(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN });
  85. }
  86. }
  87. }
  88. }, {
  89. key: 'cleanup',
  90. value: function cleanup() {
  91. _pdf_thumbnail_view.PDFThumbnailView.cleanup();
  92. }
  93. }, {
  94. key: '_resetView',
  95. value: function _resetView() {
  96. this._thumbnails = [];
  97. this._pageLabels = null;
  98. this._pagesRotation = 0;
  99. this._pagesRequests = [];
  100. this.container.textContent = '';
  101. }
  102. }, {
  103. key: 'setDocument',
  104. value: function setDocument(pdfDocument) {
  105. var _this = this;
  106. if (this.pdfDocument) {
  107. this._cancelRendering();
  108. this._resetView();
  109. }
  110. this.pdfDocument = pdfDocument;
  111. if (!pdfDocument) {
  112. return;
  113. }
  114. pdfDocument.getPage(1).then(function (firstPage) {
  115. var pagesCount = pdfDocument.numPages;
  116. var viewport = firstPage.getViewport(1.0);
  117. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  118. var thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  119. container: _this.container,
  120. id: pageNum,
  121. defaultViewport: viewport.clone(),
  122. linkService: _this.linkService,
  123. renderingQueue: _this.renderingQueue,
  124. disableCanvasToImageConversion: false,
  125. l10n: _this.l10n
  126. });
  127. _this._thumbnails.push(thumbnail);
  128. }
  129. }).catch(function (reason) {
  130. console.error('Unable to initialize thumbnail viewer', reason);
  131. });
  132. }
  133. }, {
  134. key: '_cancelRendering',
  135. value: function _cancelRendering() {
  136. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  137. if (this._thumbnails[i]) {
  138. this._thumbnails[i].cancelRendering();
  139. }
  140. }
  141. }
  142. }, {
  143. key: 'setPageLabels',
  144. value: function setPageLabels(labels) {
  145. if (!this.pdfDocument) {
  146. return;
  147. }
  148. if (!labels) {
  149. this._pageLabels = null;
  150. } else if (!(labels instanceof Array && this.pdfDocument.numPages === labels.length)) {
  151. this._pageLabels = null;
  152. console.error('PDFThumbnailViewer_setPageLabels: Invalid page labels.');
  153. } else {
  154. this._pageLabels = labels;
  155. }
  156. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  157. var label = this._pageLabels && this._pageLabels[i];
  158. this._thumbnails[i].setPageLabel(label);
  159. }
  160. }
  161. }, {
  162. key: '_ensurePdfPageLoaded',
  163. value: function _ensurePdfPageLoaded(thumbView) {
  164. var _this2 = this;
  165. if (thumbView.pdfPage) {
  166. return Promise.resolve(thumbView.pdfPage);
  167. }
  168. var pageNumber = thumbView.id;
  169. if (this._pagesRequests[pageNumber]) {
  170. return this._pagesRequests[pageNumber];
  171. }
  172. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  173. thumbView.setPdfPage(pdfPage);
  174. _this2._pagesRequests[pageNumber] = null;
  175. return pdfPage;
  176. }).catch(function (reason) {
  177. console.error('Unable to get page for thumb view', reason);
  178. _this2._pagesRequests[pageNumber] = null;
  179. });
  180. this._pagesRequests[pageNumber] = promise;
  181. return promise;
  182. }
  183. }, {
  184. key: 'forceRendering',
  185. value: function forceRendering() {
  186. var _this3 = this;
  187. var visibleThumbs = this._getVisibleThumbs();
  188. var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, this.scroll.down);
  189. if (thumbView) {
  190. this._ensurePdfPageLoaded(thumbView).then(function () {
  191. _this3.renderingQueue.renderView(thumbView);
  192. });
  193. return true;
  194. }
  195. return false;
  196. }
  197. }, {
  198. key: 'pagesRotation',
  199. get: function get() {
  200. return this._pagesRotation;
  201. },
  202. set: function set(rotation) {
  203. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  204. throw new Error('Invalid thumbnails rotation angle.');
  205. }
  206. if (!this.pdfDocument) {
  207. return;
  208. }
  209. if (this._pagesRotation === rotation) {
  210. return;
  211. }
  212. this._pagesRotation = rotation;
  213. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  214. this._thumbnails[i].update(rotation);
  215. }
  216. }
  217. }]);
  218. return PDFThumbnailViewer;
  219. }();
  220. exports.PDFThumbnailViewer = PDFThumbnailViewer;