From 9e7b0bb0b1b3fd781c5702fa3585c6cfd1156d74 Mon Sep 17 00:00:00 2001 From: jsteuer <jan.steuer.htw@gmail.com> Date: Mon, 12 Aug 2019 13:20:12 +0200 Subject: [PATCH] ... --- src/index.ts | 118 ++++++++++++++++++++++++++++------------------------------- 1 files changed, 56 insertions(+), 62 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0513988..3d8644f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,72 +1,66 @@ /// <reference path="../mathcoach-api.d.ts"/> -namespace MathCoach { +/** + * 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 { - export namespace Ide { - /** - * 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("")); - 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 { - 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; + 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)]); } } - 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; - } + }; + 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; + } } + -- Gitblit v1.10.0-SNAPSHOT