import {Component} from '@angular/core'; import {Author} from 'src/app/models/author'; import {NavController} from '@ionic/angular'; import {TranslateService} from '@ngx-translate/core'; import {CorpusService} from 'src/app/corpus.service'; import {HttpClient} from '@angular/common/http'; import {HelperService} from '../helper.service'; import {ExerciseService} from '../exercise.service'; import {ApplicationState} from '../models/applicationState'; import {Observable} from 'rxjs'; import {take} from 'rxjs/operators'; /** * Generated class for the AuthorPage page. * * See https://ionicframework.com/docs/components/#navigation for more info on * Ionic pages and navigation. */ @Component({ selector: 'app-author', templateUrl: './author.page.html', styleUrls: ['./author.page.scss'], }) export class AuthorPage { constructor(public navCtrl: NavController, public translate: TranslateService, public corpusService: CorpusService, public http: HttpClient, public exerciseService: ExerciseService, public helperService: HelperService) { if (!this.corpusService.availableAuthors) { this.corpusService.loadCorporaFromLocalStorage(); } this.toggleTreebankAuthors(); } public authorsDisplayed: Author[]; public baseAuthorList: Author[]; HelperService = HelperService; showOnlyTreebanks = true; currentSearchValue = ''; static filterAuthor(author: Author, filterValue: string) { return author.name.toLowerCase().includes(filterValue.toLowerCase()); } getAuthors(newSearchValue: string) { this.baseAuthorList = this.showOnlyTreebanks ? this.getAuthorsFiltered() : this.corpusService.availableAuthors; if (!newSearchValue) { this.authorsDisplayed = this.baseAuthorList; } else { this.authorsDisplayed = this.baseAuthorList.filter((author: Author) => { return AuthorPage.filterAuthor(author, newSearchValue); }); } } getAuthorsFiltered() { return this.corpusService.availableAuthors.filter(author => author.corpora.some(corpus => this.corpusService.isTreebank(corpus))); } restoreLastSetup() { this.corpusService.restoreLastCorpus().then(() => { if (HelperService.isVocabularyCheck) { HelperService.goToVocabularyCheckPage(this.navCtrl).then(); } else { HelperService.goToShowTextPage(this.navCtrl).then(); } }, () => { }); } showCorpora(author: Author) { this.corpusService.currentAuthor = author; HelperService.applicationState.pipe(take(1)).subscribe((as: ApplicationState) => { as.currentSetup.currentAuthor = author; this.helperService.saveApplicationState(as).then(); this.navCtrl.navigateForward('/author-detail').then(); }); } toggleTreebankAuthors() { this.baseAuthorList = this.showOnlyTreebanks ? this.getAuthorsFiltered() : this.corpusService.availableAuthors; this.authorsDisplayed = this.baseAuthorList.filter((author: Author) => { return AuthorPage.filterAuthor(author, this.currentSearchValue); }); } }