blob: 9c60d4f230d5b9157879841f99242abf21fd00ac [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import { Component } from '@angular/core';
import { AgFilterComponent } from 'ag-grid-angular';
import { IDoesFilterPassParams, IFilterParams, RowDataChangedEvent, RowNode } from 'ag-grid-community';
import { convertEmptyValueToNull } from '@grid-failure-information-app/shared/utility/utilityHelpers';
@Component({
selector: 'filter-cell',
templateUrl: './set-filter.component.html',
styleUrls: ['./set-filter.component.css'],
})
export class SetFilterComponent implements AgFilterComponent {
public setItems: any = {};
public gridHeight: number;
public filterText: string = '';
public selectAllChecked: boolean = true;
private _filteredItems: any = {};
private _params: IFilterParams;
private _valueGetter: (rowNode: RowNode) => any;
private _filterId: string = '';
filterCheckboxList(text: string) {
text
.toLowerCase()
.split(' ')
.forEach(filterWord => {
for (var itemKey in this.setItems) {
if (!this._contains(itemKey, filterWord)) {
this._filteredItems[itemKey] = this.setItems[itemKey];
delete this.setItems[itemKey];
} else {
for (var filteredItem in this._filteredItems) {
if (this._contains(filteredItem, filterWord)) {
this.setItems[filteredItem] = this._filteredItems[filteredItem];
}
}
}
}
if (this.filterText.length !== 0) {
this.selectAllChecked = false;
} else {
this.selectAll();
}
});
this._params.filterChangedCallback();
}
private _contains(sourceText: string, subString: string): boolean {
return sourceText.toString().toLowerCase().indexOf(subString) >= 0;
}
notifyFilter(event: Event) {
let selectedFilter = event.target['id'];
this.setItems[selectedFilter].checked = event.target['checked'];
let allSelected: boolean = this._areAllFilterItemsSelected();
this.selectAllChecked = allSelected && !!this.filterText && this.filterText.length === 0;
this._params.filterChangedCallback({ filterId: this._filterId });
}
isFilterActive(): boolean {
this.selectAllChecked = this._areAllFilterItemsSelected();
return !this.selectAllChecked;
}
doesFilterPass(params: IDoesFilterPassParams): boolean {
let nodeValue = this._valueGetter(params.node);
nodeValue = !!nodeValue ? nodeValue : null; //Set to null if undefined, null or ''
let passed: boolean = false;
for (let itemKey in this.setItems) {
if (isNaN(nodeValue)) {
passed = itemKey === 'null' ? !nodeValue || nodeValue === '' : !!nodeValue && nodeValue.includes(itemKey);
} else {
// is numeric or null
if (!nodeValue) {
passed = nodeValue === null && itemKey === 'null';
} else {
passed = nodeValue.toString() === itemKey;
}
}
passed = passed && this.setItems[itemKey].checked;
if (passed) return passed;
}
return passed;
}
getModel(): any {
return { values: this.setItems };
}
setModel(model: any): void {
this.setItems = model ? model.values : [];
this.selectAllChecked = !model;
}
afterGuiAttached(params?: any) {
this._setFilterItems(params);
}
agInit(params: IFilterParams): void {
this._params = params;
this._filterId = params.colDef.colId;
this._valueGetter = params.valueGetter;
this._setFilterItems(params);
this._params.api['gridOptionsWrapper'].gridOptions = {
...this._params.api['gridOptionsWrapper'].gridOptions,
onGridSizeChanged: (event: any) => {
this.gridHeight = event.clientHeight;
},
onRowDataChanged: (event: RowDataChangedEvent) => {
this.filterText = null;
this._setFilterItems(params);
},
};
}
private _setFilterItems(params: IFilterParams) {
const allGridRowNodes = (this._params.rowModel as any).nodeManager.allNodesMap || {};
const oldFilterItems = {};
Object.assign(oldFilterItems, this.setItems);
this.setItems = {};
for (const allRowsKey in Object.keys(allGridRowNodes)) {
const rowNode = allGridRowNodes[allRowsKey];
if (this._params.doesRowPassOtherFilter(rowNode)) {
let filterItem = convertEmptyValueToNull(this._valueGetter(rowNode));
const valueSeparator: string = this._params.colDef['valueSeparator'];
if (!!filterItem && !!valueSeparator) {
const filterItems: string[] = filterItem.split(valueSeparator);
filterItems.forEach(item => {
const isFilterItemInOldFilter = oldFilterItems.hasOwnProperty(item);
let checked: boolean = (!isFilterItemInOldFilter && this.selectAllChecked) || (isFilterItemInOldFilter && oldFilterItems[item]['checked'] === true);
this.setItems[item] = { visible: true, checked: checked };
});
continue;
}
const isFilterItemInOldFilter = oldFilterItems.hasOwnProperty(filterItem);
let checked: boolean =
(!isFilterItemInOldFilter && this.selectAllChecked) || (isFilterItemInOldFilter && oldFilterItems[filterItem]['checked'] === true);
this.setItems[filterItem] = { visible: true, checked: checked };
}
}
this.selectAllChecked = this._areAllFilterItemsSelected();
this._params.filterChangedCallback();
}
public selectAll(event?: Event) {
this.selectAllChecked = event ? event.target['checked'] : true;
this.filterText = '';
for (var itemKey in this._filteredItems) {
this.setItems[itemKey] = this._filteredItems[itemKey];
}
for (var itemKey in this.setItems) {
if (this.setItems.hasOwnProperty(itemKey)) {
this.setItems[itemKey].checked = this.selectAllChecked;
}
}
this._params.filterChangedCallback();
}
private _areAllFilterItemsSelected(): boolean {
for (var itemKey in this.setItems) {
if (!this.setItems[itemKey].checked) {
return false;
}
}
return true;
}
constructor() {}
}