11import {
2- AfterViewInit , ChangeDetectorRef , Component , ContentChildren , HostBinding , Input , QueryList , Type ,
2+ AfterViewInit , ChangeDetectorRef , Component , ContentChildren , HostBinding , Input , OnChanges , QueryList , Type ,
33} from "@angular/core" ;
44import { FormControl } from "@angular/forms" ;
55
6- import { Dto , autobind } from "app/core" ;
6+ import { AsyncTask , Dto , autobind } from "app/core" ;
77import { ServerError } from "app/models" ;
88import { log } from "app/utils" ;
99import { validJsonConfig } from "app/utils/validators" ;
10- import { Observable } from "rxjs" ;
10+ import { Observable , Subscription } from "rxjs" ;
1111import { FormBase } from "../form-base" ;
1212import { FormPageComponent } from "../form-page" ;
13+ import { FormActionConfig } from "./footer" ;
14+
1315import "./complex-form.scss" ;
1416
1517export type FormSize = "small" | "medium" | "large" ;
@@ -34,7 +36,7 @@ export const defaultComplexFormConfig: ComplexFormConfig = {
3436 selector : "bl-complex-form" ,
3537 templateUrl : "complex-form.html" ,
3638} )
37- export class ComplexFormComponent extends FormBase implements AfterViewInit {
39+ export class ComplexFormComponent extends FormBase implements AfterViewInit , OnChanges {
3840 /**
3941 * If the form should allow multi use. \
4042 * If true the form will have a "Save" AND a "Save and Close" button.
@@ -62,6 +64,7 @@ export class ComplexFormComponent extends FormBase implements AfterViewInit {
6264 * Needs to return an observable that will have a {ServerError} if failing.
6365 */
6466 @Input ( ) public submit : ( dto ?: Dto < any > ) => Observable < any > ;
67+ @Input ( ) public asyncTasks : Observable < AsyncTask [ ] > ;
6568
6669 @Input ( ) @HostBinding ( "class" ) public size : FormSize = "large" ;
6770
@@ -73,8 +76,13 @@ export class ComplexFormComponent extends FormBase implements AfterViewInit {
7376 public currentPage : FormPageComponent ;
7477 public showJsonEditor = false ;
7578 public jsonValue = new FormControl ( null , null , validJsonConfig ) ;
79+ public waitingForAsyncTask = false ;
80+ public asyncTaskList : AsyncTask [ ] ;
81+ public actionConfig : FormActionConfig ;
7682
7783 private _pageStack : FormPageComponent [ ] = [ ] ;
84+ private _asyncTaskSub : Subscription ;
85+ private _hasAsyncTask = false ;
7886
7987 constructor ( private changeDetector : ChangeDetectorRef ) {
8088 super ( ) ;
@@ -90,14 +98,28 @@ export class ComplexFormComponent extends FormBase implements AfterViewInit {
9098 this . changeDetector . detectChanges ( ) ;
9199 }
92100
93- public get isMainWindow ( ) {
94- return this . currentPage === this . mainPage ;
101+ public ngOnChanges ( changes ) {
102+ if ( changes . asyncTasks ) {
103+ this . _listenToAsyncTasks ( ) ;
104+ }
105+ this . _buildActionConfig ( ) ;
95106 }
96107
97108 @autobind ( )
98109 public save ( ) : Observable < any > {
110+ let ready ;
111+ if ( this . _hasAsyncTask ) {
112+ this . waitingForAsyncTask = true ;
113+ ready = this . asyncTasks . filter ( x => [ ...x ] . length === 0 ) . first ( ) . do ( ( ) => {
114+ this . waitingForAsyncTask = false ;
115+ } ) . share ( ) ;
116+ } else {
117+ ready = Observable . of ( null ) ;
118+ }
99119 this . loading = true ;
100- const obs = this . submit ( this . getCurrentDto ( ) ) ;
120+ const obs = ready . flatMap ( ( ) => {
121+ return this . submit ( this . getCurrentDto ( ) ) ;
122+ } ) . shareReplay ( 1 ) ;
101123 obs . subscribe ( {
102124 next : ( ) => {
103125 this . loading = false ;
@@ -163,6 +185,14 @@ export class ComplexFormComponent extends FormBase implements AfterViewInit {
163185 this . closePage ( ) ;
164186 }
165187
188+ public toggleJsonEditor ( jsonEditor ) {
189+ if ( jsonEditor ) {
190+ this . switchToJsonEditor ( ) ;
191+ } else {
192+ this . switchToClassicForm ( ) ;
193+ }
194+ }
195+
166196 public switchToJsonEditor ( ) {
167197 if ( ! this . config . jsonEditor ) { return ; }
168198 const obj = this . getCurrentDto ( ) . toJS ( ) ;
@@ -189,21 +219,6 @@ export class ComplexFormComponent extends FormBase implements AfterViewInit {
189219 }
190220 }
191221
192- public get saveAndCloseText ( ) {
193- return this . multiUse ? `${ this . actionName } and close` : this . actionName ;
194- }
195-
196- /**
197- * Enabled if the formGroup is valid or there is no formGroup
198- */
199- public get submitEnabled ( ) {
200- if ( this . showJsonEditor ) {
201- return this . jsonValue . valid ;
202- } else {
203- return ! this . formGroup || this . formGroup . valid ;
204- }
205- }
206-
207222 /**
208223 * There are two cases that classic form selector in footer should be disabled
209224 * 1. showJsonEditor variable is false which means current form is already classic form
@@ -218,4 +233,27 @@ export class ComplexFormComponent extends FormBase implements AfterViewInit {
218233 const data = JSON . parse ( this . jsonValue . value ) ;
219234 return new this . config . jsonEditor . dtoType ( data || { } ) ;
220235 }
236+
237+ private _listenToAsyncTasks ( ) {
238+ if ( this . _asyncTaskSub ) {
239+ this . _asyncTaskSub . unsubscribe ( ) ;
240+ this . _asyncTaskSub = null ;
241+ this . _hasAsyncTask = false ;
242+ }
243+ if ( this . asyncTasks ) {
244+ this . _asyncTaskSub = this . asyncTasks . subscribe ( ( asyncTasks ) => {
245+ this . asyncTaskList = asyncTasks ;
246+ this . _hasAsyncTask = asyncTasks . length > 0 ;
247+ } ) ;
248+ }
249+ }
250+
251+ private _buildActionConfig ( ) {
252+ this . actionConfig = {
253+ name : this . actionName ,
254+ color : this . actionColor ,
255+ cancel : this . cancelText ,
256+ multiUse : this . multiUse ,
257+ } ;
258+ }
221259}
0 commit comments