import { CdkDragDrop } from '@angular/cdk/drag-drop' ; import { EdaBlankPanelComponent } from '@eda/components/eda-panels/eda-blank-panel/eda-blank-panel.component' ; import { Column } from '@eda/models/model.index' ; import * as _ from 'lodash' ; import { TableUtils } from './tables-utils' ; export const PanelInteractionUtils = { param table loadColumns : ( ebp , table ) => { ebp . userSelectedTable = table . table_name ; ebp . disableBtnSave ( ) ; ebp . columns = [ ] ; table . columns . forEach ( c => { c . table_id = table . table_name ; const matcher = _ . find ( ebp . currentQuery , ( x ) => c . table_id === x . table_id && c . column_name === x . column_name ) ; if ( ! matcher ) { ebp . columns . push ( c ) ; } ebp . columns = ebp . columns . filter ( col => col . visible === true ) . sort ( ( a , b ) => ( a . display_name . default > b . display_name . default ) ? 1 : ( ( b . display_name . default > a . display_name . default ) ? - 1 : 0 ) ) ; } ) ; if ( ! _ . isEqual ( ebp . inputs . findTable . ngModel , '' ) ) { ebp . inputs . findTable . reset ( ) ; ebp . setTablesData ( ) ; } } , param column handleFilters : ( ebp , content ) : void => { ebp . selectedFilters = _ . cloneDeep ( content . filters ) ; ebp . globalFilters = content . filters . filter ( f => f . isGlobal === true ) ; ebp . selectedFilters . forEach ( filter => { filter . removed = false ; } ) ; ebp . selectedFilters = ebp . selectedFilters . filter ( f => f . isGlobal === false ) ; } , handleFilterColumns : ( ebp , filterList , query ) : void => { filterList . forEach ( filter => { const table = ebp . tables . filter ( table => table . table_name === filter . filter_table ) [ 0 ] ; const column = table . columns . filter ( column => column . column_name === filter . filter_column ) [ 0 ] ; const columnInQuery = query . filter ( col => col . column_name === filter . filter_column ) . length > 0 ; if ( ! filter . isGlobal && ! columnInQuery ) { ebp . filtredColumns . push ( column ) ; } } ) ; } , handleCurrentQuery : ( ebp ) : void => { if ( ebp . panel . content ) { const fields = ebp . panel . content . query . query . fields ; for ( let i = 0 , n = fields . length ; i < n ; i ++ ) { ebp . currentQuery [ i ] . format = fields [ i ] . format ; ebp . currentQuery [ i ] . cumulativeSum = fields [ i ] . cumulativeSum ; } } } , /**
    * Sets tables and tablesToShow when column is selected
    */ searchRelations : ( ebp , c , event ? ) => { if ( ! _ . isNil ( event ) && event . container . id === event . previousContainer . id ) { return ; } const originTable = ebp . tables . filter ( t => t . table_name === c . table_id ) [ 0 ] ; const tablesMap = TableUtils . findRelationsRecursive ( ebp . inject . dataSource . model . tables , originTable , new Map ( ) ) ; ebp . tablesToShow = Array . from ( tablesMap . values ( ) ) ; ebp . tablesToShow = ebp . tablesToShow . filter ( table => table . visible === true ) . sort ( ( a , b ) => ( a . display_name . default > b . display_name . default ) ? 1 : ( ( b . display_name . default > a . display_name . default ) ? - 1 : 0 ) ) ; } , param column handleAggregationType : ( ebp , column ) : void => { const voidPanel = ebp . panel . content === undefined ; const tmpAggTypes = [ ] ; const colName = column . column_name ; const initializeAgregations = ( column , tmpAggTypes ) => { column . aggregation_type . forEach ( ( agg ) => { tmpAggTypes . push ( { display_name : agg . display_name , value : agg . value , selected : agg . value === 'none' } ) ; } ) ; } ; if ( ! voidPanel ) { const colInCurrentQuery = ebp . currentQuery . find ( c => c . column_name === colName ) . aggregation_type . find ( agg => agg . selected === true ) ; const queryFromServer = ebp . panel . content . query . query . fields ; if ( colInCurrentQuery ) { column . aggregation_type . forEach ( agg => tmpAggTypes . push ( agg ) ) ; ebp . aggregationsTypes = tmpAggTypes ; } else { const columnInServer = queryFromServer . filter ( c => c . column_name === colName && c . table_id === column . table_id ) [ 0 ] ; if ( columnInServer ) { const aggregation = columnInServer . aggregation_type ; column . aggregation_type . forEach ( agg => { tmpAggTypes . push ( agg . value === aggregation ? { display_name : agg . display_name , value : agg . value , selected : true } : { display_name : agg . display_name , value : agg . value , selected : false } ) ; } ) ; } else initializeAgregations ( column , tmpAggTypes ) ; ebp . aggregationsTypes = tmpAggTypes ; } } else { initializeAgregations ( column , tmpAggTypes ) ; ebp . aggregationsTypes = tmpAggTypes ; } ebp . currentQuery . find ( c => { return colName === c . column_name && column . table_id === c . table_id ; } ) . aggregation_type = _ . cloneDeep ( ebp . aggregationsTypes ) ; } , param column handleOrdTypes : ( ebp , column ) : void => { let addOrd ; const voidPanel = ebp . panel . content === undefined ; if ( ! voidPanel ) { const queryFromServer = ebp . panel . content . query . query . fields ; if ( ! column . ordenation_type ) { column . ordenation_type = 'No' ; } const colInServer = queryFromServer . filter ( c => c . column_name === column . column_name && c . table_id === column . table_id ) [ 0 ] ; let ordenation = colInServer ? colInServer . ordenation_type : column . ordenation_type ; const d = ebp . ordenationTypes . find ( ag => ag . selected === true && ordenation !== ag . value ) ; const ord = ebp . ordenationTypes . find ( o => o . value === ordenation ) ; if ( ! _ . isNil ( d ) ) { d . selected = false ; } if ( ! _ . isNil ( ord ) ) { ord . selected = true ; } } else if ( ! column . ordenation_type ) { ebp . ordenationTypes = [ { display_name : 'ASC' , value : 'Asc' , selected : false } , { display_name : 'DESC' , value : 'Desc' , selected : false } , { display_name : 'NO' , value : 'No' , selected : true } ] ; } else { ebp . ordenationTypes . forEach ( ord => { ord . value !== column . ordenation_type ? ord . selected = false : ord . selected = true ; } ) ; } const colIncurrentQuery = ebp . currentQuery . find ( c => column . column_name === c . column_name && column . table_id === c . table_id ) ; try { colIncurrentQuery . ordenation_type = ebp . ordenationTypes . filter ( ord => ord . selected === true ) [ 0 ] . value ; } catch ( e ) { colIncurrentQuery . ordenation_type = 'No' ; } } , param c moveItem : ( ebp , c ) => { ebp . disableBtnSave ( ) ; const match = _ . findIndex ( ebp . columns , { column_name : c . column_name , table_id : c . table_id } ) ; ebp . columns . splice ( match , 1 ) ; ebp . currentQuery . push ( _ . cloneDeep ( c ) ) ; PanelInteractionUtils . searchRelations ( ebp , c ) ; PanelInteractionUtils . handleAggregationType ( ebp , c ) ; PanelInteractionUtils . handleOrdTypes ( ebp , c ) ; if ( ! _ . isEqual ( ebp . inputs . findColumn . ngModel , '' ) ) { ebp . inputs . findColumn . reset ( ) ; PanelInteractionUtils . loadColumns ( ebp , ebp . tablesToShow . filter ( table => table . table_name === ebp . userSelectedTable ) [ 0 ] ) ; } } , param charts notAllowedCharts : ( ebp , notAllowedCharts ) => { for ( const notAllowed of notAllowedCharts ) { for ( const chart of ebp . chartTypes ) { if ( notAllowed === chart . subValue ) { chart . ngIf = true ; } } } } , param charts tooManyDataForCharts ( ebp , tooManyDataForCharts ) { for ( const myElem of tooManyDataForCharts ) { for ( const chart of ebp . chartTypes ) { if ( myElem === chart . value ) { chart . tooManyData = true ; } } } } , /**
      * Check data and set notAllowed charts
      */ verifyData : ( ebp ) => { for ( const chart of ebp . chartTypes ) { chart . ngIf = false ; chart . tooManyData = false ; } if ( ! _ . isEmpty ( ebp . currentQuery ) ) { let notAllowedCharts = [ ] ; let tooManyDataForCharts = [ ] ; const dataDescription = ebp . chartUtils . describeData ( ebp . currentQuery , ebp . chartLabels ) ; if ( dataDescription . totalColumns === 0 || _ . isEmpty ( ebp . chartData ) ) { } else { notAllowedCharts = ebp . chartUtils . getNotAllowedCharts ( dataDescription , ebp . currentQuery ) ; tooManyDataForCharts = ebp . chartUtils . getTooManyDataForCharts ( ebp . chartData . length ) ; } tooManyDataForCharts = tooManyDataForCharts . filter ( x => ! notAllowedCharts . includes ( x ) ) ; PanelInteractionUtils . notAllowedCharts ( ebp , notAllowedCharts ) ; PanelInteractionUtils . tooManyDataForCharts ( ebp , tooManyDataForCharts ) ; } } , param c param list removeColumn : ( ebp , c , list ? ) => { ebp . disableBtnSave ( ) ; if ( list === 'select' ) { const match = _ . findIndex ( ebp . currentQuery , { column_name : c . column_name , table_id : c . table_id } ) ; ebp . currentQuery [ match ] . ordenation_type = 'No' ; ebp . currentQuery [ match ] . aggregation_type . forEach ( ag => ag . selected = false ) ; ebp . currentQuery [ match ] . format = '' ; ebp . currentQuery . splice ( match , 1 ) ; } else if ( list === 'filter' ) { const match = _ . findIndex ( ebp . filtredColumns , { column_name : c . column_name , table_id : c . table_id } ) ; ebp . filtredColumns . splice ( match , 1 ) ; } PanelInteractionUtils . loadColumns ( ebp , _ . find ( ebp . tables , ( t ) => t . table_name === c . table_id ) ) ; if ( ebp . currentQuery . length === 0 && ebp . filtredColumns . length === 0 ) { ebp . tablesToShow = ebp . tables ; } else { _ . map ( ebp . currentQuery , selected => selected . table_id === c . table_id ) ; } const filters = ebp . selectedFilters . filter ( f => f . filter_column === c . column_name ) ; filters . forEach ( f => ebp . selectedFilters = ebp . selectedFilters . filter ( ff => ff . filter_id !== f . filter_id ) ) ; } } ;