@ -25,6 +25,7 @@ import {
CanComponentDeactivate ,
CanComponentDeactivate ,
ContentDto ,
ContentDto ,
ContentsService ,
ContentsService ,
fieldInvariant ,
SchemaDetailsDto ,
SchemaDetailsDto ,
Version
Version
} from 'shared' ;
} from 'shared' ;
@ -40,6 +41,7 @@ import {
export class ContentPageComponent implements CanComponentDeactivate , OnDestroy , OnInit {
export class ContentPageComponent implements CanComponentDeactivate , OnDestroy , OnInit {
private contentStatusChangedSubscription : Subscription ;
private contentStatusChangedSubscription : Subscription ;
private contentDeletedSubscription : Subscription ;
private contentDeletedSubscription : Subscription ;
private contentUpdatedSubscription : Subscription ;
private contentVersionSelectedSubscription : Subscription ;
private contentVersionSelectedSubscription : Subscription ;
public schema : SchemaDetailsDto ;
public schema : SchemaDetailsDto ;
@ -62,6 +64,7 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
public ngOnDestroy() {
public ngOnDestroy() {
this . contentVersionSelectedSubscription . unsubscribe ( ) ;
this . contentVersionSelectedSubscription . unsubscribe ( ) ;
this . contentStatusChangedSubscription . unsubscribe ( ) ;
this . contentStatusChangedSubscription . unsubscribe ( ) ;
this . contentUpdatedSubscription . unsubscribe ( ) ;
this . contentDeletedSubscription . unsubscribe ( ) ;
this . contentDeletedSubscription . unsubscribe ( ) ;
}
}
@ -80,6 +83,14 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
}
}
} ) ;
} ) ;
this . contentUpdatedSubscription =
this . ctx . bus . of ( ContentUpdated )
. subscribe ( message = > {
if ( this . content && message . content . id === this . content . id ) {
this . reloadContentForm ( message . content ) ;
}
} ) ;
this . contentStatusChangedSubscription =
this . contentStatusChangedSubscription =
this . ctx . bus . of ( ContentStatusChanged )
this . ctx . bus . of ( ContentStatusChanged )
. subscribe ( message = > {
. subscribe ( message = > {
@ -96,15 +107,12 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
const routeData = allData ( this . ctx . route ) ;
const routeData = allData ( this . ctx . route ) ;
this . languages = routeData . appLanguages ;
this . setupLanguages ( routeData ) ;
this . setupContentForm ( routeData . schema ) ;
this . setupContentForm ( routeData . schema ) ;
this . ctx . route . data . map ( d = > d . content )
this . ctx . route . data . map ( d = > d . content )
. subscribe ( ( content : ContentDto ) = > {
. subscribe ( ( content : ContentDto ) = > {
this . content = content ;
this . reloadContentForm ( content ) ;
this . populateContentForm ( ) ;
} ) ;
} ) ;
}
}
@ -122,7 +130,7 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
this . contentOld = null ;
this . contentOld = null ;
this . emitContentUpdated ( this . content ) ;
this . emitContentUpdated ( this . content ) ;
this . populateContentForm ( ) ;
this . reloadContentForm ( this . content ) ;
}
}
}
}
@ -159,13 +167,13 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
} else {
} else {
this . contentsService . putContent ( this . ctx . appName , this . schema . name , this . content . id , requestDto , this . content . version )
this . contentsService . putContent ( this . ctx . appName , this . schema . name , this . content . id , requestDto , this . content . version )
. subscribe ( dto = > {
. subscribe ( dto = > {
this . content = this . content . update ( dto . payload , this . ctx . userToken , dto . version ) ;
const content = this . content . update ( dto . payload , this . ctx . userToken , dto . version ) ;
this . ctx . notifyInfo ( 'Content saved successfully.' ) ;
this . ctx . notifyInfo ( 'Content saved successfully.' ) ;
this . emitContentUpdated ( this . content ) ;
this . emitContentUpdated ( content ) ;
this . enableContentForm ( ) ;
this . enableContentForm ( ) ;
this . populateContentForm ( ) ;
this . reloadContentForm ( content ) ;
} , error = > {
} , error = > {
this . ctx . notifyError ( error ) ;
this . ctx . notifyError ( error ) ;
@ -187,12 +195,9 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
this . contentOld = null ;
this . contentOld = null ;
}
}
this . content = this . content . setData ( dto ) ;
this . ctx . notifyInfo ( 'Content version loaded successfully.' ) ;
this . ctx . notifyInfo ( 'Content version loaded successfully.' ) ;
this . emitContentUpdated ( this . content ) ;
this . reloadContentForm ( this . content . setData ( dto ) ) ;
this . populateContentForm ( ) ;
} , error = > {
} , error = > {
this . ctx . notifyError ( error ) ;
this . ctx . notifyError ( error ) ;
} ) ;
} ) ;
@ -221,35 +226,48 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
if ( this . schema . fields . length === 0 ) {
if ( this . schema . fields . length === 0 ) {
this . contentForm . enable ( ) ;
this . contentForm . enable ( ) ;
} else {
} else {
for ( const field of this . schema . fields . filter ( f = > ! f . isDisabled ) ) {
for ( const field of this . schema . fields ) {
this . contentForm . controls [ field . name ] . enable ( ) ;
const fieldForm = < FormGroup > this . contentForm . controls [ field . name ] ;
if ( field . isDisabled ) {
fieldForm . disable ( ) ;
} else {
fieldForm . enable ( ) ;
}
}
}
}
}
}
}
private setupLanguages ( routeData : { [ name : string ] : any ; } ) {
this . languages = routeData . appLanguages ;
}
private setupContentForm ( schema : SchemaDetailsDto ) {
private setupContentForm ( schema : SchemaDetailsDto ) {
this . schema = schema ;
this . schema = schema ;
const controls : { [ key : string ] : AbstractControl } = { } ;
const controls : { [ key : string ] : AbstractControl } = { } ;
for ( const field of schema . fields ) {
for ( const field of schema . fields ) {
const group = new FormGroup ( { } ) ;
const fieldForm = new FormGroup ( { } ) ;
if ( field . partitioning === 'language' ) {
if ( field . isLocalizable ) {
for ( let language of this . languages ) {
for ( let language of this . languages ) {
group . add Control( language . iso2Code , new FormControl ( undefined , field . createValidators ( language . isOptional ) ) ) ;
fieldForm . set Control( language . iso2Code , new FormControl ( undefined , field . createValidators ( language . isOptional ) ) ) ;
}
}
} else {
} else {
group . addControl ( 'iv' , new FormControl ( undefined , field . createValidators ( false ) ) ) ;
fieldForm . setControl ( fieldInvariant , new FormControl ( undefined , field . createValidators ( false ) ) ) ;
}
}
controls [ field . name ] = group ;
controls [ field . name ] = fieldForm ;
}
}
this . contentForm = new FormGroup ( controls ) ;
this . contentForm = new FormGroup ( controls ) ;
this . enableContentForm ( ) ;
}
}
private populateContentForm() {
private reloadContentForm ( content : ContentDto ) {
this . content = content ;
this . contentForm . markAsPristine ( ) ;
this . contentForm . markAsPristine ( ) ;
this . isNewMode = ! this . content ;
this . isNewMode = ! this . content ;
@ -257,14 +275,14 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
if ( ! this . isNewMode ) {
if ( ! this . isNewMode ) {
for ( const field of this . schema . fields ) {
for ( const field of this . schema . fields ) {
const fieldValue = this . content . data [ field . name ] || { } ;
const fieldValue = this . content . data [ field . name ] || { } ;
const fieldForm = < FormGroup > this . contentForm . get ( field . name ) ;
const fieldForm = < FormGroup > this . contentForm . controls [ field . name ] ;
if ( field . partitioning === 'language' ) {
if ( field . isLocalizable ) {
for ( let language of this . languages ) {
for ( let language of this . languages ) {
fieldForm . controls [ language . iso2Code ] . setValue ( fieldValue [ language . iso2Code ] ) ;
fieldForm . controls [ language . iso2Code ] . setValue ( fieldValue [ language . iso2Code ] ) ;
}
}
} else {
} else {
fieldForm . controls [ 'iv' ] . setValue ( fieldValue [ 'iv' ] === undefined ? null : fieldValue [ 'iv' ] ) ;
fieldForm . controls [ fieldInvariant ] . setValue ( fieldValue [ fieldInvariant ] === undefined ? null : fieldValue [ fieldInvariant ] ) ;
}
}
}
}
if ( this . content . status === 'Archived' ) {
if ( this . content . status === 'Archived' ) {
@ -273,14 +291,16 @@ export class ContentPageComponent implements CanComponentDeactivate, OnDestroy,
} else {
} else {
for ( const field of this . schema . fields ) {
for ( const field of this . schema . fields ) {
const defaultValue = field . defaultValue ( ) ;
const defaultValue = field . defaultValue ( ) ;
if ( defaultValue ) {
if ( defaultValue ) {
const fieldForm = < FormGroup > this . contentForm . get ( field . name ) ;
const fieldForm = < FormGroup > this . contentForm . controls [ field . name ] ;
if ( field . partitioning === 'language' ) {
if ( field . isLocalizable ) {
for ( let language of this . languages ) {
for ( let language of this . languages ) {
fieldForm . controls [ language . iso2Code ] . setValue ( defaultValue ) ;
fieldForm . controls [ language . iso2Code ] . setValue ( defaultValue ) ;
}
}
} else {
} else {
fieldForm . controls [ 'iv' ] . setValue ( defaultValue ) ;
fieldForm . controls [ fieldInvariant ] . setValue ( defaultValue ) ;
}
}
}
}
}
}