@@ -28,57 +28,6 @@ const LinkTarget = {
28
28
TOP : 4 ,
29
29
} ;
30
30
31
- /**
32
- * @typedef {Object } ExternalLinkParameters
33
- * @property {string } url - An absolute URL.
34
- * @property {LinkTarget } [target] - The link target. The default value is
35
- * `LinkTarget.NONE`.
36
- * @property {string } [rel] - The link relationship. The default value is
37
- * `DEFAULT_LINK_REL`.
38
- * @property {boolean } [enabled] - Whether the link should be enabled. The
39
- * default value is true.
40
- */
41
-
42
- /**
43
- * Adds various attributes (href, title, target, rel) to hyperlinks.
44
- * @param {HTMLAnchorElement } link - The link element.
45
- * @param {ExternalLinkParameters } params
46
- */
47
- function addLinkAttributes ( link , { url, target, rel, enabled = true } = { } ) {
48
- if ( ! url || typeof url !== "string" ) {
49
- throw new Error ( 'A valid "url" parameter must provided.' ) ;
50
- }
51
-
52
- if ( enabled ) {
53
- link . href = link . title = url ;
54
- } else {
55
- link . href = "" ;
56
- link . title = `Disabled: ${ url } ` ;
57
- link . onclick = ( ) => false ;
58
- }
59
-
60
- let targetStr = "" ; // LinkTarget.NONE
61
- switch ( target ) {
62
- case LinkTarget . NONE :
63
- break ;
64
- case LinkTarget . SELF :
65
- targetStr = "_self" ;
66
- break ;
67
- case LinkTarget . BLANK :
68
- targetStr = "_blank" ;
69
- break ;
70
- case LinkTarget . PARENT :
71
- targetStr = "_parent" ;
72
- break ;
73
- case LinkTarget . TOP :
74
- targetStr = "_top" ;
75
- break ;
76
- }
77
- link . target = targetStr ;
78
-
79
- link . rel = typeof rel === "string" ? rel : DEFAULT_LINK_REL ;
80
- }
81
-
82
31
/**
83
32
* @typedef {Object } PDFLinkServiceOptions
84
33
* @property {EventBus } eventBus - The application event bus.
@@ -98,6 +47,8 @@ function addLinkAttributes(link, { url, target, rel, enabled = true } = {}) {
98
47
* @implements {IPDFLinkService}
99
48
*/
100
49
class PDFLinkService {
50
+ externalLinkEnabled = true ;
51
+
101
52
#pagesRefCache = new Map ( ) ;
102
53
103
54
/**
@@ -112,7 +63,6 @@ class PDFLinkService {
112
63
this . eventBus = eventBus ;
113
64
this . externalLinkTarget = externalLinkTarget ;
114
65
this . externalLinkRel = externalLinkRel ;
115
- this . externalLinkEnabled = true ;
116
66
this . _ignoreDestinationZoom = ignoreDestinationZoom ;
117
67
118
68
this . baseUrl = null ;
@@ -146,35 +96,39 @@ class PDFLinkService {
146
96
* @type {number }
147
97
*/
148
98
get page ( ) {
149
- return this . pdfViewer . currentPageNumber ;
99
+ return this . pdfDocument ? this . pdfViewer . currentPageNumber : 1 ;
150
100
}
151
101
152
102
/**
153
103
* @param {number } value
154
104
*/
155
105
set page ( value ) {
156
- this . pdfViewer . currentPageNumber = value ;
106
+ if ( this . pdfDocument ) {
107
+ this . pdfViewer . currentPageNumber = value ;
108
+ }
157
109
}
158
110
159
111
/**
160
112
* @type {number }
161
113
*/
162
114
get rotation ( ) {
163
- return this . pdfViewer . pagesRotation ;
115
+ return this . pdfDocument ? this . pdfViewer . pagesRotation : 0 ;
164
116
}
165
117
166
118
/**
167
119
* @param {number } value
168
120
*/
169
121
set rotation ( value ) {
170
- this . pdfViewer . pagesRotation = value ;
122
+ if ( this . pdfDocument ) {
123
+ this . pdfViewer . pagesRotation = value ;
124
+ }
171
125
}
172
126
173
127
/**
174
128
* @type {boolean }
175
129
*/
176
130
get isInPresentationMode ( ) {
177
- return this . pdfViewer . isInPresentationMode ;
131
+ return this . pdfDocument ? this . pdfViewer . isInPresentationMode : false ;
178
132
}
179
133
180
134
#goToDestinationHelper( rawDest , namedDest = null , explicitDest ) {
@@ -294,18 +248,46 @@ class PDFLinkService {
294
248
}
295
249
296
250
/**
297
- * Wrapper around the `addLinkAttributes` helper function .
251
+ * Adds various attributes (href, title, target, rel) to hyperlinks .
298
252
* @param {HTMLAnchorElement } link
299
253
* @param {string } url
300
254
* @param {boolean } [newWindow]
301
255
*/
302
256
addLinkAttributes ( link , url , newWindow = false ) {
303
- addLinkAttributes ( link , {
304
- url,
305
- target : newWindow ? LinkTarget . BLANK : this . externalLinkTarget ,
306
- rel : this . externalLinkRel ,
307
- enabled : this . externalLinkEnabled ,
308
- } ) ;
257
+ if ( ! url || typeof url !== "string" ) {
258
+ throw new Error ( 'A valid "url" parameter must provided.' ) ;
259
+ }
260
+ const target = newWindow ? LinkTarget . BLANK : this . externalLinkTarget ,
261
+ rel = this . externalLinkRel ;
262
+
263
+ if ( this . externalLinkEnabled ) {
264
+ link . href = link . title = url ;
265
+ } else {
266
+ link . href = "" ;
267
+ link . title = `Disabled: ${ url } ` ;
268
+ link . onclick = ( ) => false ;
269
+ }
270
+
271
+ let targetStr = "" ; // LinkTarget.NONE
272
+ switch ( target ) {
273
+ case LinkTarget . NONE :
274
+ break ;
275
+ case LinkTarget . SELF :
276
+ targetStr = "_self" ;
277
+ break ;
278
+ case LinkTarget . BLANK :
279
+ targetStr = "_blank" ;
280
+ break ;
281
+ case LinkTarget . PARENT :
282
+ targetStr = "_parent" ;
283
+ break ;
284
+ case LinkTarget . TOP :
285
+ targetStr = "_top" ;
286
+ break ;
287
+ }
288
+ link . target = targetStr ;
289
+
290
+ link . rel = typeof rel === "string" ? rel : DEFAULT_LINK_REL ;
309
291
}
310
292
311
293
/**
@@ -467,6 +449,9 @@ class PDFLinkService {
467
449
* @param {string } action
468
450
*/
469
451
executeNamedAction ( action ) {
452
+ if ( ! this . pdfDocument ) {
453
+ return ;
454
+ }
470
455
// See PDF reference, table 8.45 - Named action
471
456
switch ( action ) {
472
457
case "GoBack" :
@@ -507,9 +492,11 @@ class PDFLinkService {
507
492
* @param {Object } action
508
493
*/
509
494
async executeSetOCGState ( action ) {
510
- const pdfDocument = this . pdfDocument ;
511
- const optionalContentConfig =
512
- await this . pdfViewer . optionalContentConfigPromise ;
495
+ if ( ! this . pdfDocument ) {
496
+ return ;
497
+ }
498
+ const pdfDocument = this . pdfDocument ,
499
+ optionalContentConfig = await this . pdfViewer . optionalContentConfigPromise ;
513
500
514
501
if ( pdfDocument !== this . pdfDocument ) {
515
502
return ; // The document was closed while the optional content resolved.
@@ -603,98 +590,8 @@ class PDFLinkService {
603
590
/**
604
591
* @implements {IPDFLinkService}
605
592
*/
606
- class SimpleLinkService {
607
- constructor ( ) {
608
- this . externalLinkEnabled = true ;
609
- }
610
-
611
- /**
612
- * @type {number }
613
- */
614
- get pagesCount ( ) {
615
- return 0 ;
616
- }
617
-
618
- /**
619
- * @type {number }
620
- */
621
- get page ( ) {
622
- return 0 ;
623
- }
624
-
625
- /**
626
- * @param {number } value
627
- */
628
- set page ( value ) { }
629
-
630
- /**
631
- * @type {number }
632
- */
633
- get rotation ( ) {
634
- return 0 ;
635
- }
636
-
637
- /**
638
- * @param {number } value
639
- */
640
- set rotation ( value ) { }
641
-
642
- /**
643
- * @type {boolean }
644
- */
645
- get isInPresentationMode ( ) {
646
- return false ;
647
- }
648
-
649
- /**
650
- * @param {string|Array } dest - The named, or explicit, PDF destination.
651
- */
652
- async goToDestination ( dest ) { }
653
-
654
- /**
655
- * @param {number|string } val - The page number, or page label.
656
- */
657
- goToPage ( val ) { }
658
-
659
- /**
660
- * @param {HTMLAnchorElement } link
661
- * @param {string } url
662
- * @param {boolean } [newWindow]
663
- */
664
- addLinkAttributes ( link , url , newWindow = false ) {
665
- addLinkAttributes ( link , { url, enabled : this . externalLinkEnabled } ) ;
666
- }
667
-
668
- /**
669
- * @param dest - The PDF destination object.
670
- * @returns {string } The hyperlink to the PDF object.
671
- */
672
- getDestinationHash ( dest ) {
673
- return "#" ;
674
- }
675
-
676
- /**
677
- * @param hash - The PDF parameters/hash.
678
- * @returns {string } The hyperlink to the PDF object.
679
- */
680
- getAnchorUrl ( hash ) {
681
- return "#" ;
682
- }
683
-
684
- /**
685
- * @param {string } hash
686
- */
687
- setHash ( hash ) { }
688
-
689
- /**
690
- * @param {string } action
691
- */
692
- executeNamedAction ( action ) { }
693
-
694
- /**
695
- * @param {Object } action
696
- */
697
- executeSetOCGState ( action ) { }
593
+ class SimpleLinkService extends PDFLinkService {
594
+ setDocument ( pdfDocument , baseUrl = null ) { }
698
595
699
596
/**
700
597
* @param {number } pageNum - page number.
0 commit comments