@@ -5,10 +5,10 @@ import { FormControl, FormGroup } from '@angular/forms';
55import { TranslateService } from '@ngx-translate/core' ;
66
77import { AngularForms , Status } from '.' ;
8- import { Group } from './group' ;
9- import { DependencyService , Select , SelectService , Question } from './question' ;
8+ import { DataTable , Fieldset , Group , GroupType } from './group' ;
9+ import { Answer , DependencyService , Question , Select , SelectService } from './question' ;
1010import { ReactiveFormsFactory } from './factory' ;
11- import { StringUtils } from './util' ;
11+ import { ObjectUtils , StringUtils } from './util' ;
1212
1313@Component ( {
1414 selector : 'rb-angular-forms' ,
@@ -187,6 +187,7 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
187187 @Output ( ) public error : EventEmitter < Error > = new EventEmitter ( ) ;
188188 @Output ( ) public ready : EventEmitter < boolean > = new EventEmitter ( ) ;
189189
190+ private _originalValueFormGroup : Object ;
190191 private _status : Status ;
191192
192193 public constructor (
@@ -249,28 +250,28 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
249250 }
250251
251252 public isPristine ( ) : boolean {
252- return this . formGroup . pristine ;
253+ return ObjectUtils . isEquals ( this . _originalValueFormGroup , this . formGroup . value ) ;
253254 }
254255
255256 public isDirty ( ) : boolean {
256- return this . formGroup . dirty ;
257+ return ! ObjectUtils . isEquals ( this . _originalValueFormGroup , this . formGroup . value ) ;
257258 }
258259
259260 public isValid ( ) : boolean {
260261 return this . formGroup . valid ;
261262 }
262263
263264 public getAnswersGroups ( ) : Object {
264- const answersGroups : Object = this . formGroup . value ;
265+ const answersGroups : Object = ObjectUtils . clone ( this . formGroup . value ) ;
265266
266267 Object . keys ( answersGroups ) . forEach ( ( groupIndex : string ) => {
267268 if ( answersGroups [ groupIndex ] instanceof Array ) {
268- ( < Array < Object > > answersGroups [ groupIndex ] ) . map ( ( answersGroup : Object ) => this . convertAnswersOfGroupToString ( answersGroup ) ) ;
269-
269+ ( < Array < Object > > answersGroups [ groupIndex ] ) . map ( ( answersGroup : Object , index : number ) =>
270+ this . convertAnswersDataTable ( answersGroup , < DataTable > this . getGroupByCode ( groupIndex ) , index + 1 ) ) ;
270271 return ;
271272 }
272273
273- answersGroups [ groupIndex ] = this . convertAnswersOfGroupToString ( answersGroups [ groupIndex ] ) ;
274+ answersGroups [ groupIndex ] = this . convertAnswersFieldset ( answersGroups [ groupIndex ] , < Fieldset > this . getGroupByCode ( groupIndex ) ) ;
274275 } ) ;
275276
276277 return answersGroups ;
@@ -283,7 +284,6 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
283284 Object . keys ( answersGroups ) . forEach ( ( groupIndex : string ) => {
284285 if ( answersGroups [ groupIndex ] instanceof Array ) {
285286 answers [ groupIndex ] = answersGroups [ groupIndex ] ;
286-
287287 return ;
288288 }
289289
@@ -300,6 +300,10 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
300300 this . error . emit ( error ) ;
301301 }
302302
303+ public get originalValueFormGroup ( ) : Object {
304+ return this . _originalValueFormGroup ;
305+ }
306+
303307 public get status ( ) : Status {
304308 return this . _status ;
305309 }
@@ -315,16 +319,38 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
315319 try {
316320 this . groups = await AngularForms . fromJson ( this . groups ) ;
317321 this . formGroup = await ReactiveFormsFactory . createFormGroupFromGroups ( this . groups ) ;
322+ setTimeout ( ( ) => this . initOriginalValueFormGroup ( ) , 2000 ) ;
318323 resolve ( ) ;
319324 } catch ( error ) {
320325 reject ( error ) ;
321326 }
322327 } ) ;
323328 }
324329
325- private convertAnswersOfGroupToString ( answersGroup : Object ) : Object {
326- Object . keys ( answersGroup )
327- . forEach ( ( questionIndex : string ) => answersGroup [ questionIndex ] = StringUtils . convertToString ( answersGroup [ questionIndex ] ) ) ;
330+ private initOriginalValueFormGroup ( ) : void {
331+ this . _originalValueFormGroup = ObjectUtils . clone ( this . formGroup . value ) ;
332+ }
333+
334+ private convertAnswersFieldset ( answersGroup : Object , group : Fieldset ) : Object {
335+ Object . keys ( answersGroup ) . forEach ( ( questionName : string ) => {
336+ const question : Question < any > = group . getQuestionByName ( questionName ) ;
337+ const id : number | string = ( < Answer < any > > question . answer ) . id || null ;
338+ const value : string = StringUtils . convertToString ( answersGroup [ questionName ] ) ;
339+
340+ answersGroup [ questionName ] = new Answer ( value , id ) ;
341+ } ) ;
342+
343+ return answersGroup ;
344+ }
345+
346+ private convertAnswersDataTable ( answersGroup : Object , group : DataTable , index : number ) : Object {
347+ Object . keys ( answersGroup ) . forEach ( ( questionName : string ) => {
348+ const question : Question < any > = group . getQuestionByNameAndIndex ( questionName , index ) ;
349+ const id : number | string = question ? ( < Answer < any > > question . answer ) . id : null ;
350+ const value : string = StringUtils . convertToString ( answersGroup [ questionName ] ) ;
351+
352+ answersGroup [ questionName ] = new Answer ( value , id ) ;
353+ } ) ;
328354
329355 return answersGroup ;
330356 }
0 commit comments