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 5 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
27 changes: 15 additions & 12 deletions src/app/components/experiment/experiment.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ <h1 class="t-page-headline">Experiment</h1>
<mat-icon>cancel</mat-icon>
</button>
</mat-card-title>
<mat-card-content>
<mat-form-field>
<mat-label>Default Template</mat-label>
<mat-select (selectionChange)="changeDefaultTemplate($event.value)" [value]="experimentTemplateId">
<mat-option [value]="null">
None
</mat-option>
<mat-option *ngFor="let template of experimentTemplates | keyvalue" [value]="template.key">
{{template.value}}
</mat-option>
</mat-select>
</mat-form-field>
<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>
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
51 changes: 31 additions & 20 deletions src/app/components/experiment/experiment.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +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 { PageApiObject } from 'src/app/services/api-data-types';
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 { PluginRegistryBaseService } from 'src/app/services/registry.service';
import { TemplatesService } from 'src/app/services/templates.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 @@ -27,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 @@ -37,10 +36,9 @@ export class ExperimentComponent implements OnInit, OnDestroy {
experimentDescription: string = ""; // only updated on initial experiment load
currentExperimentDescription: string = "";

experimentTemplates: { [id: string]: string } = {};
experimentTemplateId: string | number | null = null;
uiTemplate: TemplateApiObject | null = null;

constructor(private route: ActivatedRoute, private router: Router, private experimentService: CurrentExperimentService, private backend: QhanaBackendService, private registry: PluginRegistryBaseService, private templates: TemplatesService, public dialog: MatDialog) { }
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 @@ -54,7 +52,15 @@ export class ExperimentComponent implements OnInit, OnDestroy {
this.lastSavedDescription = experiment?.description ?? "";
this.experimentDescription = experiment?.description ?? "";
this.currentExperimentDescription = experiment?.description ?? "";
this.experimentTemplateId = experiment?.templateId ?? null;
});
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),
Expand All @@ -64,22 +70,14 @@ export class ExperimentComponent implements OnInit, OnDestroy {
filter(value => value != null && value !== this.lastSavedDescription),
debounceTime(500)
).subscribe(this.saveDescription);
this.registry.getByRel<PageApiObject>([["ui-template", "collection"]]).then(result => {
result?.data.items.forEach(item => {
const templateId = item.resourceKey?.uiTemplateId;
const name = item.name;
if (templateId != null && name != null) {
this.experimentTemplates[templateId] = name;
}
});
});
}

ngOnDestroy(): void {
this.routeSubscription?.unsubscribe();
this.experimentSubscription?.unsubscribe();
this.autoSaveTitleSubscription?.unsubscribe();
this.autoSaveDescriptionSubscription?.unsubscribe();
this.uiTemplateIdSubscription?.unsubscribe();
}

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

async changeDefaultTemplate(templateId: string | null) {
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;
}
this.experimentTemplateId = templateId;
await this.templates.setDefaultTemplate(this.experimentId, templateId);
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();
}
}
4 changes: 4 additions & 0 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 Down
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
4 changes: 2 additions & 2 deletions src/app/services/templates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ export class TemplatesService {
return templateResponse?.data?.groups ?? [];
}

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