import * as Specification from "../specification/specification" ; import { BindingValue , BindingPrimitive } from "../binding/binding" ; export type ScaleArgument = BindingValue | ScaleBinding ; export interface ScaleAttributeInfo { name : string ; type : string ; binding : BindingValue ; } export interface ScaleBindingAttributeInfo extends ScaleAttributeInfo { scaleBinding : ScaleBinding ; bindedName : string ; } export class ScaleBinding { private _scale : Scale ; private _returnType : string ; private _argTypes : string [ ] ; private _args : ScaleArgument [ ] ; constructor ( scale , returnType , argTypes , ... args ) { this . _scale = scale ; this . _returnType = returnType ; this . _argTypes = argTypes ; this . _args = args ; } public getReturnType ( ) { return this . _returnType ; } public getAttributes ( ) { let result = [ ] ; for ( let attr of this . _scale . getAttributes ( ) ) { result . push ( { scaleBinding : this , bindedName : `s${ attr . name }` , name : attr . name , type : attr . type , binding : attr . binding } ) ; } this . _args . forEach ( ( arg , index ) => { if ( arg instanceof ScaleBinding ) { let a = arg as ScaleBinding ; let attributes = a . getAttributes ( ) ; for ( let attr of attributes ) { result . push ( { scaleBinding : this , bindedName : `a${ index }${ attr . bindedName }` , name : attr . name , type : attr . type , binding : attr . binding } ) ; } } else { result . push ( { scaleBinding : this , bindedName : `a${ index }` , name : `a${ index }` , type : this . _argTypes [ index ] , binding : arg as BindingValue } ) ; } } ) ; return result ; } public getExpression ( attrs ) { let sAttrs = { } ; for ( let attr of this . _scale . getAttributes ( ) ) { sAttrs [ attr . name ] = attrs [ `s${ attr . name }` ] ; } let values = this . _args . map ( ( arg , index ) => { if ( arg instanceof ScaleBinding ) { let a = arg as ScaleBinding ; let attributes = a . getAttributes ( ) ; let aAttrs = { } ; for ( let attr of attributes ) { aAttrs [ attr . bindedName ] = attrs [ `a${ index }${ attr . bindedName }` ] ; } return arg . getExpression ( aAttrs ) ; } else { return attrs [ `a${ index }` ] ; } } ) ; return this . _scale . getExpression ( sAttrs , ... values ) ; } } export interface Scale { ( ... args ) : ScaleBinding ; getExpression : ( attrs : { [ name ] : Specification . Expression ; } , ... values : Specification . Expression [ ] ) => Specification . Expression ; getAttributes : ( ) => ScaleAttributeInfo [ ] ; } export interface DomainRangeScale extends Scale { domain ( ) : [ BindingValue , BindingValue ] ; domain ( value ) : DomainRangeScale ; range ( ) : [ BindingValue , BindingValue ] ; range ( value ) : DomainRangeScale ; } export interface InterpolateScale extends Scale { t ( ) : BindingValue ; t ( value ) : InterpolateScale ; } export interface CustomScale extends Scale { attr ( name ) : BindingValue ; attr ( name , value ) : CustomScale ; attr ( name , type , value ) : CustomScale ; }