1
1
import MicroEvent from '../../../vendor/microevent' ;
2
2
import * as translations from '../../translations' ;
3
3
import Verso from '../../verso-browser/verso' ;
4
- import { V2Hotspot , V2PageDecoration } from '../core' ;
4
+ import { V2Hotspot , V2PageDecoration , V2Offer } from '../core' ;
5
5
import PageDecorations from '../core-ui/page-decorations' ;
6
6
import singleChoicePopover from '../core-ui/single-choice-popover' ;
7
7
import { Tracker } from '../events' ;
@@ -56,6 +56,7 @@ export interface ViewerInit {
56
56
keyboard : 'disabled' | 'enabled' | 'global' ;
57
57
hotspotRatio ?: number ;
58
58
pickHotspot ?: typeof defaultPickHotspot ;
59
+ fetchOffers : any ;
59
60
}
60
61
class Viewer extends MicroEvent {
61
62
_hotspots = new Hotspots ( ) ;
@@ -94,6 +95,8 @@ class Viewer extends MicroEvent {
94
95
_eventTracking : EventTracking ;
95
96
pageDecorations : V2PageDecoration [ ] ;
96
97
options : ViewerInit ;
98
+ offersCache : V2Offer [ ] = [ ] ;
99
+ pagesCache : number [ ] = [ ] ;
97
100
// @ts -expect-error
98
101
constructor ( el : HTMLElement , options : ViewerInit = { } ) {
99
102
super ( ) ;
@@ -183,9 +186,10 @@ class Viewer extends MicroEvent {
183
186
this . _eventTracking . trigger ( 'pageLoaded' , e ) ;
184
187
this . trigger ( 'pageLoaded' , e ) ;
185
188
} ) ;
186
- this . _core . bind ( 'pagesLoaded' , ( e ) => {
189
+ this . _core . bind ( 'pagesLoaded' , async ( e ) => {
187
190
this . _hotspots . trigger ( 'pagesLoaded' , e ) ;
188
191
this . trigger ( 'pagesLoaded' , e ) ;
192
+ await this . processOffers ( e ) ;
189
193
} ) ;
190
194
this . _core . bind ( 'resized' , ( e ) => {
191
195
this . _hotspots . trigger ( 'resized' ) ;
@@ -336,6 +340,8 @@ class Viewer extends MicroEvent {
336
340
( pageSpread ) => pageSpread . getId ( ) === hotspotRequest . id
337
341
) ;
338
342
343
+ console . log ( 'hotspots:::' , hotspots ) ;
344
+
339
345
this . _hotspots . trigger ( 'hotspotsReceived' , {
340
346
pageSpread : this . _core . pageSpreads . get ( hotspotRequest . id ) ,
341
347
versoPageSpread,
@@ -349,6 +355,7 @@ class Viewer extends MicroEvent {
349
355
}
350
356
351
357
hotspotsRequested = ( e ) => {
358
+ console . log ( 'hjotspotsRequested:::' , e ) ;
352
359
this . hotspotQueue . push ( e ) ;
353
360
this . processHotspotQueue ( ) ;
354
361
} ;
@@ -443,6 +450,47 @@ class Viewer extends MicroEvent {
443
450
this . trigger ( 'hotspotPressed' , hotspot ) ;
444
451
} ) ;
445
452
} ;
453
+
454
+ // getHotspotsByPageNumber(pageNumber: number): {
455
+ // if (!this.hotspots) return [];
456
+
457
+ // return Object.values(this.hotspots).filter(hotspot =>
458
+ // hotspot.locations.hasOwnProperty(pageNumber)
459
+ // );
460
+ // }
461
+
462
+ getHotspotIdsByPageNumber = ( pageNumber : number ) : string [ ] => {
463
+ if ( ! this . hotspots ) {
464
+ return [ ] ;
465
+ }
466
+
467
+ return Object . values ( this . hotspots )
468
+ . filter ( ( hotspot ) => hotspot . locations . hasOwnProperty ( pageNumber ) )
469
+ . map ( ( hotspot ) => hotspot . id ) ;
470
+ } ;
471
+
472
+ processOffers = async ( e ) => {
473
+ for ( const { pageNumber} of e . pages ) {
474
+ if ( ! this . pagesCache . includes ( pageNumber ) ) {
475
+ const hotspotIds = this . getHotspotIdsByPageNumber ( pageNumber ) ;
476
+ console . log ( 'hotspotIds' , hotspotIds ) ;
477
+ if ( hotspotIds . length ) {
478
+ const offers = await this . options . fetchOffers ( hotspotIds ) ;
479
+ if ( offers ) {
480
+ this . pagesCache . push ( pageNumber ) ;
481
+ this . offersCache . push ( ...offers ) ;
482
+
483
+ console . log ( 'saved!' , pageNumber ) ;
484
+ console . log ( 'offers' , offers ) ;
485
+ }
486
+ }
487
+ }
488
+ }
489
+
490
+ console . log ( 'pagesCache' , this . pagesCache ) ;
491
+ console . log ( 'offersCache' , this . offersCache ) ;
492
+ // offers: V2Offer[]
493
+ } ;
446
494
}
447
495
448
496
export default Viewer ;
0 commit comments