2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 import {Component, Input, ViewEncapsulation} from "@angular/core";
22 import {GabService, IServerResponse} from "../../../services/gab.service";
23 import {PathsAndNamesDefinition} from "../../../../models/paths-and-names";
25 const COLUMN_PREFIX: string = 'col';
29 templateUrl: './generic-artifact-browser.component.html',
30 styleUrls: ['./generic-artifact-browser.component.less'],
31 encapsulation: ViewEncapsulation.None
33 export class GenericArtifactBrowserComponent {
35 pathsandnames: PathsAndNamesDefinition[];
41 columns: ColumnDefinition[];
42 originColumns: ColumnDefinition[];
47 columnsFilters: Map<string, string>;
48 addNewColumn: boolean;
50 constructor(private gabService: GabService) {
55 this.isLoading = true;
61 this.addNewColumn = false;
62 this.columnsFilters = new Map<string, string>();
63 let paths: string[] = this.pathsandnames.map(item => item.path);
64 this.gabService.getArtifact(this.artifactid, this.resourceid, paths)
67 // let typedServerResponse: IServerResponse = response.json() as IServerResponse;
68 this.normalizeDataForNgxDatatable(response['data']);
72 this.isLoading = false;
76 this.isLoading = false;
85 canBeDeleted(name: string){
86 return this.originColumns.filter(function(column){
87 return column.name === name;
91 deleteColumn(col: ColumnDefinition) {
92 this.pathsandnames = this.pathsandnames.filter(function(pathandname){
93 return pathandname.friendlyName != col.name;
95 this.columns = this.columns.filter(function(column){
101 this.addNewColumn = false;
105 this.addNewColumn = true;
108 isSearchable(columnName){
109 let selectedPathAndName = this.pathsandnames.filter(function(pathandname){
110 return pathandname.friendlyName === columnName;
112 return selectedPathAndName.searchable;
115 updateColumnFilter(event, column) {
116 const val = event.target.value.toLowerCase();
117 this.columnsFilters.set(column, val);
118 let temp_rows = [...this.originRows];
120 this.columnsFilters.forEach((value: string, key: string) => {
121 temp_rows = this.updateSingleColumnFilter(value, key, temp_rows);
125 this.rows = temp_rows
128 private updateSingleColumnFilter(value, column, rows) {
129 return rows.filter(function (obj) {
130 const row = obj[column];
131 return row !== undefined && row.toLowerCase().indexOf(value) !== -1 || !value;
135 private normalizeDataForNgxDatatable(data: [{ [key: string]: string }]) {
136 let result: NormalizationResult = this.getNormalizationResult(data, this.pathsandnames);
137 this.rows = result.rows;
138 this.originRows = result.rows;
139 this.columns = result.columns;
140 if (!this.originColumns){
141 this.originColumns = [...result.columns];
145 private getNormalizationResult(data: [{ [key: string]: string }],
146 pathsAndNames: PathsAndNamesDefinition[]): NormalizationResult {
147 //Prepare column names and column data property names
148 let mappingsPathToProp = new Map<string, string>();
149 let columnsDefinitions = this.normalizeColumns(pathsAndNames, mappingsPathToProp);
151 //Convert rows from { "string": "string" } to { prop : "string" } format
152 //This is required by NgxDatatable component
153 let arrayOfRows = this.normalizeRows(data, mappingsPathToProp);
155 return new NormalizationResult(arrayOfRows, columnsDefinitions);
158 private normalizeColumns(pathsAndNames: PathsAndNamesDefinition[], mappingsPathToProp: Map<string, string>) {
159 let columnsDefinitions: ColumnDefinition[] = [];
160 let index: number = 1;
162 pathsAndNames.forEach(function (col) {
163 let columnDataPropertyName: string = COLUMN_PREFIX + index;
164 mappingsPathToProp.set(col.path, columnDataPropertyName);
165 let cell: ColumnDefinition = new ColumnDefinition(col.friendlyName, columnDataPropertyName);
166 columnsDefinitions.push(cell);
169 return columnsDefinitions;
172 private normalizeRows(data: [{ [key: string]: string }], mappingsPathToProp: Map<string, string>) {
173 let arrayOfRows = [];
174 data.forEach(function (col) {
176 for (let key in col) {
177 if (col.hasOwnProperty(key)) {
178 let columnNameAsProp = mappingsPathToProp.get(key);
179 row[columnNameAsProp] = col[key];
182 arrayOfRows.push(row);
189 class NormalizationResult {
190 constructor(public rows: any[], public columns: ColumnDefinition[]) {
194 export class ColumnDefinition {
195 constructor(public name: string, public prop: string) {