preview.page.ts 5.1 KB
Newer Older
1
/* tslint:disable:no-string-literal */
2
3
4
5
6
7
import {AnnisResponse} from 'src/app/models/annisResponse';
import {ExerciseType, FileType} from 'src/app/models/enum';
import {HelperService} from 'src/app/helper.service';
import {NavController} from '@ionic/angular';
import {ExerciseService} from 'src/app/exercise.service';
import {CorpusService} from 'src/app/corpus.service';
8
import {Component, OnDestroy} from '@angular/core';
9
10
11
12
13
14
15
16
17
18
19
import {TranslateService} from '@ngx-translate/core';
import {Solution} from 'src/app/models/solution';
import {HttpClient} from '@angular/common/http';

declare var H5P: any;

@Component({
    selector: 'app-preview',
    templateUrl: './preview.page.html',
    styleUrls: ['./preview.page.scss'],
})
20
export class PreviewPage implements OnDestroy {
21
22
23
24
25
26
27
    HelperService = HelperService;
    public ExerciseType = ExerciseType;
    public FileType = FileType;
    public currentSolutions: Solution[];
    public maxGapLength = 0;
    public showInstructions = false;
    public solutionIndicesString: string;
28
29
    public solutionNodeIdSet: Set<string> = new Set<string>();
    public urlBase: string;
30
31
32
33
34

    constructor(public navCtrl: NavController,
                public http: HttpClient,
                public exerciseService: ExerciseService,
                public translateService: TranslateService,
35
36
                public corpusService: CorpusService,
                public helperService: HelperService) {
37
38
39
40
        this.currentSolutions = [];
        if (!HelperService.isVocabularyCheck) {
            this.exerciseService.excludeOOV = false;
        }
41
42
43
44
        HelperService.waitForConfig().then(() => {
            this.corpusService.checkAnnisResponse().then(() => {
                this.processAnnisResponse(this.corpusService.annisResponse);
                this.initH5P();
45
            }, () => {
46
            });
47
        });
48
49
    }

50
    initH5P() {
51
        if (this.corpusService.exercise.type !== ExerciseType.kwic) {
52
            const solutionIndicesString: string = this.exerciseService.excludeOOV ? '&solution_indices=' +
53
                JSON.stringify(this.currentSolutions.map(x => this.corpusService.annisResponse.solutions.indexOf(x))) : '';
54
55
            // this will be called via GET request from the h5p standalone javascript library
            const url: string = HelperService.config['backendBaseUrl'] + HelperService.config['backendApiH5pPath'] + '?' + 'eid='
56
                + this.corpusService.annisResponse.exercise_id + '&lang=' + this.translateService.getBrowserLang() + solutionIndicesString;
57
            window.localStorage.setItem(HelperService.config['localStorageKeyH5P'], url);
58
59
            const exerciseTypePath: string = this.corpusService.exercise.type === ExerciseType.markWords ? 'mark_words' : 'drag_text';
            this.exerciseService.initH5P(exerciseTypePath);
60
        }
61
        this.updateFileUrl();
62
63
    }

64
65
66
67
    ngOnDestroy(): void {
        H5P.externalDispatcher.off('xAPI');
    }

68
    processAnnisResponse(ar: AnnisResponse) {
69
        this.corpusService.annisResponse.solutions = ar.solutions;
70
        this.processSolutions(ar.solutions);
71
        this.corpusService.annisResponse.uri = ar.uri;
72
73
74
        const isUrn: boolean = this.corpusService.currentUrn.startsWith('urn:');
        this.corpusService.annisResponse.nodes = isUrn ? this.corpusService.annisResponse.nodes : ar.nodes;
        this.corpusService.annisResponse.links = isUrn ? this.corpusService.annisResponse.links : ar.links;
75
76
    }

77
    processSolutions(solutions: Solution[]) {
78
        const isCloze: boolean = this.corpusService.exercise.type === ExerciseType.cloze;
79
        if (this.exerciseService.excludeOOV) {
80
            const nodeIdSet: Set<string> = new Set(this.corpusService.annisResponse.nodes.filter(
81
                x => !x.is_oov).map(x => x.id));
82
            solutions = this.corpusService.annisResponse.solutions.filter(
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
                x => nodeIdSet.has(x.target.salt_id) && (isCloze || nodeIdSet.has(x.value.salt_id)));
        }
        let newSolutions: Solution[] = [];
        if (isCloze) {
            this.maxGapLength = Math.max.apply(Math, solutions.map(x => x.target.content.length));
            this.solutionNodeIdSet = new Set(solutions.map(x => x.target.salt_id));
            newSolutions = solutions.concat();
        } else {
            newSolutions = solutions.concat().sort((s1, s2) => {
                return s1.target.content < s2.target.content ? -1 : (s1.target.content > s2.target.content ? 1 : 0);
            });
        }
        this.currentSolutions = newSolutions;
    }

    switchOOV() {
        this.currentSolutions = [];
100
        this.processSolutions(this.corpusService.annisResponse.solutions);
101
102
103
        this.initH5P();
    }

104
    updateFileUrl() {
105
106
107
        const fileId: string = this.corpusService.annisResponse.exercise_id;
        const fileTypeBase = '&type=';
        this.urlBase = HelperService.config['backendBaseUrl'] + HelperService.config['backendApiFilePath'] + '?id=' + fileId + fileTypeBase;
108
        this.solutionIndicesString = this.exerciseService.excludeOOV ? '&solution_indices=' +
109
            JSON.stringify(this.currentSolutions.map(x => this.corpusService.annisResponse.solutions.indexOf(x))) : '';
110
111
    }
}