1/** 2 * Copyright (C) 2018 The Android Open Source Project 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 { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; 17import { FilterItem } from '../../model/filter_item'; 18import { FilterCondition } from '../../model/filter_condition'; 19 20@Component({ 21 selector: 'app-filter', 22 templateUrl: './filter.component.html', 23 styleUrls: ['./filter.component.scss'] 24}) 25export class FilterComponent implements OnInit { 26 currentFilter: FilterItem; 27 applyingFilters: FilterItem[] = []; 28 applyingFilterChanged = false; 29 appliedFilters: FilterItem[] = []; 30 selectorList: string[]; 31 32 filterMethods = [ 33 {value: FilterCondition.EqualTo, text: 'is equal to', sign: '='}, 34 {value: FilterCondition.LessThan, text: 'is less than', sign: '<'}, 35 {value: FilterCondition.GreaterThan, text: 'is greater than', sign: '>'}, 36 {value: FilterCondition.LessThanOrEqualTo, text: 'is less than or equal to', sign: '<='}, 37 {value: FilterCondition.GreaterThanOrEqualTo, text: 'is greater than or equal to', sign: '>='}, 38 {value: FilterCondition.NotEqualTo, text: 'is not equal to', sign: '!='}, 39 {value: FilterCondition.Has, text: 'has', sign: 'has'}, 40 ]; 41 42 @Output() applyFilters = new EventEmitter(); 43 @Input() disabled: boolean; 44 45 panelOpenState = false; 46 47 ngOnInit(): void { 48 this.currentFilter = new FilterItem(); 49 this.currentFilter.value = ''; 50 } 51 52 /** Sets a filter key list with the given class. */ 53 setSelectorList(typeOfClass: any) { 54 const instance = new typeOfClass(); 55 this.selectorList = Object.getOwnPropertyNames(instance); 56 } 57 58 /** Adds the current filter to the list of filters to be applied. */ 59 addFilter() { 60 this.applyingFilters.push(this.currentFilter); 61 this.currentFilter = new FilterItem(); 62 this.currentFilter.value = ''; 63 this.applyingFilterChanged = true; 64 } 65 66 /** Clears the current filter. */ 67 clearCurrentFilter() { 68 this.currentFilter.key = undefined; 69 this.currentFilter.method = undefined; 70 this.currentFilter.value = ''; 71 } 72 73 /** Removes the selected filter from the list of filters to be applied. */ 74 removed(filter: FilterItem) { 75 const index = this.applyingFilters.indexOf(filter); 76 if (index >= 0) { 77 this.applyingFilters.splice(index, 1); 78 this.applyingFilterChanged = true; 79 } 80 } 81 82 /** Gets a filter sign with method value. */ 83 getSign(filter: FilterItem) { 84 return this.filterMethods.find((x) => x.value === filter.method).sign; 85 } 86 87 /** Applies the list of filters. */ 88 onApplyClicked() { 89 this.applyFilters.emit(this.applyingFilters); 90 this.appliedFilters = this.applyingFilters.slice(); 91 this.applyingFilterChanged = false; 92 } 93 94 /** Cancels the current changes and roll back to the last applied filters. */ 95 onCancelChangesClicked() { 96 this.applyingFilters = this.appliedFilters.slice(); 97 this.applyingFilterChanged = false; 98 } 99 100 /** Reset all filters. */ 101 onClearAllClicked() { 102 this.applyingFilters = []; 103 this.appliedFilters = []; 104 this.applyFilters.emit(this.appliedFilters); 105 this.applyingFilterChanged = false; 106 } 107} 108