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 , spaceAfter } from "../utils" ; import { PropertyResolver } from "../resolvers/property-resolver" ; export class RepeatHandler extends CustomPropertyHandler { priority = 1000000 ; 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 , widget } ; const listNameWithPipes = attr . value . substr ( attr . value . indexOf ( listName ) ) ; const contentWidget = { controllers : [ ] , vars : [ ] , formControls : [ ] , properties : [ { dataType : 'function' , name : ':repeat' , value : '' } , { dataType : 'widget' , name : 'child' , value : widget , skipGeneratingCode : true } ] , type : '' , tempData : tempData , wrappedWidgets : [ ] , 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 - 1 ) ; const data = widget . tempData ; const originalWidget = data . widget as WidgetModel ; const code = `...WidgetHelpers.mapToWidgetList(${ data . listValueVariableName }, (${ spaceAfter ( data . typeName ) }${ data . itemName || 'item' }, ${ data . indexName || 'index' }) {
${ tabs }    return ${ generateChildWidgetCode ( originalWidget , tabsLevel + 1 ) };
${ tabs }  }
${ tabs })` ; return code ; } }