summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.ts
blob: 0f450ce2a57c84a8409b95e4c698368c746e019a (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import {
  Component,
  EventEmitter,
  Input,
  OnChanges,
  OnInit,
  Output,
  ViewChild
} from '@angular/core';

import _ from 'lodash';

import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
import { CdTableColumn } from '~/app/shared/models/cd-table-column';
import { CdDatePipe } from '~/app/shared/pipes/cd-date.pipe';
import { TableComponent } from '../table/table.component';

interface KeyValueItem {
  key: string;
  value: any;
}

/**
 * Display the given data in a 2 column data table. The left column
 * shows the 'key' attribute, the right column the 'value' attribute.
 * The data table has the following characteristics:
 * - No header and footer is displayed
 * - The relation of the width for the columns 'key' and 'value' is 1:3
 * - The 'key' column is displayed in bold text
 */
@Component({
  selector: 'cd-table-key-value',
  templateUrl: './table-key-value.component.html',
  styleUrls: ['./table-key-value.component.scss']
})
export class TableKeyValueComponent implements OnInit, OnChanges {
  @ViewChild(TableComponent, { static: true })
  table: TableComponent;

  @Input()
  data: any;
  @Input()
  autoReload: any = 5000;
  @Input()
  renderObjects = false;
  // Only used if objects are rendered
  @Input()
  appendParentKey = true;
  @Input()
  hideEmpty = false;
  @Input()
  hideKeys: string[] = []; // Keys of pairs not to be displayed

  // If set, the classAddingTpl is used to enable different css for different values
  @Input()
  customCss?: { [css: string]: number | string | ((any: any) => boolean) };

  columns: Array<CdTableColumn> = [];
  tableData: KeyValueItem[];

  /**
   * The function that will be called to update the input data.
   */
  @Output()
  fetchData = new EventEmitter();

  constructor(private datePipe: CdDatePipe) {}

  ngOnInit() {
    this.columns = [
      {
        prop: 'key',
        flexGrow: 1,
        cellTransformation: CellTemplate.bold
      },
      {
        prop: 'value',
        flexGrow: 3
      }
    ];
    if (this.customCss) {
      this.columns[1].cellTransformation = CellTemplate.classAdding;
    }
    // We need to subscribe the 'fetchData' event here and not in the
    // HTML template, otherwise the data table will display the loading
    // indicator infinitely if data is only bound via '[data]="xyz"'.
    // See for 'loadingIndicator' in 'TableComponent::ngOnInit()'.
    if (this.fetchData.observers.length > 0) {
      this.table.fetchData.subscribe(() => {
        // Forward event triggered by the 'cd-table' data table.
        this.fetchData.emit();
      });
    }
    this.useData();
  }

  ngOnChanges() {
    this.useData();
  }

  useData() {
    if (!this.data) {
      return; // Wait for data
    }
    let pairs = this.makePairs(this.data);
    if (this.hideKeys) {
      pairs = pairs.filter((pair) => !this.hideKeys.includes(pair.key));
    }
    this.tableData = pairs;
  }

  private makePairs(data: any): KeyValueItem[] {
    let result: KeyValueItem[] = [];
    if (!data) {
      return undefined; // Wait for data
    } else if (_.isArray(data)) {
      result = this.makePairsFromArray(data);
    } else if (_.isObject(data)) {
      result = this.makePairsFromObject(data);
    } else {
      throw new Error('Wrong data format');
    }
    result = result
      .map((item) => {
        item.value = this.convertValue(item.value);
        return item;
      })
      .filter((i) => i.value !== null);
    return _.sortBy(this.renderObjects ? this.insertFlattenObjects(result) : result, 'key');
  }

  private makePairsFromArray(data: any[]): KeyValueItem[] {
    let temp: any[] = [];
    const first = data[0];
    if (_.isArray(first)) {
      if (first.length === 2) {
        temp = data.map((a) => ({
          key: a[0],
          value: a[1]
        }));
      } else {
        throw new Error(
          `Array contains too many elements (${first.length}). ` +
            `Needs to be of type [string, any][]`
        );
      }
    } else if (_.isObject(first)) {
      if (_.has(first, 'key') && _.has(first, 'value')) {
        temp = [...data];
      } else {
        temp = data.reduce(
          (previous: any[], item) => previous.concat(this.makePairsFromObject(item)),
          temp
        );
      }
    }
    return temp;
  }

  private makePairsFromObject(data: any): KeyValueItem[] {
    return Object.keys(data).map((k) => ({
      key: k,
      value: data[k]
    }));
  }

  private insertFlattenObjects(data: KeyValueItem[]): any[] {
    return _.flattenDeep(
      data.map((item) => {
        const value = item.value;
        const isObject = _.isObject(value);
        if (!isObject || _.isEmpty(value)) {
          if (isObject) {
            item.value = '';
          }
          return item;
        }
        return this.splitItemIntoItems(item);
      })
    );
  }

  /**
   * Split item into items will call _makePairs inside _makePairs (recursion), in oder to split
   * the object item up into items as planned.
   */
  private splitItemIntoItems(data: { key: string; value: object }): KeyValueItem[] {
    return this.makePairs(data.value).map((item) => {
      if (this.appendParentKey) {
        item.key = data.key + ' ' + item.key;
      }
      return item;
    });
  }

  private convertValue(value: any): KeyValueItem {
    if (_.isArray(value)) {
      if (_.isEmpty(value) && this.hideEmpty) {
        return null;
      }
      value = value.map((item) => (_.isObject(item) ? JSON.stringify(item) : item)).join(', ');
    } else if (_.isObject(value)) {
      if ((this.hideEmpty && _.isEmpty(value)) || !this.renderObjects) {
        return null;
      }
    } else if (_.isString(value)) {
      if (value === '' && this.hideEmpty) {
        return null;
      }
      if (this.isDate(value)) {
        value = this.datePipe.transform(value) || value;
      }
    }

    return value;
  }

  private isDate(s: string) {
    const sep = '[ -:.TZ]';
    const n = '\\d{2}' + sep;
    //                            year     -    m - d - h : m : s . someRest  Z (if UTC)
    return s.match(new RegExp('^\\d{4}' + sep + n + n + n + n + n + '\\d*' + 'Z?$'));
  }
}