Dynamic columns in GAB table
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / generic-artifact-browser / generic-artifact-browser-column-provider.component.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import {Component, EventEmitter, Input, Output, ViewChild, ViewEncapsulation} from "@angular/core";
22 import {PathsAndNamesDefinition} from "../../../../models/paths-and-names";
23
24 @Component({
25   selector: 'gab-column-provider',
26   templateUrl: './generic-artifact-browser-column-provider.component.html',
27   styleUrls: ['./generic-artifact-browser-column-provider.component.less'],
28   encapsulation: ViewEncapsulation.None
29 })
30 export class GenericArtifactBrowserColumnProviderComponent {
31   @Input()
32   pathsAndNames: PathsAndNamesDefinition[];
33
34   @Output()
35   onCancel = new EventEmitter();
36   @Output()
37   onSave = new EventEmitter();
38
39   @ViewChild('generalForm') generalForm;
40   name: string;
41   path: string;
42
43   constructor() {}
44
45   checkNameDuplications(event) {
46     const tmp = event.target.value;
47     if (tmp && !this.columnsContainsName(tmp)) {
48       this.name = tmp;
49       this.generalForm.form.controls['name'].setErrors(null);
50     } else {
51       this.generalForm.form.controls['name'].setErrors({incorrect: true});
52     }
53   }
54
55   checkPathDuplications(event) {
56     const tmp = event.target.value;
57     if (tmp && !this.columnsContainsPath(tmp)) {
58       this.path = tmp;
59       this.generalForm.form.controls['path'].setErrors(null);
60     } else {
61       this.generalForm.form.controls['path'].setErrors({incorrect: true});
62     }
63   }
64
65   cancelAddingNewColumn() {
66     this.onCancel.emit();
67   }
68
69   saveNewColumn() {
70     this.onSave.emit();
71   }
72
73   addColumn() {
74     this.updateColumnFilter(this.name, this.path);
75     this.saveNewColumn();
76   }
77
78   private updateColumnFilter = (name: string, prop: string): void => {
79     this.pathsAndNames.push(new PathsAndNamesDefinition(prop, name));
80     this.generalForm.form.controls['name'].setValue("");
81     this.generalForm.form.controls['path'].setValue("");
82   }
83
84   private columnsContainsName = (name: string): boolean => {
85     const columnDefinitions = this.pathsAndNames.filter(column => column.friendlyName.toLowerCase() === name.toLowerCase());
86     return columnDefinitions.length > 0;
87   }
88
89   private columnsContainsPath = (path: string): boolean => {
90     const columnDefinitions = this.pathsAndNames.filter(column => column.path.toLowerCase() === path.toLowerCase());
91     return columnDefinitions.length > 0;
92   }
93
94 }