From c48c05f8ee10458b85b45884b99ff8bcec2b2098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr> Date: Thu, 28 Jul 2022 16:56:02 +0200 Subject: [PATCH] feat: load local session file by its URL refs #476 --- src/app/app.module.ts | 3 ++ .../load-session-url.component.ts | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/app/components/load-session-url/load-session-url.component.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index a76bf4558..04b113670 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -118,6 +118,7 @@ import { JalhydModelValidationStepDirective } from "./directives/jalhyd-model-validation.directive"; import { ImmediateErrorStateMatcher } from "./formulaire/immediate-error-state-matcher"; +import { LoadSessionURLComponent } from "./components/load-session-url/load-session-url.component"; const appRoutes: Routes = [ { path: "list/search", component: CalculatorListComponent }, @@ -126,6 +127,7 @@ const appRoutes: Routes = [ { path: "setup", component: ApplicationSetupComponent }, { path: "diagram", component: ModulesDiagramComponent }, { path: "properties", component: SessionPropertiesComponent }, + { path: "loadsession/:path", component: LoadSessionURLComponent }, { path: "**", redirectTo: "list", pathMatch: "full" } ]; @@ -217,6 +219,7 @@ const appRoutes: Routes = [ JalhydModelValidationStepDirective, JetResultsComponent, JetTrajectoryChartComponent, + LoadSessionURLComponent, LogComponent, LogDrawerComponent, LogEntryComponent, diff --git a/src/app/components/load-session-url/load-session-url.component.ts b/src/app/components/load-session-url/load-session-url.component.ts new file mode 100644 index 000000000..99c3bae84 --- /dev/null +++ b/src/app/components/load-session-url/load-session-url.component.ts @@ -0,0 +1,52 @@ +import { Component, forwardRef, Inject } from "@angular/core"; +import { ActivatedRoute } from "@angular/router"; +import { AppComponent } from "app/app.component"; +import { HttpService } from "app/services/http.service"; + +// load a session file by its URL (either local or remote) + +@Component({ + selector: "load-session-url", + template: "" +}) +export class LoadSessionURLComponent { + private path: string; + + constructor( + @Inject(forwardRef(() => AppComponent)) private appComponent: AppComponent, + private route: ActivatedRoute, + private httpService: HttpService + ) { + } + + ngOnInit() { + // get "path" argument from URL + const path = this.route.snapshot.params.path; + + if (path.startsWith("http")) { + // general URL path + } + else { + // local path + + // input URL example : http://localhost:4200/#/loadsession/app%2Fexamples%2Fpab-complete-chain.json + // extracted path : app/examples/pab-complete-chain.json + + this.loadLocalSession(path); + } + } + + /** + * load a locally stored session file + * @param path local path in the form eg. app/examples/pab-complete-chain.json + */ + private async loadLocalSession(path: string) { + try { + const d = await this.httpService.httpGetBlobRequestPromise(path); + const f: any = new Blob([d], { type: "application/json" }); + this.appComponent.loadSessionFile(f); + } catch (e) { + alert("ERROR: session " + path + " does not exist"); + } + } +} -- GitLab