jsteuer
2019-09-10 3da0810906dfe16ed874e7e4214b3ad990053be2
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
/// <reference path="../mathcoach-api.d.ts"/>
 
/**
 * Offline MathCoach-API falls notwendig bereitstellen. Dabei wird das Dateisystem durch den
 * LocalStorage des Browsers implementiert - einige Features der IDE (beispielsweise 
 * der Navigator) sind nicht verfügbar und führen keine Aktionen durch. 
 * 
 * **Hinweis**: Wenn die echte MathCoach-API der IDE verfügbar ist, hat der Aufruf 
 * dieser Funktion keinen Seiteneffekt.
 * 
 * @param contextFileExtension Datei-Erweiterung der Kontext-Datei
 * 
 */
export function enableOfflineUsageIfNecessary(contextFileExtension: string = "dummy.json"): boolean {
 
    const fileIdentifier = (file: MathCoach.File) => `mock-file://${file.owner}@${file.part}/${file.path}`;
    const traceMethod = (method: string, args?: any[]) => console.log(["[MC MOCK API]", " ", method, "(", (args ? args : [""]).join(","), ")"].join(""));
 
    class MockAPI implements MathCoach.Api {
 
        public ide: MathCoach.IdeApi = {
            async getContextFile(): Promise<MathCoach.File> {
                traceMethod("MC.ide.getContextFile");
                return {
                    owner: "jdoe",
                    path: `/file.${contextFileExtension}`,
                    part: "vfs"
                }
            },
            async getUserName(): Promise<string> {
                traceMethod("MC.ide.getUserName");
                return "jdoe"
            },
            fs: {
                async readFile(file: MathCoach.File) {
                    traceMethod("MC.ide.fs.readFile", [JSON.stringify(file)]);
                    return localStorage.getItem(fileIdentifier(file)) || "";
                },
                async writeFile(file: MathCoach.File, text: string) {
                    traceMethod("MC.ide.fs.writeFile", [JSON.stringify(file), JSON.stringify(`...${text.length} chars...`)]);
                    return localStorage.setItem(fileIdentifier(file), text);
                }
            },
            navigator: {
                async navigateTo(link: string, forceOpen?: boolean) {
                    traceMethod("MC.ide.navigator.navigateTo", [JSON.stringify(link), JSON.stringify(forceOpen ? true : false)]);
                },
                async navigateToExercise(file: MathCoach.File, forceOpen?: boolean) {
                    traceMethod("MC.ide.navigator.navigateToExercise", [JSON.stringify(file), JSON.stringify(forceOpen ? true : false)]);
                }
            }
        };
        public async isReady(): Promise<boolean> {
            traceMethod("MC.isReady()");
            return true;
        }
    }
    if (typeof MC === "undefined") {
        console.warn("you are offline - offline api is used");
        (window as any).MC = new MockAPI(); // fake the MathCoach-API
        return true;
    } else {
        return false;
    }
}