pdf_sidebar.js
11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* Copyright 2016 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('pdfjs-web/pdf_sidebar', ['exports',
'pdfjs-web/pdf_rendering_queue'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports, require('./pdf_rendering_queue.js'));
} else {
factory((root.pdfjsWebPDFSidebar = {}), root.pdfjsWebPDFRenderingQueue);
}
}(this, function (exports, pdfRenderingQueue) {
var RenderingStates = pdfRenderingQueue.RenderingStates;
var SidebarView = {
NONE: 0,
THUMBS: 1,
OUTLINE: 2,
ATTACHMENTS: 3
};
/**
* @typedef {Object} PDFSidebarOptions
* @property {PDFViewer} pdfViewer - The document viewer.
* @property {PDFThumbnailViewer} pdfThumbnailViewer - The thumbnail viewer.
* @property {PDFOutlineViewer} pdfOutlineViewer - The outline viewer.
* @property {HTMLDivElement} mainContainer - The main container
* (in which the viewer element is placed).
* @property {HTMLDivElement} outerContainer - The outer container
* (encasing both the viewer and sidebar elements).
* @property {EventBus} eventBus - The application event bus.
* @property {HTMLButtonElement} toggleButton - The button used for
* opening/closing the sidebar.
* @property {HTMLButtonElement} thumbnailButton - The button used to show
* the thumbnail view.
* @property {HTMLButtonElement} outlineButton - The button used to show
* the outline view.
* @property {HTMLButtonElement} attachmentsButton - The button used to show
* the attachments view.
* @property {HTMLDivElement} thumbnailView - The container in which
* the thumbnails are placed.
* @property {HTMLDivElement} outlineView - The container in which
* the outline is placed.
* @property {HTMLDivElement} attachmentsView - The container in which
* the attachments are placed.
*/
/**
* @class
*/
var PDFSidebar = (function PDFSidebarClosure() {
/**
* @constructs PDFSidebar
* @param {PDFSidebarOptions} options
*/
function PDFSidebar(options) {
this.isOpen = false;
this.active = SidebarView.THUMBS;
this.isInitialViewSet = false;
/**
* Callback used when the sidebar has been opened/closed, to ensure that
* the viewers (PDFViewer/PDFThumbnailViewer) are updated correctly.
*/
this.onToggled = null;
this.pdfViewer = options.pdfViewer;
this.pdfThumbnailViewer = options.pdfThumbnailViewer;
this.pdfOutlineViewer = options.pdfOutlineViewer;
this.mainContainer = options.mainContainer;
this.outerContainer = options.outerContainer;
this.eventBus = options.eventBus;
this.toggleButton = options.toggleButton;
this.thumbnailButton = options.thumbnailButton;
this.outlineButton = options.outlineButton;
this.attachmentsButton = options.attachmentsButton;
this.thumbnailView = options.thumbnailView;
this.outlineView = options.outlineView;
this.attachmentsView = options.attachmentsView;
this._addEventListeners();
}
PDFSidebar.prototype = {
reset: function PDFSidebar_reset() {
this.isInitialViewSet = false;
this.close();
this.switchView(SidebarView.THUMBS);
this.outlineButton.disabled = false;
this.attachmentsButton.disabled = false;
},
/**
* @returns {number} One of the values in {SidebarView}.
*/
get visibleView() {
return (this.isOpen ? this.active : SidebarView.NONE);
},
get isThumbnailViewVisible() {
return (this.isOpen && this.active === SidebarView.THUMBS);
},
get isOutlineViewVisible() {
return (this.isOpen && this.active === SidebarView.OUTLINE);
},
get isAttachmentsViewVisible() {
return (this.isOpen && this.active === SidebarView.ATTACHMENTS);
},
/**
* @param {number} view - The sidebar view that should become visible,
* must be one of the values in {SidebarView}.
*/
setInitialView: function PDFSidebar_setInitialView(view) {
if (this.isInitialViewSet) {
return;
}
this.isInitialViewSet = true;
if (this.isOpen && view === SidebarView.NONE) {
this._dispatchEvent();
// If the user has already manually opened the sidebar,
// immediately closing it would be bad UX.
return;
}
var isViewPreserved = (view === this.visibleView);
this.switchView(view, /* forceOpen */ true);
if (isViewPreserved) {
// Prevent dispatching two back-to-back `sidebarviewchanged` events,
// since `this.switchView` dispatched the event if the view changed.
this._dispatchEvent();
}
},
/**
* @param {number} view - The sidebar view that should be switched to,
* must be one of the values in {SidebarView}.
* @param {boolean} forceOpen - (optional) Ensure that the sidebar is open.
* The default value is false.
*/
switchView: function PDFSidebar_switchView(view, forceOpen) {
if (view === SidebarView.NONE) {
this.close();
return;
}
var isViewChanged = (view !== this.active);
var shouldForceRendering = false;
switch (view) {
case SidebarView.THUMBS:
this.thumbnailButton.classList.add('toggled');
this.outlineButton.classList.remove('toggled');
this.attachmentsButton.classList.remove('toggled');
this.thumbnailView.classList.remove('hidden');
this.outlineView.classList.add('hidden');
this.attachmentsView.classList.add('hidden');
if (this.isOpen && isViewChanged) {
this._updateThumbnailViewer();
shouldForceRendering = true;
}
break;
case SidebarView.OUTLINE:
if (this.outlineButton.disabled) {
return;
}
this.thumbnailButton.classList.remove('toggled');
this.outlineButton.classList.add('toggled');
this.attachmentsButton.classList.remove('toggled');
this.thumbnailView.classList.add('hidden');
this.outlineView.classList.remove('hidden');
this.attachmentsView.classList.add('hidden');
break;
case SidebarView.ATTACHMENTS:
if (this.attachmentsButton.disabled) {
return;
}
this.thumbnailButton.classList.remove('toggled');
this.outlineButton.classList.remove('toggled');
this.attachmentsButton.classList.add('toggled');
this.thumbnailView.classList.add('hidden');
this.outlineView.classList.add('hidden');
this.attachmentsView.classList.remove('hidden');
break;
default:
console.error('PDFSidebar_switchView: "' + view +
'" is an unsupported value.');
return;
}
// Update the active view *after* it has been validated above,
// in order to prevent setting it to an invalid state.
this.active = view | 0;
if (forceOpen && !this.isOpen) {
this.open();
// NOTE: `this.open` will trigger rendering, and dispatch the event.
return;
}
if (shouldForceRendering) {
this._forceRendering();
}
if (isViewChanged) {
this._dispatchEvent();
}
},
open: function PDFSidebar_open() {
if (this.isOpen) {
return;
}
this.isOpen = true;
this.toggleButton.classList.add('toggled');
this.outerContainer.classList.add('sidebarMoving');
this.outerContainer.classList.add('sidebarOpen');
if (this.active === SidebarView.THUMBS) {
this._updateThumbnailViewer();
}
this._forceRendering();
this._dispatchEvent();
},
close: function PDFSidebar_close() {
if (!this.isOpen) {
return;
}
this.isOpen = false;
this.toggleButton.classList.remove('toggled');
this.outerContainer.classList.add('sidebarMoving');
this.outerContainer.classList.remove('sidebarOpen');
this._forceRendering();
this._dispatchEvent();
},
toggle: function PDFSidebar_toggle() {
if (this.isOpen) {
this.close();
} else {
this.open();
}
},
/**
* @private
*/
_dispatchEvent: function PDFSidebar_dispatchEvent() {
this.eventBus.dispatch('sidebarviewchanged', {
source: this,
view: this.visibleView
});
},
/**
* @private
*/
_forceRendering: function PDFSidebar_forceRendering() {
if (this.onToggled) {
this.onToggled();
} else { // Fallback
this.pdfViewer.forceRendering();
this.pdfThumbnailViewer.forceRendering();
}
},
/**
* @private
*/
_updateThumbnailViewer: function PDFSidebar_updateThumbnailViewer() {
var pdfViewer = this.pdfViewer;
var thumbnailViewer = this.pdfThumbnailViewer;
// Use the rendered pages to set the corresponding thumbnail images.
var pagesCount = pdfViewer.pagesCount;
for (var pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
var pageView = pdfViewer.getPageView(pageIndex);
if (pageView && pageView.renderingState === RenderingStates.FINISHED) {
var thumbnailView = thumbnailViewer.getThumbnail(pageIndex);
thumbnailView.setImage(pageView);
}
}
thumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
},
/**
* @private
*/
_addEventListeners: function PDFSidebar_addEventListeners() {
var self = this;
self.mainContainer.addEventListener('transitionend', function(evt) {
if (evt.target === /* mainContainer */ this) {
self.outerContainer.classList.remove('sidebarMoving');
}
});
// Buttons for switching views.
self.thumbnailButton.addEventListener('click', function() {
self.switchView(SidebarView.THUMBS);
});
self.outlineButton.addEventListener('click', function() {
self.switchView(SidebarView.OUTLINE);
});
self.outlineButton.addEventListener('dblclick', function() {
self.pdfOutlineViewer.toggleOutlineTree();
});
self.attachmentsButton.addEventListener('click', function() {
self.switchView(SidebarView.ATTACHMENTS);
});
// Disable/enable views.
self.eventBus.on('outlineloaded', function(e) {
var outlineCount = e.outlineCount;
self.outlineButton.disabled = !outlineCount;
if (!outlineCount && self.active === SidebarView.OUTLINE) {
self.switchView(SidebarView.THUMBS);
}
});
self.eventBus.on('attachmentsloaded', function(e) {
var attachmentsCount = e.attachmentsCount;
self.attachmentsButton.disabled = !attachmentsCount;
if (!attachmentsCount && self.active === SidebarView.ATTACHMENTS) {
self.switchView(SidebarView.THUMBS);
}
});
// Update the thumbnailViewer, if visible, when exiting presentation mode.
self.eventBus.on('presentationmodechanged', function(e) {
if (!e.active && !e.switchInProgress && self.isThumbnailViewVisible) {
self._updateThumbnailViewer();
}
});
},
};
return PDFSidebar;
})();
exports.SidebarView = SidebarView;
exports.PDFSidebar = PDFSidebar;
}));