import { CustomPropertyHandler , PropertyResolveResult } from "../providers/property-handler-provider" ; import * as parseXml from '../parser/types' ; import { WidgetModel , ExtraDataModel , AttributeModel , PropertyModel } from '../models/models' ; import { makeTabs } from "../utils" ; import { PropertyResolver } from "../resolvers/property-resolver" ; export class SwitchHandler extends CustomPropertyHandler { priority = - 100000 ; constructor ( private readonly propertyResolver ) { super ( ) ; } canResolve ( element , handlerProperty , widget ) { return true ; } resolve ( element , attr , widget ) { let wrapperWidget = null ; let extraData = null ; const { casesParentWidget , casesProperty } = this . getSwitchCasesAndParent ( widget ) ; const tempData = { casesWidgets : JSON . parse ( JSON . stringify ( casesProperty . value ) ) } ; const switchWidget = { controllers : [ ] , vars : [ ] , formControls : [ ] , properties : [ ] , type : ':switch' , tempData : tempData , wrappedWidgets : [ ] , onResolved : [ ] } ; const casesWidgets = casesProperty . value as WidgetModel [ ] ; for ( let index = 0 ; index < casesWidgets . length ; index ++ ) { const a = casesWidgets [ index ] ; const hasSwitchCase = ( a . properties as any [ ] ) . filter ( p => p . name === ':switchCase' ) . length === 1 ; if ( hasSwitchCase ) { casesWidgets . splice ( casesWidgets . findIndex ( t => a === t ) , 1 ) ; index -- ; } } casesProperty . value = switchWidget ; casesProperty . name = 'child' ; casesProperty . dataType = 'widget' ; const result = this . propertyResolver . pipeValueResolver . resolve ( element , attr . name , attr . value , casesParentWidget , true ) ; wrapperWidget = result . wrapperWidget || casesParentWidget ; tempData . switchValue = result . value ; return { extraData , wrapperWidget , value : attr . value , handled : true } ; } private getSwitchCasesAndParent ( widget ) { const childrenWidgets = widget . properties . filter ( a => a . dataType === 'widgetList' ) ; const childWidget = widget . properties . filter ( a => a . dataType === 'widget' ) ; if ( childWidget . length ) { childrenWidgets . push ( childWidget [ 0 ] ) ; } if ( childrenWidgets . length ) { return { casesProperty : childrenWidgets [ 0 ] , casesParentWidget : widget } ; } if ( widget . wrappedWidgets ) { for ( const w of widget . wrappedWidgets ) { const result = this . getSwitchCasesAndParent ( w ) ; if ( result . casesParentWidget ) { if ( result . casesProperty ) { return result ; } return this . getSwitchCasesAndParent ( w ) ; } } } return { casesProperty : null as any , casesParentWidget : null as any } ; } canGenerate ( widget ) { return true ; } generate ( widget , tabsLevel , generateChildWidgetCode , generatePropertyCode ) { const tabs = makeTabs ( tabsLevel ) ; const tempData = widget . tempData ; const casesWidgets = ( tempData . casesWidgets instanceof Array ? tempData . casesWidgets : [ tempData . casesWidgets ] ) as WidgetModel [ ] ; const defaultWidget = 'Container(width: 0, height: 0)' ; let cases = '' ; let code = '' ; if ( casesWidgets ) { casesWidgets . filter ( a => ! ! a ) . forEach ( w => { const caseProp = w . properties . filter ( a => a . name === ':switchCase' ) [ 0 ] ; if ( caseProp ) { cases += `\n${ tabs }    new SwitchCase(${ caseProp . value }, \n${ tabs }      () => ${ generateChildWidgetCode ( w , tabsLevel + 3 ) }\n${ tabs }    ),` ; } } ) ; } code += `WidgetHelpers.switchValue(\n${ tabs }  ${ tempData . switchValue },\n${ tabs }  () => ${ defaultWidget },\n${ tabs }  [${ cases }\n${ tabs }  ]\n${ tabs })` ; return code ; } }