import { Column } from '../models' ; import { Group } from '../Models/Group' ; import { FormatFunc } from '../types' ; import { getValueByColumn } from './DataUtils' ; export const groupMark = { } ; export const groupSummaryMark = { } ; const getGroupSummary = ( groupData , key , groupIndex ) => ( { groupData , groupSummaryMark , key : JSON . stringify ( [ key , '--:+summary--' ] ) , groupIndex } ) ; export const updateExpandedGroups = ( groupsExpanded , groupKey ) : any [ ] [ ] => { const newGroupsExpanded = groupsExpanded . filter ( ( ge ) => JSON . stringify ( ge ) !== JSON . stringify ( groupKey ) ) ; if ( newGroupsExpanded . length === groupsExpanded . length ) { newGroupsExpanded . push ( groupKey ) ; } return newGroupsExpanded ; } ; export const getExpandedGroups = ( groupedData ) : any [ ] [ ] => { return groupedData . filter ( ( g ) => g . groupMark === groupMark ) . map ( ( g ) => g . key ) ; } ; export const getGroupedData = ( data , groups , groupedColumns , groupsExpanded ? ) : any [ ] => { const grouped = getGroupedStructure ( data , groups , groupedColumns , 0 , groupsExpanded ) as Map < any , any > ; return convertToFlat ( grouped ) ; } ; export const convertToFlat = ( grouped , key = [ ] ) => { let result = [ ] ; grouped . forEach ( ( value , groupValue ) => { if ( groupValue === groupSummaryMark ) { result . push ( value ) ; } else { const groupKey = [ ... key ] ; groupKey . push ( groupValue ) ; result . push ( { groupMark , key : groupKey , value : groupValue } ) ; result = [ ... result , ... ( Array . isArray ( value ) ? value : convertToFlat ( value , groupKey ) ) ] ; } } ) ; return result ; } ; export const getGroupedStructure = ( data , groups , groupedColumns , expandedDeep = 0 , groupsExpanded ? , parentGroupKey = [ ] ) : Map < any , any > | void => { groups = [ ... groups ] ; const group = groups . shift ( ) ; if ( group ) { const column = groupedColumns && groupedColumns . find ( ( g ) => g . key === group . columnKey ) ; if ( column ) { const grouped = groupBy ( data , ( item ) => getValueByColumn ( item , column ) ) ; grouped . forEach ( ( groupData , key ) => { const groupExpandedItems = groupsExpanded && groupsExpanded . filter ( ( ge ) => ge [ expandedDeep ] === key ) ; const isGroupExpanded = ! groupExpandedItems || groupExpandedItems . some ( ( ge ) => ge . length === expandedDeep + 1 ) ; if ( isGroupExpanded ) { const fullKey = [ ... parentGroupKey , key ] ; const newStructure = getGroupedStructure ( groupData , groups , groupedColumns , expandedDeep + 1 , groupExpandedItems && groupExpandedItems . filter ( ( ge ) => ge . length > expandedDeep + 1 ) , fullKey ) ; if ( newStructure ) { if ( group . enableSummary ) { newStructure . set ( groupSummaryMark , getGroupSummary ( groupData , fullKey , expandedDeep ) ) ; } grouped . set ( key , newStructure ) ; } else if ( group . enableSummary ) { groupData . push ( getGroupSummary ( [ ... groupData ] , fullKey , expandedDeep ) ) ; } } else { grouped . set ( key , [ ] ) ; } } ) ; return grouped ; } } } ; export const groupBy = ( data , keyGetter , isEmptyValue = false ) => { const map = new Map ( ) ; data . forEach ( ( item ) => { const key = keyGetter ( item ) ; if ( isEmptyValue ) { map . set ( key , [ ] ) ; } else { const collection = map . get ( key ) ; if ( ! collection ) { map . set ( key , [ item ] ) ; } else { collection . push ( item ) ; } } } ) ; return map ; } ; export const getGroupMark = ( ) => groupMark ; export const getGroupText = ( value , column , format ? ) => { return format ? format ( { column , value } ) : `${ ( column && column . title ? column . title + ': ' : '' ) }${ value }` ; } ;