projects/web-mev/src/app/features/workspace-detail/components/metadata/dialogs/add-observation-set-dialog/add-observation-set-dialog.component.ts
Add Observation Dialog Component
Modal dialog component which is used to add a custom observation set
| changeDetection | ChangeDetectionStrategy.OnPush |
| selector | mev-add-observation-set-dialog |
| styleUrls | ./add-observation-set-dialog.component.scss |
| templateUrl | ./add-observation-set-dialog.component.html |
Properties |
Methods |
Accessors |
constructor(formBuilder: FormBuilder, dialogRef: MatDialogRef
|
||||||||||||
|
Parameters :
|
| applyFilter | ||||||
applyFilter(filterValue: string)
|
||||||
|
Filtering observations by name
Parameters :
Returns :
void
|
| confirmAdd |
confirmAdd()
|
|
Function is triggered when user clicks the Add button
Returns :
void
|
| isAllSelected |
isAllSelected()
|
|
Whether the number of selected elements matches the total number of rows.
Returns :
boolean
|
| masterToggle |
masterToggle()
|
|
Selects all rows if they are not all selected; otherwise clear selection.
Returns :
void
|
| ngAfterViewInit |
ngAfterViewInit()
|
|
Returns :
void
|
| ngOnInit |
ngOnInit()
|
|
Returns :
void
|
| onNoClick |
onNoClick()
|
|
Function is triggered when user clicks the Cancel button
Returns :
void
|
| submit |
submit()
|
|
Returns :
void
|
| allObservationSetsDS |
| Public data |
Type : any
|
Decorators :
@Inject(MAT_DIALOG_DATA)
|
| Public dialogRef |
Type : MatDialogRef<AddObservationSetDialogComponent>
|
| observationForm |
Type : FormGroup
|
| observationSetsDisplayedColumns |
| observationSetsDisplayedColumnsAttributesOnly |
| paginator |
Type : MatPaginator
|
Decorators :
@ViewChild(MatPaginator)
|
| selection |
Default value : new SelectionModel(true, [])
|
| submitted |
Default value : false
|
| f |
getf()
|
|
Convenience getter for easy access to form fields |
import {
Component,
OnInit,
ChangeDetectionStrategy,
Inject,
ViewChild
} from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { SelectionModel } from '@angular/cdk/collections';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MatPaginator } from '@angular/material/paginator';
import { CustomSetType } from '@app/_models/metadata';
/**
* Add Observation Dialog Component
*
* Modal dialog component which is used to add a custom observation set
*/
@Component({
selector: 'mev-add-observation-set-dialog',
templateUrl: './add-observation-set-dialog.component.html',
styleUrls: ['./add-observation-set-dialog.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AddObservationSetDialogComponent implements OnInit {
selection = new SelectionModel(true, []);
allObservationSetsDS;
observationForm: FormGroup;
submitted = false;
observationSetsDisplayedColumns;
observationSetsDisplayedColumnsAttributesOnly;
@ViewChild(MatPaginator) paginator: MatPaginator;
constructor(
private formBuilder: FormBuilder,
public dialogRef: MatDialogRef<AddObservationSetDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any
) {}
ngOnInit(): void {
this.observationForm = this.formBuilder.group({
observationSetName: ['', Validators.required],
observationSetColor: ['#000000', Validators.required]
});
this.allObservationSetsDS = this.data.observationSetDS;
this.observationSetsDisplayedColumns = this.data.observationSetsDisplayedColumns;
this.observationSetsDisplayedColumnsAttributesOnly = this.data.observationSetsDisplayedColumnsAttributesOnly;
}
ngAfterViewInit() {
this.allObservationSetsDS.paginator = this.paginator;
}
/**
* Function is triggered when user clicks the Cancel button
*
*/
onNoClick(): void {
this.dialogRef.close();
}
submit() {
//empty stuff
}
/**
* Function is triggered when user clicks the Add button
*
*/
confirmAdd() {
const name = this.observationForm.value.observationSetName;
const color = this.observationForm.value.observationSetColor;
const samples = this.selection.selected;
const observationSet = {
name: name,
color: color,
type: CustomSetType.ObservationSet,
elements: samples,
multiple: true
};
this.dialogRef.close(observationSet);
}
/**
* Whether the number of selected elements matches the total number of rows.
*/
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.allObservationSetsDS.filteredData.length;
return numSelected === numRows;
}
/**
* Selects all rows if they are not all selected; otherwise clear selection.
*/
masterToggle() {
this.isAllSelected()
? this.selection.clear()
: this.allObservationSetsDS.filteredData.forEach(row =>
this.selection.select(row)
);
}
/**
* Filtering observations by name
*/
applyFilter(filterValue: string) {
filterValue = filterValue.trim();
filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
this.allObservationSetsDS.filter = filterValue;
}
/**
* Convenience getter for easy access to form fields
*/
get f() {
return this.observationForm.controls;
}
}
<div class="container">
<p>Enter the name of a new observation set and select samples. Then click the Add button.
</p>
<form class="mat-dialog-content" (ngSubmit)="submit" [formGroup]="observationForm">
<div class="form">
<mat-form-field class="form-control" color="accent">
<mat-label>Enter observation set name</mat-label>
<input matInput formControlName=observationSetName name=observationSetName>
</mat-form-field>
<mat-form-field class="form-control" color="accent">
<mat-label>Select your color for the observation set:</mat-label>
<input type="color" class="color-picker-input" matInput formControlName=observationSetColor name=observationSetColor>
</mat-form-field>
Select samples:
<div class="mat-elevation-z8">
<div class="filter-panel">
<mat-form-field class="form-control">
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
</div>
<table mat-table [dataSource]="allObservationSetsDS">
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()">
</mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null" [checked]="selection.isSelected(row)">
</mat-checkbox>
</td>
</ng-container>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<ng-container *ngFor="let attrCol of observationSetsDisplayedColumnsAttributesOnly; let colIndex = index"
matColumnDef="{{attrCol}}">
<th mat-header-cell *matHeaderCellDef>{{ attrCol }}</th>
<td mat-cell *matCellDef="let element"> {{ element.attributes[attrCol] ? element.attributes[attrCol].value : '' }}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="observationSetsDisplayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: observationSetsDisplayedColumns;"
(click)="selection.toggle(row)">
</tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 25]">
</mat-paginator>
</div>
</div>
<div class="button-panel">
<button mat-button [mat-dialog-close]="1" mat-raised-button color="accent"
[type]="submit" [disabled]="!observationForm.valid" (click)="confirmAdd()">Add</button>
<button mat-button (click)="onNoClick()" tabindex="-1">Cancel</button>
</div>
</form>
</div>
./add-observation-set-dialog.component.scss
.container {
font-size: 14px;
}
.mat-dialog-content {
overflow-y: auto;
}
.form-control {
width: 100%;
}
.filter-panel {
padding: 0 10px;
}
table {
width: 100%;
}
.color-picker-input {
width: 40px;
height: 40px;
margin: 0;
padding: 0;
}
.button-panel {
margin-top: 25px;
}