definma-ui/src/app/templates/templates.component.ts

140 lines
4.6 KiB
TypeScript
Raw Normal View History

2020-07-27 17:52:03 +02:00
import { Component, OnInit } from '@angular/core';
import {ApiService} from '../services/api.service';
import {TemplateModel} from '../models/template.model';
import {animate, style, transition, trigger} from '@angular/animations';
import {ValidationService} from '../services/validation.service';
2020-07-30 14:23:51 +02:00
import cloneDeep from 'lodash/cloneDeep';
import omit from 'lodash/omit';
import {DataService} from '../services/data.service';
2020-07-27 17:52:03 +02:00
@Component({
selector: 'app-templates',
templateUrl: './templates.component.html',
styleUrls: ['./templates.component.scss'],
animations: [
trigger(
'inOut', [
transition(':enter', [
style({height: 0, opacity: 0}),
animate('0.5s ease-out', style({height: '*', opacity: 1}))
]),
transition(':leave', [
style({height: '*', opacity: 1}),
animate('0.5s ease-in', style({height: 0, opacity: 0}))
])
]
)
]
})
export class TemplatesComponent implements OnInit {
2020-09-03 15:51:53 +02:00
collection = 'measurement'; // collection to view
templates: TemplateModel[] = []; // all templates of current collection
2020-07-27 17:52:03 +02:00
templateGroups: {[first_id: string]: TemplateModel[]} = {}; // templates grouped by first_id
templateEdit: {[first_id: string]: TemplateModel} = {}; // latest template of each first_id for editing
2020-09-03 15:51:53 +02:00
groupsView: { // grouped templates
first_id: string,
name: string,
version: number,
expanded: boolean,
edit: boolean,
entries: TemplateModel[]
}[] = [];
2020-07-27 17:52:03 +02:00
constructor(
private api: ApiService,
private validate: ValidationService,
public d: DataService
2020-07-27 17:52:03 +02:00
) { }
ngOnInit(): void {
this.loadTemplates();
}
loadTemplates() {
this.d.load(this.collection + 'Templates', () => {
this.templates = this.d.arr[this.collection + 'Templates'];
2020-07-27 17:52:03 +02:00
this.templateFormat();
});
}
templateFormat() {
this.templateGroups = {};
this.templateEdit = {};
2020-09-03 15:51:53 +02:00
this.templates.forEach(template => { // group templates
2020-07-27 17:52:03 +02:00
if (this.templateGroups[template.first_id]) {
this.templateGroups[template.first_id].push(template);
}
else {
this.templateGroups[template.first_id] = [template];
}
});
Object.keys(this.templateGroups).forEach(id => {
this.templateGroups[id] = this.templateGroups[id].sort((a, b) => a.version - b.version);
2020-07-30 14:23:51 +02:00
this.templateEdit[id] = cloneDeep(this.templateGroups[id][this.templateGroups[id].length - 1]);
this.templateEdit[id].parameters = this.templateEdit[id].parameters
.map(e => {e.rangeString = JSON.stringify(e.range, null, 2); return e; });
2020-07-27 17:52:03 +02:00
});
this.groupsView = Object.values(this.templateGroups)
.map(e => ({
first_id: e[e.length - 1].first_id,
name: e[e.length - 1].name,
version: e[e.length - 1].version,
expanded: false,
edit: false,
entries: e
}));
}
saveTemplate(first_id) {
2020-07-30 14:23:51 +02:00
const template = cloneDeep(this.templateEdit[first_id]);
2020-07-27 17:52:03 +02:00
template.parameters = template.parameters.filter(e => e.name !== '');
let valid = true;
valid = valid && this.validate.string(template.name).ok;
template.parameters.forEach(parameter => {
valid = valid && this.validate.parameterName(parameter.name).ok;
valid = valid && this.validate.parameterRange(parameter.rangeString).ok;
if (valid) {
parameter.range = JSON.parse(parameter.rangeString);
}
});
if (valid) {
2020-07-30 14:23:51 +02:00
const sendData = {name: template.name, parameters: template.parameters.map(e => omit(e, ['rangeString']))};
2020-09-03 15:51:53 +02:00
if (first_id === 'null') { // new template
2020-09-02 14:11:00 +02:00
this.api.post<TemplateModel>(`/template/${this.collection}/new`, sendData, () => {
delete this.d.arr[this.collection + 'Templates'];
this.d.load(this.collection + 'Templates', () => {
this.templates = this.d.arr[this.collection + 'Templates'];
this.templateFormat();
});
2020-07-27 17:52:03 +02:00
});
}
else {
2020-09-02 14:11:00 +02:00
this.api.put<TemplateModel>(`/template/${this.collection}/${template.first_id}`, sendData, () => {
delete this.d.arr[this.collection + 'Templates'];
this.d.load(this.collection + 'Templates', () => {
this.templates = this.d.arr[this.collection + 'Templates'];
this.templateFormat();
});
2020-07-27 17:52:03 +02:00
});
}
}
}
newTemplate() {
if (!this.templateEdit.null) {
const template = new TemplateModel();
template.name = 'new template';
2020-09-03 15:51:53 +02:00
this.groupsView.push({
first_id: 'null',
name: 'new template',
version: 0,
expanded: true,
edit: true,
entries: [template]
});
2020-07-27 17:52:03 +02:00
this.templateEdit.null = new TemplateModel();
}
}
}