declare let SmartFormRepeaterDataStore:any; declare let RedNaoFormulaManagerVar:any; namespace SmartFormsFields { export class rednaorepeater extends sfFormElementBase{ private latestRowId:number=0; public AnimateInsertion=false; IsFieldContainer:boolean=true; public DynamicItems:DynamicRow[]=[]; GetValueString() { let rows:any[]=[]; for(let row of this.DynamicItems) { let data:any={}; rows.push(data); for(let dynamicField of row.Fields) { if(dynamicField.StoresInformation()) data[dynamicField.Id]=dynamicField.GetValueString(); } } return {value:rows}; } private IsDynamicField=true; constructor(options:any,serverOptions:any) { super(options,serverOptions); this.HandleFieldsInternally=true; if(this.IsNew) { this.Options.ClassName='rednaorepeater'; this.Options.Label="Repeater"; this.Options.CustomCSS=''; this.Options.NumberOfItems=1; this.Options.ManuallyAdd='y'; this.Options.IncludeItemNumberInLabels='y'; this.Options.FieldOptions=[]; } RedNaoEventManager.Subscribe('formPropertyChanged', (data)=> { for(let option of this.Options.FieldOptions) { if(typeof data.Field.OriginalId=='undefined') return; if(data.Field.OriginalId==option.Id) { this.FirePropertyChanged(option.Id); } } }); } InitializeField(){ this.latestRowId=0; this.AnimateInsertion=false; if(this.DynamicItems!=null&&this.DynamicItems.length>0) for(let row of this.DynamicItems) for(let field of row.Fields) SmartFormsModules.ContainerManager.DeleteContainerOfField(field); this.DynamicItems=[]; this.Fields=[]; if(this.JQueryElement!=null) this.JQueryElement.find('.fieldContainerOfFields').empty(); } GenerationCompleted($element: any) { if(!smartFormsDesignMode) { this.JQueryElement.find('.repeaterAddButton').click((e)=>{ e.preventDefault(); this.AddNewItem(true); }); } let numberOfItems=this.Options.NumberOfItems; if(isNaN(parseInt(numberOfItems.toString()))) return; if(smartFormsDesignMode) numberOfItems=1; let currentAddedItemsCount=this.DynamicItems.length; let delta=numberOfItems-currentAddedItemsCount; if(delta>0) { if(delta>99) delta=99; for(let i=0;idelta;i--) this.RemoveItem(this.AnimateInsertion); } this.AnimateInsertion=true; this.RefreshRowNumbers(); this.FirePropertyChanged(); } RefreshElement(propertyName?: string, previousValue?: any) { this.JQueryElement.find('.rednao-control-group').each((fieldIndex: number, fieldElement: any) => { let fieldId=rnJQuery(fieldElement).attr('id'); let fieldOptions=this.Options.FieldOptions.find(x=>x.Id==fieldId); if(fieldOptions==null) return; rnJQuery(fieldElement).find('.rednao_label_container label').text(fieldOptions.Label); }); return super.RefreshElement(propertyName, previousValue); } SetData(data: any) { for(let i=0;i
Repeater `; }else{ component= `
` } if(this.Options.ManuallyAdd=="y") component+=`
`; component+='
'; return component; } CreateProperties() { this.Properties.push(new PropertyContainer('general','General').AddProperties([ new SimpleNumericProperty(this,this.Options,"NumberOfItems","How many times do you want to repeat this section?",{ManipulatorType:'basic'}).SetEnableFormula(), new CheckBoxProperty(this,this.Options,"ManuallyAdd","Include add button",{ManipulatorType:'basic'}) ])); this.Properties.push(new PropertyContainer('icons','Tweaks').AddProperties([ new CheckBoxProperty(this,this.Options,"IncludeItemNumberInLabels","Include item number in labels",{ManipulatorType:'basic'}) ])); this.Properties.push(new PropertyContainer('advanced','Advanced').AddProperties([ new IdProperty(this,this.Options), new CustomCSSProperty(this,this.Options) ])); } private AddNewItem(animate=false) { let newDynamicRow:DynamicRow={ Fields:[], RowId:this.latestRowId++, $Container:rnJQuery('
') }; this.DynamicItems.push(newDynamicRow); let $container=newDynamicRow.$Container; if(animate) $container.css({opacity:0,position:'absolute',overflow:'hidden'}); this.JQueryElement.find('.fieldContainerOfFields').append($container); for(let field of this.Options.FieldOptions){ let originalId=field.Id; if(!smartFormsDesignMode) { field = Object.assign({}, field); field.Id = field.Id + '_row_' + newDynamicRow.RowId; field.ContainerOptions.Id += '_row_' + newDynamicRow.RowId; } field.FormId=this.Options.FormId; let fieldElement:sfFormElementBase=sfRedNaoCreateFormElementByName(field.ClassName,field); fieldElement.OriginalId=originalId; fieldElement.IsInternal=true; fieldElement.FormId=this.FormId; fieldElement.Generator=this.Generator; fieldElement.RowIndex=newDynamicRow.RowId; fieldElement.InvalidInputMessage=this.InvalidInputMessage; fieldElement.ClientOptions=this.ClientOptions; this.Fields.push(fieldElement); fieldElement.AppendElementToContainer($container); fieldElement.FieldContainer=this; fieldElement._parentId=this.Options.Id; newDynamicRow.Fields.push(fieldElement); fieldElement.FirePropertyChanged(); if(!smartFormsDesignMode&&typeof RedNaoFormulaManagerVar!='undefined') { let formulas = RedNaoFormulaManagerVar.Formulas.filter(x => x.FormElement == fieldElement); if (formulas != null) for (let formula of formulas) formula.UpdateFieldWithValue(RedNaoFormulaManagerVar.Data); } } $container.append('
'); if(this.Options.ManuallyAdd=="y"&&!smartFormsDesignMode) { let $button=rnJQuery(`
`); $button.find('.repeaterAddButton').click((e)=>{ e.preventDefault(); this.RemoveItem(true,this.DynamicItems.indexOf(newDynamicRow)); }); $container.append($button); } if(animate) { let height=$container.outerHeight(); $container.height(0); $container.css('width','100%'); $container.css('opacity',''); $container.css('position',''); $container.velocity({height:[height,0]},200,'easeInExp',()=>{ $container.css('overflow',''); $container.css('height',''); $container.css('width',''); }); } this.RefreshRowNumbers(); } private RemoveItem(animate=false,index:number=-1) { if(this.DynamicItems.length==0) return; let itemToRemove; if(index==-1) index=this.DynamicItems.length-1; itemToRemove=this.DynamicItems[index]; itemToRemove.$Container.css('overflow','hidden'); let fieldsToRemove=this.DynamicItems[this.DynamicItems.length-1].Fields; for(let field of fieldsToRemove) this.Fields.splice(this.Fields.indexOf(field),1); this.DynamicItems.splice(index,1); if(this.DynamicItems.length>=index) for(let i=index;i{itemToRemove.$Container.remove();},()=>{ itemToRemove.$Container.remove(); this.RefreshRowNumbers(); }); else { itemToRemove.$Container.remove(); this.RefreshRowNumbers(); } } private RefreshRowNumbers() { this.JQueryElement.find('.repeaterRow').each((index:number,element:any)=>{ let $repeaterSection=rnJQuery(element); $repeaterSection.attr('data-row-number',index); if(this.Options.IncludeItemNumberInLabels=="y") { $repeaterSection.find('.rednao-control-group').each((fieldIndex: number, fieldElement: any) => { let fieldId=rnJQuery(fieldElement).attr('id'); fieldId=fieldId.replace(/_row_[0-9]*/,''); let fieldOptions=this.Options.FieldOptions.find(x=>x.Id==fieldId); if(fieldOptions==null) return; rnJQuery(fieldElement).find('.rednao_label_container label').text(fieldOptions.Label+' #'+(index+1)); }); } }); this.FirePropertyChanged(); } } } interface DynamicRow{ RowId:number, Fields:sfFormElementBase[], $Container:JQuery } interface RepeaterOptions extends FieldOptions{ NumberOfItems:number; ManuallyAdd:'y'|'n'; IncludeItemNumberInLabels:'y'|'n'; FieldOptions:FieldOptions[]; }