@@ -16,14 +16,42 @@ export async function init(id, invoke, options) {
1616 return ;
1717 }
1818
19- if ( options . url === null ) {
19+ Data . set ( id , { el, invoke, options } ) ;
20+
21+ if ( options . url ) {
22+ setUrl ( id , options . url ) ;
23+ }
24+ else {
25+ setData ( id , options . data ) ;
26+ }
27+ }
28+
29+ export async function setUrl ( id , url ) {
30+ const pdf = Data . get ( id ) ;
31+ disposePdf ( pdf ) ;
32+
33+ if ( ! url ) {
2034 return ;
2135 }
2236
23- const pdfViewer = await loadPdf ( el , invoke , options ) ;
24- const observer = setObserver ( el ) ;
37+ const { options } = pdf ;
38+ options . url = url ;
39+ options . data = null ;
40+ await loadPdf ( pdf ) ;
41+ }
42+
43+ export async function setData ( id , data ) {
44+ const pdf = Data . get ( id ) ;
45+ disposePdf ( pdf ) ;
46+
47+ if ( ! data ) {
48+ return ;
49+ }
2550
26- Data . set ( id , { el, pdfViewer, observer } ) ;
51+ const { options } = pdf ;
52+ options . url = null ;
53+ options . data = data ;
54+ await loadPdf ( pdf ) ;
2755}
2856
2957export function setScaleValue ( id , value ) {
@@ -74,7 +102,8 @@ export function resetThumbnails(id) {
74102 }
75103}
76104
77- const loadPdf = async ( el , invoke , options ) => {
105+ const loadPdf = async pdf => {
106+ const { el, invoke, options } = pdf ;
78107 const loadingTask = pdfjsLib . getDocument ( options ) ;
79108 loadingTask . onProgress = function ( progressData ) {
80109
@@ -96,6 +125,7 @@ const loadPdf = async (el, invoke, options) => {
96125 eventBus
97126 } ) ;
98127
128+ resetToolbarView ( el , pdfViewer ) ;
99129 addEventBus ( el , pdfViewer , eventBus , invoke , options ) ;
100130
101131 const pdfDocument = await loadingTask . promise ;
@@ -105,7 +135,37 @@ const loadPdf = async (el, invoke, options) => {
105135 loadMetadata ( el , pdfViewer , metadata ) ;
106136 } ) ;
107137
108- return pdfViewer ;
138+ pdf . pdfViewer = pdfViewer ;
139+ pdf . loadingTask = loadingTask ;
140+ pdf . observer = setObserver ( el ) ;
141+ }
142+
143+ const disposePdf = pdf => {
144+ const { el, observer, loadingTask } = pdf ;
145+ if ( observer ) {
146+ observer . disconnect ( ) ;
147+ }
148+
149+ if ( loadingTask && ! loadingTask . destroyed ) {
150+ loadingTask . destroy ( ) ;
151+ }
152+
153+ if ( el ) {
154+ removeToolbarEventHandlers ( el ) ;
155+ const viewContainer = el . querySelector ( ".bb-view-container" ) ;
156+ if ( viewContainer ) {
157+ [ ...viewContainer . children ] . forEach ( i => {
158+ if ( ! i . classList . contains ( "pdfViewer" ) ) {
159+ i . remove ( ) ;
160+ }
161+ } )
162+ }
163+
164+ const thumbnailsContainer = el . querySelector ( ".bb-view-thumbnails" ) ;
165+ if ( thumbnailsContainer ) {
166+ thumbnailsContainer . innerHTML = "" ;
167+ }
168+ }
109169}
110170
111171const loadMetadata = ( el , pdfViewer , metadata ) => {
@@ -474,6 +534,37 @@ const addToolbarEventHandlers = (el, pdfViewer, invoke, options) => {
474534 } ) ;
475535}
476536
537+ const removeToolbarEventHandlers = el => {
538+ if ( el ) {
539+ const towPagesOneView = el . querySelector ( ".dropdown-item-pages" ) ;
540+ if ( towPagesOneView ) {
541+ EventHandler . off ( towPagesOneView , "click" ) ;
542+ }
543+
544+ const toolbar = el . querySelector ( ".bb-view-toolbar" ) ;
545+ if ( toolbar ) {
546+ EventHandler . off ( toolbar , "click" ) ;
547+ EventHandler . off ( toolbar , "change" ) ;
548+ EventHandler . off ( toolbar , "focus" ) ;
549+ }
550+
551+ const thumbnailsContainer = el . querySelector ( ".bb-view-thumbnails" ) ;
552+ if ( thumbnailsContainer ) {
553+ EventHandler . off ( thumbnailsContainer , "click" ) ;
554+ }
555+
556+ const iframe = document . querySelector ( '.bb-view-print-iframe' ) ;
557+ if ( iframe ) {
558+ iframe . remove ( ) ;
559+ }
560+
561+ const closeButton = el . querySelector ( ".btn-close-doc" ) ;
562+ if ( closeButton ) {
563+ EventHandler . off ( closeButton , "click" ) ;
564+ }
565+ }
566+ }
567+
477568const resetToolbarView = ( el , pdfViewer ) => {
478569 const scaleEl = el . querySelector ( ".bb-view-scale-input" ) ;
479570 updateScaleValue ( el , pdfViewer . currentScale ) ;
@@ -624,39 +715,8 @@ const printPdf = url => {
624715}
625716
626717export function dispose ( id ) {
627- const { el , observer } = Data . get ( id ) ;
718+ const pdf = Data . get ( id ) ;
628719 Data . remove ( id ) ;
629720
630- if ( observer ) {
631- observer . disconnect ( ) ;
632- }
633-
634- if ( el ) {
635- const towPagesOneView = el . querySelector ( ".dropdown-item-pages" ) ;
636- if ( towPagesOneView ) {
637- EventHandler . off ( towPagesOneView , "click" ) ;
638- }
639-
640- const toolbar = el . querySelector ( ".bb-view-toolbar" ) ;
641- if ( toolbar ) {
642- EventHandler . off ( toolbar , "click" ) ;
643- EventHandler . off ( toolbar , "change" ) ;
644- EventHandler . off ( toolbar , "focus" ) ;
645- }
646-
647- const thumbnailsContainer = el . querySelector ( ".bb-view-thumbnails" ) ;
648- if ( thumbnailsContainer ) {
649- EventHandler . off ( thumbnailsContainer , "click" ) ;
650- }
651-
652- const iframe = document . querySelector ( '.bb-view-print-iframe' ) ;
653- if ( iframe ) {
654- iframe . remove ( ) ;
655- }
656-
657- const closeButton = el . querySelector ( ".btn-close-doc" ) ;
658- if ( closeButton ) {
659- EventHandler . off ( closeButton , "click" ) ;
660- }
661- }
721+ disposePdf ( pdf ) ;
662722}
0 commit comments