blob: 927f7561f434e455eb3fe7d6869916bf00f836e8 [file] [log] [blame]
Sean Condon28ecc5f2018-06-25 12:50:16 +01001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the 'License');
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an 'AS IS' BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { Pipe, PipeTransform } from '@angular/core';
17import { TableFilter } from './table.base';
18
19/**
20 * Only return the tabledata that matches filtering with some queries
21 *
22 * Note: the pipe is marked pure here as we need to filter on the
23 * content of the filter object (it's not a primitive type)
24 */
25@Pipe({
26 name: 'filter',
27 pure: false
28})
29export class TableFilterPipe implements PipeTransform {
30
31 /**
32 * From an array of table items just return those that match the filter
33 */
34 transform(items: any[], tableDataFilter: TableFilter): any[] {
35 if (!items) {
36 return [];
37 }
38 if (!tableDataFilter.queryStr) {
39 return items;
40 }
41
42 const queryStr = tableDataFilter.queryStr.toLowerCase();
43
44 return items.filter( it => {
45 if (tableDataFilter.queryBy === '$') {
46 const t1 = Object.values(it);
47 const t2 = Object.values(it).filter(value => {
Sean Condonf20b8ef2019-08-13 16:45:52 +010048 return JSON.stringify(value).toLowerCase().includes(queryStr);
Sean Condon28ecc5f2018-06-25 12:50:16 +010049 });
50 return Object.values(it).filter(value => {
Sean Condonf20b8ef2019-08-13 16:45:52 +010051 return JSON.stringify(value).toLowerCase().includes(queryStr);
Sean Condon28ecc5f2018-06-25 12:50:16 +010052 }).length > 0;
53 } else {
54 return it[tableDataFilter.queryBy].toLowerCase().includes(queryStr);
55 }
56 });
57 }
58}