@ -5,25 +5,40 @@
* Copyright ( c ) Squidex UG ( haftungsbeschränkt ) . All rights reserved .
* /
import { Component , ElementRef , EventEmitter , Input , OnInit , Output , ViewChild } from '@angular/core' ;
import { FormBuilder } from '@angular/forms' ;
import {
Component ,
ElementRef ,
EventEmitter ,
Input ,
OnInit ,
Output ,
ViewChild
} from "@angular/core" ;
import { FormBuilder } from "@angular/forms" ;
import {
AddFieldForm ,
AppPatternDto ,
createProperties ,
EditFieldForm ,
FieldDto ,
fieldTypes ,
ImmutableArray ,
PatternsState ,
RootFieldDto ,
SchemaDetailsDto ,
SchemasState ,
Types
} from '@app/shared' ;
Types ,
UpdateFieldDto
} from "@app/shared" ;
import { onErrorResumeNext } from "rxjs/operators" ;
@Component ( {
selector : 'sqx-field-wizard' ,
styleUrls : [ './field-wizard.component.scss' ] ,
templateUrl : './field-wizard.component.html'
selector : "sqx-field-wizard" ,
styleUrls : [ "./field-wizard.component.scss" ] ,
templateUrl : "./field-wizard.component.html"
} )
export class FieldWizardComponent implements OnInit {
@ViewChild ( 'nameInput' )
@ViewChild ( "nameInput" )
public nameInput : ElementRef ;
@Input ( )
@ -38,42 +53,94 @@ export class FieldWizardComponent implements OnInit {
public fieldTypes = fieldTypes ;
public addFieldForm = new AddFieldForm ( this . formBuilder ) ;
public editForm = new EditFieldForm ( this . formBuilder ) ;
public field : FieldDto ;
public isEditing = false ;
public selectedTab = 0 ;
public patterns : ImmutableArray < AppPatternDto > ;
public step = 1 ;
constructor (
private readonly formBuilder : FormBuilder ,
private readonly schemasState : SchemasState
) {
}
private readonly schemasState : SchemasState ,
public readonly patternsState : PatternsState
) { }
public ngOnInit() {
if ( this . parent ) {
this . fieldTypes = this . fieldTypes . filter ( x = > x . type !== 'Array' ) ;
this . fieldTypes = this . fieldTypes . filter ( x = > x . type !== "Array" ) ;
}
this . patternsState
. load ( )
. pipe ( onErrorResumeNext ( ) )
. subscribe ( ) ;
}
public complete() {
this . completed . emit ( ) ;
}
public addField ( next : boolean ) {
public addField ( next : boolean , edi t : boolean ) {
const value = this . addFieldForm . submit ( ) ;
if ( value ) {
this . schemasState . addField ( this . schema , value , this . parent )
. subscribe ( dto = > {
this . schemasState . addField ( this . schema , value , this . parent ) . subscribe (
dto = > {
this . field = dto ;
this . addFieldForm . submitCompleted ( { type : fieldTypes [ 0 ] . type } ) ;
if ( next ) {
if ( Types . isFunction ( this . nameInput . nativeElement . focus ) ) {
this . nameInput . nativeElement . focus ( ) ;
}
} else if ( edit ) {
this . selectTab ( 0 ) ;
this . step ++ ;
} else {
this . complete ( ) ;
}
} , error = > {
} ,
error = > {
this . addFieldForm . submitFailed ( error ) ;
} ) ;
}
) ;
}
}
public selectTab ( tab : number ) {
this . selectedTab = tab ;
}
public save ( addNew : boolean ) {
const value = this . editForm . submit ( ) ;
if ( value ) {
const properties = createProperties (
this . field . properties [ "fieldType" ] ,
value
) ;
this . schemasState
. updateField (
this . schema ,
this . field as RootFieldDto ,
new UpdateFieldDto ( properties )
)
. subscribe (
( ) = > {
this . isEditing = false ;
this . editForm . submitCompleted ( ) ;
if ( addNew ) {
this . step -- ;
} else {
this . complete ( ) ;
}
} ,
error = > {
this . editForm . submitFailed ( error ) ;
}
) ;
}
}
}