jsteuer
2019-12-16 b95c6a3de43121c061457039a4c926d5d34f2737
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/// <reference path="../mathcoach-api.d.ts"/>
 
/**
 * Hilfsfunktionen für Werkzeug-Entwickler. Diese werden nicht durch die `ide-lib.js` 
 * ausgeliefert!
 */
export namespace Helpers {
    /**
     * Bildet die MathCoach API nach, sodass diese auch Offline (ohne IDE) 
     * verfügbar ist.
     * 
     * Das Dateisystem wird durch den LocalStorage des Browsers implementiert.
     * Einige Features der IDE (beispielsweise der Navigator) sind nicht verfügbar 
     * und führen keine Aktionen durch. Alle Aktionen werden in der Browser-Console
     * geloggt.
     * 
     * **Hinweis**: Wenn die echte MathCoach-API der IDE verfügbar ist, hat der Aufruf 
     * dieser Funktion keinen Seiteneffekt.
     * 
     * Anwendungsbeispiel 
     * 
     *      import { Helpers } from "@mathcoach/ide-api";
     *      Helpers.enableOfflineUsageIfNecessary();
     * 
     * 
     * @param contextFileExtension Datei-Erweiterung der Kontext-Datei (Das Werkzeug soll 
     * jedoch unabhängig von der Endung arbeiten können)
     * @returns Gibt `true` zurück, wenn die Offline-Funktionalität aktiviert 
     * wurde, andernfalls `false`
     */
    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;
        }
    }
 
 
 
 
    /**
     * Bildet die Kontext-Datei auf eine Aufgaben-Datei mit selben Namen ab.
     * 
     * Ein Werkzeug speichert sein Datenmodell in der sogenannten Kontext-Datei. Soll 
     * eine MathCoach-Aufgabe generiert werden, ist es sinnvoll, wenn die Aufgaben-Datei
     * den selben Namen wie die Kontext-Datei trägt (natürlich mit anderer Dateiendung).
     * 
     * Beispielsweise wird eine Dateireferenz mit dem Pfad `"/path/to/myExercise.mcq.json"` 
     * auf eine Dateireferenz mit dem Pfad `"/path/to/myExercise.groovy"` abgebildet.
     * 
     * Anwendungsbeispiel
     * 
     *      import { Helpers } from "@mathcoach/ide-api";
     *      const contextFile: MathCoach.File = await MC.ide.getContextFile();
     *      const exerciseFile: MathCoach.File = Helpers.contextFileToExerciseFile(contextFile);
     */
    export function contextFileToExerciseFile(contextFile: MathCoach.File): MathCoach.File {
        if (contextFile) {
            if (!contextFile.owner || !(typeof contextFile.owner === "string") || !(contextFile.owner.trim() === "")) {
                throw new Error("Context file has no valid 'owner' property.");
            }
            if (!contextFile.part || !(typeof contextFile.part === "string")) {
                if (contextFile.part !== "vfs" && contextFile.part !== "www") {
                    throw new Error("Context file has no valid 'part' property. Allowed values are 'vfs' and 'www'.");
                }
            }
            if (!contextFile.path || !(typeof contextFile.path === "string") || !(contextFile.owner.trim().startsWith("/"))) {
                throw new Error("Context file has no valid 'path' property.");
            }
        } else {
            throw new Error("No context file object given.")
        }
        let file: MathCoach.File = {
            part: contextFile.part,
            owner: contextFile.owner,
            path: contextFile.path.split(".")[0] + ".groovy"
        };
        return file;
    }
 
 
 
}