UNPKG

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