-
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathdata-store.ts
More file actions
80 lines (72 loc) · 2.62 KB
/
data-store.ts
File metadata and controls
80 lines (72 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ActivityStore } from './activity-store';
import { Progress } from './types';
import { MetaStore, MetaStrings } from './meta-store';
import { ProgressStore } from './progress-store';
import { EvidenceData, EvidenceStore } from './evidence-store';
export class DataStore {
public meta: MetaStore | null = null;
public activityStore: ActivityStore | null = null;
public progressStore: ProgressStore | null = null;
public evidenceStore: EvidenceStore | null = null;
constructor() {
this.meta = new MetaStore();
this.activityStore = new ActivityStore();
this.progressStore = new ProgressStore();
this.evidenceStore = new EvidenceStore();
}
public addActivities(activities: ActivityStore): void {
this.activityStore = activities;
}
public addProgressData(progress: Progress): void {
this.progressStore?.addProgressData(progress);
}
public addEvidenceData(evidence: EvidenceData): void {
this.evidenceStore?.addEvidenceData(evidence);
}
public getMetaStrings(): MetaStrings {
if (this.meta == null) {
throw Error('Meta yaml has not yet been loaded successfully');
}
let lang: string = this.meta.lang || 'en';
if (!this.meta.strings?.hasOwnProperty(lang)) {
// Requested lang does not exist. Fall back to first available lang
let availableLangs: string[] = Object.keys(this.meta?.strings || {});
if (availableLangs.length > 0) {
lang = availableLangs[0];
this.meta.lang = lang;
}
}
return this.meta?.strings?.[lang];
}
public getMetaString(name: keyof MetaStrings, index: number = 0): string {
let meta: MetaStrings = this.getMetaStrings();
if (meta === undefined) {
throw Error('Meta strings not loaded');
}
if (!meta.hasOwnProperty(name)) {
throw Error(`Meta string '${name}' not found in meta.yaml`);
}
if (Array.isArray(meta[name])) {
if (index < 0 || index >= meta[name].length) {
return index.toString();
}
return meta[name][index];
} else if (typeof meta[name] === 'string') {
return meta[name] as string;
}
throw Error(`Meta string '${name}' is not a string or array in meta.yaml`);
}
public getMaxLevel(): number {
return this.activityStore?.getMaxLevel() || 0;
}
public getLevelTitles(maxLevel: number | null = null): string[] {
if (maxLevel == null) maxLevel = this.getMaxLevel();
let titles: string[] = this.getMetaStrings()?.maturityLevels?.slice(0, maxLevel) || [];
if (titles.length < maxLevel) {
for (let i = titles.length + 1; i <= maxLevel; i++) {
titles.push(`Level ${i}`);
}
}
return titles;
}
}