Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | import { Pipe, PipeTransform } from '@angular/core'; |
| 17 | import { 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 | }) |
| 29 | export 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 === '$') { |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 46 | const t1 = (<any>Object).values(it); |
| 47 | const t2 = (<any>Object).values(it).filter(value => { |
| 48 | return JSON.stringify(value).toLowerCase().indexOf(queryStr) !== -1; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 49 | }); |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 50 | return (<any>Object).values(it).filter(value => { |
| 51 | return JSON.stringify(value).toLowerCase().indexOf(queryStr) !== -1; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 52 | }).length > 0; |
| 53 | } else { |
| 54 | return it[tableDataFilter.queryBy].toLowerCase().includes(queryStr); |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | } |