/// <reference path="./mathcoach-api.d.ts"/>
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
return new (P || (P = Promise))(function (resolve, reject) {
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
});
|
};
|
/**
|
* 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 = "dummy.json") {
|
const fileIdentifier = (file) => `mock-file://${file.owner}@${file.part}/${file.path}`;
|
const traceMethod = (method, args) => console.log(["[MC MOCK API]", " ", method, "(", (args ? args : [""]).join(","), ")"].join(""));
|
class MockAPI {
|
constructor() {
|
this.ide = {
|
getContextFile() {
|
return __awaiter(this, void 0, void 0, function* () {
|
traceMethod("MC.ide.getContextFile");
|
return {
|
owner: "jdoe",
|
path: `/file.${contextFileExtension}`,
|
part: "vfs"
|
};
|
});
|
},
|
getUserName() {
|
return __awaiter(this, void 0, void 0, function* () {
|
traceMethod("MC.ide.getUserName");
|
return "jdoe";
|
});
|
},
|
fs: {
|
readFile(file) {
|
return __awaiter(this, void 0, void 0, function* () {
|
traceMethod("MC.ide.fs.readFile", [JSON.stringify(file)]);
|
return localStorage.getItem(fileIdentifier(file)) || "";
|
});
|
},
|
writeFile(file, text) {
|
return __awaiter(this, void 0, void 0, function* () {
|
traceMethod("MC.ide.fs.writeFile", [JSON.stringify(file), JSON.stringify(`...${text.length} chars...`)]);
|
return localStorage.setItem(fileIdentifier(file), text);
|
});
|
}
|
},
|
navigator: {
|
navigateTo(link, forceOpen) {
|
return __awaiter(this, void 0, void 0, function* () {
|
traceMethod("MC.ide.navigator.navigateTo", [JSON.stringify(link), JSON.stringify(forceOpen ? true : false)]);
|
});
|
},
|
navigateToExercise(file, forceOpen) {
|
return __awaiter(this, void 0, void 0, function* () {
|
traceMethod("MC.ide.navigator.navigateToExercise", [JSON.stringify(file), JSON.stringify(forceOpen ? true : false)]);
|
});
|
}
|
}
|
};
|
}
|
isReady() {
|
return __awaiter(this, void 0, void 0, function* () {
|
traceMethod("MC.isReady()");
|
return true;
|
});
|
}
|
}
|
if (typeof MC === "undefined") {
|
console.warn("you are offline - offline api is used");
|
window.MC = new MockAPI(); // fake the MathCoach-API
|
return true;
|
}
|
else {
|
return false;
|
}
|
}
|
/**
|
* Kontext-Datei auf Aufgaben-Datei abbilden.
|
* Beispiel: "/path/to/myExercise.mcq.json" zu "/path/to/myExercise.groovy"
|
*/
|
export function contextFileToExerciseFile(contextFile) {
|
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 = {
|
part: contextFile.part,
|
owner: contextFile.owner,
|
path: contextFile.path.split(".")[0] + ".groovy"
|
};
|
return file;
|
}
|
//# sourceMappingURL=index.js.map
|