Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show/change experiment default template in UI #51

Merged
merged 14 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import { ExperimentWorkspaceDetailComponent } from './components/experiment-work
import { PluginFilterNodeComponent } from './components-small/plugin-filter-node/plugin-filter-node.component';
import { PluginFilterEditorComponent } from './components-small/plugin-filter-editor/plugin-filter-editor.component';
import { TabGroupListComponent } from './components/tab-group-list/tab-group-list.component';
import { ChooseTemplateComponent } from './dialogs/choose-template/choose-template.component';

@NgModule({
declarations: [
Expand Down Expand Up @@ -131,6 +132,7 @@ import { TabGroupListComponent } from './components/tab-group-list/tab-group-lis
PluginFilterNodeComponent,
PluginFilterEditorComponent,
TabGroupListComponent,
ChooseTemplateComponent,
],
imports: [
BrowserModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class ExperimentWorkspaceDetailComponent implements OnInit {
}

private async updateTemplateId(templateId: string | null) {
if (templateId == null) {
if (templateId == null || templateId === "") {
return; // no template selected
}
if (templateId === this.templateId) {
Expand Down
17 changes: 15 additions & 2 deletions src/app/components/experiment/experiment.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,25 @@ <h1 class="t-page-headline">Experiment</h1>
<mat-icon>cancel</mat-icon>
</button>
</mat-card-title>
<mat-card-content>
<mat-card-content class="experiment-settings">
<div class="default-template-selection t-subheading">
<span>
Default Template:
</span>
<a [routerLink]="['/experiments', experimentId, 'workspace']" [queryParams]="{ template: uiTemplate?.self?.resourceKey?.uiTemplateId ?? 'all-plugins' }">
{{uiTemplate?.name ?? 'All Plugins'}}
</a>
<button mat-icon-button (click)="showSelectDefaultTemplateDialog()">
<mat-icon>edit</mat-icon>
</button>
<button mat-icon-button *ngIf="uiTemplate != null" (click)="updateExperimentDefaultTemplate(null)">
<mat-icon>cancel</mat-icon>
infacc marked this conversation as resolved.
Show resolved Hide resolved
</button>
</div>
<qhana-markdown class="description" [markdown]="experimentDescription"
(markdownChanges)="updateDescription($event)" [editable]="true">
</qhana-markdown>
</mat-card-content>

</mat-card>
<div class="updates">
<div class="update-status" *ngIf="updateStatus === 'changed'">
Expand Down
12 changes: 12 additions & 0 deletions src/app/components/experiment/experiment.component.sass
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@
display: inline-flex
align-items: center
gap: 0.5em

.experiment-settings
display: flex
flex-direction: column
gap: 1rem

.default-template-selection
display: flex
flex-direction: row
justify-content: flex-start
align-items: center
gap: 1rem
39 changes: 37 additions & 2 deletions src/app/components/experiment/experiment.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { ActivatedRoute, Router } from '@angular/router';
import { BehaviorSubject, Subscription } from 'rxjs';
import { debounceTime, filter, map } from 'rxjs/operators';
import { ExportExperimentDialog } from 'src/app/dialogs/export-experiment/export-experiment.component';
import { ChooseTemplateComponent } from 'src/app/dialogs/choose-template/choose-template.component';
import { CurrentExperimentService } from 'src/app/services/current-experiment.service';
import { ExperimentApiObject, QhanaBackendService } from 'src/app/services/qhana-backend.service';
import { TemplateApiObject, TemplatesService } from 'src/app/services/templates.service';

@Component({
selector: 'qhana-experiment',
templateUrl: './experiment.component.html',
styleUrls: ['./experiment.component.sass']
})
export class ExperimentComponent implements OnInit, OnDestroy {

private routeSubscription: Subscription | null = null;

private experimentSubscription: Subscription | null = null;
Expand All @@ -24,6 +25,7 @@ export class ExperimentComponent implements OnInit, OnDestroy {
private descriptionUpdates: BehaviorSubject<string | null> = new BehaviorSubject<string | null>(null);
private autoSaveTitleSubscription: Subscription | null = null;
private autoSaveDescriptionSubscription: Subscription | null = null;
private uiTemplateIdSubscription: Subscription | null = null;

updateStatus: "original" | "changed" | "saved" = "original";

Expand All @@ -34,7 +36,9 @@ export class ExperimentComponent implements OnInit, OnDestroy {
experimentDescription: string = ""; // only updated on initial experiment load
currentExperimentDescription: string = "";

constructor(private route: ActivatedRoute, private router: Router, private experimentService: CurrentExperimentService, private backend: QhanaBackendService, public dialog: MatDialog) { }
uiTemplate: TemplateApiObject | null = null;

constructor(private route: ActivatedRoute, private router: Router, private experimentService: CurrentExperimentService, private backend: QhanaBackendService, private templates: TemplatesService, public dialog: MatDialog) { }

ngOnInit(): void {
this.routeSubscription = this.route.params.pipe(map(params => params.experimentId)).subscribe(experimentId => {
Expand All @@ -49,6 +53,15 @@ export class ExperimentComponent implements OnInit, OnDestroy {
this.experimentDescription = experiment?.description ?? "";
this.currentExperimentDescription = experiment?.description ?? "";
});
this.uiTemplateIdSubscription = this.experimentService.experimentTemplateId.subscribe(templateId => {
if (templateId == null) {
this.uiTemplate = null;
return;
}
this.templates.getTemplate(templateId.toString()).then(templateResponse => {
this.uiTemplate = templateResponse?.data ?? null;
});
});
this.autoSaveTitleSubscription = this.titleUpdates.pipe(
filter(value => value != null && value !== this.lastSavedTitle),
debounceTime(500)
Expand All @@ -64,6 +77,7 @@ export class ExperimentComponent implements OnInit, OnDestroy {
this.experimentSubscription?.unsubscribe();
this.autoSaveTitleSubscription?.unsubscribe();
this.autoSaveDescriptionSubscription?.unsubscribe();
this.uiTemplateIdSubscription?.unsubscribe();
}

cloneExperiment() {
Expand Down Expand Up @@ -156,4 +170,25 @@ export class ExperimentComponent implements OnInit, OnDestroy {
});
}

showSelectDefaultTemplateDialog() {
const dialogRef = this.dialog.open(ChooseTemplateComponent, {
minWidth: "20rem", maxWidth: "40rem", width: "60%", maxHeight: "95%",
data: this.uiTemplate
});
dialogRef.afterClosed().subscribe(templateId => {
if (templateId != null) {
const id = templateId === 'all-plugins' ? null : templateId;
infacc marked this conversation as resolved.
Show resolved Hide resolved
this.updateExperimentDefaultTemplate(id);
}
});
}

async updateExperimentDefaultTemplate(templateId: string | null) {
if (this.experimentId == null) {
console.warn("Experiment ID is null!");
infacc marked this conversation as resolved.
Show resolved Hide resolved
return;
}
await this.templates.setExperimentDefaultTemplate(this.experimentId, templateId);
this.experimentService.reloadExperiment();
}
}
18 changes: 18 additions & 0 deletions src/app/dialogs/choose-template/choose-template.component.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also rename the html template and sass files to *.dialog.*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other dialogs also do not follow this convention. Should I rename them as well?

src/app/dialogs/
├── change-ui-template
│   ├── change-ui-template.component.html
│   ├── change-ui-template.component.sass
│   ├── change-ui-template.component.spec.ts
│   └── change-ui-template.component.ts
├── choose-data
│   ├── choose-data.component.html
│   ├── choose-data.component.sass
│   ├── choose-data.component.spec.ts
│   └── choose-data.component.ts
├── choose-plugin
│   ├── choose-plugin.component.html
│   ├── choose-plugin.component.sass
│   ├── choose-plugin.component.spec.ts
│   └── choose-plugin.component.ts
├── choose-template
│   ├── choose-template.dialog.html
│   ├── choose-template.dialog.sass
│   ├── choose-template.dialog.spec.ts
│   └── choose-template.dialog.ts
├── create-experiment
│   ├── create-experiment.component.html
│   ├── create-experiment.component.sass
│   ├── create-experiment.component.spec.ts
│   └── create-experiment.component.ts
├── delete-dialog
│   ├── delete-dialog.dialog.html
│   ├── delete-dialog.dialog.sass
│   └── delete-dialog.dialog.ts
├── export-experiment
│   ├── export-experiment.component.html
│   ├── export-experiment.component.sass
│   ├── export-experiment.component.spec.ts
│   └── export-experiment.component.ts
└── markdown-help
    ├── markdown-help.component.html
    ├── markdown-help.component.sass
    ├── markdown-help.component.spec.ts
    └── markdown-help.component.ts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this PR only rename the new dialog. If you want to rename the others (which should be done eventually) please open another PR.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1 mat-dialog-title>Select a default template:</h1>
<div mat-dialog-content>
<div class="template-list">
<qhana-growing-list [rels]="['ui-template', 'collection']" [newItemRels]="['ui-template']"
[highlighted]="highlightedTemplateSet" [highlightByKey]="'uiTemplateId'" (clickItem)="selectTemplate($event)">
</qhana-growing-list>
</div>
<span class="t-title">Selected Template: {{template?.name ?? 'All Plugins'}}</span><br>
<span><i>{{template?.description ?? 'This template contains all plugins that are available in Qhana.'}}</i></span>
</div>
<div class="dialog-actions" mat-dialog-actions>
<button mat-button [mat-dialog-close]="'all-plugins'">Unset</button>
<button mat-button (click)="onCancel()">Cancel</button>
<button mat-button [mat-dialog-close]="templateId"
[disabled]="templateId == null">
Choose
</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.template-list
max-height: 33rem
overflow-y: auto
23 changes: 23 additions & 0 deletions src/app/dialogs/choose-template/choose-template.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ChooseTemplateComponent } from './choose-template.component';

describe('ChooseTemplateDialog', () => {
let component: ChooseTemplateComponent;
let fixture: ComponentFixture<ChooseTemplateComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ChooseTemplateComponent ]
})
.compileComponents();

fixture = TestBed.createComponent(ChooseTemplateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
51 changes: 51 additions & 0 deletions src/app/dialogs/choose-template/choose-template.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { ApiLink } from 'src/app/services/api-data-types';
import { PluginRegistryBaseService } from 'src/app/services/registry.service';
import { TemplateApiObject } from 'src/app/services/templates.service';

@Component({
selector: 'qhana-choose-template',
templateUrl: './choose-template.component.html',
styleUrls: ['./choose-template.component.sass']
})
export class ChooseTemplateComponent implements OnInit {
infacc marked this conversation as resolved.
Show resolved Hide resolved
highlightedTemplateSet: Set<string> = new Set();
templateId: string | null = null;
template: TemplateApiObject | null = null;

constructor(public dialogRef: MatDialogRef<ChooseTemplateComponent>, @Inject(MAT_DIALOG_DATA) public data: TemplateApiObject, private registry: PluginRegistryBaseService) {
if (data != null && data.self.resourceKey?.uiTemplateId != null) {
const templateId = data.self.resourceKey?.uiTemplateId;
this.templateId = templateId;
this.highlightedTemplateSet = new Set([templateId]);
this.template = data;
}
}

ngOnInit(): void {
}

async selectTemplate(templateLink: ApiLink) {
const templateId = templateLink.resourceKey?.uiTemplateId ?? null;
if (templateId == null) {
return;
}
if (this.highlightedTemplateSet.has(templateId)) {
// double click deselects template
this.highlightedTemplateSet.clear();
this.templateId = null;
this.template = null;
return;
}
this.highlightedTemplateSet.clear();
this.highlightedTemplateSet = new Set([templateId]);
const template = await this.registry.getByApiLink<TemplateApiObject>(templateLink, null, false);
this.template = template?.data ?? null;
this.templateId = templateId;
}

onCancel(): void {
this.dialogRef.close();
}
}
7 changes: 5 additions & 2 deletions src/app/services/current-experiment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class CurrentExperimentService {
return this.currentExperiment.asObservable().pipe(map(experiment => experiment?.description ?? null));
}

get experimentTemplateId() {
return this.currentExperiment.asObservable().pipe(map(experiment => experiment?.templateId ?? null));
}

constructor(private backend: QhanaBackendService) { }

private updateCurrentExperiment(experimentId: string | null) {
Expand All @@ -64,7 +68,7 @@ export class CurrentExperimentService {
this.currentExperimentId.next(experimentId);
}
const current = this.currentExperiment.getValue();
if (current != experiment || current?.name !== experiment?.name || current?.description !== experiment?.description) {
if (current != experiment || current?.name !== experiment?.name || current?.description !== experiment?.description || current?.templateId !== experiment?.templateId) {
this.currentExperiment.next(experiment);
}
}
Expand All @@ -81,5 +85,4 @@ export class CurrentExperimentService {
this.updateCurrentExperiment(experimentId);
}
}

}
21 changes: 21 additions & 0 deletions src/app/services/qhana-backend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ export interface TimelineStepPageOptions {
unclearedSubstep?: number;
}

export interface TemplateApiObject extends ApiObject {
templateId: string;
}

export interface TemplatePostResponseApiObject extends ApiObject {
experimentId: number;
templateId?: string;
}

function urlIsString(url: string | null): url is string {
return url != null;
}
Expand Down Expand Up @@ -491,4 +500,16 @@ export class QhanaBackendService {
rootUrl => this.http.get<TimelineSubStepApiObject>(`${rootUrl}/experiments/${experimentId}/timeline/${step}/substeps/${substep}`)
);
}

public getExperimentDefaultTemplate(experimentId: number | string): Observable<TemplateApiObject> {
return this.callWithRootUrl<TemplateApiObject>(
rootUrl => this.http.get<TemplateApiObject>(`${rootUrl}/experiments/${experimentId}/template`)
);
}

public updateExperimentDefaultTemplate(experimentId: number | string, templateId: string | number | null): Observable<TemplatePostResponseApiObject> {
return this.callWithRootUrl<TemplatePostResponseApiObject>(
rootUrl => this.http.post<TemplatePostResponseApiObject>(`${rootUrl}/experiments/${experimentId}/template`, { templateId: templateId })
);
}
}
8 changes: 7 additions & 1 deletion src/app/services/registry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,13 @@ export class PluginRegistryBaseService {

public async getByApiLink<T>(link: ApiLink, searchParams: URLSearchParams | null = null, ignoreCache: boolean | "ignore-embedded" = false): Promise<ApiResponse<T> | null> {
const url = new URL(link.href)
searchParams?.forEach((value, key) => url.searchParams.append(key, value));
searchParams?.forEach((value, key) => {
if (url.searchParams.has(key)) {
url.searchParams.set(key, value);
} else {
url.searchParams.append(key, value);
infacc marked this conversation as resolved.
Show resolved Hide resolved
}
});
infacc marked this conversation as resolved.
Show resolved Hide resolved
return await this._fetch<ApiResponse<T>>(url.toString(), ignoreCache);
}

Expand Down
15 changes: 12 additions & 3 deletions src/app/services/templates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import { ApiLink, ApiObject, PageApiObject } from './api-data-types';
import { CurrentExperimentService } from './current-experiment.service';
import { PluginRegistryBaseService } from './registry.service';
import { EnvService } from './env.service';
import { distinctUntilChanged } from 'rxjs/operators';
import { distinctUntilChanged, take } from 'rxjs/operators';
import { ActivatedRoute } from '@angular/router';
import { QhanaBackendService } from './qhana-backend.service';


export interface TemplateApiObject extends ApiObject { // TODO check fields
Expand Down Expand Up @@ -104,7 +105,7 @@ export class TemplatesService {
return this.currentTemplateSubject.asObservable();
}

constructor(private registry: PluginRegistryBaseService, private env: EnvService, private currentExperiment: CurrentExperimentService, private route: ActivatedRoute) {
constructor(private registry: PluginRegistryBaseService, private env: EnvService, private currentExperiment: CurrentExperimentService, private backend: QhanaBackendService, private route: ActivatedRoute) {
this.envSubscription = env.uiTemplateId.subscribe((defaultTemplateId) => {
this.envTemplateIdSubject.next(defaultTemplateId);
});
Expand Down Expand Up @@ -200,7 +201,7 @@ export class TemplatesService {
}

private async updateTemplate(templateId: string | null, subject: BehaviorSubject<TemplateApiObject | null>) {
if (templateId == null) {
if (templateId == null || templateId === "") {
subject.next(null);
return;
}
Expand Down Expand Up @@ -231,4 +232,12 @@ export class TemplatesService {
const templateResponse = await this.getTemplate(templateId, ignoreCache);
return templateResponse?.data?.groups ?? [];
}

async setExperimentDefaultTemplate(experimentId: string, templateId: string | null) {
this.backend.updateExperimentDefaultTemplate(experimentId, templateId).pipe(take(1)).subscribe(
response => {
this.experimentTemplateIdSubject.next(response?.templateId ?? null);
}
);
}
}
Loading