import { ViewConfigHash , ViewComponentType } from './view-config' ; import { ViewOptions } from '../options' ; export interface ViewDef { type : string ; component : ViewComponentType ; overrides : ViewOptions ; defaults : ViewOptions ; } export type ViewDefHash = { [ viewType ] : ViewDef ; } ; export function compileViewDefs ( defaultConfigs , overrideConfigs ) { let hash = { } ; let viewType ; for ( viewType in defaultConfigs ) { ensureViewDef ( viewType , hash , defaultConfigs , overrideConfigs ) ; } for ( viewType in overrideConfigs ) { ensureViewDef ( viewType , hash , defaultConfigs , overrideConfigs ) ; } return hash ; } function ensureViewDef ( viewType , hash , defaultConfigs , overrideConfigs ) { if ( hash [ viewType ] ) { return hash [ viewType ] ; } let viewDef = buildViewDef ( viewType , hash , defaultConfigs , overrideConfigs ) ; if ( viewDef ) { hash [ viewType ] = viewDef ; } return viewDef ; } function buildViewDef ( viewType , hash , defaultConfigs , overrideConfigs ) { let defaultConfig = defaultConfigs [ viewType ] ; let overrideConfig = overrideConfigs [ viewType ] ; let queryProp = ( name ) => ( ( defaultConfig && defaultConfig [ name ] !== null ) ? defaultConfig [ name ] : ( ( overrideConfig && overrideConfig [ name ] !== null ) ? overrideConfig [ name ] : null ) ) ; let theComponent = queryProp ( 'component' ) as ViewComponentType ; let superType = queryProp ( 'superType' ) as string ; let superDef = null ; if ( superType ) { if ( superType === viewType ) { throw new Error ( 'Can\'t have a custom view type that references itself' ) ; } superDef = ensureViewDef ( superType , hash , defaultConfigs , overrideConfigs ) ; } if ( ! theComponent && superDef ) { theComponent = superDef . component ; } if ( ! theComponent ) { return null ; } return { type : viewType , component : theComponent , defaults : { ... ( superDef ? superDef . defaults : { } ) , ... ( defaultConfig ? defaultConfig . rawOptions : { } ) , } , overrides : { ... ( superDef ? superDef . overrides : { } ) , ... ( overrideConfig ? overrideConfig . rawOptions : { } ) , } , } ; }