2020-08-06 08:18:57 +02:00
|
|
|
import {Component, ElementRef, isDevMode, OnInit, TemplateRef, ViewChild} from '@angular/core';
|
2020-06-19 08:43:22 +02:00
|
|
|
import {ApiService} from '../services/api.service';
|
2020-07-13 10:52:10 +02:00
|
|
|
import {AutocompleteService} from '../services/autocomplete.service';
|
2020-07-30 14:23:51 +02:00
|
|
|
import cloneDeep from 'lodash/cloneDeep';
|
|
|
|
import pick from 'lodash/pick';
|
2020-08-12 11:06:46 +02:00
|
|
|
import omit from 'lodash/omit';
|
2020-07-27 17:52:03 +02:00
|
|
|
import {SampleModel} from '../models/sample.model';
|
2020-08-06 08:18:57 +02:00
|
|
|
import {LoginService} from '../services/login.service';
|
|
|
|
import {ModalService} from '@inst-iot/bosch-angular-ui-components';
|
|
|
|
import {DataService} from '../services/data.service';
|
2020-08-12 17:38:12 +02:00
|
|
|
import {LocalStorageService} from 'angular-2-local-storage';
|
2020-08-21 16:11:57 +02:00
|
|
|
import {Router} from '@angular/router';
|
2020-05-22 09:36:50 +02:00
|
|
|
|
2020-08-16 20:01:56 +02:00
|
|
|
// TODO: turn off sort field
|
2020-08-18 12:37:27 +02:00
|
|
|
// TODO reset sort when field is excluded
|
|
|
|
// TODO: Eh DPT
|
|
|
|
// TODO: filter button
|
|
|
|
// TODO: check if connect-src to model works
|
2020-08-16 20:01:56 +02:00
|
|
|
|
2020-06-26 11:09:59 +02:00
|
|
|
|
|
|
|
interface LoadSamplesOptions {
|
|
|
|
toPage?: number;
|
|
|
|
event?: Event;
|
|
|
|
firstPage?: boolean;
|
|
|
|
}
|
2020-07-22 10:45:34 +02:00
|
|
|
interface KeyInterface {
|
|
|
|
id: string;
|
|
|
|
label: string;
|
|
|
|
active: boolean;
|
2020-07-27 17:52:03 +02:00
|
|
|
sortable: boolean;
|
2020-07-22 10:45:34 +02:00
|
|
|
}
|
2020-06-26 11:09:59 +02:00
|
|
|
|
2020-05-22 09:36:50 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-samples',
|
|
|
|
templateUrl: './samples.component.html',
|
|
|
|
styleUrls: ['./samples.component.scss']
|
|
|
|
})
|
2020-06-26 11:09:59 +02:00
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
export class SamplesComponent implements OnInit {
|
2020-06-26 11:09:59 +02:00
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
@ViewChild('pageSizeSelection') pageSizeSelection: ElementRef<HTMLElement>;
|
|
|
|
@ViewChild('linkarea') linkarea: ElementRef<HTMLTextAreaElement>;
|
|
|
|
|
2020-08-11 16:25:09 +02:00
|
|
|
downloadSpectra = false; // TODO: streamline these options after csv option handling is clear
|
|
|
|
downloadCondition = false;
|
2020-08-10 12:34:14 +02:00
|
|
|
downloadFlatten = true;
|
2020-07-27 17:52:03 +02:00
|
|
|
samples: SampleModel[] = [];
|
2020-06-26 11:09:59 +02:00
|
|
|
totalSamples = 0; // total number of samples
|
2020-07-13 10:52:10 +02:00
|
|
|
csvUrl = ''; // store url separate so it only has to be generated when clicking the download button
|
|
|
|
filters = {
|
2020-08-07 10:49:18 +02:00
|
|
|
status: {new: true, validated: true, deleted: false},
|
2020-07-13 10:52:10 +02:00
|
|
|
pageSize: 25,
|
|
|
|
toPage: 0,
|
|
|
|
sort: 'added-asc',
|
|
|
|
filters: [
|
|
|
|
{field: 'number', label: 'Number', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
2020-08-31 17:10:44 +02:00
|
|
|
{field: 'material.name', label: 'Product name', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
2020-07-13 10:52:10 +02:00
|
|
|
{field: 'material.supplier', label: 'Supplier', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
|
|
|
{field: 'material.group', label: 'Material', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
|
|
|
{field: 'material.glass_fiber', label: 'GF', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
|
|
|
{field: 'material.carbon_fiber', label: 'CF', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
|
|
|
{field: 'material.mineral', label: 'M', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
|
|
|
{field: 'type', label: 'Type', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
|
|
|
{field: 'color', label: 'Color', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
|
|
|
{field: 'batch', label: 'Batch', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
2020-08-24 12:43:39 +02:00
|
|
|
{field: 'notes.comment', label: 'Comment', active: false, autocomplete: [], mode: 'eq', values: ['']},
|
2020-07-30 11:33:56 +02:00
|
|
|
{field: 'added', label: 'Added', active: false, autocomplete: [], mode: 'eq', values: ['']}
|
2020-07-13 10:52:10 +02:00
|
|
|
]
|
|
|
|
};
|
2020-06-26 11:09:59 +02:00
|
|
|
page = 1;
|
2020-07-22 10:45:34 +02:00
|
|
|
pages = 1;
|
2020-06-26 11:09:59 +02:00
|
|
|
loadSamplesQueue = []; // arguments of queued up loadSamples() calls
|
2020-07-22 10:45:34 +02:00
|
|
|
keys: KeyInterface[] = [
|
2020-07-27 17:52:03 +02:00
|
|
|
{id: 'number', label: 'Number', active: true, sortable: true},
|
2020-08-12 11:06:46 +02:00
|
|
|
{id: 'material.numbers', label: 'Material numbers', active: false, sortable: false},
|
2020-08-31 17:10:44 +02:00
|
|
|
{id: 'material.name', label: 'Product name', active: true, sortable: true},
|
2020-08-18 12:37:27 +02:00
|
|
|
{id: 'material.supplier', label: 'Supplier', active: false, sortable: true},
|
|
|
|
{id: 'material.group', label: 'Material', active: true, sortable: true},
|
2020-07-27 17:52:03 +02:00
|
|
|
{id: 'type', label: 'Type', active: true, sortable: true},
|
2020-08-18 12:37:27 +02:00
|
|
|
{id: 'color', label: 'Color', active: false, sortable: true},
|
2020-07-27 17:52:03 +02:00
|
|
|
{id: 'batch', label: 'Batch', active: true, sortable: true},
|
2020-08-24 12:43:39 +02:00
|
|
|
{id: 'notes.comment', label: 'Comment', active: false, sortable: false},
|
2020-07-27 17:52:03 +02:00
|
|
|
{id: 'notes', label: 'Notes', active: false, sortable: false},
|
2020-08-18 12:37:27 +02:00
|
|
|
{id: 'status', label: 'Status', active: false, sortable: true},
|
2020-08-07 10:49:18 +02:00
|
|
|
{id: 'added', label: 'Added', active: true, sortable: true}
|
2020-06-26 11:09:59 +02:00
|
|
|
];
|
2020-07-22 10:45:34 +02:00
|
|
|
isActiveKey: {[key: string]: boolean} = {};
|
|
|
|
activeKeys: KeyInterface[] = [];
|
2020-08-14 14:29:17 +02:00
|
|
|
activeTemplateKeys = {material: [], condition: [], measurements: []};
|
2020-08-06 08:18:57 +02:00
|
|
|
sampleDetailsSample: any = null;
|
2020-08-21 16:11:57 +02:00
|
|
|
sampleSelect = 0; // modes: 0 - no selection, 1 - sample edit selection, 2 - validation selection
|
2020-08-18 12:37:27 +02:00
|
|
|
loading = 0;
|
2020-05-22 12:52:17 +02:00
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
|
2020-05-22 12:52:17 +02:00
|
|
|
constructor(
|
2020-07-13 10:52:10 +02:00
|
|
|
private api: ApiService,
|
2020-08-06 08:18:57 +02:00
|
|
|
public autocomplete: AutocompleteService,
|
|
|
|
public login: LoginService,
|
|
|
|
private modalService: ModalService,
|
2020-08-12 17:38:12 +02:00
|
|
|
public d: DataService,
|
2020-08-14 14:29:17 +02:00
|
|
|
private storage: LocalStorageService,
|
2020-08-21 16:11:57 +02:00
|
|
|
private window: Window,
|
|
|
|
private router: Router
|
2020-07-13 10:52:10 +02:00
|
|
|
) {
|
|
|
|
}
|
2020-05-22 09:36:50 +02:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
2020-08-18 12:37:27 +02:00
|
|
|
this.loading = 8;
|
2020-08-12 17:38:12 +02:00
|
|
|
const onLoad = () => {
|
2020-08-18 12:37:27 +02:00
|
|
|
if ((--this.loading) <= 0) {
|
2020-08-12 17:38:12 +02:00
|
|
|
this.loadSamples();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-22 10:45:34 +02:00
|
|
|
this.calcFieldSelectKeys();
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('materials', () => {
|
|
|
|
this.filters.filters.find(e => e.field === 'material.name').autocomplete = this.d.arr.materials.map(e => e.name);
|
2020-08-12 17:38:12 +02:00
|
|
|
onLoad();
|
2020-06-19 08:43:22 +02:00
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('materialSuppliers', () => {
|
|
|
|
this.filters.filters.find(e => e.field === 'material.supplier').autocomplete = this.d.arr.materialSuppliers;
|
2020-08-12 17:38:12 +02:00
|
|
|
onLoad();
|
2020-07-13 10:52:10 +02:00
|
|
|
});
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load('materialGroups', () => {
|
|
|
|
this.filters.filters.find(e => e.field === 'material.group').autocomplete = this.d.arr.materialGroups;
|
2020-08-12 17:38:12 +02:00
|
|
|
onLoad();
|
2020-07-13 10:52:10 +02:00
|
|
|
});
|
2020-08-12 17:38:12 +02:00
|
|
|
this.d.load('userKey', onLoad);
|
|
|
|
this.d.load('conditionTemplates', onLoad);
|
|
|
|
this.loadTemplateKeys('material', 'type', onLoad);
|
2020-08-24 12:43:39 +02:00
|
|
|
this.loadTemplateKeys('condition', 'notes.comment', onLoad);
|
2020-08-12 17:38:12 +02:00
|
|
|
this.loadTemplateKeys('measurement', 'status', onLoad);
|
2020-07-22 10:45:34 +02:00
|
|
|
}
|
|
|
|
|
2020-08-12 17:38:12 +02:00
|
|
|
loadTemplateKeys(collection, insertBefore, f) {
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.load(collection + 'Templates', () => {
|
2020-07-22 10:45:34 +02:00
|
|
|
const templateKeys = [];
|
2020-08-06 08:18:57 +02:00
|
|
|
this.d.arr[collection + 'Templates'].forEach(item => {
|
2020-07-13 10:52:10 +02:00
|
|
|
item.parameters.forEach(parameter => {
|
2020-07-27 17:52:03 +02:00
|
|
|
const parameterName = encodeURIComponent(parameter.name);
|
2020-08-06 08:18:57 +02:00
|
|
|
// exclude spectrum and duplicates
|
2020-07-30 11:33:56 +02:00
|
|
|
if (parameter.name !== 'dpt' && !templateKeys.find(e => new RegExp('.' + parameterName + '$').test(e.id))) {
|
2020-08-18 08:06:59 +02:00
|
|
|
const collectionNames = {
|
|
|
|
material: 'material.properties',
|
|
|
|
condition: 'condition',
|
|
|
|
measurement: 'measurements.' + encodeURIComponent(item.name)
|
|
|
|
};
|
2020-07-30 11:33:56 +02:00
|
|
|
templateKeys.push({
|
2020-08-14 14:29:17 +02:00
|
|
|
id: `${collectionNames[collection]}.${parameterName}`,
|
|
|
|
label: `${this.ucFirst(item.name)} ${parameter.name}`,
|
2020-07-30 11:33:56 +02:00
|
|
|
active: false,
|
|
|
|
sortable: true
|
|
|
|
});
|
|
|
|
this.filters.filters.push({
|
2020-08-14 14:29:17 +02:00
|
|
|
field: `${collectionNames[collection]}.${parameterName}`,
|
|
|
|
label: `${this.ucFirst(item.name)} ${parameter.name}`,
|
2020-07-30 11:33:56 +02:00
|
|
|
active: false,
|
|
|
|
autocomplete: [],
|
|
|
|
mode: 'eq',
|
|
|
|
values: ['']
|
|
|
|
});
|
2020-07-27 17:52:03 +02:00
|
|
|
}
|
2020-07-13 10:52:10 +02:00
|
|
|
});
|
|
|
|
});
|
2020-07-22 10:45:34 +02:00
|
|
|
this.keys.splice(this.keys.findIndex(e => e.id === insertBefore), 0, ...templateKeys);
|
|
|
|
this.keys = [...this.keys]; // complete overwrite array to invoke update in rb-multiselect
|
2020-08-12 17:38:12 +02:00
|
|
|
this.loadPreferences();
|
|
|
|
f();
|
2020-06-26 11:09:59 +02:00
|
|
|
});
|
2020-06-19 08:43:22 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 10:45:34 +02:00
|
|
|
loadSamples(options: LoadSamplesOptions = {}, event = null) { // set toPage to null to reload first page, queues calls
|
|
|
|
if (event) { // adjust active keys
|
|
|
|
this.keys.forEach(key => {
|
|
|
|
if (event.hasOwnProperty(key.id)) {
|
|
|
|
key.active = event[key.id];
|
|
|
|
}
|
|
|
|
});
|
2020-08-24 12:43:39 +02:00
|
|
|
const sortId = this.filters.sort.replace(/(-asc|-desc)/, '');
|
|
|
|
if (event.hasOwnProperty(sortId) && !event[sortId]) { // reset sort if sort field was unselected
|
|
|
|
this.setSort('_id-asc');
|
|
|
|
}
|
2020-07-22 10:45:34 +02:00
|
|
|
this.updateActiveKeys();
|
|
|
|
}
|
2020-06-26 11:09:59 +02:00
|
|
|
this.loadSamplesQueue.push(options);
|
|
|
|
if (this.loadSamplesQueue.length <= 1) { // nothing queued up
|
|
|
|
this.sampleLoader(this.loadSamplesQueue[0]);
|
|
|
|
}
|
2020-08-12 17:38:12 +02:00
|
|
|
this.storePreferences();
|
2020-06-26 11:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private sampleLoader(options: LoadSamplesOptions) { // actual loading of the sample, do not call directly
|
2020-08-18 12:37:27 +02:00
|
|
|
this.loading ++;
|
2020-08-14 14:29:17 +02:00
|
|
|
this.api.get(this.sampleUrl({paging: true, pagingOptions: options}), (sData, err, headers) => {
|
2020-08-18 12:37:27 +02:00
|
|
|
this.loading --;
|
2020-08-14 14:29:17 +02:00
|
|
|
if (err) {
|
|
|
|
this.storage.remove('samplesPreferences');
|
|
|
|
this.api.requestError(err);
|
2020-06-26 11:09:59 +02:00
|
|
|
}
|
2020-08-14 14:29:17 +02:00
|
|
|
else {
|
|
|
|
if (!options.toPage && headers['x-total-items']) {
|
|
|
|
this.totalSamples = headers['x-total-items'];
|
|
|
|
}
|
|
|
|
this.pages = Math.ceil(this.totalSamples / this.filters.pageSize);
|
|
|
|
this.samples = sData as any;
|
|
|
|
this.loadSamplesQueue.shift();
|
|
|
|
if (this.loadSamplesQueue.length > 0) { // execute next queue item
|
|
|
|
this.sampleLoader(this.loadSamplesQueue[0]);
|
|
|
|
}
|
2020-06-26 11:09:59 +02:00
|
|
|
}
|
2020-05-22 12:52:17 +02:00
|
|
|
});
|
2020-05-22 09:36:50 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 11:33:56 +02:00
|
|
|
sampleUrl(options: {
|
|
|
|
paging?: boolean,
|
|
|
|
pagingOptions?: {
|
|
|
|
firstPage?: boolean,
|
|
|
|
toPage?: number,
|
|
|
|
event?: Event
|
|
|
|
},
|
|
|
|
csv?: boolean,
|
|
|
|
export?: boolean,
|
|
|
|
host?: boolean
|
|
|
|
}) { // return url to fetch samples
|
2020-08-06 08:18:57 +02:00
|
|
|
const additionalTableKeys = ['material_id', '_id', 'user_id']; // keys which should always be added if export = false
|
2020-07-13 10:52:10 +02:00
|
|
|
const query: string[] = [];
|
2020-08-07 10:49:18 +02:00
|
|
|
query.push(...Object.keys(this.filters.status).filter(e => this.filters.status[e]).map(e => 'status[]=' + e));
|
2020-07-13 10:52:10 +02:00
|
|
|
if (options.paging) {
|
|
|
|
if (this.samples[0]) { // do not include from-id when page size was changed
|
|
|
|
if (!options.pagingOptions.firstPage) {
|
|
|
|
query.push('from-id=' + this.samples[0]._id);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.page = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.pagingOptions.toPage) {
|
|
|
|
query.push('to-page=' + options.pagingOptions.toPage);
|
|
|
|
}
|
|
|
|
query.push('page-size=' + this.filters.pageSize);
|
|
|
|
}
|
|
|
|
query.push('sort=' + this.filters.sort);
|
|
|
|
if (options.export) {
|
2020-08-06 08:18:57 +02:00
|
|
|
query.push('key=' + this.d.d.userKey.key);
|
2020-07-13 10:52:10 +02:00
|
|
|
}
|
2020-07-22 10:45:34 +02:00
|
|
|
this.keys.forEach(key => {
|
2020-07-30 11:33:56 +02:00
|
|
|
// do not load material properties for table
|
2020-08-14 14:29:17 +02:00
|
|
|
if (key.active && (options.export || (!options.export && key.id.indexOf('material.') < 0))) {
|
2020-07-22 10:45:34 +02:00
|
|
|
query.push('fields[]=' + key.id);
|
2020-07-13 10:52:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-30 14:23:51 +02:00
|
|
|
query.push(...cloneDeep(this.filters.filters)
|
2020-07-13 10:52:10 +02:00
|
|
|
.map(e => {
|
2020-07-22 10:45:34 +02:00
|
|
|
e.values = e.values.filter(el => el !== ''); // do not include empty values
|
|
|
|
if (e.field === 'added') { // correct timezone
|
2020-07-13 10:52:10 +02:00
|
|
|
e.values = e.values.map(el => new Date(new Date(el).getTime() - new Date(el).getTimezoneOffset() * 60000).toISOString());
|
|
|
|
}
|
2020-08-17 14:51:02 +02:00
|
|
|
if (e.mode === 'null') {
|
2020-08-24 12:43:39 +02:00
|
|
|
e.mode = 'in';
|
|
|
|
e.values = [null, ''];
|
2020-08-17 14:51:02 +02:00
|
|
|
}
|
2020-07-13 10:52:10 +02:00
|
|
|
return e;
|
|
|
|
})
|
2020-07-22 10:45:34 +02:00
|
|
|
.filter(e => e.active && e.values.length > 0)
|
2020-07-30 14:23:51 +02:00
|
|
|
.map(e => 'filters[]=' + encodeURIComponent(JSON.stringify(pick(e, ['mode', 'field', 'values']))))
|
2020-07-13 10:52:10 +02:00
|
|
|
);
|
|
|
|
if (!options.export) {
|
|
|
|
additionalTableKeys.forEach(key => {
|
|
|
|
if (query.indexOf('fields[]=' + key) < 0) { // add key if not already added
|
|
|
|
query.push('fields[]=' + key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-10 12:34:14 +02:00
|
|
|
else {
|
2020-08-10 13:03:51 +02:00
|
|
|
if (options.csv) {
|
|
|
|
query.push('output=csv');
|
|
|
|
}
|
|
|
|
else if (this.downloadFlatten) {
|
2020-08-10 12:34:14 +02:00
|
|
|
query.push('output=flatten');
|
|
|
|
}
|
|
|
|
if (this.downloadSpectra) {
|
|
|
|
query.push('fields[]=measurements.spectrum.dpt');
|
|
|
|
}
|
2020-08-11 16:25:09 +02:00
|
|
|
if (this.downloadCondition) {
|
|
|
|
query.push('fields[]=condition');
|
|
|
|
}
|
2020-07-13 10:52:10 +02:00
|
|
|
}
|
2020-08-14 14:29:17 +02:00
|
|
|
return (options.host && isDevMode() ? this.window.location.host : '') +
|
2020-07-30 11:33:56 +02:00
|
|
|
(options.export ? this.api.hostName : '') +
|
|
|
|
'/samples?' + query.join('&');
|
2020-07-13 10:52:10 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 11:09:59 +02:00
|
|
|
loadPage(delta) {
|
2020-08-21 16:11:57 +02:00
|
|
|
if (!/[0-9]+/.test(delta) || this.page + delta < 1 || this.page + delta > this.pages) { // invalid delta
|
2020-06-26 11:09:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.page += delta;
|
|
|
|
this.loadSamples({toPage: delta});
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:38:12 +02:00
|
|
|
storePreferences() {
|
|
|
|
const store = {
|
|
|
|
filters: {
|
|
|
|
...pick(this.filters, ['status', 'pageSize', 'toPage', 'sort']),
|
|
|
|
filters: this.filters.filters.map(e => pick(e, ['field', 'active', 'mode', 'values']))
|
|
|
|
},
|
|
|
|
keys: this.keys.map(e => pick(e, ['id', 'active']))
|
|
|
|
};
|
|
|
|
this.storage.set('samplesPreferences', store);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadPreferences() {
|
|
|
|
const store: any = this.storage.get('samplesPreferences');
|
|
|
|
if (store) {
|
|
|
|
this.filters = {...this.filters, ...pick(store.filters, ['status', 'pageSize', 'toPage', 'sort'])};
|
|
|
|
store.filters.filters.forEach(filter => {
|
|
|
|
const filterIndex = this.filters.filters.findIndex(e => e.field === filter.field);
|
|
|
|
if (filterIndex >= 0) {
|
|
|
|
this.filters.filters[filterIndex] = {...this.filters.filters[filterIndex], ...filter};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
store.keys.forEach(key => {
|
|
|
|
const keyIndex = this.keys.findIndex(e => e.id === key.id);
|
|
|
|
if (keyIndex >= 0) {
|
|
|
|
this.keys[keyIndex].active = key.active;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-14 14:29:17 +02:00
|
|
|
this.calcFieldSelectKeys();
|
|
|
|
this.updateActiveKeys();
|
2020-08-12 17:38:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 12:37:27 +02:00
|
|
|
// TODO: avoid reloading
|
|
|
|
resetPreferences() {
|
|
|
|
this.storage.remove('samplesPreferences');
|
|
|
|
this.window.location.reload();
|
|
|
|
}
|
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
updateFilterFields(field) {
|
|
|
|
const filter = this.filters.filters.find(e => e.field === field);
|
2020-09-02 14:11:00 +02:00
|
|
|
console.log(filter);
|
2020-08-27 13:56:55 +02:00
|
|
|
filter.active = !(filter.values.length === 1 && filter.values[0] === '');
|
2020-07-13 10:52:10 +02:00
|
|
|
}
|
|
|
|
|
2020-06-26 11:09:59 +02:00
|
|
|
setSort(string) {
|
|
|
|
this.filters.sort = string;
|
|
|
|
this.loadSamples({firstPage: true});
|
|
|
|
}
|
2020-07-13 10:52:10 +02:00
|
|
|
|
2020-07-22 10:45:34 +02:00
|
|
|
updateActiveKeys() { // array with all activeKeys
|
|
|
|
this.activeKeys = this.keys.filter(e => e.active);
|
2020-08-24 12:43:39 +02:00
|
|
|
this.filters.filters.forEach(filter => { // disable filters of fields not displayed
|
2020-08-14 14:29:17 +02:00
|
|
|
if (!this.isActiveKey[filter.field]) {
|
|
|
|
filter.active = false;
|
|
|
|
}
|
|
|
|
});
|
2020-07-30 11:33:56 +02:00
|
|
|
this.activeTemplateKeys.material = this.keys
|
|
|
|
.filter(e => e.id.indexOf('material.properties.') >= 0 && e.active)
|
|
|
|
.map(e => e.id.split('.')
|
|
|
|
.map(el => decodeURIComponent(el)));
|
2020-08-14 14:29:17 +02:00
|
|
|
this.activeTemplateKeys.condition = this.keys.filter(e => e.id.indexOf('condition.') >= 0 && e.active)
|
|
|
|
.map(e => e.id.split('.')
|
|
|
|
.map(el => decodeURIComponent(el)));
|
2020-07-30 11:33:56 +02:00
|
|
|
this.activeTemplateKeys.measurements = this.keys.filter(e => e.id.indexOf('measurements.') >= 0 && e.active)
|
|
|
|
.map(e => e.id.split('.')
|
2020-07-30 14:23:51 +02:00
|
|
|
.map(el => decodeURIComponent(el)));
|
2020-07-13 10:52:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 10:45:34 +02:00
|
|
|
calcFieldSelectKeys() {
|
|
|
|
this.keys.forEach(key => {
|
|
|
|
this.isActiveKey[key.id] = key.active;
|
|
|
|
});
|
2020-07-13 10:52:10 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
sampleDetails(id: string, modal: TemplateRef<any>) {
|
|
|
|
this.sampleDetailsSample = null;
|
|
|
|
this.api.get<SampleModel>('/sample/' + id, data => {
|
|
|
|
this.sampleDetailsSample = new SampleModel().deserialize(data);
|
|
|
|
if (data.notes.custom_fields) { // convert custom_fields for more optimized display
|
|
|
|
this.sampleDetailsSample.notes.custom_fields_entries = Object.entries(this.sampleDetailsSample.notes.custom_fields);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sampleDetailsSample.custom_fields_entries = [];
|
|
|
|
}
|
2020-08-12 11:06:46 +02:00
|
|
|
if (Object.keys(data.condition).length) {
|
|
|
|
this.sampleDetailsSample.condition_entries = Object.entries(omit(this.sampleDetailsSample.condition, ['condition_template']))
|
|
|
|
.map(e => {
|
|
|
|
e[0] = `${this.ucFirst(this.d.id.conditionTemplates[this.sampleDetailsSample.condition.condition_template].name)} ${e[0]}`;
|
|
|
|
return e;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sampleDetailsSample.condition_entries = [];
|
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
this.sampleDetailsSample.measurement_entries = [];
|
|
|
|
this.sampleDetailsSample.measurements.forEach(measurement => { // convert measurements for more optimized display without dpt
|
|
|
|
const name = this.d.id.measurementTemplates[measurement.measurement_template].name;
|
|
|
|
this.sampleDetailsSample.measurement_entries.push(...Object.entries(measurement.values).filter(e => e[0] !== 'dpt')
|
2020-08-12 11:06:46 +02:00
|
|
|
.map(e => ({name: this.ucFirst(name) + ' ' + e[0], value: e[1]})));
|
2020-08-06 08:18:57 +02:00
|
|
|
});
|
|
|
|
new Promise(resolve => {
|
|
|
|
if (data.notes.sample_references.length) { // load referenced samples if available
|
|
|
|
let loadingCounter = data.notes.sample_references.length;
|
|
|
|
this.sampleDetailsSample.notes.sample_references.forEach(reference => {
|
|
|
|
this.api.get<SampleModel>('/sample/' + reference.sample_id, rData => {
|
|
|
|
reference.number = rData.number;
|
|
|
|
loadingCounter --;
|
|
|
|
if (!loadingCounter) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
this.modalService.open(modal).then(() => {});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
validate() {
|
2020-08-21 16:11:57 +02:00
|
|
|
if (this.sampleSelect) {
|
2020-08-06 08:18:57 +02:00
|
|
|
this.samples.forEach(sample => {
|
2020-08-21 16:11:57 +02:00
|
|
|
if (sample.selected) {
|
2020-08-06 08:18:57 +02:00
|
|
|
this.api.put('/sample/validate/' + sample._id);
|
|
|
|
}
|
|
|
|
});
|
2020-08-07 10:49:18 +02:00
|
|
|
this.loadSamples();
|
2020-08-21 16:11:57 +02:00
|
|
|
this.sampleSelect = 0;
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-08-21 16:11:57 +02:00
|
|
|
this.sampleSelect = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
batchEdit() {
|
|
|
|
if (this.sampleSelect) {
|
|
|
|
this.router.navigate(['/samples/edit/' + this.samples.filter(e => e.selected).map(e => e._id).join(',')]);
|
|
|
|
this.sampleSelect = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.sampleSelect = 1;
|
2020-08-06 08:18:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:49:18 +02:00
|
|
|
restoreSample(id, modal, event) {
|
|
|
|
this.stopPropagation(event);
|
|
|
|
this.modalService.open(modal).then(res => {
|
|
|
|
if (res) {
|
|
|
|
this.api.put('/sample/restore/' + id, {}, ignore => {
|
|
|
|
this.samples.find(e => e._id === id).status = 'new';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
selectAll(event) {
|
|
|
|
this.samples.forEach(sample => {
|
2020-08-07 10:49:18 +02:00
|
|
|
if (sample.status !== 'deleted') {
|
2020-08-21 16:11:57 +02:00
|
|
|
sample.selected = event.target.checked;
|
2020-08-07 10:49:18 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-08-21 16:11:57 +02:00
|
|
|
sample.selected = false;
|
2020-08-07 10:49:18 +02:00
|
|
|
}
|
2020-08-06 08:18:57 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
preventDefault(event, key = 'all') {
|
|
|
|
if (key === 'all' || event.key === key) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 08:18:57 +02:00
|
|
|
stopPropagation(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2020-07-13 10:52:10 +02:00
|
|
|
clipboard() {
|
|
|
|
this.linkarea.nativeElement.select();
|
|
|
|
this.linkarea.nativeElement.setSelectionRange(0, 99999);
|
|
|
|
document.execCommand('copy');
|
|
|
|
}
|
|
|
|
|
|
|
|
ucFirst(string) {
|
|
|
|
return string[0].toUpperCase() + string.slice(1);
|
|
|
|
}
|
2020-08-12 17:38:12 +02:00
|
|
|
|
|
|
|
|
2020-05-22 09:36:50 +02:00
|
|
|
}
|