import powerbi from 'powerbi-visuals-api' ; import DataView = powerbi . DataView ; import DataViewMetadataColumn = powerbi . DataViewMetadataColumn ; import { ContentFormattingSettings , VisualSettings } from './VisualSettings' ; /**
 * View model structure
 */ export interface IViewModel { isValid : boolean ; isEmpty : boolean ; contentIndex : number ; contentFormatting ? : ContentFormattingSettings ; htmlEntries : string [ ] ; } /**
 * Visual view model and necessary logic to manage its state.
 */ export class ViewModelHandler { viewModel : IViewModel ; constructor ( ) { this . reset ( ) ; } /**
     * Initialises an empty view model for the visual.
     */ reset ( ) { this . viewModel = { isValid : false , isEmpty : true , contentIndex : - 1 , htmlEntries : [ ] } ; } param dataViews validateDataView ( dataViews ) { const hasBasicDataView = ( dataViews && dataViews [ 0 ] && dataViews [ 0 ] . table && dataViews [ 0 ] . metadata && dataViews [ 0 ] . metadata . columns && true ) || false ; this . viewModel . contentIndex = hasBasicDataView ? this . getContentMetadataIndex ( dataViews [ 0 ] . metadata . columns ) : - 1 ; this . viewModel . isValid = hasBasicDataView && this . viewModel . contentIndex > - 1 ; } param dataViews param settings mapDataView ( dataViews , settings ) { if ( this . viewModel . isValid ) { const rows = dataViews [ 0 ] . table . rows , htmlEntries = rows . map ( v => v [ this . viewModel . contentIndex ] . toString ( ) ) ; this . viewModel . contentFormatting = settings . contentFormatting ; this . viewModel . htmlEntries = htmlEntries ; this . viewModel . isEmpty = rows . length === 0 ; } } param columns private getContentMetadataIndex ( columns ) { return columns . findIndex ( c => c . roles . content ) ; } }