import { IDataFilter , IDataFilterConfiguration , FilterType , IDataFilterResult , FilterComparisonOperator } from "@pnp/modern-search-extensibility" ; import { BuiltinTokenNames } from "../services/tokenService/TokenService" ; export class DataFilterHelper { param filter param filtersConfiguration public static getConfigurationForFilter ( filter , filtersConfiguration ) { let filterConfiguration = undefined ; const filterConfigIdx = filtersConfiguration . map ( configuration => { return configuration . filterName ; } ) . indexOf ( filter . filterName ) ; if ( filterConfigIdx !== - 1 ) { filterConfiguration = filtersConfiguration [ filterConfigIdx ] ; } return filterConfiguration ; } param filters param type param filtersConfiguration public static getSelectedFilterValuesByType ( filters , type , filtersConfiguration ) { let selectedValues = [ ] ; filters . forEach ( filter => { const configuration = this . getConfigurationForFilter ( filter , filtersConfiguration ) ; if ( configuration && configuration . type === type ) { selectedValues = selectedValues . concat ( filter . values . map ( value => value . value ) ) ; } } ) ; return selectedValues . sort ( ) ; } param selectedFilters param inputString public static getAppliedFiltersFromTokens ( selectedFilters , inputString ) { const tokens = inputString . match ( /\{[^\{]*?\}/gi ) ; let selectedFiltersInTokens = [ ] ; if ( tokens !== null && tokens . length > 0 ) { tokens . forEach ( token => { const tokenName = token . substr ( 1 ) . slice ( 0 , - 1 ) ; const tokenParts = tokenName . split ( '.' ) ; if ( tokenName . indexOf ( BuiltinTokenNames . filters ) !== - 1 && tokenParts . length > 1 ) { const selectedFiltersByName = selectedFilters . filter ( selectedFilter => selectedFilter . filterName === tokenParts [ 1 ] ) ; const alreadyAddedFitlers = selectedFiltersInTokens . filter ( selectedFiltersInToken => selectedFiltersInToken . filterName === tokenParts [ 1 ] ) ; if ( selectedFiltersByName . length > 0 && alreadyAddedFitlers . length === 0 ) { selectedFiltersInTokens . push ( selectedFilters [ 0 ] ) ; } } } ) ; } return selectedFiltersInTokens ; } param selectedFilters param filtersConfiguration param moment param encodeTokens public static buildFqlRefinementString ( selectedFilters , filtersConfiguration , moment , encodeTokens ? ) { let refinementQueryConditions = [ ] ; selectedFilters . forEach ( filter => { let operator = filter . operator ; if ( filter . values . length > 1 ) { let startDate = null ; let endDate = null ; const conditions = filter . values . map ( filterValue => { let value = filterValue . value ; if ( moment ( value , moment . ISO_8601 , true ) . isValid ( ) ) { if ( ! startDate && ( filterValue . operator === FilterComparisonOperator . Geq || filterValue . operator === FilterComparisonOperator . Gt ) ) { startDate = value ; } if ( ! endDate && ( filterValue . operator === FilterComparisonOperator . Lt || filterValue . operator === FilterComparisonOperator . Leq ) ) { endDate = value ; } } return /ǂǂ/ . test ( value ) && encodeTokens ? encodeURIComponent ( value ) : value ; } ) . filter ( c => c ) ; if ( startDate && endDate ) { refinementQueryConditions . push ( `${ filter . filterName }:range(${ startDate },${ endDate })` ) ; } else { refinementQueryConditions . push ( `${ filter . filterName }:${ operator }(${ conditions . join ( ',' ) })` ) ; } } else { if ( filter . values . length === 1 ) { const filterValue = filter . values [ 0 ] ; let refinementToken = /ǂǂ/ . test ( filterValue . value ) && encodeTokens ? encodeURIComponent ( filterValue . value ) : filterValue . value ; if ( moment ( refinementToken , moment . ISO_8601 , true ) . isValid ( ) ) { if ( filterValue . operator === FilterComparisonOperator . Gt || filterValue . operator === FilterComparisonOperator . Geq ) { refinementToken = `range(${ refinementToken },max)` ; } if ( filterValue . operator === FilterComparisonOperator . Leq || filterValue . operator === FilterComparisonOperator . Lt ) { refinementToken = `range(min,${ refinementToken })` ; } } refinementQueryConditions . push ( `${ filter . filterName }:${ refinementToken }` ) ; } } } ) ; return refinementQueryConditions ; } }