@@ -25,7 +25,7 @@ import { AddonModGlossarySyncProvider } from '../../providers/sync';
2525import { AddonModGlossaryModePickerPopoverComponent } from '../mode-picker/mode-picker' ;
2626import { AddonModGlossaryPrefetchHandler } from '../../providers/prefetch-handler' ;
2727
28- type FetchMode = 'author_all' | 'cat_all' | 'newest_first' | 'recently_updated' | 'search' | ' letter_all';
28+ type FetchMode = 'author_all' | 'cat_all' | 'newest_first' | 'recently_updated' | 'letter_all' ;
2929
3030/**
3131 * Component that displays a glossary entry page.
@@ -41,8 +41,6 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
4141 component = AddonModGlossaryProvider . COMPONENT ;
4242 moduleName = 'glossary' ;
4343
44- fetchMode : FetchMode ;
45- viewMode : string ;
4644 isSearch = false ;
4745 entries = [ ] ;
4846 offlineEntries = [ ] ;
@@ -60,6 +58,10 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
6058 protected showDivider : ( entry : any , previous ?: any ) => boolean ;
6159 protected getDivider : ( entry : any ) => string ;
6260 protected addEntryObserver : any ;
61+ protected fetchMode : FetchMode ;
62+ protected viewMode : string ;
63+ protected fetchedEntriesCanLoadMore = false ;
64+ protected fetchedEntries = [ ] ;
6365
6466 hasOfflineRatings : boolean ;
6567 protected ratingOfflineObserver : any ;
@@ -252,6 +254,7 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
252254 */
253255 protected switchMode ( mode : FetchMode ) : void {
254256 this . fetchMode = mode ;
257+ this . isSearch = false ;
255258
256259 switch ( mode ) {
257260 case 'author_all' :
@@ -294,15 +297,6 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
294297 this . getDivider = null ;
295298 this . showDivider = ( ) : boolean => false ;
296299 break ;
297- case 'search' :
298- // Search for entries.
299- this . viewMode = 'search' ;
300- this . fetchFunction = this . glossaryProvider . getEntriesBySearch ;
301- this . fetchInvalidate = this . glossaryProvider . invalidateEntriesBySearch ;
302- this . fetchArguments = null ; // Dynamically set later.
303- this . getDivider = null ;
304- this . showDivider = ( ) : boolean => false ;
305- break ;
306300 case 'letter_all' :
307301 default :
308302 // Consider it is 'letter_all'.
@@ -341,26 +335,15 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
341335 */
342336 openModePicker ( event : MouseEvent ) : void {
343337 const popover = this . popoverCtrl . create ( AddonModGlossaryModePickerPopoverComponent , {
344- glossary : this . glossary ,
345- selectedMode : this . fetchMode
338+ browsemodes : this . glossary . browsemodes ,
339+ selectedMode : this . isSearch ? '' : this . fetchMode
346340 } ) ;
347341
348- popover . onDidDismiss ( ( newMode : FetchMode ) => {
349- if ( newMode === this . fetchMode ) {
350- return ;
351- }
352-
353- this . loadingMessage = this . translate . instant ( 'core.loading' ) ;
354- this . domUtils . scrollToTop ( this . content ) ;
355- this . switchMode ( newMode ) ;
356-
357- if ( this . fetchMode === 'search' ) {
358- // If it's not an instant search, then we reset the values.
359- this . entries = [ ] ;
360- this . canLoadMore = false ;
361- } else {
362- this . loaded = false ;
363- this . loadContent ( ) ;
342+ popover . onDidDismiss ( ( mode : FetchMode ) => {
343+ if ( mode !== this . fetchMode ) {
344+ this . changeFetchMode ( mode ) ;
345+ } else if ( this . isSearch ) {
346+ this . toggleSearch ( ) ;
364347 }
365348 } ) ;
366349
@@ -369,6 +352,44 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity
369352 } ) ;
370353 }
371354
355+ /**
356+ * Toggles between search and fetch mode.
357+ */
358+ toggleSearch ( ) : void {
359+ if ( this . isSearch ) {
360+ this . isSearch = false ;
361+ this . entries = this . fetchedEntries ;
362+ this . canLoadMore = this . fetchedEntriesCanLoadMore ;
363+ this . switchMode ( this . fetchMode ) ;
364+ } else {
365+ // Search for entries.
366+ this . fetchFunction = this . glossaryProvider . getEntriesBySearch ;
367+ this . fetchInvalidate = this . glossaryProvider . invalidateEntriesBySearch ;
368+ this . fetchArguments = null ; // Dynamically set later.
369+ this . getDivider = null ;
370+ this . showDivider = ( ) : boolean => false ;
371+ this . isSearch = true ;
372+
373+ this . fetchedEntries = this . entries ;
374+ this . fetchedEntriesCanLoadMore = this . canLoadMore ;
375+ this . canLoadMore = false ;
376+ this . entries = [ ] ;
377+ }
378+ }
379+
380+ /**
381+ * Change fetch mode
382+ * @param {FetchMode } mode [description]
383+ */
384+ changeFetchMode ( mode : FetchMode ) : void {
385+ this . isSearch = false ;
386+ this . loadingMessage = this . translate . instant ( 'core.loading' ) ;
387+ this . domUtils . scrollToTop ( this . content ) ;
388+ this . switchMode ( mode ) ;
389+ this . loaded = false ;
390+ this . loadContent ( ) ;
391+ }
392+
372393 /**
373394 * Opens an entry.
374395 *
0 commit comments