2020-07-30 14:23:51 +02:00
|
|
|
import cloneDeep from 'lodash/cloneDeep';
|
2020-08-06 08:18:57 +02:00
|
|
|
import merge from 'lodash/merge';
|
|
|
|
import omit from 'lodash/omit';
|
2020-08-21 16:11:57 +02:00
|
|
|
import isEqual from 'lodash/isEqual';
|
2020-07-30 11:33:56 +02:00
|
|
|
import strCompare from 'str-compare';
|
2020-06-19 08:43:22 +02:00
|
|
|
import {
|
|
|
|
AfterContentChecked,
|
|
|
|
Component,
|
2020-07-30 11:33:56 +02:00
|
|
|
OnInit, TemplateRef,
|
2020-06-19 08:43:22 +02:00
|
|
|
ViewChild
|
|
|
|
} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {AutocompleteService} from '../services/autocomplete.service';
|
|
|
|
import {ApiService} from '../services/api.service';
|
|
|
|
import {MaterialModel} from '../models/material.model';
|
|
|
|
import {SampleModel} from '../models/sample.model';
|
|
|
|
import {NgForm, Validators} from '@angular/forms';
|
|
|
|
import {ValidationService} from '../services/validation.service';
|
|
|
|
import {MeasurementModel} from '../models/measurement.model';
|
2020-07-14 09:39:37 +02:00
|
|
|
import { ChartOptions } from 'chart.js';
|
|
|
|
import {animate, style, transition, trigger} from '@angular/animations';
|
2020-07-22 10:45:34 +02:00
|
|
|
import {Observable} from 'rxjs';
|
2020-07-30 11:33:56 +02:00
|
|
|
import {ModalService} from '@inst-iot/bosch-angular-ui-components';
|
2020-08-06 08:18:57 +02:00
|
|
|
import {DataService} from '../services/data.service';
|
2020-08-21 16:11:57 +02:00
|
|
|
import {LoginService} from '../services/login.service';
|
2020-07-30 11:33:56 +02:00
|
|
|
|
2020-08-17 14:51:02 +02:00
|
|
|
// TODO: additional property value not validated on edit
|
2020-06-19 08:43:22 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-sample',
|
|
|
|
templateUrl: './sample.component.html',
|
2020-07-14 09:39:37 +02:00
|
|
|
styleUrls: ['./sample.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}))
|
|
|
|
])
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2020-06-19 08:43:22 +02:00
|
|
|
})
|
|
|
|
export class SampleComponent implements OnInit, AfterContentChecked {
|
|
|
|
|
|
|
|
@ViewChild('sampleForm') sampleForm: NgForm;
|
2020-08-06 08:18:57 +02:00
|
|
|
@ViewChild('cmForm') cmForm: NgForm;
|
|
|
|
|
2020-08-21 16:11:57 +02:00
|
|
|
baseSample = new SampleModel(); // base sample which is saved
|
2020-08-06 08:18:57 +02:00
|
|
|
sampleCount = 1; // number of samples to be generated
|
2020-08-21 16:11:57 +02:00
|
|
|
samples: SampleModel[] = []; // gets filled with response data after saving the sample
|
2020-06-19 08:43:22 +02:00
|
|
|
|
2020-07-22 10:45:34 +02:00
|
|
|
sampleReferences: [string, string, string][] = [['', '', '']];
|
2020-08-06 08:18:57 +02:00
|
|
|
sampleReferenceFinds: {_id: string, number: string}[] = []; // raw sample reference data from db
|
|
|
|
currentSRIndex = 0; // index of last entered sample reference
|
2020-07-22 10:45:34 +02:00
|
|
|
sampleReferenceAutocomplete: string[][] = [[]];
|
2020-08-06 08:18:57 +02:00
|
|
|
|
|
|
|
customFields: [string, string][] = [];
|
|
|
|
availableCustomFields: string[] = [];
|
|
|
|
|
|
|
|
newMaterial = false; // true if new material should be created
|
|
|
|
materials: MaterialModel[] = []; // all materials
|
|
|
|
materialNames = []; // only names for autocomplete
|
|
|
|
material = new MaterialModel(); // object of current selected material
|
|
|
|
defaultDevice = ''; // default device for spectra
|
|
|
|
|
2020-08-21 16:11:57 +02:00
|
|
|
// component mode, either new for generating new samples, editOne or editMulti, editing one or multiple samples
|
|
|
|
mode = 'new';
|
|
|
|
view = { // active views
|
|
|
|
base: false, // base sample
|
|
|
|
baseSum: false, // base sample summary
|
|
|
|
cm: false, // conditions and measurements
|
|
|
|
cmSum: false // conditions and measurements summary
|
|
|
|
};
|
2020-08-06 08:18:57 +02:00
|
|
|
loading = 0; // number of currently loading instances
|
2020-06-19 08:43:22 +02:00
|
|
|
checkFormAfterInit = false;
|
2020-07-30 11:33:56 +02:00
|
|
|
modalText = {list: '', suggestion: ''};
|
2020-08-21 16:11:57 +02:00
|
|
|
cmSampleIndex = '0';
|
|
|
|
measurementDeleteList = []; // buffer with measurements to delete, if the user confirms and saves the cm changes
|
|
|
|
measurementRestoreData: MeasurementModel[] = []; // deleted measurements if user is allowed and measurements are available
|
2020-08-06 08:18:57 +02:00
|
|
|
|
|
|
|
charts = [[]]; // chart data for spectra
|
2020-07-14 09:39:37 +02:00
|
|
|
readonly chartInit = [{
|
|
|
|
data: [],
|
|
|
|
label: 'Spectrum',
|
|
|
|
showLine: true,
|
|
|
|
fill: false,
|
|
|
|
pointRadius: 0,
|
|
|
|
borderColor: '#00a8b0',
|
|
|
|
borderWidth: 2
|
|
|
|
}];
|
|
|
|
readonly chartOptions: ChartOptions = {
|
|
|
|
scales: {
|
|
|
|
xAxes: [{ticks: {min: 400, max: 4000, stepSize: 400, reverse: true}}],
|
|
|
|
yAxes: [{ticks: {min: 0, max: 1}}]
|
|
|
|
},
|
|
|
|
responsive: true,
|
|
|
|
tooltips: {enabled: false},
|
|
|
|
hover: {mode: null},
|
|
|
|
maintainAspectRatio: true,
|
|
|
|
plugins: {datalabels: {display: false}}
|
|
|
|
};
|
2020-06-19 08:43:22 +02:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private api: ApiService,
|
|
|
|
private validation: ValidationService,
|
2020-07-30 11:33:56 +02:00
|
|
|
public autocomplete: AutocompleteService,
|
2020-08-06 08:18:57 +02:00
|
|
|
private modal: ModalService,
|
2020-08-21 16:11:57 +02:00
|
|
|
public d: DataService,
|
|
|
|
private login: LoginService
|
2020-06-19 08:43:22 +02:00
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2020-08-21 16:11:57 +02:00
|
|
|
this.mode = this.router.url === '/samples/new' ? 'new' : '';
|
2020-07-22 10:45:34 +02:00
|
|
|
this.loading = 7;
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('materials', () => {
|
|
|
|
this.materialNames = this.d.arr.materials.map(e => e.name);
|
2020-06-19 08:43:22 +02:00
|
|
|
this.loading--;
|
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('materialSuppliers', () => {
|
2020-06-19 08:43:22 +02:00
|
|
|
this.loading--;
|
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('materialGroups', () => {
|
2020-06-19 08:43:22 +02:00
|
|
|
this.loading--;
|
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('conditionTemplates', () => {
|
2020-06-19 08:43:22 +02:00
|
|
|
this.loading--;
|
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('measurementTemplates', () => {
|
2020-08-06 15:23:44 +02:00
|
|
|
this.d.load('user', () => {
|
|
|
|
this.defaultDevice = this.d.d.user.devices[0];
|
|
|
|
// spectrum device must be from user's devices list
|
|
|
|
this.d.arr.measurementTemplates.forEach(template => {
|
|
|
|
const device = template.parameters.find(e => e.name === 'device');
|
|
|
|
if (device) {
|
|
|
|
device.range.values = this.d.d.user.devices;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.d.idReload('measurementTemplates');
|
|
|
|
});
|
2020-07-22 10:45:34 +02:00
|
|
|
this.loading--;
|
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('materialTemplates', () => {
|
|
|
|
if (!this.material.properties.material_template) {
|
2020-08-10 16:15:17 +02:00
|
|
|
this.material.properties.material_template = this.d.latest.materialTemplates.find(e => e.name === 'plastic')._id;
|
2020-07-22 10:45:34 +02:00
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
this.loading--;
|
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('sampleNotesFields', () => {
|
|
|
|
this.availableCustomFields = this.d.arr.sampleNotesFields.map(e => e.name);
|
2020-06-19 08:43:22 +02:00
|
|
|
this.loading--;
|
|
|
|
});
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.mode !== 'new') {
|
|
|
|
const sampleIds = this.route.snapshot.paramMap.get('id').split(',');
|
|
|
|
if (sampleIds.length === 1) {
|
|
|
|
this.mode = 'editOne';
|
|
|
|
this.view.baseSum = true;
|
|
|
|
this.view.cm = true;
|
|
|
|
if (this.login.isLevel.dev) { // load measurement restore data
|
|
|
|
this.api.get<MeasurementModel[]>('/measurement/sample/' + sampleIds[0], (data, ignore) => {
|
|
|
|
if (data) {
|
|
|
|
this.measurementRestoreData = data.filter(e => e.status === 'deleted').map(e => new MeasurementModel().deserialize(e));
|
|
|
|
}
|
|
|
|
console.log(this.measurementRestoreData);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.mode = 'editMulti';
|
|
|
|
this.view.base = true;
|
|
|
|
}
|
|
|
|
this.loading += sampleIds.length;
|
2020-08-24 12:43:39 +02:00
|
|
|
this.api.get<SampleModel>('/sample/' + sampleIds[0], sData => { // special treatment for first id
|
|
|
|
this.samples = [new SampleModel().deserialize(sData)];
|
|
|
|
this.baseSample.deserialize(sData);
|
|
|
|
this.material = new MaterialModel().deserialize(sData.material); // read material
|
|
|
|
this.customFields = this.baseSample.notes.custom_fields && this.baseSample.notes.custom_fields !== {} ? // read custom fields
|
|
|
|
Object.keys(this.baseSample.notes.custom_fields).map(e => [e, this.baseSample.notes.custom_fields[e]]) : [];
|
|
|
|
if (this.baseSample.notes.sample_references.length) { // read sample references
|
|
|
|
this.sampleReferences = [];
|
|
|
|
this.sampleReferenceAutocomplete = [];
|
|
|
|
let loadCounter = this.baseSample.notes.sample_references.length; // count down instances still loading
|
|
|
|
this.baseSample.notes.sample_references.forEach(reference => {
|
|
|
|
this.api.get<SampleModel>('/sample/' + reference.sample_id, srData => { // get sample numbers for ids
|
|
|
|
this.sampleReferences.push([srData.number, reference.relation, reference.sample_id]);
|
|
|
|
this.sampleReferenceAutocomplete.push([srData.number]);
|
|
|
|
if (!--loadCounter) { // insert empty template when all instances were loaded
|
|
|
|
this.sampleReferences.push(['', '', '']);
|
|
|
|
this.sampleReferenceAutocomplete.push([]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.mode === 'editOne') {
|
|
|
|
this.charts = [[]];
|
|
|
|
let spectrumCounter = 0; // generate charts for spectrum measurements
|
|
|
|
this.samples[0].measurements.forEach((measurement, i) => {
|
|
|
|
this.charts[0].push(cloneDeep(this.chartInit));
|
|
|
|
if (measurement.values.dpt) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.generateChart(measurement.values.dpt, 0, i);
|
|
|
|
}, spectrumCounter * 20); // generate charts one after another to avoid freezing the UI
|
|
|
|
spectrumCounter ++;
|
2020-08-21 16:11:57 +02:00
|
|
|
}
|
2020-08-24 12:43:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.loading--;
|
|
|
|
sampleIds.slice(1).forEach(sampleId => {
|
|
|
|
this.api.get<SampleModel>('/sample/' + sampleId, data => {
|
|
|
|
this.samples.push(new SampleModel().deserialize(data));
|
2020-08-21 16:11:57 +02:00
|
|
|
['type', 'color', 'batch', 'notes'].forEach((key) => {
|
2020-08-24 12:43:39 +02:00
|
|
|
console.log(isEqual(data[key], this.baseSample[key]));
|
|
|
|
if (!isEqual(data[key], this.baseSample[key])) {
|
2020-08-21 16:11:57 +02:00
|
|
|
this.baseSample[key] = undefined;
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|
|
|
|
});
|
2020-08-24 12:43:39 +02:00
|
|
|
if (!isEqual(data.material.name, this.baseSample.material.name)) {
|
2020-08-21 16:11:57 +02:00
|
|
|
this.baseSample.material.name = undefined;
|
|
|
|
}
|
2020-08-24 12:43:39 +02:00
|
|
|
this.loading--;
|
|
|
|
this.checkFormAfterInit = true;
|
|
|
|
});
|
2020-08-21 16:11:57 +02:00
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
});
|
|
|
|
}
|
2020-08-21 16:11:57 +02:00
|
|
|
else {
|
|
|
|
this.view.base = true;
|
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterContentChecked() {
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.samples.length) { // conditions are displayed
|
|
|
|
this.samples.forEach((gSample, gIndex) => {
|
2020-08-06 08:18:57 +02:00
|
|
|
if (this.d.id.conditionTemplates[gSample.condition.condition_template]) {
|
|
|
|
this.d.id.conditionTemplates[gSample.condition.condition_template].parameters.forEach((parameter, pIndex) => {
|
2020-08-24 15:24:55 +02:00
|
|
|
this.attachValidator(this.cmForm, `conditionParameter-${gIndex}-${pIndex}`, parameter.range);
|
2020-08-06 08:18:57 +02:00
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
gSample.measurements.forEach((measurement, mIndex) => {
|
|
|
|
this.d.id.measurementTemplates[measurement.measurement_template].parameters.forEach((parameter, pIndex) => {
|
2020-08-24 15:24:55 +02:00
|
|
|
this.attachValidator(this.cmForm, `measurementParameter-${gIndex}-${mIndex}-${pIndex}`, parameter.range);
|
2020-08-06 08:18:57 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
if (this.sampleForm && this.material.properties.material_template) { // material template is set
|
|
|
|
this.d.id.materialTemplates[this.material.properties.material_template].parameters.forEach((parameter, i) => {
|
2020-08-24 15:24:55 +02:00
|
|
|
this.attachValidator(this.sampleForm, 'materialParameter' + i, parameter.range);
|
2020-08-06 08:18:57 +02:00
|
|
|
});
|
2020-07-22 10:45:34 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
if (this.checkFormAfterInit) {
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.view.base) { // validate sampleForm
|
2020-08-06 08:18:57 +02:00
|
|
|
if (this.sampleForm !== undefined && this.sampleForm.form.get('cf-key0')) {
|
|
|
|
this.checkFormAfterInit = false;
|
|
|
|
Object.keys(this.sampleForm.form.controls).forEach(field => {
|
|
|
|
this.sampleForm.form.get(field).updateValueAndValidity();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // validate cmForm
|
|
|
|
// check that all fields are ready for validation
|
|
|
|
let formReady: boolean = this.cmForm !== undefined; // forms exist
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.samples[0].condition.condition_template) { // if condition is set, last condition field exists
|
2020-08-06 08:18:57 +02:00
|
|
|
formReady = formReady && this.cmForm.form.get('conditionParameter-0-' +
|
2020-08-21 16:11:57 +02:00
|
|
|
(this.d.id.conditionTemplates[this.samples[0].condition.condition_template].parameters.length - 1)) as any;
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.samples[0].measurements.length) { // if there are measurements, last measurement field exists
|
|
|
|
formReady = formReady && this.cmForm.form.get('measurementParameter-0-' + (this.samples[0].measurements.length - 1) +
|
|
|
|
'-' + (this.d.id.measurementTemplates[this.samples[0].measurements[this.samples[0].measurements.length - 1]
|
2020-08-06 08:18:57 +02:00
|
|
|
.measurement_template].parameters.length - 1)) as any;
|
|
|
|
}
|
|
|
|
if (formReady) { // fields are ready, do validation
|
|
|
|
this.checkFormAfterInit = false;
|
|
|
|
Object.keys(this.cmForm.form.controls).forEach(field => {
|
|
|
|
this.cmForm.form.get(field).updateValueAndValidity();
|
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
// attach validators specified in range to input with name
|
2020-08-24 15:24:55 +02:00
|
|
|
attachValidator(form, name: string, range: {[prop: string]: any}) {
|
2020-08-06 08:18:57 +02:00
|
|
|
if (form && form.form.get(name)) {
|
2020-06-19 08:43:22 +02:00
|
|
|
const validators = [];
|
2020-08-24 15:24:55 +02:00
|
|
|
if (range.hasOwnProperty('required')) {
|
2020-06-19 08:43:22 +02:00
|
|
|
validators.push(Validators.required);
|
|
|
|
}
|
|
|
|
if (range.hasOwnProperty('values')) {
|
|
|
|
validators.push(this.validation.generate('stringOf', [range.values]));
|
|
|
|
}
|
|
|
|
else if (range.hasOwnProperty('min') && range.hasOwnProperty('max')) {
|
|
|
|
validators.push(this.validation.generate('minMax', [range.min, range.max]));
|
|
|
|
}
|
|
|
|
else if (range.hasOwnProperty('min')) {
|
|
|
|
validators.push(this.validation.generate('min', [range.min]));
|
|
|
|
}
|
|
|
|
else if (range.hasOwnProperty('max')) {
|
|
|
|
validators.push(this.validation.generate('max', [range.max]));
|
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
form.form.get(name).setValidators(validators);
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
// save base sample
|
2020-06-19 08:43:22 +02:00
|
|
|
saveSample() {
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.samples.length === 0) {
|
2020-08-06 08:18:57 +02:00
|
|
|
this.loading = this.sampleCount; // set up loading spinner
|
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
new Promise<void>(resolve => {
|
|
|
|
if (this.newMaterial) { // save material first if new one exists
|
2020-08-10 16:15:17 +02:00
|
|
|
this.material.numbers = this.material.numbers.filter(e => e !== '');
|
2020-06-19 08:43:22 +02:00
|
|
|
this.api.post<MaterialModel>('/material/new', this.material.sendFormat(), data => {
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.arr.materials.push(data); // add material to data
|
2020-06-19 08:43:22 +02:00
|
|
|
this.material = data;
|
2020-08-21 16:11:57 +02:00
|
|
|
this.baseSample.material_id = data._id; // add new material id to sample data
|
2020-06-19 08:43:22 +02:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}).then(() => { // save sample
|
2020-08-26 19:25:31 +02:00
|
|
|
if (this.baseSample.notes) {
|
|
|
|
this.baseSample.notes.custom_fields = {};
|
|
|
|
this.customFields.forEach(element => {
|
|
|
|
if (element[0] !== '') {
|
|
|
|
this.baseSample.notes.custom_fields[element[0]] = element[1];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.baseSample.notes.sample_references = this.sampleReferences
|
|
|
|
.filter(e => e[0] && e[1] && e[2])
|
|
|
|
.map(e => ({sample_id: e[2], relation: e[1]}));
|
|
|
|
}
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.samples.length === 0) { // only save new sample for the first time in mode new, otherwise save changes
|
2020-08-06 08:18:57 +02:00
|
|
|
for (let i = 0; i < this.sampleCount; i ++) {
|
2020-08-21 16:11:57 +02:00
|
|
|
this.api.post<SampleModel>('/sample/new', this.baseSample.sendFormat(), data => {
|
|
|
|
this.samples[i] = new SampleModel().deserialize(data);
|
|
|
|
this.samples[i].material = this.d.arr.materials.find(e => e._id === this.samples[i].material_id);
|
2020-08-06 08:18:57 +02:00
|
|
|
this.loading --;
|
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
2020-08-21 16:11:57 +02:00
|
|
|
this.view.base = false;
|
|
|
|
this.view.baseSum = true;
|
|
|
|
this.view.cm = true;
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-08-21 16:11:57 +02:00
|
|
|
this.samples.forEach((sample, i) => {
|
|
|
|
console.log(sample._id);
|
|
|
|
this.api.put<SampleModel>('/sample/' + sample._id, this.baseSample.sendFormat(), data => {
|
|
|
|
merge(this.samples[i], omit(data, ['condition']));
|
|
|
|
this.samples[i].material = this.d.arr.materials.find(e => e._id === this.samples[0].material_id);
|
|
|
|
this.view.base = false;
|
|
|
|
this.view.baseSum = true;
|
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// save conditions and measurements
|
|
|
|
cmSave() { // save measurements and conditions
|
2020-08-21 16:11:57 +02:00
|
|
|
this.samples.forEach(sample => {
|
2020-08-06 08:18:57 +02:00
|
|
|
if (sample.condition.condition_template) { // condition was set
|
|
|
|
this.api.put('/sample/' + sample._id, {condition: sample.condition});
|
|
|
|
}
|
|
|
|
sample.measurements.forEach(measurement => { // save measurements
|
|
|
|
if (Object.keys(measurement.values).map(e => measurement.values[e]).join('') !== '') {
|
|
|
|
Object.keys(measurement.values).forEach(key => { // map empty values to null
|
|
|
|
measurement.values[key] = measurement.values[key] === '' ? null : measurement.values[key];
|
|
|
|
});
|
|
|
|
if (measurement._id === null) { // new measurement
|
|
|
|
measurement.sample_id = sample._id;
|
|
|
|
this.api.post<MeasurementModel>('/measurement/new', measurement.sendFormat());
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
else { // update measurement
|
|
|
|
this.api.put<MeasurementModel>('/measurement/' + measurement._id,
|
|
|
|
measurement.sendFormat(['sample_id', 'measurement_template']));
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|
|
|
|
else if (measurement._id !== null) { // existing measurement was left empty to delete
|
|
|
|
this.api.delete('/measurement/' + measurement._id);
|
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
});
|
2020-08-21 16:11:57 +02:00
|
|
|
this.measurementDeleteList.forEach(measurement => {
|
|
|
|
this.api.delete('/measurement/' + measurement);
|
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
|
|
|
|
this.router.navigate(['/samples']);
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 16:11:57 +02:00
|
|
|
restoreMeasurements() {
|
|
|
|
let spectrumCounter = 0; // generate charts for spectrum measurements
|
|
|
|
const measurementCount = this.samples[0].measurements.length;
|
|
|
|
this.measurementRestoreData.forEach((measurement, i) => {
|
|
|
|
this.api.put('/measurement/restore/' + measurement._id, {}, () => {
|
|
|
|
this.samples[0].measurements.push(measurement);
|
|
|
|
this.charts[0].push(cloneDeep(this.chartInit));
|
|
|
|
if (measurement.values.dpt) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.generateChart(measurement.values.dpt, 0, measurementCount + i);
|
|
|
|
}, spectrumCounter * 20); // generate charts one after another to avoid freezing the UI
|
|
|
|
spectrumCounter ++;
|
|
|
|
}
|
|
|
|
this.checkFormAfterInit = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
// set material based on found material name
|
2020-06-19 08:43:22 +02:00
|
|
|
findMaterial(name) {
|
2020-08-06 08:18:57 +02:00
|
|
|
const res = this.d.arr.materials.find(e => e.name === name); // search for match
|
|
|
|
if (res) { // material found
|
2020-07-30 14:23:51 +02:00
|
|
|
this.material = cloneDeep(res);
|
2020-08-21 16:11:57 +02:00
|
|
|
this.baseSample.material_id = this.material._id;
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
else { // no matching material found
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.baseSample.material_id !== null) { // reset previous match
|
2020-07-22 10:45:34 +02:00
|
|
|
this.material = new MaterialModel();
|
2020-08-10 16:15:17 +02:00
|
|
|
this.material.properties.material_template = this.d.latest.materialTemplates.find(e => e.name === 'plastic')._id;
|
2020-07-22 10:45:34 +02:00
|
|
|
}
|
2020-08-21 16:11:57 +02:00
|
|
|
this.baseSample.material_id = null;
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
this.setNewMaterial();
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
// set newMaterial, if value === null -> toggle
|
2020-06-19 08:43:22 +02:00
|
|
|
setNewMaterial(value = null) {
|
2020-08-06 08:18:57 +02:00
|
|
|
if (value === null) { // toggle dialog
|
2020-08-21 16:11:57 +02:00
|
|
|
this.newMaterial = !this.baseSample.material_id;
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
2020-08-21 16:11:57 +02:00
|
|
|
else if (value || (!value && this.baseSample.material_id !== null )) { // set to false only if material already exists
|
2020-06-19 08:43:22 +02:00
|
|
|
this.newMaterial = value;
|
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
if (this.newMaterial) { // set validators if dialog is open
|
2020-06-19 08:43:22 +02:00
|
|
|
this.sampleForm.form.get('materialname').setValidators([Validators.required]);
|
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
else { // material name must be from list if dialog is closed
|
2020-07-30 11:33:56 +02:00
|
|
|
this.sampleForm.form.get('materialname')
|
2020-08-06 08:18:57 +02:00
|
|
|
.setValidators([Validators.required, this.validation.generate('stringOf', [this.materialNames])]);
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
this.sampleForm.form.get('materialname').updateValueAndValidity();
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
// add a new measurement for generated sample at index
|
|
|
|
addMeasurement(gIndex) {
|
2020-08-21 16:11:57 +02:00
|
|
|
this.samples[gIndex].measurements.push(
|
2020-08-10 16:15:17 +02:00
|
|
|
new MeasurementModel(this.d.latest.measurementTemplates.find(e => e.name === 'spectrum')._id)
|
2020-08-06 08:18:57 +02:00
|
|
|
);
|
|
|
|
if (!this.charts[gIndex]) { // add array if there are no charts yet
|
|
|
|
this.charts[gIndex] = [];
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
this.charts[gIndex].push(cloneDeep(this.chartInit));
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
// remove the measurement at the specified index
|
2020-08-21 16:11:57 +02:00
|
|
|
removeMeasurement(gIndex, mIndex) { // TODO: do not delete directly but only after confirmation
|
|
|
|
if (this.samples[gIndex].measurements[mIndex]._id !== null) {
|
|
|
|
this.measurementDeleteList.push(this.samples[gIndex].measurements[mIndex]._id);
|
2020-07-22 10:45:34 +02:00
|
|
|
}
|
2020-08-21 16:11:57 +02:00
|
|
|
this.samples[gIndex].measurements.splice(mIndex, 1);
|
2020-08-06 08:18:57 +02:00
|
|
|
this.charts[gIndex].splice(mIndex, 1);
|
2020-07-22 10:45:34 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 16:11:57 +02:00
|
|
|
// clear entered measurement data at the specified index due to template change
|
2020-08-06 08:18:57 +02:00
|
|
|
clearMeasurement(gIndex, mIndex) {
|
|
|
|
this.charts[gIndex][mIndex][0].data = [];
|
2020-08-21 16:11:57 +02:00
|
|
|
this.samples[gIndex].measurements[mIndex].values = {};
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
fileToArray(files, gIndex, mIndex, parameter) {
|
2020-07-30 11:33:56 +02:00
|
|
|
for (const i in files) {
|
|
|
|
if (files.hasOwnProperty(i)) {
|
|
|
|
const fileReader = new FileReader();
|
|
|
|
fileReader.onload = () => {
|
|
|
|
let index: number = mIndex;
|
|
|
|
if (Number(i) > 0) { // append further spectra
|
2020-08-06 08:18:57 +02:00
|
|
|
this.addMeasurement(gIndex);
|
2020-08-21 16:11:57 +02:00
|
|
|
index = this.samples[gIndex].measurements.length - 1;
|
2020-07-30 11:33:56 +02:00
|
|
|
}
|
2020-08-21 16:11:57 +02:00
|
|
|
this.samples[gIndex].measurements[index].values.device =
|
|
|
|
this.samples[gIndex].measurements[mIndex].values.device;
|
|
|
|
this.samples[gIndex].measurements[index].values.filename = files[i].name;
|
|
|
|
this.samples[gIndex].measurements[index].values[parameter] =
|
|
|
|
fileReader.result.toString().split('\r\n').map(e => e.split(',')).filter(el => el.length === 2);
|
|
|
|
this.generateChart(this.samples[gIndex].measurements[index].values[parameter], gIndex, index);
|
2020-07-30 11:33:56 +02:00
|
|
|
};
|
|
|
|
fileReader.readAsText(files[i]);
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 09:39:37 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
generateChart(spectrum, gIndex, mIndex) {
|
|
|
|
this.charts[gIndex][mIndex][0].data = spectrum.map(e => ({x: parseFloat(e[0]), y: parseFloat(e[1])}));
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
toggleCondition(sample) {
|
|
|
|
if (sample.condition.condition_template) {
|
|
|
|
sample.condition.condition_template = null;
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-08-10 16:15:17 +02:00
|
|
|
sample.condition.condition_template = this.d.latest.conditionTemplates[0]._id;
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:28:17 +02:00
|
|
|
checkTypo(event, list, mKey, modal: TemplateRef<any>) {
|
|
|
|
// user did not click on suggestion and entry is not in list
|
|
|
|
if (!(event.relatedTarget && (event.relatedTarget.className.indexOf('rb-dropdown-item') >= 0 ||
|
|
|
|
event.relatedTarget.className.indexOf('close-btn rb-btn rb-passive-link') >= 0)) &&
|
|
|
|
this.d.arr[list].indexOf(this.material[mKey]) < 0) {
|
2020-08-06 08:18:57 +02:00
|
|
|
this.modalText.list = mKey;
|
|
|
|
this.modalText.suggestion = this.d.arr[list] // find possible entry from list
|
|
|
|
.map(e => ({v: e, s: strCompare.sorensenDice(e, this.material[mKey])}))
|
2020-07-30 11:33:56 +02:00
|
|
|
.sort((a, b) => b.s - a.s)[0].v;
|
|
|
|
this.modal.open(modal).then(result => {
|
|
|
|
if (result) { // use suggestion
|
2020-08-06 08:18:57 +02:00
|
|
|
this.material[mKey] = this.modalText.suggestion;
|
2020-07-30 11:33:56 +02:00
|
|
|
}
|
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
deleteConfirm(modal) {
|
|
|
|
this.modal.open(modal).then(result => {
|
|
|
|
if (result) {
|
2020-08-24 12:43:39 +02:00
|
|
|
this.samples.forEach(sample => {
|
|
|
|
this.api.delete('/sample/' + sample._id);
|
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.router.navigate(['/samples']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 10:45:34 +02:00
|
|
|
checkSampleReference(value, index) {
|
|
|
|
if (value) {
|
|
|
|
this.sampleReferences[index][0] = value;
|
|
|
|
}
|
|
|
|
this.currentSRIndex = index;
|
|
|
|
const fieldNo = this.sampleReferences.length;
|
|
|
|
let filledFields = 0;
|
|
|
|
this.sampleReferences.forEach(field => {
|
|
|
|
if (field[0] !== '') {
|
|
|
|
filledFields ++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// append new field
|
|
|
|
if (filledFields === fieldNo) {
|
|
|
|
this.sampleReferences.push(['', '', '']);
|
|
|
|
this.sampleReferenceAutocomplete.push([]);
|
|
|
|
}
|
|
|
|
// remove if two end fields are empty
|
|
|
|
if (fieldNo > 1 && this.sampleReferences[fieldNo - 1][0] === '' && this.sampleReferences[fieldNo - 2][0] === '') {
|
|
|
|
this.sampleReferences.pop();
|
|
|
|
this.sampleReferenceAutocomplete.pop();
|
|
|
|
}
|
|
|
|
this.sampleReferenceIdFind(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
sampleReferenceList(value) {
|
|
|
|
return new Observable(observer => {
|
2020-08-06 08:18:57 +02:00
|
|
|
if (value !== '') {
|
|
|
|
this.api.get<{ _id: string, number: string }[]>(
|
2020-08-10 13:30:26 +02:00
|
|
|
'/samples?status[]=validated&status[]=new&page-size=25&sort=number-asc&fields[]=number&fields[]=_id&' +
|
2020-08-06 08:18:57 +02:00
|
|
|
'filters[]=%7B%22mode%22%3A%22stringin%22%2C%22field%22%3A%22number%22%2C%22values%22%3A%5B%22' + value + '%22%5D%7D', data => {
|
|
|
|
this.sampleReferenceAutocomplete[this.currentSRIndex] = data.map(e => e.number);
|
|
|
|
this.sampleReferenceFinds = data;
|
|
|
|
observer.next(data.map(e => e.number));
|
|
|
|
observer.complete();
|
|
|
|
this.sampleReferenceIdFind(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
observer.next([]);
|
2020-07-22 10:45:34 +02:00
|
|
|
observer.complete();
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|
2020-07-22 10:45:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sampleReferenceIdFind(value) {
|
|
|
|
const idFind = this.sampleReferenceFinds.find(e => e.number === value);
|
|
|
|
if (idFind) {
|
|
|
|
this.sampleReferences[this.currentSRIndex][2] = idFind._id;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sampleReferences[this.currentSRIndex][2] = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sampleReferenceListBind() {
|
|
|
|
return this.sampleReferenceList.bind(this);
|
|
|
|
}
|
|
|
|
|
2020-06-19 08:43:22 +02:00
|
|
|
uniqueCfValues(index) { // returns all names until index for unique check
|
2020-07-30 11:33:56 +02:00
|
|
|
return this.customFields ? this.customFields.slice(0, index).map(e => e[0]) : [];
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
|
|
|
|
preventDefault(event) {
|
|
|
|
if (event.key && event.key === 'Enter' || event.type === 'dragover') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-30 11:33:56 +02:00
|
|
|
// 1. ngAfterViewInit wird ja jedes mal nach einem ngOnChanges aufgerufen, also zB wenn sich dein ngFor aufbaut. Du könntest also in der
|
|
|
|
// Methode prüfen, ob die Daten schon da sind und dann dementsprechend handeln. Das wäre die Eleganteste Variante
|
2020-06-19 08:43:22 +02:00
|
|
|
// 2. Der state "dirty" soll eigentlich anzeigen, wenn ein Form-Field vom User geändert wurde; damit missbrauchst du es hier etwas
|
2020-07-30 11:33:56 +02:00
|
|
|
// 3. Die Dirty-Variante: Pack in deine ngFor ein {{ onFirstLoad(data) }} rein, das einfach ausgeführt wird. müsstest dann natürlich
|
|
|
|
// abfangen, dass das nicht nach jedem view-cycle neu getriggert wird. Schön ist das nicht, aber besser als mit Timeouts^^
|