@@ -52,14 +52,15 @@ import { PagesCountLimit } from "./pdf_viewer.js";
52
52
*/
53
53
54
54
class SecondaryToolbar {
55
+ #opts;
56
+
55
57
/**
56
58
* @param {SecondaryToolbarOptions } options
57
59
* @param {EventBus } eventBus
58
60
*/
59
61
constructor ( options , eventBus ) {
60
- this . toolbar = options . toolbar ;
61
- this . toggleButton = options . toggleButton ;
62
- this . buttons = [
62
+ this . #opts = options ;
63
+ const buttons = [
63
64
{
64
65
element : options . presentationModeButton ,
65
66
eventName : "presentationmode" ,
@@ -141,28 +142,19 @@ class SecondaryToolbar {
141
142
} ,
142
143
] ;
143
144
if ( typeof PDFJSDev === "undefined" || PDFJSDev . test ( "GENERIC" ) ) {
144
- this . buttons . push ( {
145
+ buttons . push ( {
145
146
element : options . openFileButton ,
146
147
eventName : "openfile" ,
147
148
close : true ,
148
149
} ) ;
149
150
}
150
- this . items = {
151
- firstPage : options . firstPageButton ,
152
- lastPage : options . lastPageButton ,
153
- pageRotateCw : options . pageRotateCwButton ,
154
- pageRotateCcw : options . pageRotateCcwButton ,
155
- } ;
156
151
157
152
this . eventBus = eventBus ;
158
153
this . opened = false ;
159
154
160
155
// Bind the event listeners for click, cursor tool, and scroll/spread mode
161
156
// actions.
162
- this . #bindClickListeners( ) ;
163
- this . #bindCursorToolsListener( options ) ;
164
- this . #bindScrollModeListener( options ) ;
165
- this . #bindSpreadModeListener( options ) ;
157
+ this . #bindListeners( buttons ) ;
166
158
167
159
this . reset ( ) ;
168
160
}
@@ -190,30 +182,40 @@ class SecondaryToolbar {
190
182
this . #updateUIState( ) ;
191
183
192
184
// Reset the Scroll/Spread buttons too, since they're document specific.
193
- this . eventBus . dispatch ( "secondarytoolbarreset" , { source : this } ) ;
185
+ this . #scrollModeChanged( { mode : ScrollMode . VERTICAL } ) ;
186
+ this . #spreadModeChanged( { mode : SpreadMode . NONE } ) ;
194
187
}
195
188
196
189
#updateUIState( ) {
197
- this . items . firstPage . disabled = this . pageNumber <= 1 ;
198
- this . items . lastPage . disabled = this . pageNumber >= this . pagesCount ;
199
- this . items . pageRotateCw . disabled = this . pagesCount === 0 ;
200
- this . items . pageRotateCcw . disabled = this . pagesCount === 0 ;
190
+ const {
191
+ firstPageButton,
192
+ lastPageButton,
193
+ pageRotateCwButton,
194
+ pageRotateCcwButton,
195
+ } = this . #opts;
196
+
197
+ firstPageButton . disabled = this . pageNumber <= 1 ;
198
+ lastPageButton . disabled = this . pageNumber >= this . pagesCount ;
199
+ pageRotateCwButton . disabled = this . pagesCount === 0 ;
200
+ pageRotateCcwButton . disabled = this . pagesCount === 0 ;
201
201
}
202
202
203
- #bindClickListeners( ) {
203
+ #bindListeners( buttons ) {
204
+ const { eventBus } = this ;
205
+ const { toggleButton } = this . #opts;
204
206
// Button to toggle the visibility of the secondary toolbar.
205
- this . toggleButton . addEventListener ( "click" , this . toggle . bind ( this ) ) ;
207
+ toggleButton . addEventListener ( "click" , this . toggle . bind ( this ) ) ;
206
208
207
209
// All items within the secondary toolbar.
208
- for ( const { element, eventName, close, eventDetails } of this . buttons ) {
210
+ for ( const { element, eventName, close, eventDetails } of buttons ) {
209
211
element . addEventListener ( "click" , evt => {
210
212
if ( eventName !== null ) {
211
- this . eventBus . dispatch ( eventName , { source : this , ...eventDetails } ) ;
213
+ eventBus . dispatch ( eventName , { source : this , ...eventDetails } ) ;
212
214
}
213
215
if ( close ) {
214
216
this . close ( ) ;
215
217
}
216
- this . eventBus . dispatch ( "reporttelemetry" , {
218
+ eventBus . dispatch ( "reporttelemetry" , {
217
219
source : this ,
218
220
details : {
219
221
type : "buttons" ,
@@ -222,88 +224,78 @@ class SecondaryToolbar {
222
224
} ) ;
223
225
} ) ;
224
226
}
227
+
228
+ eventBus . _on ( "cursortoolchanged" , this . #cursorToolChanged. bind ( this ) ) ;
229
+ eventBus . _on ( "scrollmodechanged" , this . #scrollModeChanged. bind ( this ) ) ;
230
+ eventBus . _on ( "spreadmodechanged" , this . #spreadModeChanged. bind ( this ) ) ;
225
231
}
226
232
227
- #bindCursorToolsListener ( { cursorSelectToolButton , cursorHandToolButton } ) {
228
- this . eventBus . _on ( "cursortoolchanged" , ( { tool } ) => {
229
- toggleCheckedBtn ( cursorSelectToolButton , tool === CursorTool . SELECT ) ;
230
- toggleCheckedBtn ( cursorHandToolButton , tool === CursorTool . HAND ) ;
231
- } ) ;
233
+ #cursorToolChanged ( { tool } ) {
234
+ const { cursorSelectToolButton , cursorHandToolButton } = this . #opts ;
235
+
236
+ toggleCheckedBtn ( cursorSelectToolButton , tool === CursorTool . SELECT ) ;
237
+ toggleCheckedBtn ( cursorHandToolButton , tool === CursorTool . HAND ) ;
232
238
}
233
239
234
- #bindScrollModeListener( {
235
- scrollPageButton,
236
- scrollVerticalButton,
237
- scrollHorizontalButton,
238
- scrollWrappedButton,
239
- spreadNoneButton,
240
- spreadOddButton,
241
- spreadEvenButton,
242
- } ) {
243
- const scrollModeChanged = ( { mode } ) => {
244
- toggleCheckedBtn ( scrollPageButton , mode === ScrollMode . PAGE ) ;
245
- toggleCheckedBtn ( scrollVerticalButton , mode === ScrollMode . VERTICAL ) ;
246
- toggleCheckedBtn ( scrollHorizontalButton , mode === ScrollMode . HORIZONTAL ) ;
247
- toggleCheckedBtn ( scrollWrappedButton , mode === ScrollMode . WRAPPED ) ;
240
+ #scrollModeChanged( { mode } ) {
241
+ const {
242
+ scrollPageButton,
243
+ scrollVerticalButton,
244
+ scrollHorizontalButton,
245
+ scrollWrappedButton,
246
+ spreadNoneButton,
247
+ spreadOddButton,
248
+ spreadEvenButton,
249
+ } = this . #opts;
248
250
249
- // Permanently *disable* the Scroll buttons when PAGE-scrolling is being
250
- // enforced for *very* long/large documents; please see the `BaseViewer`.
251
- const forceScrollModePage =
252
- this . pagesCount > PagesCountLimit . FORCE_SCROLL_MODE_PAGE ;
253
- scrollPageButton . disabled = forceScrollModePage ;
254
- scrollVerticalButton . disabled = forceScrollModePage ;
255
- scrollHorizontalButton . disabled = forceScrollModePage ;
256
- scrollWrappedButton . disabled = forceScrollModePage ;
251
+ toggleCheckedBtn ( scrollPageButton , mode === ScrollMode . PAGE ) ;
252
+ toggleCheckedBtn ( scrollVerticalButton , mode === ScrollMode . VERTICAL ) ;
253
+ toggleCheckedBtn ( scrollHorizontalButton , mode === ScrollMode . HORIZONTAL ) ;
254
+ toggleCheckedBtn ( scrollWrappedButton , mode === ScrollMode . WRAPPED ) ;
257
255
258
- // Temporarily *disable* the Spread buttons when horizontal scrolling is
259
- // enabled, since the non-default Spread modes doesn't affect the layout .
260
- const isHorizontal = mode === ScrollMode . HORIZONTAL ;
261
- spreadNoneButton . disabled = isHorizontal ;
262
- spreadOddButton . disabled = isHorizontal ;
263
- spreadEvenButton . disabled = isHorizontal ;
264
- } ;
265
- this . eventBus . _on ( "scrollmodechanged" , scrollModeChanged ) ;
256
+ // Permanently *disable* the Scroll buttons when PAGE- scrolling is being
257
+ // enforced for *very* long/large documents; please see the `BaseViewer` .
258
+ const forceScrollModePage =
259
+ this . pagesCount > PagesCountLimit . FORCE_SCROLL_MODE_PAGE ;
260
+ scrollPageButton . disabled = forceScrollModePage ;
261
+ scrollVerticalButton . disabled = forceScrollModePage ;
262
+ scrollHorizontalButton . disabled = forceScrollModePage ;
263
+ scrollWrappedButton . disabled = forceScrollModePage ;
266
264
267
- this . eventBus . _on ( "secondarytoolbarreset" , evt => {
268
- if ( evt . source === this ) {
269
- scrollModeChanged ( { mode : ScrollMode . VERTICAL } ) ;
270
- }
271
- } ) ;
265
+ // Temporarily *disable* the Spread buttons when horizontal scrolling is
266
+ // enabled, since the non-default Spread modes doesn't affect the layout.
267
+ const isHorizontal = mode === ScrollMode . HORIZONTAL ;
268
+ spreadNoneButton . disabled = isHorizontal ;
269
+ spreadOddButton . disabled = isHorizontal ;
270
+ spreadEvenButton . disabled = isHorizontal ;
272
271
}
273
272
274
- #bindSpreadModeListener( {
275
- spreadNoneButton,
276
- spreadOddButton,
277
- spreadEvenButton,
278
- } ) {
279
- const spreadModeChanged = ( { mode } ) => {
280
- toggleCheckedBtn ( spreadNoneButton , mode === SpreadMode . NONE ) ;
281
- toggleCheckedBtn ( spreadOddButton , mode === SpreadMode . ODD ) ;
282
- toggleCheckedBtn ( spreadEvenButton , mode === SpreadMode . EVEN ) ;
283
- } ;
284
- this . eventBus . _on ( "spreadmodechanged" , spreadModeChanged ) ;
273
+ #spreadModeChanged( { mode } ) {
274
+ const { spreadNoneButton, spreadOddButton, spreadEvenButton } = this . #opts;
285
275
286
- this . eventBus . _on ( "secondarytoolbarreset" , evt => {
287
- if ( evt . source === this ) {
288
- spreadModeChanged ( { mode : SpreadMode . NONE } ) ;
289
- }
290
- } ) ;
276
+ toggleCheckedBtn ( spreadNoneButton , mode === SpreadMode . NONE ) ;
277
+ toggleCheckedBtn ( spreadOddButton , mode === SpreadMode . ODD ) ;
278
+ toggleCheckedBtn ( spreadEvenButton , mode === SpreadMode . EVEN ) ;
291
279
}
292
280
293
281
open ( ) {
294
282
if ( this . opened ) {
295
283
return ;
296
284
}
297
285
this . opened = true ;
298
- toggleExpandedBtn ( this . toggleButton , true , this . toolbar ) ;
286
+
287
+ const { toggleButton, toolbar } = this . #opts;
288
+ toggleExpandedBtn ( toggleButton , true , toolbar ) ;
299
289
}
300
290
301
291
close ( ) {
302
292
if ( ! this . opened ) {
303
293
return ;
304
294
}
305
295
this . opened = false ;
306
- toggleExpandedBtn ( this . toggleButton , false , this . toolbar ) ;
296
+
297
+ const { toggleButton, toolbar } = this . #opts;
298
+ toggleExpandedBtn ( toggleButton , false , toolbar ) ;
307
299
}
308
300
309
301
toggle ( ) {
0 commit comments