695d782a1538a4c140a3942b549eccf3b9d19105
[sdc.git] /
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, Input, ViewEncapsulation} from "@angular/core";
22 import {GabService, IServerResponse} from "../../../services/gab.service";
23 import {PathsAndNamesDefinition} from "../../../../models/paths-and-names";
24
25 const COLUMN_PREFIX: string = 'col';
26
27 @Component({
28     selector: 'gab',
29     templateUrl: './generic-artifact-browser.component.html',
30     styleUrls:['./generic-artifact-browser.component.less'],
31     encapsulation: ViewEncapsulation.None
32 })
33 export class GenericArtifactBrowserComponent {
34     @Input()
35     pathsandnames: PathsAndNamesDefinition[];
36     @Input()
37     artifactid: string;
38     @Input()
39     resourceid: string;
40
41     columns: ColumnDefinition[];
42     rows: any[];
43     originRows: any[];
44     selectedRows: any[];
45     isLoading: boolean;
46     ready: boolean;
47     columnsFilters: Map<string, string>;
48
49     constructor(private gabService: GabService) {
50     }
51
52     ngOnInit() {
53         this.ready = false;
54         this.isLoading = true;
55         this.columns = [];
56         this.columnsFilters = new Map<string,string>();
57         let paths: string[] = this.pathsandnames.map(item => item.path);
58         this.gabService.getArtifact(this.artifactid, this.resourceid, paths)
59         .subscribe(
60             response => {
61             let typedServerResponse:IServerResponse = <IServerResponse>response.json();
62             this.normalizeDataForNgxDatatable(typedServerResponse.data);
63             },
64             err => console.log(err),
65             () => {
66                 this.ready = true;
67                 this.isLoading = false;
68             }
69         );
70     }
71
72   updateColumnFilter(event, column) {
73     const val = event.target.value.toLowerCase();
74     this.columnsFilters.set(column, val);
75     let temp_rows = [...this.originRows];
76
77     this.columnsFilters.forEach((value: string, key: string) => {
78       temp_rows = this.updateSingleColumnFilter(value, key, temp_rows);
79     });
80
81     // update the rows
82     this.rows = temp_rows
83   }
84
85   private updateSingleColumnFilter(value, column, rows) {
86     return rows.filter(function(obj) {
87       const row = obj[column];
88       return row !== undefined && row.toLowerCase().indexOf(value) !== -1 || !value;
89     });
90   }
91
92     private normalizeDataForNgxDatatable(data: [{ [key: string]: string }]) {
93         let result: NormalizationResult = this.getNormalizationResult(data, this.pathsandnames);
94         this.rows = result.rows;
95         this.originRows = result.rows;
96         this.columns = result.columns;
97     }
98
99     private getNormalizationResult(data: [{ [key: string]: string }],
100                                    pathsAndNames: PathsAndNamesDefinition[]): NormalizationResult {
101         //Prepare column names and column data property names
102         let mappingsPathToProp = new Map<string,string>();
103         let columnsDefinitions = this.normalizeColumns(pathsAndNames, mappingsPathToProp);
104
105         //Convert rows from { "string": "string" } to { prop : "string" } format
106         //This is required by NgxDatatable component
107         let arrayOfRows = this.normalizeRows(data, mappingsPathToProp);
108
109         return new NormalizationResult(arrayOfRows, columnsDefinitions);
110     }
111
112     private normalizeColumns(pathsAndNames: PathsAndNamesDefinition[], mappingsPathToProp: Map<string,string>) {
113         let columnsDefinitions: ColumnDefinition[] = [];
114         let index: number = 1;
115
116         pathsAndNames.forEach(function (col) {
117             let columnDataPropertyName: string = COLUMN_PREFIX + index;
118             mappingsPathToProp.set(col.path, columnDataPropertyName);
119             let cell: ColumnDefinition = new ColumnDefinition(col.friendlyName, columnDataPropertyName);
120             columnsDefinitions.push(cell);
121             index += 1;
122         });
123         return columnsDefinitions;
124     }
125
126     private normalizeRows(data: [{ [key: string]: string }], mappingsPathToProp: Map<string,string>) {
127         let arrayOfRows = [];
128         data.forEach(function (col) {
129             let row = {};
130             for (let key in col) {
131                 if (col.hasOwnProperty(key)) {
132                     let columnNameAsProp = mappingsPathToProp.get(key);
133                     row[columnNameAsProp] = col[key];
134                 }
135             }
136             arrayOfRows.push(row);
137         });
138
139         return arrayOfRows;
140     }
141 }
142
143 class NormalizationResult {
144     constructor(public rows: any[], public columns: ColumnDefinition[]) {}
145 }
146
147 export class ColumnDefinition {
148     constructor(public name: string, public prop: string) {}
149 }
150