UNPKG

5.05 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});
27
28var _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; }; }();
29
30function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
31
32var CLEANUP_TIMEOUT = 30000;
33var RenderingStates = {
34 INITIAL: 0,
35 RUNNING: 1,
36 PAUSED: 2,
37 FINISHED: 3
38};
39
40var PDFRenderingQueue = function () {
41 function PDFRenderingQueue() {
42 _classCallCheck(this, PDFRenderingQueue);
43
44 this.pdfViewer = null;
45 this.pdfThumbnailViewer = null;
46 this.onIdle = null;
47 this.highestPriorityPage = null;
48 this.idleTimeout = null;
49 this.printing = false;
50 this.isThumbnailViewEnabled = false;
51 }
52
53 _createClass(PDFRenderingQueue, [{
54 key: "setViewer",
55 value: function setViewer(pdfViewer) {
56 this.pdfViewer = pdfViewer;
57 }
58 }, {
59 key: "setThumbnailViewer",
60 value: function setThumbnailViewer(pdfThumbnailViewer) {
61 this.pdfThumbnailViewer = pdfThumbnailViewer;
62 }
63 }, {
64 key: "isHighestPriority",
65 value: function isHighestPriority(view) {
66 return this.highestPriorityPage === view.renderingId;
67 }
68 }, {
69 key: "renderHighestPriority",
70 value: function renderHighestPriority(currentlyVisiblePages) {
71 if (this.idleTimeout) {
72 clearTimeout(this.idleTimeout);
73 this.idleTimeout = null;
74 }
75 if (this.pdfViewer.forceRendering(currentlyVisiblePages)) {
76 return;
77 }
78 if (this.pdfThumbnailViewer && this.isThumbnailViewEnabled) {
79 if (this.pdfThumbnailViewer.forceRendering()) {
80 return;
81 }
82 }
83 if (this.printing) {
84 return;
85 }
86 if (this.onIdle) {
87 this.idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
88 }
89 }
90 }, {
91 key: "getHighestPriority",
92 value: function getHighestPriority(visible, views, scrolledDown) {
93 var visibleViews = visible.views;
94 var numVisible = visibleViews.length;
95 if (numVisible === 0) {
96 return false;
97 }
98 for (var i = 0; i < numVisible; ++i) {
99 var view = visibleViews[i].view;
100 if (!this.isViewFinished(view)) {
101 return view;
102 }
103 }
104 if (scrolledDown) {
105 var nextPageIndex = visible.last.id;
106 if (views[nextPageIndex] && !this.isViewFinished(views[nextPageIndex])) {
107 return views[nextPageIndex];
108 }
109 } else {
110 var previousPageIndex = visible.first.id - 2;
111 if (views[previousPageIndex] && !this.isViewFinished(views[previousPageIndex])) {
112 return views[previousPageIndex];
113 }
114 }
115 return null;
116 }
117 }, {
118 key: "isViewFinished",
119 value: function isViewFinished(view) {
120 return view.renderingState === RenderingStates.FINISHED;
121 }
122 }, {
123 key: "renderView",
124 value: function renderView(view) {
125 var _this = this;
126
127 switch (view.renderingState) {
128 case RenderingStates.FINISHED:
129 return false;
130 case RenderingStates.PAUSED:
131 this.highestPriorityPage = view.renderingId;
132 view.resume();
133 break;
134 case RenderingStates.RUNNING:
135 this.highestPriorityPage = view.renderingId;
136 break;
137 case RenderingStates.INITIAL:
138 this.highestPriorityPage = view.renderingId;
139 var continueRendering = function continueRendering() {
140 _this.renderHighestPriority();
141 };
142 view.draw().then(continueRendering, continueRendering);
143 break;
144 }
145 return true;
146 }
147 }]);
148
149 return PDFRenderingQueue;
150}();
151
152exports.RenderingStates = RenderingStates;
153exports.PDFRenderingQueue = PDFRenderingQueue;
\No newline at end of file