-
Notifications
You must be signed in to change notification settings - Fork 66
/
dynamicConstraints.component.ts
115 lines (104 loc) · 4.17 KB
/
dynamicConstraints.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*******************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*******************************************************************************/
import { WineryDynamicTableMetadata } from '../../wineryDynamicTableMetadata';
import { Component, OnInit } from '@angular/core';
import { DynamicFieldComponent } from '../dynamic-field/dynamicFieldComponent';
import { FormGroup } from '@angular/forms';
import { Constraint } from '../../../model/constraint';
@Component({
selector: 'winery-dynamic-constraints',
templateUrl: 'dynamicConstraints.component.html',
styleUrls: [
'dynamicConstraints.component.css'
],
})
export class DynamicConstraintsComponent implements DynamicFieldComponent, OnInit {
config: DynamicConstraintsData;
group: FormGroup;
ngOnInit(): void {
if (this.config.defaultValue) {
this.group.controls[this.config.key].setValue(this.config.defaultValue);
}
}
removeConstraint(clause: Constraint) {
const tmp = (this.currentConstraints as Constraint[])
.filter((item: any) => !(item.key === clause.key && item.value === clause.value));
this.group.controls[this.config.key].setValue(tmp);
}
addConstraint(clause: Constraint) {
if (clause && !this.alreadyExists(clause)) {
this.currentConstraints.push(clause);
}
}
alreadyExists(clause: Constraint) {
return this.currentConstraints
.find((item) => item.key === clause.key && item.value === clause.value);
}
toConstraint(key: string, value: string): Constraint {
let clauseList: string[] = [];
if (this.config.listConstraintsKeys.find((listKey) => listKey === key)) {
clauseList = value.split(',');
value = '';
} else if (this.config.rangeConstraintsKeys.find((listKey) => listKey === key)) {
clauseList = value.split(',');
if (clauseList.length !== 2) {
// range constraints can not contain more than 2 values
return undefined;
}
value = '';
}
return new Constraint(key, value, clauseList);
}
get currentConstraints(): Constraint[] {
if (!this.group.controls[this.config.key].value) {
this.group.controls[this.config.key].setValue([]);
}
return this.group.controls[this.config.key].value as Constraint[];
}
}
export class DynamicConstraintsData extends WineryDynamicTableMetadata<Constraint[]> {
controlType = 'constraints';
constructor(key: string,
label: string,
public possibleConstraintKeys: string[],
public listConstraintsKeys: string[],
public rangeConstraintsKeys: string[],
order?: number,
disabled?: boolean,
sortTableCol?: boolean,
isVisible?: boolean) {
super(key, label, order, undefined, disabled, sortTableCol, isVisible);
this.possibleConstraintKeys = possibleConstraintKeys || [];
this.listConstraintsKeys = listConstraintsKeys || [];
this.rangeConstraintsKeys = rangeConstraintsKeys || [];
}
// @override
toHumanReadable(value: Constraint[]): string {
let res = '';
let length = value.length;
for (const clause of value) {
res += clause.key + ': ';
if (clause.list && clause.list.length > 0) {
res += clause.list.toString();
} else {
res += clause.value;
}
length -= 1;
if (length > 0) {
res += '; ';
}
}
return res;
}
}