2
0

pdf_thumbnail_viewer.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 THUMBNAIL_SELECTED_CLASS = 'selected';
  33. var PDFThumbnailViewer = function () {
  34. function PDFThumbnailViewer(_ref) {
  35. var container = _ref.container,
  36. linkService = _ref.linkService,
  37. renderingQueue = _ref.renderingQueue,
  38. _ref$l10n = _ref.l10n,
  39. l10n = _ref$l10n === undefined ? _ui_utils.NullL10n : _ref$l10n;
  40. _classCallCheck(this, PDFThumbnailViewer);
  41. this.container = container;
  42. this.linkService = linkService;
  43. this.renderingQueue = renderingQueue;
  44. this.l10n = l10n;
  45. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdated.bind(this));
  46. this._resetView();
  47. }
  48. _createClass(PDFThumbnailViewer, [{
  49. key: '_scrollUpdated',
  50. value: function _scrollUpdated() {
  51. this.renderingQueue.renderHighestPriority();
  52. }
  53. }, {
  54. key: 'getThumbnail',
  55. value: function getThumbnail(index) {
  56. return this._thumbnails[index];
  57. }
  58. }, {
  59. key: '_getVisibleThumbs',
  60. value: function _getVisibleThumbs() {
  61. return (0, _ui_utils.getVisibleElements)(this.container, this._thumbnails);
  62. }
  63. }, {
  64. key: 'scrollThumbnailIntoView',
  65. value: function scrollThumbnailIntoView(pageNumber) {
  66. if (!this.pdfDocument) {
  67. return;
  68. }
  69. var thumbnailView = this._thumbnails[pageNumber - 1];
  70. if (!thumbnailView) {
  71. console.error('scrollThumbnailIntoView: Invalid "pageNumber" parameter.');
  72. return;
  73. }
  74. if (pageNumber !== this._currentPageNumber) {
  75. var prevThumbnailView = this._thumbnails[this._currentPageNumber - 1];
  76. prevThumbnailView.div.classList.remove(THUMBNAIL_SELECTED_CLASS);
  77. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  78. }
  79. var visibleThumbs = this._getVisibleThumbs();
  80. var numVisibleThumbs = visibleThumbs.views.length;
  81. if (numVisibleThumbs > 0) {
  82. var first = visibleThumbs.first.id;
  83. var last = numVisibleThumbs > 1 ? visibleThumbs.last.id : first;
  84. var shouldScroll = false;
  85. if (pageNumber <= first || pageNumber >= last) {
  86. shouldScroll = true;
  87. } else {
  88. visibleThumbs.views.some(function (view) {
  89. if (view.id !== pageNumber) {
  90. return false;
  91. }
  92. shouldScroll = view.percent < 100;
  93. return true;
  94. });
  95. }
  96. if (shouldScroll) {
  97. (0, _ui_utils.scrollIntoView)(thumbnailView.div, { top: THUMBNAIL_SCROLL_MARGIN });
  98. }
  99. }
  100. this._currentPageNumber = pageNumber;
  101. }
  102. }, {
  103. key: 'cleanup',
  104. value: function cleanup() {
  105. _pdf_thumbnail_view.PDFThumbnailView.cleanup();
  106. }
  107. }, {
  108. key: '_resetView',
  109. value: function _resetView() {
  110. this._thumbnails = [];
  111. this._currentPageNumber = 1;
  112. this._pageLabels = null;
  113. this._pagesRotation = 0;
  114. this._pagesRequests = [];
  115. this.container.textContent = '';
  116. }
  117. }, {
  118. key: 'setDocument',
  119. value: function setDocument(pdfDocument) {
  120. var _this = this;
  121. if (this.pdfDocument) {
  122. this._cancelRendering();
  123. this._resetView();
  124. }
  125. this.pdfDocument = pdfDocument;
  126. if (!pdfDocument) {
  127. return;
  128. }
  129. pdfDocument.getPage(1).then(function (firstPage) {
  130. var pagesCount = pdfDocument.numPages;
  131. var viewport = firstPage.getViewport(1.0);
  132. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  133. var thumbnail = new _pdf_thumbnail_view.PDFThumbnailView({
  134. container: _this.container,
  135. id: pageNum,
  136. defaultViewport: viewport.clone(),
  137. linkService: _this.linkService,
  138. renderingQueue: _this.renderingQueue,
  139. disableCanvasToImageConversion: false,
  140. l10n: _this.l10n
  141. });
  142. _this._thumbnails.push(thumbnail);
  143. }
  144. var thumbnailView = _this._thumbnails[_this._currentPageNumber - 1];
  145. thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
  146. }).catch(function (reason) {
  147. console.error('Unable to initialize thumbnail viewer', reason);
  148. });
  149. }
  150. }, {
  151. key: '_cancelRendering',
  152. value: function _cancelRendering() {
  153. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  154. if (this._thumbnails[i]) {
  155. this._thumbnails[i].cancelRendering();
  156. }
  157. }
  158. }
  159. }, {
  160. key: 'setPageLabels',
  161. value: function setPageLabels(labels) {
  162. if (!this.pdfDocument) {
  163. return;
  164. }
  165. if (!labels) {
  166. this._pageLabels = null;
  167. } else if (!(labels instanceof Array && this.pdfDocument.numPages === labels.length)) {
  168. this._pageLabels = null;
  169. console.error('PDFThumbnailViewer_setPageLabels: Invalid page labels.');
  170. } else {
  171. this._pageLabels = labels;
  172. }
  173. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  174. var label = this._pageLabels && this._pageLabels[i];
  175. this._thumbnails[i].setPageLabel(label);
  176. }
  177. }
  178. }, {
  179. key: '_ensurePdfPageLoaded',
  180. value: function _ensurePdfPageLoaded(thumbView) {
  181. var _this2 = this;
  182. if (thumbView.pdfPage) {
  183. return Promise.resolve(thumbView.pdfPage);
  184. }
  185. var pageNumber = thumbView.id;
  186. if (this._pagesRequests[pageNumber]) {
  187. return this._pagesRequests[pageNumber];
  188. }
  189. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  190. thumbView.setPdfPage(pdfPage);
  191. _this2._pagesRequests[pageNumber] = null;
  192. return pdfPage;
  193. }).catch(function (reason) {
  194. console.error('Unable to get page for thumb view', reason);
  195. _this2._pagesRequests[pageNumber] = null;
  196. });
  197. this._pagesRequests[pageNumber] = promise;
  198. return promise;
  199. }
  200. }, {
  201. key: 'forceRendering',
  202. value: function forceRendering() {
  203. var _this3 = this;
  204. var visibleThumbs = this._getVisibleThumbs();
  205. var thumbView = this.renderingQueue.getHighestPriority(visibleThumbs, this._thumbnails, this.scroll.down);
  206. if (thumbView) {
  207. this._ensurePdfPageLoaded(thumbView).then(function () {
  208. _this3.renderingQueue.renderView(thumbView);
  209. });
  210. return true;
  211. }
  212. return false;
  213. }
  214. }, {
  215. key: 'pagesRotation',
  216. get: function get() {
  217. return this._pagesRotation;
  218. },
  219. set: function set(rotation) {
  220. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  221. throw new Error('Invalid thumbnails rotation angle.');
  222. }
  223. if (!this.pdfDocument) {
  224. return;
  225. }
  226. if (this._pagesRotation === rotation) {
  227. return;
  228. }
  229. this._pagesRotation = rotation;
  230. for (var i = 0, ii = this._thumbnails.length; i < ii; i++) {
  231. this._thumbnails[i].update(rotation);
  232. }
  233. }
  234. }]);
  235. return PDFThumbnailViewer;
  236. }();
  237. exports.PDFThumbnailViewer = PDFThumbnailViewer;