|
| 1 | +// This file is part of BenchExec, a framework for reliable benchmarking: |
| 2 | +// https://github.com/sosy-lab/benchexec |
| 3 | +// |
| 4 | +// SPDX-FileCopyrightText: 2019-2020 Dirk Beyer <https://www.sosy-lab.org> |
| 5 | +// |
| 6 | +// SPDX-License-Identifier: Apache-2.0 |
| 7 | + |
| 8 | +import React, { FC } from "react"; |
| 9 | +import FilterContainer from "./FilterContainer"; |
| 10 | +import TaskFilterCard from "./TaskFilterCard"; |
| 11 | +import { faClose, faTrash } from "@fortawesome/free-solid-svg-icons"; |
| 12 | +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; |
| 13 | +import equals from "deep-equal"; |
| 14 | +import { decodeFilter, isNil } from "../../utils/utils"; |
| 15 | +const classNames = require("classnames"); |
| 16 | + |
| 17 | +interface FilterBoxProps { |
| 18 | + filtered: any[]; |
| 19 | + ids: Record<string, any>; |
| 20 | + addTypeToFilter: (filter: any[]) => void; |
| 21 | + setFilter: (filter: any[], flag: boolean) => void; |
| 22 | + hide: () => void; |
| 23 | + visible: boolean; |
| 24 | + headerComponent?: JSX.Element; |
| 25 | + filterable: Array<{ name: string; columns: any[] }>; |
| 26 | + hiddenCols?: any[]; |
| 27 | +} |
| 28 | + |
| 29 | +interface FilterBoxState { |
| 30 | + filters: any[]; |
| 31 | + idFilters: (string | null)[]; |
| 32 | +} |
| 33 | + |
| 34 | +export default class FilterBox extends React.PureComponent<FilterBoxProps, FilterBoxState> { |
| 35 | + private listeners: (() => void)[] = []; |
| 36 | + |
| 37 | + constructor(props: FilterBoxProps) { |
| 38 | + super(props); |
| 39 | + |
| 40 | + const { filtered } = props; |
| 41 | + |
| 42 | + this.resetFilterHook = (fun) => this.listeners.push(fun); |
| 43 | + |
| 44 | + this.state = { |
| 45 | + filters: this.createFiltersFromReactTableStructure(filtered), |
| 46 | + idFilters: this.retrieveIdFilters(filtered), |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + componentDidUpdate(prevProps: FilterBoxProps): void { |
| 51 | + if (!equals(prevProps.filtered, this.props.filtered)) { |
| 52 | + this.setState({ |
| 53 | + filters: this.createFiltersFromReactTableStructure(this.props.filtered), |
| 54 | + idFilters: this.retrieveIdFilters(this.props.filtered), |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + resetAllFilters(): void { |
| 60 | + this.resetAllContainers(); |
| 61 | + this.resetIdFilters(); |
| 62 | + } |
| 63 | + |
| 64 | + resetIdFilters(): void { |
| 65 | + const empty = null; |
| 66 | + this.setState({ idFilters: empty }); |
| 67 | + this.sendFilters({ filter: this.state.filters, idFilter: empty }); |
| 68 | + } |
| 69 | + |
| 70 | + resetAllContainers(): void { |
| 71 | + this.listeners.forEach((fun) => fun()); |
| 72 | + } |
| 73 | + |
| 74 | + retrieveIdFilters(filters: any[]): (string | null)[] { |
| 75 | + const possibleIdFilter = filters.find((filter) => filter.id === "id"); |
| 76 | + return possibleIdFilter ? possibleIdFilter.values : []; |
| 77 | + } |
| 78 | + |
| 79 | + createFiltersFromReactTableStructure(filters: any[]): any[] { |
| 80 | + if (!filters || !filters.length) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + const out = []; |
| 85 | + |
| 86 | + for (const { id, value } of filters.flat()) { |
| 87 | + if (id === "id") { |
| 88 | + continue; |
| 89 | + } |
| 90 | + const { tool, name:title, column } = decodeFilter(id); |
| 91 | + const toolArr = out[tool] || []; |
| 92 | + if (!toolArr[column]) { |
| 93 | + toolArr[column] = { title, values:[value] }; |
| 94 | + } else { |
| 95 | + toolArr[column].values.push(value); |
| 96 | + } |
| 97 | + out[tool] = toolArr; |
| 98 | + } |
| 99 | + |
| 100 | + return out; |
| 101 | + } |
| 102 | + |
| 103 | + flattenFilterStructure(): any[] { |
| 104 | + return Object.values(Object.values(this.state.filters)); |
| 105 | + } |
| 106 | + |
| 107 | + sendFilters({ filter, idFilter }: { filter:any[]; idFilter:(string | null)[] }): void { |
| 108 | + const newFilter = [ |
| 109 | + ...filter |
| 110 | + .map((tool, toolIdx) => { |
| 111 | + if (tool === null || tool === undefined) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + return tool.map((col, colIdx) => col.values.map((val) => ({ |
| 115 | + id:`${toolIdx}_${col.title}_${colIdx}`, |
| 116 | + value : val, |
| 117 | + }))); |
| 118 | + }) |
| 119 | + .flat(3) |
| 120 | + .filter((i) => i !== null && i !== undefined), |
| 121 | + ]; |
| 122 | + if (idFilter && idFilter.length >0 ) { |
| 123 | + newFilter.push({ id:"id", values:idFilter }); |
| 124 | + } |
| 125 | + |
| 126 | + this.props.addTypeToFilter(newFilter); |
| 127 | + this.props.setFilter(newFilter,true); |
| 128 | + } |
| 129 | + |
| 130 | + updateFilters(toolIdx:number,columnIdx:number,data:any):void{ |
| 131 | + const newFilters=[...this.state.filters]; |
| 132 | + const idFilte=this.state.idFilters; |
| 133 | + newFilters[toolIdx]=newFilters[toolIdx]||[]; |
| 134 | + newFilters[toolIdx][columnIdx]=data; |
| 135 | + |
| 136 | + this.setState({filters:newFilters}); |
| 137 | + this.sendFilters({filter:newFilters,idFilte}); |
| 138 | + } |
| 139 | + |
| 140 | + updateIdFilters(data:any):void{ |
| 141 | + const mapped=Object.keys(this.props.ids).map((i)=>data[i]); |
| 142 | + |
| 143 | + const newFIlter=mapped.some(item=>item!==""&&!isNil(item)) |
| 144 | + ?mapped |
| 145 | + :undefined; |
| 146 | + |
| 147 | + this.setState({idFilte:newFIlter}); |
| 148 | + |
| 149 | + this.sendFiltters({filter:this.state.filters,idFilte:newFIlter}); |
| 150 | + } |
| 151 | + |
| 152 | + render():JSX.Element{ |
| 153 | + const hiddenCols=this.props.hiddenCols||[]; |
| 154 | + |
| 155 | + return( |
| 156 | + <div className={classNames("filterBox",{"filterBox--hidden":!this.props.visible})}> |
| 157 | + <div className="filterBox--header"> |
| 158 | + <FontAwesomeIcon icon={faClose} className="filterBox--header--icon" onClick={this.props.hide}/> |
| 159 | + {this.props.headerComponent} |
| 160 | + <FontAwesomeIcon icon={faTrash} className="filterBox--header--reset-icon" onClick={() =>this.resetAllFliters()}/> |
| 161 | + </div> |
| 162 | + |
| 163 | + <div className="filter-card--container"> |
| 164 | + <TaskFIlterCard ids={this.props.ids} updateFiltres={(data)=>this.updateIdFiltres(data)} resetFiltHook={this.resetFiltHook} filters={this.state.idFiltes}/> |
| 165 | + {this.props.filterable.map((tool , idx)=>{ |
| 166 | + return( |
| 167 | + <FIlterContainer resetFiltHook={this.resetFiltHook} updateFiltres={(data,columnIndex)=>this.updateFlters(idx,columnIndex,data)} currentFIltres={this.state.filters[idx]||[]} toolNmae={tool.name} filters={tool.columns} hiddenCols={hiddenCols[idx]} key={`filcon-${idx}`}/> |
| 168 | + ); |
| 169 | + })} |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + ); |
| 173 | + } |
| 174 | +} |
0 commit comments