author.page.ts 3.24 KB
Newer Older
1 2 3 4 5 6 7
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';
8
import {ExerciseService} from '../exercise.service';
9 10 11
import {ApplicationState} from '../models/applicationState';
import {Observable} from 'rxjs';
import {take} from 'rxjs/operators';
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

/**
 * 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,
30
                public http: HttpClient,
31 32
                public exerciseService: ExerciseService,
                public helperService: HelperService) {
33 34 35
        if (!this.corpusService.availableAuthors) {
            this.corpusService.loadCorporaFromLocalStorage();
        }
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        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)));
    }

64
    restoreLastSetup() {
65
        this.corpusService.restoreLastCorpus().then(() => {
66
            if (HelperService.isVocabularyCheck) {
67
                HelperService.goToVocabularyCheckPage(this.navCtrl).then();
68
            } else {
69
                HelperService.goToShowTextPage(this.navCtrl).then();
70 71 72 73
            }
        }, () => {
        });
    }
74

75 76
    showCorpora(author: Author) {
        this.corpusService.currentAuthor = author;
77 78 79 80 81
        HelperService.applicationState.pipe(take(1)).subscribe((as: ApplicationState) => {
            as.currentSetup.currentAuthor = author;
            this.helperService.saveApplicationState(as).then();
            this.navCtrl.navigateForward('/author-detail').then();
        });
82 83 84 85 86 87 88 89 90
    }

    toggleTreebankAuthors() {
        this.baseAuthorList = this.showOnlyTreebanks ? this.getAuthorsFiltered() : this.corpusService.availableAuthors;
        this.authorsDisplayed = this.baseAuthorList.filter((author: Author) => {
            return AuthorPage.filterAuthor(author, this.currentSearchValue);
        });
    }
}