import { CustomPropertyHandler , PropertyResolveResult } from "../providers/property-handler-provider" ; import * as parseXml from '../parser/types' ; import { WidgetModel , ExtraDataModel , AttributeModel , PropertyModel } from '../models/models' ; import { extractForLoopParams , makeTabs , sortProperties , spaceAfter } from "../utils" ; import { PropertyResolver } from "../resolvers/property-resolver" ; export class ChildBuilderHandler extends CustomPropertyHandler { priority = - 100000 ; valueSnippet = '"item of ${0:items}"' ; constructor ( private readonly propertyResolver ) { super ( ) ; } canResolve ( element , handlerProperty , widget ) { return true ; } resolve ( element , attr , widget ) { let wrapperWidget = null ; let extraData = null ; const { listName , indexName , itemName , typeName } = extractForLoopParams ( attr . value ) ; const tempData = { listName , indexName , itemName , typeName } ; const listNameWithPipes = attr . value . substr ( attr . value . indexOf ( listName ) ) ; const contentWidget = { controllers : [ ] , vars : [ ] , formControls : [ ] , properties : [ { dataType : 'function' , name : ':childBuilder' , value : '' } ] , type : widget . type === 'builder' ? 'YouShouldNotSetChildBuilderPropertyInBuilderTag' : widget . type , tempData : tempData , wrappedWidgets : [ widget ] , onResolved : [ ] } ; const result = this . propertyResolver . pipeValueResolver . resolve ( element , attr . name , listNameWithPipes , contentWidget ) ; wrapperWidget = result . wrapperWidget || contentWidget ; tempData . listValueVariableName = result . wrapperWidget ? result . value : tempData . listName ; return { extraData , wrapperWidget , value : result . value , handled : true } ; } canGenerate ( widget ) { return true ; } generate ( widget , tabsLevel , generateChildWidgetCode , generatePropertyCode ) { const tabs = makeTabs ( tabsLevel ) ; const data = widget . tempData ; const originalWidget = widget . wrappedWidgets [ 0 ] ; const indexName = data . indexName || 'index' ; let widgetProp = originalWidget . properties . filter ( a => a . dataType === 'widget' ) [ 0 ] ; let code = '' ; if ( ! originalWidget . isPropertyElement ) { originalWidget . properties . sort ( sortProperties ) . filter ( a => ! a . skipGeneratingCode && [ 'children' ] . indexOf ( a . name ) === - 1 && a !== widgetProp ) . forEach ( p => { code += generatePropertyCode ( originalWidget as WidgetModel , p , tabsLevel ) + `,\n` ; } ) ; } let childWidget = widgetProp ? widgetProp . value as WidgetModel : null as any ; if ( ! childWidget ) { const widgetProp = originalWidget . properties . filter ( a => a . dataType === 'widgetList' ) [ 0 ] ; childWidget = widgetProp && widgetProp . value ? ( widgetProp . value as WidgetModel [ ] ) [ 0 ] : null as any ; } code += `${ tabs }children: WidgetHelpers.mapToWidgetList(${ data . listValueVariableName }, (${ spaceAfter ( data . typeName ) }${ data . itemName || 'item' }, ${ data . indexName || 'index' }) {
${ tabs }  return ${ generateChildWidgetCode ( childWidget , tabsLevel + 1 ) };
${ tabs }})` ; return code ; } }