pdf_thumbnail_viewer.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFThumbnailViewer = undefined;
  27. 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; }; }();
  28. var _ui_utils = require('./ui_utils');
  29. var _pdf_thumbnail_view = require('./pdf_thumbnail_view');
  30. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  31. var THUMBNAIL_SCROLL_MARGIN = -19;
  32. var PDFThumbnailViewer = function () {
  33. function PDFThumbnailViewer(_ref) {
  34. var container = _ref.container,
  35. linkService = _ref.linkService,
  36. renderingQueue = _ref.renderingQueue,
  37. _ref$l10n = _ref.l10n,
  38. l10n = _ref$l10n === undefined ? _ui_utils.NullL10n : _ref$l10n;
  39. _classCallCheck(this, PDFThumbnailViewer);
  40. this.container = container;
  41. this.linkService = linkService;
  42. this.renderingQueue = renderingQueue;
  43. this.l10n = l10n;
  44. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdated.bind(this));
  45. this._resetView();
  46. }
  47. _createClass(PDFThumbnailViewer, [{
  48. key: '_scrollUpdated',
  49. value: function _scrollUpdated() {
  50. this.renderingQueue.renderHighestPriority();
  51. }
  52. }, {
  53. key: 'getThumbnail',
  54. value: function getThumbnail(index) {
  55. return this._thumbnails[index];
  56. }
  57. }, {
  58. key: '_getVisibleThumbs',
  59. value: function _getVisibleThumbs() {
  60. return (0, _ui_utils.getVisibleElements)(this.container, this._thumbnails);
  61. }
  62. }, {
  63. key: 'scrollThumbnailIntoView',
  64. value: function scrollThumbnailIntoView(page) {
  65. var selected = document.querySelector('.thumbnail.selected');
  66. if (selected) {
  67. selected.classList.remove('selected');
  68. }
  69. var thumbnail = document.querySelector('div.thumbnail[data-page-number="' + page + '"]');
  70. if (thumbnail) {
  71. thumbnail.classList.add('selected');
  72. }
  73. var visibleThumbs = this._getVisibleThumbs();
  74. var numVisibleThumbs = visibleThumbs.views.length;
  75. if (numVisibleThumbs > 0) {
  76. var first = visibleThumbs.first.id;
  77. var last = numVisibleThumbs > 1 ? visibleThumbs.last.id : first;
  78. var shouldScroll = false;
  79. if (page <= first || page >= last) {
  80. shouldScroll = true;
  81. } else {
  82. visibleThumbs.views.some(function (view) {
  83. if (view.id !== page) {
  84. return false;
  85. }
  86. shouldScroll = view.percent < 100;
  87. return true;
  88. });
  89. }
  90. if (shouldScroll) {
  91. (0, _ui_utils.scrollIntoView)(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN });
  92. }
  93. }
  94. }
  95. }, {
  96. key: 'cleanup',
  97. value: function cleanup() {
  98. _pdf_thumbnail_view.PDFThumbnailView.cleanup();
  99. }
  100. }, {
  101. key: '_resetView',
  102. value: function _resetView() {
  103. this._thumbnails = [];
  104. this._pageLabels = null;
  105. this._pagesRotation = 0;
  106. this._pagesRequests = [];
  107. this.container.textContent = '';
  108. }
  109. }, {
  110. key: 'setDocument',
  111. value: function setDocument(pdfDocument) {
  112. var _this = this;
  113. if (this.pdfDocument) {
  114. this._cancelRendering();
  115. this._resetView();
  116. }
  117. this.pdfDocument = pdfDocument;
  118. if (!pdfDocument) {
  119. return;
  120. }
  121. pdfDocument.getPage(1).then(function (firstPage) {
  122. var pagesCount = pdfDocument.numPages;
  123. var viewport = firstPage.getViewport(1.0);
  124. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  125. var thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  126. container: _this.container,
  127. id: pageNum,
  128. defaultViewport: viewport.clone(),
  129. linkService: _this.linkService,
  130. renderingQueue: _this.renderingQueue,
  131. disableCanvasToImageConversion: false,
  132. l10n: _this.l10n
  133. });
  134. _this._thumbnails.push(thumbnail);
  135. }
  136. }).catch(function (reason) {
  137. console.error('Unable to initialize thumbnail viewer', reason);
  138. });
  139. }
  140. }, {
  141. key: '_cancelRendering',
  142. value: function _cancelRendering() {
  143. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  144. if (this._thumbnails[i]) {
  145. this._thumbnails[i].cancelRendering();
  146. }
  147. }
  148. }
  149. }, {
  150. key: 'setPageLabels',
  151. value: function setPageLabels(labels) {
  152. if (!this.pdfDocument) {
  153. return;
  154. }
  155. if (!labels) {
  156. this._pageLabels = null;
  157. } else if (!(labels instanceof Array && this.pdfDocument.numPages === labels.length)) {
  158. this._pageLabels = null;
  159. console.error('PDFThumbnailViewer_setPageLabels: Invalid page labels.');
  160. } else {
  161. this._pageLabels = labels;
  162. }
  163. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  164. var label = this._pageLabels && this._pageLabels[i];
  165. this._thumbnails[i].setPageLabel(label);
  166. }
  167. }
  168. }, {
  169. key: '_ensurePdfPageLoaded',
  170. value: function _ensurePdfPageLoaded(thumbView) {
  171. var _this2 = this;
  172. if (thumbView.pdfPage) {
  173. return Promise.resolve(thumbView.pdfPage);
  174. }
  175. var pageNumber = thumbView.id;
  176. if (this._pagesRequests[pageNumber]) {
  177. return this._pagesRequests[pageNumber];
  178. }
  179. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  180. thumbView.setPdfPage(pdfPage);
  181. _this2._pagesRequests[pageNumber] = null;
  182. return pdfPage;
  183. }).catch(function (reason) {
  184. console.error('Unable to get page for thumb view', reason);
  185. _this2._pagesRequests[pageNumber] = null;
  186. });
  187. this._pagesRequests[pageNumber] = promise;
  188. return promise;
  189. }
  190. }, {
  191. key: 'forceRendering',
  192. value: function forceRendering() {
  193. var _this3 = this;
  194. var visibleThumbs = this._getVisibleThumbs();
  195. var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, this.scroll.down);
  196. if (thumbView) {
  197. this._ensurePdfPageLoaded(thumbView).then(function () {
  198. _this3.renderingQueue.renderView(thumbView);
  199. });
  200. return true;
  201. }
  202. return false;
  203. }
  204. }, {
  205. key: 'pagesRotation',
  206. get: function get() {
  207. return this._pagesRotation;
  208. },
  209. set: function set(rotation) {
  210. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  211. throw new Error('Invalid thumbnails rotation angle.');
  212. }
  213. if (!this.pdfDocument) {
  214. return;
  215. }
  216. if (this._pagesRotation === rotation) {
  217. return;
  218. }
  219. this._pagesRotation = rotation;
  220. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  221. this._thumbnails[i].update(rotation);
  222. }
  223. }
  224. }]);
  225. return PDFThumbnailViewer;
  226. }();
  227. exports.PDFThumbnailViewer = PDFThumbnailViewer;