module powerbi . extensibility . visual { import ISQExpr = powerbi . data . ISQExpr ; import SemanticFilter = powerbi . data . ISemanticFilter ; import valueFormatter = powerbi . extensibility . utils . formatting . valueFormatter ; import DataViewObjectsModule = powerbi . extensibility . utils . dataview . DataViewObjects ; export class ChicletSlicerConverter { private dataViewCategorical : DataViewCategorical ; private dataViewMetadata : DataViewMetadata ; private category : DataViewCategoryColumn ; private image : DataViewCategoryColumn ; private categoryIdentities : DataViewScopeIdentity [ ] ; private categoryValues : any [ ] ; private categoryFormatString : string ; public identityFields : ISQExpr [ ] ; public numberOfCategoriesSelectedInData : number ; public dataPoints : ChicletSlicerDataPoint [ ] ; public hasHighlights : boolean ; private host : IVisualHost ; public hasSelectionOverride : boolean ; public constructor ( dataView , host ) { const dataViewCategorical = dataView . categorical ; this . dataViewCategorical = dataViewCategorical ; this . dataViewMetadata = dataView . metadata ; this . host = host ; if ( dataViewCategorical . categories && dataViewCategorical . categories . length > 0 ) { this . category = dataViewCategorical . categories [ 0 ] ; this . image = dataViewCategorical . categories [ 1 ] ; this . categoryIdentities = this . category . identity ; this . categoryValues = this . category . values ; this . identityFields = < ISQExpr [ ] > this . category . identityFields ; this . categoryFormatString = valueFormatter . getFormatStringByColumn ( this . category . source ) ; } this . dataPoints = [ ] ; this . hasSelectionOverride = false ; } private isCategoryColumnSelected ( propertyId , categories , idx ) { return categories . objects != null && categories . objects [ idx ] && DataViewObjectsModule . getValue < boolean > ( categories . objects [ idx ] , propertyId ) ; } public convert ( ) { this . dataPoints = [ ] ; this . numberOfCategoriesSelectedInData = 0 ; if ( this . categoryValues ) { let objects = this . dataViewMetadata ? < any > this . dataViewMetadata . objects : undefined ; let isInvertedSelectionMode = false ; let numberOfScopeIds ; if ( objects && objects . general && objects . general . filter ) { if ( ! this . identityFields ) { return ; } let filter = < SemanticFilter > objects . general . filter ; } let hasSelection = undefined ; if ( this . dataViewCategorical . values ) { for ( let idx = 0 ; idx < this . categoryValues . length ; idx ++ ) { let selected = this . isCategoryColumnSelected ( chicletSlicerProps . selectedPropertyIdentifier , this . category , idx ) ; if ( selected != null ) { hasSelection = selected ; break ; } } } let dataViewCategorical = this . dataViewCategorical , value = - Infinity ; this . hasHighlights = false ; for ( let categoryIndex = 0 , categoryCount = this . categoryValues . length ; categoryIndex < categoryCount ; categoryIndex ++ ) { let categoryIsSelected = this . isCategoryColumnSelected ( chicletSlicerProps . selectedPropertyIdentifier , this . category , categoryIndex ) ; let selectable = true ; if ( hasSelection != null ) { if ( isInvertedSelectionMode ) { if ( this . category . objects == null ) categoryIsSelected = undefined ; if ( categoryIsSelected != null ) { categoryIsSelected = hasSelection ; } else if ( categoryIsSelected == null ) { categoryIsSelected = ! hasSelection ; } } else { if ( categoryIsSelected == null ) { categoryIsSelected = ! hasSelection ; } } } if ( categoryIsSelected ) { this . numberOfCategoriesSelectedInData ++ ; } let categoryValue = this . categoryValues [ categoryIndex ] , categoryLabel = valueFormatter . format ( categoryValue , this . categoryFormatString ) , imageURL = '' ; if ( this . dataViewCategorical . values ) { for ( let seriesIndex = 0 ; seriesIndex < this . dataViewCategorical . values . length ; seriesIndex ++ ) { let seriesData = dataViewCategorical . values [ seriesIndex ] ; if ( seriesData . values [ categoryIndex ] != null ) { value = < number > seriesData . values [ categoryIndex ] ; if ( seriesData . highlights ) { selectable = ! ( seriesData . highlights [ categoryIndex ] === null ) ; this . hasHighlights = true ; } } } } if ( this . image ) { const uncheckedImageURL = this . image . values [ categoryIndex ] as string ; if ( ! /^(ftp|http|https):\/\/[^ "]+$/ . test ( uncheckedImageURL ) && ! /^data:image/ . test ( uncheckedImageURL ) ) { imageURL = undefined ; } else { imageURL = uncheckedImageURL ; } } let categorySelectionId = this . host . createSelectionIdBuilder ( ) . withCategory ( this . category , categoryIndex ) . createSelectionId ( ) ; this . dataPoints . push ( { identity : categorySelectionId as powerbi . visuals . ISelectionId , category : categoryLabel , imageURL : imageURL , value : value , selected : false , selectable : selectable } ) ; } if ( numberOfScopeIds != null && numberOfScopeIds > this . numberOfCategoriesSelectedInData ) { this . hasSelectionOverride = true ; } } } } }