"use strict" ; import "@babel/polyfill" ; import "./../style/visual.less" ; import powerbi from "powerbi-visuals-api" ; import VisualConstructorOptions = powerbi . extensibility . visual . VisualConstructorOptions ; import VisualUpdateOptions = powerbi . extensibility . visual . VisualUpdateOptions ; import IVisual = powerbi . extensibility . visual . IVisual ; import EnumerateVisualObjectInstancesOptions = powerbi . EnumerateVisualObjectInstancesOptions ; import VisualObjectInstance = powerbi . VisualObjectInstance ; import DataView = powerbi . DataView ; import VisualObjectInstanceEnumerationObject = powerbi . VisualObjectInstanceEnumerationObject ; import { valueFormatter as vf , textMeasurementService as tms } from "powerbi-visuals-utils-formattingutils" ; import TextProperties = tms . TextProperties ; import textMeasurementService = tms . textMeasurementService ; import valueFormatter = vf . valueFormatter ; import { VisualSettings } from "./settings" ; import { Renderer } from "./renderer" ; import { RendererEditMode } from "./rendererEditMode" ; function visualTransform ( options , thisRef ) { let dataViews = options . dataViews ; var a = options . dataViews [ 0 ] . metadata . columns [ 1 ] ; let tblView = dataViews [ 0 ] . table ; var tblData = [ ] ; thisRef . tableDefinitionFromDataset = null ; for ( let r of dataViews [ 0 ] . table . rows ) { var colData = [ ] ; for ( var t = 0 ; t < r . length ; t ++ ) { var rawValue = r [ t ] ; var columnName = dataViews [ 0 ] . table . columns [ t ] . displayName ; if ( columnName === 'json' ) { thisRef . tableDefinitionFromDataset = rawValue ; } else { var formatString = dataViews [ 0 ] . table . columns [ t ] . format ; var isColumnNumeric = dataViews [ 0 ] . table . columns [ t ] . type . numeric ; colData . push ( { rawValue , formatString , displayName : columnName , refName : "[" + columnName + "]" , isNumeric : isColumnNumeric } ) ; } } var row = { title : r [ 0 ] , name : "[" + r [ 0 ] + "]" , values : colData , } ; tblData . push ( row ) ; } return tblData ; } export class Visual implements IVisual { public target : HTMLElement ; public settings : VisualSettings ; private tableDefinition : any ; private model : any ; public host : any ; private internalVersionNo : string = "3.1.1" ; public tableDefinitionFromDataset : any ; private renderer : Renderer ; private rendererEditMode : RendererEditMode ; private events : powerbi . extensibility . IVisualEventService ; constructor ( options ) { this . host = options . host ; this . target = options . element ; this . renderer = new Renderer ( this ) ; this . rendererEditMode = new RendererEditMode ( this , this . renderer ) ; this . events = options . host . eventService ; } public getModel ( ) { return this . model ; } public getHost ( ) { return this . host ; } private getTableDefinition ( ) { this . tableDefinition = null ; var errorMsg = "" ; if ( this . tableDefinitionFromDataset !== null && this . tableDefinitionFromDataset . length > 0 ) { try { this . tableDefinition = JSON . parse ( this . tableDefinitionFromDataset ) ; } catch ( e ) { errorMsg = "Error parsing table definition from dataset. " + e ; } } else { if ( this . settings . dataPoint . tableConfiguration . trim ( ) . length > 0 ) { try { this . tableDefinition = JSON . parse ( this . settings . dataPoint . tableConfiguration ) ; } catch ( e ) { errorMsg = "Error parsing table definition. Enter edit mode and correct the error." ; } } } return errorMsg ; } public RenderVersionNo ( ) { var a = document . createElement ( "div" ) ; a . style . display = "none" ; a . appendChild ( document . createTextNode ( "Version: " + this . internalVersionNo ) ) ; this . target . appendChild ( a ) ; } public RenderErrorMessage ( err ) { var a = document . createTextNode ( "ERROR: " + err ) ; this . target . appendChild ( a ) ; } public ClearAllContent ( ) { while ( this . target . firstChild ) { this . target . removeChild ( this . target . firstChild ) ; } } public update ( options ) { try { this . events . renderingStarted ( options ) ; this . mainUpdate ( options ) ; this . events . renderingFinished ( options ) ; } catch ( err ) { this . events . renderingFailed ( options , err ) ; this . RenderErrorMessage ( err ) ; } } public mainUpdate ( options ) { var w = options . viewport . width ; var h = options . viewport . height ; this . settings = Visual . parseSettings ( options && options . dataViews && options . dataViews [ 0 ] ) ; this . model = visualTransform ( options , this ) ; this . tableDefinition = null ; var errorMsg = this . getTableDefinition ( ) ; if ( options . editMode === 1 ) { this . ClearAllContent ( ) ; this . rendererEditMode . RenderEditMode ( this . target , this . settings ) ; } else { if ( errorMsg . length === 0 ) { this . ClearAllContent ( ) ; this . renderer . RenderAllContent ( this . target , this . tableDefinition ) ; } else { this . ClearAllContent ( ) ; this . target . appendChild ( document . createTextNode ( errorMsg ) ) ; } } this . target . style . overflow = "auto" ; this . RenderVersionNo ( ) ; this . events . renderingFinished ( options ) ; } private static parseSettings ( dataView ) { return VisualSettings . parse ( dataView ) as VisualSettings ; } public enumerateObjectInstances ( options ) { return VisualSettings . enumerateObjectInstances ( this . settings || VisualSettings . getDefault ( ) , options ) ; } }