@@ -19,7 +19,7 @@ export class FieldsController {
1919 formId : string
2020 fields : Array < any >
2121 fetching : boolean
22- newOptions : string
22+ optionsMap : any
2323
2424 constructor ( public stateService : StateService , public router : Router ,
2525 public fieldsService : FieldsService , public http : Http , public params : RouteParams ) {
@@ -54,9 +54,11 @@ export class FieldsController {
5454 }
5555 }
5656 this . formId = params . get ( 'formId' )
57+ this . optionsMap = { }
5758 fieldsService . getFieldsForForm ( this . formId )
5859 . subscribe ( resp => {
5960 this . fields = resp . json ( ) . fields
61+ this . updateOptions ( )
6062 this . fetching = false
6163 } )
6264 }
@@ -72,32 +74,53 @@ export class FieldsController {
7274 this . fieldsService . createFieldForForm ( this . formId , this . getNewField ( ) )
7375 . subscribe ( resp => {
7476 this . fields = resp . json ( ) . fields
77+ this . updateOptions ( )
7578 } )
7679 }
7780
7881 deleteField ( fieldId : string ) {
7982 this . fieldsService . deleteFieldFromForm ( this . formId , fieldId )
8083 . subscribe ( resp => {
8184 this . fields = resp . json ( ) . fields
85+ this . updateOptions ( )
8286 } )
8387 }
8488
8589 update ( field ) {
90+ if ( field . options ) {
91+ field . options = this . getOptionsFor ( field . _id )
92+ }
8693 this . fieldsService . updateField ( this . formId , field . _id , field )
8794 . subscribe ( resp => {
8895 this . fields = resp . json ( ) . fields
96+ this . updateOptions ( )
8997 } )
9098 }
9199
100+ getOptionsFor ( id ) {
101+ const optionString = this . optionsMap [ id ]
102+ const optionsArray = optionString . split ( '\n' )
103+ return optionsArray . map ( opt => {
104+ const labelValuePair = opt . split ( ":" )
105+ return { label : labelValuePair [ 0 ] , value : labelValuePair [ 1 ] }
106+ } )
107+ }
92108 isTextField ( type ) {
93109 return type === 'TEXT' || type === 'TEXTAREA'
94110 }
95111 isOptionField ( field ) {
96112 return field . type === 'DROPDOWN' || field . type === 'RADIOS' || field . type === 'CHECKBOXES'
97113 }
98- formatOptions ( options ) {
99- options . forEach ( opt => {
100- this . newOptions += `${ opt . label } :${ opt . value } \n`
114+
115+ updateOptions ( ) {
116+ this . fields . forEach ( field => {
117+ if ( field . options ) {
118+ let base = ""
119+ field . options . forEach ( opt => {
120+ base += `${ opt . label } :${ opt . value } \n`
121+ } )
122+ this . optionsMap [ field . _id ] = base
123+ }
101124 } )
102125 }
103126}
0 commit comments