'use strict' ; import { AnyAction , Dispatch } from 'redux' ; import { ThunkDispatch } from 'redux-thunk' ; import { defaultConfig } from 'vega-lite/src/config' ; import { updateMarkProperty } from '../../actions/markActions' ; import { setSignal } from '../../actions/signalActions' ; import { State } from '../../store' ; const dl = require ( 'datalib' ) , dsUtils = require ( '../../util/dataset-utils' ) , imutils = require ( '../../util/immutable-utils' ) , getIn = imutils . getIn , getInVis = imutils . getInVis ; param { Function } dispatch param { ImmutableMap } state param { Object } parsed param { Object } scale returns { void } export function computeLayout ( dispatch , state , parsed , scale ) { const sceneId = getInVis ( state , 'scene._id' ) , scene = getInVis ( state , 'marks.' + sceneId ) , manualLayout = scene . get ( '_manualLayout' ) ; if ( manualLayout ) { return ; } const scaleType = scale . type , domain = scale . _domain , range = scale . range ; if ( scaleType !== 'ordinal' || ( range !== 'width' && range !== 'height' ) ) { return ; } const distinct = domain . reduce ( function ( count , d ) { return count + dl . count . distinct ( dsUtils . output ( d . data ) , dl . $ ( d . field ) ) ; } , 0 ) ; const size = scene . get ( range ) , minSize = ( distinct + 1 ) * defaultConfig . scale . minBandSize ; if ( size < minSize ) { resize ( dispatch , state , scene , range , minSize ) ; } } ; function resize ( dispatch , state , mark , prop , size ) { if ( mark . get ( prop ) ) { dispatch ( updateMarkProperty ( { property : prop , value : size } , mark . get ( '_id' ) ) ) ; } else { const updatePath = 'encode.update.' , propPath = updatePath + prop , signal = getIn ( mark , propPath + '.signal' ) ; if ( signal ) { dispatch ( setSignal ( size , signal ) ) ; } } const children = mark . get ( 'marks' ) ; if ( children && children . size ) { children . forEach ( function ( childId ) { const child = getInVis ( state , 'marks.' + childId ) ; if ( child . get ( 'type' ) !== 'group' && ! child . get ( '_manualLayout' ) ) { return ; } resize ( dispatch , state , child , prop , size ) ; } ) ; } }