forked from box/box-content-preview
/
DocBaseViewer.js
1178 lines (1014 loc) · 38.9 KB
/
DocBaseViewer.js
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import throttle from 'lodash/throttle';
import BaseViewer from '../BaseViewer';
import Browser from '../../Browser';
import Controls from '../../Controls';
import PageControls from '../../PageControls';
import DocFindBar from './DocFindBar';
import fullscreen from '../../Fullscreen';
import Popup from '../../Popup';
import RepStatus from '../../RepStatus';
import PreviewError from '../../PreviewError';
import {
CLASS_BOX_PREVIEW_FIND_BAR,
CLASS_CRAWLER,
CLASS_HIDDEN,
CLASS_IS_SCROLLABLE,
CLASS_SPINNER,
DOC_STATIC_ASSETS_VERSION,
PERMISSION_DOWNLOAD,
PRELOAD_REP_NAME,
STATUS_SUCCESS,
X_BOX_ACCEPT_ENCODING_HEADER,
X_BOX_ACCEPT_ENCODING_IDENTITY
} from '../../constants';
import { checkPermission, getRepresentation } from '../../file';
import { get, createAssetUrlCreator, getMidpoint, getDistance, getClosestPageToPinch } from '../../util';
import { ICON_PRINT_CHECKMARK } from '../../icons/icons';
import { JS, PRELOAD_JS, CSS } from './docAssets';
import { ERROR_CODE, VIEWER_EVENT } from '../../events';
const CURRENT_PAGE_MAP_KEY = 'doc-current-page-map';
const DEFAULT_SCALE_DELTA = 1.1;
const LOAD_TIMEOUT_MS = 180000; // 3 min timeout
const SAFARI_PRINT_TIMEOUT_MS = 1000; // Wait 1s before trying to print
const PRINT_DIALOG_TIMEOUT_MS = 500;
const MAX_SCALE = 10.0;
const MIN_SCALE = 0.1;
const MAX_PINCH_SCALE_VALUE = 3;
const MIN_PINCH_SCALE_VALUE = 0.25;
const MIN_PINCH_SCALE_DELTA = 0.01;
const IS_SAFARI_CLASS = 'is-safari';
const SCROLL_EVENT_THROTTLE_INTERVAL = 200;
const SCROLL_END_TIMEOUT = this.isMobile ? 500 : 250;
const RANGE_REQUEST_CHUNK_SIZE_US = 1048576; // 1MB
const RANGE_REQUEST_CHUNK_SIZE_NON_US = 524288; // 512KB
const MINIMUM_RANGE_REQUEST_FILE_SIZE_NON_US = 26214400; // 25MB
const MOBILE_MAX_CANVAS_SIZE = 2949120; // ~3MP 1920x1536
const PINCH_PAGE_CLASS = 'pinch-page';
const PINCHING_CLASS = 'pinching';
const PAGES_UNIT_NAME = 'pages';
class DocBaseViewer extends BaseViewer {
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @inheritdoc
*/
constructor(options) {
super(options);
// Bind context for callbacks
this.handleAssetAndRepLoad = this.handleAssetAndRepLoad.bind(this);
this.print = this.print.bind(this);
this.setPage = this.setPage.bind(this);
this.zoomIn = this.zoomIn.bind(this);
this.zoomOut = this.zoomOut.bind(this);
this.pagerenderedHandler = this.pagerenderedHandler.bind(this);
this.pagechangeHandler = this.pagechangeHandler.bind(this);
this.pagesinitHandler = this.pagesinitHandler.bind(this);
this.enterfullscreenHandler = this.enterfullscreenHandler.bind(this);
this.exitfullscreenHandler = this.exitfullscreenHandler.bind(this);
this.throttledScrollHandler = this.getScrollHandler().bind(this);
this.pinchToZoomStartHandler = this.pinchToZoomStartHandler.bind(this);
this.pinchToZoomChangeHandler = this.pinchToZoomChangeHandler.bind(this);
this.pinchToZoomEndHandler = this.pinchToZoomEndHandler.bind(this);
}
/**
* @inheritdoc
*/
setup() {
// Call super() to set up common layout
super.setup();
this.docEl = this.containerEl.appendChild(document.createElement('div'));
this.docEl.classList.add('bp-doc');
if (Browser.getName() === 'Safari') {
this.docEl.classList.add(IS_SAFARI_CLASS);
}
// We disable native pinch-to-zoom and double tap zoom on mobile to force users to use
// our viewer's zoom controls
if (this.isMobile) {
const metaEl = document.createElement('meta');
metaEl.setAttribute('name', 'viewport');
metaEl.setAttribute('content', 'width=device-width, initial-scale=1, user-scalable=no');
document.getElementsByTagName('head')[0].appendChild(metaEl);
}
this.viewerEl = this.docEl.appendChild(document.createElement('div'));
this.viewerEl.classList.add('pdfViewer');
this.loadTimeout = LOAD_TIMEOUT_MS;
this.startPageNum = this.getStartPage(this.startAt);
}
/**
* [destructor]
*
* @return {void}
*/
destroy() {
this.unbindDOMListeners();
// Clean up print blob
this.printBlob = null;
if (this.printURL) {
URL.revokeObjectURL(this.printURL);
}
if (this.pageControls) {
this.pageControls.removeListener('pagechange', this.setPage);
}
if (this.controls && typeof this.controls.destroy === 'function') {
this.controls.destroy();
}
// Clean up the find bar
if (this.findBar) {
this.findBar.destroy();
}
// Clean up PDF network requests
if (this.pdfLoadingTask) {
try {
this.pdfLoadingTask.destroy();
} catch (e) {
// Ignore these errors
}
}
// Clean up viewer and PDF document object
if (this.pdfViewer) {
this.pdfViewer.cleanup();
if (this.pdfViewer.pdfDocument) {
this.pdfViewer.pdfDocument.destroy();
}
}
if (this.printPopup) {
this.printPopup.destroy();
}
super.destroy();
}
/**
* Converts a value and unit to page number
*
* @param {Object} startAt - the unit and value that describes where to start the preview
* @return {number|undefined} a page number > 0
*/
getStartPage(startAt = {}) {
let convertedValue;
const { unit, value } = startAt;
if (!value || !unit) {
return convertedValue;
}
if (unit === PAGES_UNIT_NAME) {
convertedValue = parseInt(value, 10);
if (!convertedValue || convertedValue < 1) {
// Negative values aren't allowed, fall back to default behavior
return undefined;
}
} else {
console.error('Invalid unit for start:', unit); // eslint-disable-line no-console
}
return convertedValue;
}
/**
* Prefetches assets for a document.
*
* @param {boolean} [options.assets] - Whether or not to prefetch static assets
* @param {boolean} [options.preload] - Whether or not to prefetch preload content
* @param {boolean} [options.content] - Whether or not to prefetch rep content
* @return {void}
*/
prefetch({ assets = true, preload = true, content = true }) {
const { file, representation } = this.options;
const isWatermarked = file && file.watermark_info && file.watermark_info.is_watermarked;
if (assets) {
this.prefetchAssets(JS, CSS);
this.prefetchAssets(PRELOAD_JS, [], true);
}
if (preload && !isWatermarked) {
const preloadRep = getRepresentation(file, PRELOAD_REP_NAME);
if (preloadRep && this.isRepresentationReady(preloadRep)) {
const { url_template: template } = preloadRep.content;
// Prefetch as blob since preload needs to load image as a blob
get(this.createContentUrlWithAuthParams(template), 'blob');
}
}
if (content && !isWatermarked && this.isRepresentationReady(representation)) {
const { url_template: template } = representation.content;
get(this.createContentUrlWithAuthParams(template), 'any');
}
}
/**
* Shows a preload (first page as an image) while the full document loads.
*
* @return {void}
*/
showPreload() {
const { file } = this.options;
const isWatermarked = file && file.watermark_info && file.watermark_info.is_watermarked;
// Don't show preload if there's a cached page or startAt is set and > 1 since preloads are only for the 1st page
// Also don't show preloads for watermarked files
if (
!this.preloader ||
isWatermarked ||
((this.startPageNum && this.startPageNum !== 1) || this.getCachedPage() !== 1)
) {
return;
}
// Don't show preload if there is no preload rep, the 'preload' viewer option isn't set, or the rep isn't ready
const preloadRep = getRepresentation(file, PRELOAD_REP_NAME);
if (!preloadRep || !this.getViewerOption('preload') || RepStatus.getStatus(preloadRep) !== STATUS_SUCCESS) {
return;
}
const { url_template: template } = preloadRep.content;
const preloadUrlWithAuth = this.createContentUrlWithAuthParams(template);
this.preloader.showPreload(preloadUrlWithAuth, this.containerEl);
}
/**
* Cleans up the preload (first page as an image). Should be called when full
* document is loaded.
*
* @return {void}
*/
hidePreload() {
if (this.preloader) {
this.preloader.hidePreload();
}
}
/**
* Loads a document.
*
* @public
* @return {Promise} Promise to resolve assets
*/
load() {
this.setup();
super.load();
this.showPreload();
const template = this.options.representation.content.url_template;
this.pdfUrl = this.createContentUrlWithAuthParams(template);
return Promise.all([this.loadAssets(JS, CSS), this.getRepStatus().getPromise()])
.then(this.handleAssetAndRepLoad)
.catch(this.handleAssetError);
}
/**
* Loads a document after assets and representation are ready.
*
* @return {void}
*/
handleAssetAndRepLoad() {
this.setupPdfjs();
this.initViewer(this.pdfUrl);
this.initPrint();
this.initFind();
}
/**
* Initializes the Find Bar and Find Controller
*
* @return {void}
*/
initFind() {
this.findBarEl = this.containerEl.appendChild(document.createElement('div'));
this.findBarEl.classList.add(CLASS_BOX_PREVIEW_FIND_BAR);
this.findController = new this.pdfjsViewer.PDFFindController({
pdfViewer: this.pdfViewer
});
this.pdfViewer.setFindController(this.findController);
// Only initialize the find bar if the user has download permissions on
// the file. Users without download permissions shouldn't be able to
// interact with the text layer
const canDownload = checkPermission(this.options.file, PERMISSION_DOWNLOAD);
if (this.getViewerOption('disableFindBar')) {
return;
}
this.findBar = new DocFindBar(this.findBarEl, this.findController, canDownload);
}
/**
* Scrolls to and highlights the next occurences of a phrase in the document using the DocFindBar
*
* @public
* @param {string} phrase - Phrase to find
* @param {boolean} [openFindBar] - Option to open the findbar on find
* @return {void}
*/
find(phrase, openFindBar = false) {
if (!this.findBar) {
return;
}
// Go to page one so that we can find the first occurence in the document
this.setPage(1);
this.findBar.setFindFieldElValue(phrase);
this.findBar.findFieldHandler();
if (openFindBar) {
this.findBar.open();
}
}
/**
* Ensures that the print blob is loaded & updates the print UI.
*
* @return {void}
*/
print() {
// If print blob is not ready, fetch it
if (!this.printBlob) {
this.fetchPrintBlob(this.pdfUrl).then(this.print);
// Show print dialog after PRINT_DIALOG_TIMEOUT_MS
this.printDialogTimeout = setTimeout(() => {
this.printPopup.show(__('print_loading'), __('print'), () => {
this.printPopup.hide();
this.browserPrint();
});
this.printPopup.disableButton();
this.printDialogTimeout = null;
}, PRINT_DIALOG_TIMEOUT_MS);
return;
}
// Immediately print if either printing is ready within PRINT_DIALOG_TIMEOUT_MS
// or if popup is not visible (e.g. from initiating print again)
if (this.printDialogTimeout || !this.printPopup.isVisible()) {
clearTimeout(this.printDialogTimeout);
this.browserPrint();
} else {
// Update popup UI to reflect that print is ready
this.printPopup.enableButton();
this.printPopup.messageEl.textContent = __('print_ready');
this.printPopup.loadingIndicator.classList.add(CLASS_HIDDEN);
this.printPopup.printCheckmark.classList.remove(CLASS_HIDDEN);
}
}
/**
* Go to previous page
*
* @return {void}
*/
previousPage() {
this.setPage(this.pdfViewer.currentPageNumber - 1);
}
/**
* Go to next page
*
* @return {void}
*/
nextPage() {
this.setPage(this.pdfViewer.currentPageNumber + 1);
}
/**
* Go to specified page
*
* @param {number} pageNumber - Page to navigate to
* @return {void}
*/
setPage(pageNumber) {
const parsedPageNumber = parseInt(pageNumber, 10);
if (!parsedPageNumber || parsedPageNumber < 1 || parsedPageNumber > this.pdfViewer.pagesCount) {
return;
}
this.pdfViewer.currentPageNumber = parsedPageNumber;
this.cachePage(this.pdfViewer.currentPageNumber);
}
/**
* Gets the cached current page.
*
* @return {number} Current page
*/
getCachedPage() {
let page = 1;
if (this.cache.has(CURRENT_PAGE_MAP_KEY)) {
const currentPageMap = this.cache.get(CURRENT_PAGE_MAP_KEY);
page = currentPageMap[this.options.file.id] || page;
}
return page;
}
/**
* Sets the current page into localstorage if available. Otherwise saves
* it in-memory as a property on the document viewer.
*
* @param {number} page - Current page
* @return {void}
*/
cachePage(page) {
let currentPageMap = {};
if (this.cache.has(CURRENT_PAGE_MAP_KEY)) {
currentPageMap = this.cache.get(CURRENT_PAGE_MAP_KEY);
}
currentPageMap[this.options.file.id] = page;
this.cache.set(CURRENT_PAGE_MAP_KEY, currentPageMap, true /* useLocalStorage */);
}
/**
* Zoom into document.
*
* @param {number} ticks - Number of times to zoom in
* @return {void}
*/
zoomIn(ticks = 1) {
let numTicks = ticks;
let newScale = this.pdfViewer.currentScale;
do {
newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(3);
newScale = Math.min(MAX_SCALE, newScale);
numTicks -= 1;
} while (numTicks > 0 && newScale < MAX_SCALE);
if (this.pdfViewer.currentScale !== newScale) {
this.emit('zoom', {
zoom: newScale,
canZoomOut: true,
canZoomIn: newScale < MAX_SCALE
});
}
this.pdfViewer.currentScaleValue = newScale;
}
/**
* Zoom out of document.
*
* @param {number} ticks - Number of times to zoom out
* @return {void}
*/
zoomOut(ticks = 1) {
let numTicks = ticks;
let newScale = this.pdfViewer.currentScale;
do {
newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(3);
newScale = Math.max(MIN_SCALE, newScale);
numTicks -= 1;
} while (numTicks > 0 && newScale > MIN_SCALE);
if (this.pdfViewer.currentScale !== newScale) {
this.emit('zoom', {
zoom: newScale,
canZoomOut: newScale > MIN_SCALE,
canZoomIn: true
});
}
this.pdfViewer.currentScaleValue = newScale;
}
/**
* Handles keyboard events for document viewer.
*
* @param {string} key - keydown key
* @param {Object} event - Key event
* @return {boolean} consumed or not
*/
onKeydown(key, event) {
switch (key) {
case 'ArrowLeft':
this.previousPage();
break;
case 'ArrowRight':
this.nextPage();
break;
case '[':
this.previousPage();
break;
case ']':
this.nextPage();
break;
default:
if (this.findBar) {
return this.findBar.onKeydown(event);
}
return false;
}
return true;
}
//--------------------------------------------------------------------------
// Protected
//--------------------------------------------------------------------------
/**
* Loads PDF.js with provided PDF.
*
* @protected
* @param {string} pdfUrl - The URL of the PDF to load
* @return {Promise} Promise to initialize Viewer
*/
initViewer(pdfUrl) {
this.bindDOMListeners();
// Initialize pdf.js in container
this.pdfViewer = this.initPdfViewer();
// Use chunk size set in viewer options if available
let rangeChunkSize = this.getViewerOption('rangeChunkSize');
// Otherwise, use large chunk size if locale is en-US and the default,
// smaller chunk size if not. This is using a rough assumption that
// en-US users have higher bandwidth to Box.
if (!rangeChunkSize) {
rangeChunkSize =
this.options.location.locale === 'en-US'
? RANGE_REQUEST_CHUNK_SIZE_US
: RANGE_REQUEST_CHUNK_SIZE_NON_US;
}
const docInitParams = {
url: pdfUrl,
rangeChunkSize
};
// Fix incorrectly cached range requests on older versions of iOS webkit browsers,
// see: https://bugs.webkit.org/show_bug.cgi?id=82672
if (Browser.isIOS()) {
docInitParams.httpHeaders = {
'If-None-Match': 'webkit-no-cache'
};
}
// If range requests are enabled, request the non-gzip compressed version of the representation
if (!this.pdfjsLib.disableRange) {
docInitParams.httpHeaders = docInitParams.httpHeaders || {};
docInitParams.httpHeaders[X_BOX_ACCEPT_ENCODING_HEADER] = X_BOX_ACCEPT_ENCODING_IDENTITY;
}
// Start timing document load
this.startLoadTimer();
// Load PDF from representation URL and set as document for pdf.js. Cache
// the loading task so we can cancel if needed
this.pdfLoadingTask = this.pdfjsLib.getDocument(docInitParams);
return this.pdfLoadingTask
.then((doc) => {
this.pdfViewer.setDocument(doc);
const { linkService } = this.pdfViewer;
if (linkService instanceof this.pdfjsViewer.PDFLinkService) {
linkService.setDocument(doc, pdfUrl);
linkService.setViewer(this.pdfViewer);
}
})
.catch((err) => {
// eslint-disable-next-line
console.error(err);
// Display a generic error message but log the real one
const error = new PreviewError(ERROR_CODE.CONTENT_DOWNLOAD, __('error_document'), {}, err.message);
this.handleDownloadError(error, pdfUrl);
});
}
/**
* Initialize pdf.js viewer.
*
* @protected
* @override
* @return {Object} PDF viewer type
*/
initPdfViewer() {
return new this.pdfjsViewer.PDFViewer({
container: this.docEl,
linkService: new this.pdfjsViewer.PDFLinkService(),
// Enhanced text selection uses more memory, so disable on mobile
enhanceTextSelection: !this.isMobile
});
}
/**
* Re-sizing logic.
*
* @protected
* @override
* @return {void}
*/
resize() {
if (!this.pdfViewer || !this.pdfViewer.pageViewsReady) {
return;
}
// Save page and return after resize
const { currentPageNumber } = this.pdfViewer.currentPageNumber;
this.pdfViewer.currentScaleValue = this.pdfViewer.currentScaleValue || 'auto';
this.pdfViewer.update();
this.setPage(currentPageNumber);
super.resize();
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Sets options for PDF.js.
*
* @return {void}
* @private
*/
setupPdfjs() {
// pdf.js v2.0 no longer exposes a single global PDFJS variable
this.pdfjsLib = window['pdfjs-dist/build/pdf'];
this.pdfjsViewer = window['pdfjs-dist/web/pdf_viewer'];
// Set pdf.js worker & character maps
const { file, location } = this.options;
const { size, watermark_info: watermarkInfo } = file;
const assetUrlCreator = createAssetUrlCreator(location);
// Set pdf.js worker, image, and character map locations
this.pdfjsLib.workerSrc = assetUrlCreator(`third-party/doc/${DOC_STATIC_ASSETS_VERSION}/pdf.worker.min.js`);
this.pdfjsLib.imageResourcesPath = assetUrlCreator(`third-party/doc/${DOC_STATIC_ASSETS_VERSION}/images/`);
this.pdfjsLib.cMapUrl = `${location.staticBaseURI}third-party/doc/${DOC_STATIC_ASSETS_VERSION}/cmaps/`;
this.pdfjsLib.cMapPacked = true;
// Open links in new tab
this.pdfjsLib.externalLinkTarget = this.pdfjsLib.LinkTarget.BLANK;
// Disable streaming via fetch until performance is improved
this.pdfjsLib.disableStream = true;
// Disable font faces on IOS 10.3.X
// @NOTE(JustinHoldstock) 2017-04-11: Check to remove this after next IOS release after 10.3.1
this.pdfjsLib.disableFontFace = this.pdfjsLib.disableFontFace || Browser.hasFontIssue();
// Disable range requests for files smaller than MINIMUM_RANGE_REQUEST_FILE_SIZE (25MB) for
// previews outside of the US since the additional latency overhead per range request can be
// more than the additional time for a continuous request. This also overrides any range request
// disabling that may be set by pdf.js's compatibility checking since the browsers we support
// should all be able to properly handle range requests.
this.pdfjsLib.disableRange = location.locale !== 'en-US' && size < MINIMUM_RANGE_REQUEST_FILE_SIZE_NON_US;
// Disable range requests for watermarked files since they are streamed
this.pdfjsLib.disableRange = this.pdfjsLib.disableRange || (watermarkInfo && watermarkInfo.is_watermarked);
// Disable text layer if user doesn't have download permissions
this.pdfjsLib.disableTextLayer =
!checkPermission(file, PERMISSION_DOWNLOAD) || !!this.getViewerOption('disableTextLayer');
// Decrease mobile canvas size to ~3MP (1920x1536)
this.pdfjsLib.maxCanvasPixels = this.isMobile ? MOBILE_MAX_CANVAS_SIZE : this.pdfjsLib.maxCanvasPixels;
// Do not disable create object URL in IE11 or iOS Chrome - pdf.js issues #3977 and #8081 are
// not applicable to Box's use case and disabling causes performance issues
this.pdfjsLib.disableCreateObjectURL = false;
// Customize pdf.js loading icon. We modify the prototype of PDFPageView to get around directly modifying
// pdf_viewer.js
const resetFunc = this.pdfjsViewer.PDFPageView.prototype.reset;
this.pdfjsViewer.PDFPageView.prototype.reset = function reset(...args) {
resetFunc.bind(this)(args);
this.loadingIconDiv.classList.add(CLASS_SPINNER);
this.loadingIconDiv.innerHTML = '<div></div>';
};
}
/**
* Sets up print notification & prepare PDF for printing.
*
* @private
* @return {void}
*/
initPrint() {
this.printPopup = new Popup(this.containerEl);
const printCheckmark = document.createElement('div');
printCheckmark.className = `bp-print-check ${CLASS_HIDDEN}`;
printCheckmark.innerHTML = ICON_PRINT_CHECKMARK.trim();
const loadingIndicator = document.createElement('div');
loadingIndicator.classList.add(CLASS_CRAWLER);
loadingIndicator.innerHTML = `
<div></div>
<div></div>
<div></div>`.trim();
this.printPopup.addContent(loadingIndicator, true);
this.printPopup.addContent(printCheckmark, true);
// Save a reference so they can be hidden or shown later.
this.printPopup.loadingIndicator = loadingIndicator;
this.printPopup.printCheckmark = printCheckmark;
}
/**
* Add page IDs to each page
*
* @private
* @return {void}
*/
setupPageIds() {
const pageEls = this.containerEl.querySelectorAll('.page');
[].forEach.call(pageEls, (pageEl) => {
/* eslint-disable no-param-reassign */
const { pageNumber } = pageEl.dataset;
if (pageNumber) {
pageEl.id = `bp-page-${pageNumber}`;
}
/* eslint-enable no-param-reassign */
});
}
/**
* Fetches PDF and converts to blob for printing.
*
* @private
* @param {string} pdfUrl - URL to PDF
* @return {Promise} Promise setting print blob
*/
fetchPrintBlob(pdfUrl) {
return get(pdfUrl, 'blob').then((blob) => {
this.printBlob = blob;
});
}
/**
* Handles logic for printing the PDF representation in browser.
*
* @private
* @return {void}
*/
browserPrint() {
// For IE & Edge, use the open or save dialog since we can't open
// in a new tab due to security restrictions, see:
// http://stackoverflow.com/questions/24007073/open-links-made-by-createobjecturl-in-ie11
if (typeof window.navigator.msSaveOrOpenBlob === 'function') {
const printResult = window.navigator.msSaveOrOpenBlob(this.printBlob, 'print.pdf');
// If open/save notification is not shown, broadcast error
if (!printResult) {
this.emit('printerror');
} else {
this.emit('printsuccess');
}
// For other browsers, open and print in a new tab
} else {
if (!this.printURL) {
this.printURL = URL.createObjectURL(this.printBlob);
}
const printResult = window.open(this.printURL);
// Open print popup if possible
if (printResult && typeof printResult.print === 'function') {
const browser = Browser.getName();
// Chrome supports printing on load
if (browser === 'Chrome') {
printResult.addEventListener('load', () => {
printResult.print();
});
// Safari print on load produces blank page, so we use a timeout
} else if (browser === 'Safari') {
setTimeout(() => {
printResult.print();
}, SAFARI_PRINT_TIMEOUT_MS);
}
// Firefox has a blocking bug: https://bugzilla.mozilla.org/show_bug.cgi?id=911444
}
// If new window/tab was blocked, broadcast error
if (!printResult || printResult.closed || typeof printResult.closed === 'undefined') {
this.emit('printerror');
} else {
this.emit('printsuccess');
}
}
}
/**
* Creates UI for preview controls.
*
* @private
* @return {void}
*/
loadUI() {
this.controls = new Controls(this.containerEl);
this.pageControls = new PageControls(this.controls, this.docEl);
this.pageControls.addListener('pagechange', this.setPage);
this.bindControlListeners();
}
//--------------------------------------------------------------------------
// Event Listeners
//--------------------------------------------------------------------------
/**
* Binds DOM listeners for document viewer.
*
* @protected
* @return {void}
*/
bindDOMListeners() {
// When page structure is initialized, set default zoom, load controls,
// and broadcast that preview has loaded
this.docEl.addEventListener('pagesinit', this.pagesinitHandler);
// When a page is rendered, update scale
this.docEl.addEventListener('pagerendered', this.pagerenderedHandler);
// Update page number when page changes
this.docEl.addEventListener('pagechange', this.pagechangeHandler);
// Detects scroll so an event can be fired
this.docEl.addEventListener('scroll', this.throttledScrollHandler);
// Fullscreen
fullscreen.addListener('enter', this.enterfullscreenHandler);
fullscreen.addListener('exit', this.exitfullscreenHandler);
if (this.hasTouch) {
this.docEl.addEventListener('touchstart', this.pinchToZoomStartHandler);
this.docEl.addEventListener('touchmove', this.pinchToZoomChangeHandler);
this.docEl.addEventListener('touchend', this.pinchToZoomEndHandler);
}
}
/**
* Unbinds DOM listeners for document viewer.
*
* @protected
* @return {void}
*/
unbindDOMListeners() {
if (this.docEl) {
this.docEl.removeEventListener('pagesinit', this.pagesinitHandler);
this.docEl.removeEventListener('pagerendered', this.pagerenderedHandler);
this.docEl.removeEventListener('pagechange', this.pagechangeHandler);
this.docEl.removeEventListener('scroll', this.throttledScrollHandler);
if (this.hasTouch) {
this.docEl.removeEventListener('touchstart', this.pinchToZoomStartHandler);
this.docEl.removeEventListener('touchmove', this.pinchToZoomChangeHandler);
this.docEl.removeEventListener('touchend', this.pinchToZoomEndHandler);
}
}
fullscreen.removeListener('enter', this.enterfullscreenHandler);
fullscreen.removeListener('exit', this.exitfullscreenHandler);
}
/**
* Binds listeners for document controls. Overridden.
*
* @protected
* @return {void}
*/
bindControlListeners() {}
/**
* Handler for 'pagesinit' event.
*
* @private
* @return {void}
*/
pagesinitHandler() {
this.pdfViewer.currentScaleValue = 'auto';
this.loadUI();
const { pagesCount, currentScale } = this.pdfViewer;
// Set page to the user-defined page, previously opened page, or first page
const startPage = this.startPageNum || this.getCachedPage();
this.setPage(startPage);
// Make document scrollable after pages are set up so scrollbars don't mess with autoscaling
this.docEl.classList.add(CLASS_IS_SCROLLABLE);
// Broadcast that preview has 'loaded' when page structure is available
if (!this.loaded) {
this.loaded = true;
this.emit(VIEWER_EVENT.load, {
numPages: pagesCount,
endProgress: false, // Indicate that viewer will end progress later
scale: currentScale
});
// Add page IDs to each page after page structure is available
this.setupPageIds();
}
}
/**
* Handler for 'pagerendered' event.
*
* @private
* @param {Event} event - 'pagerendered' event
* @return {void}
*/
pagerenderedHandler(event) {
const pageNumber = event.detail ? event.detail.pageNumber : undefined;
if (pageNumber) {
// Page rendered event
this.emit('pagerender', pageNumber);
// Set scale to current numerical scale & rendered page number
this.emit('scale', {
scale: this.pdfViewer.currentScale,
pageNum: pageNumber
});
// Fire progressend event to hide progress bar and cleanup preload after a page is rendered
if (!this.somePageRendered) {
this.hidePreload();
this.emit(VIEWER_EVENT.progressEnd);
this.somePageRendered = true;
}
}
}
/**
* Handler for 'pagechange' event.
*
* @private
* @param {Event} event - Pagechange event
* @return {void}
*/
pagechangeHandler(event) {
const { pageNumber } = event;
this.pageControls.updateCurrentPage(pageNumber);
// We only set cache the current page if 'pagechange' was fired after
// preview is loaded - this filters out pagechange events fired by
// the viewer's initialization
if (this.loaded) {
this.cachePage(pageNumber);
}
this.emit('pagefocus', pageNumber);
}
/**
* Fullscreen entered handler. Add presentation mode class, set
* presentation mode state, and set zoom to fullscreen zoom.
*
* @private
* @return {void}
*/
enterfullscreenHandler() {
this.pdfViewer.currentScaleValue = 'page-fit';
this.resize();
}