import { Observable , of } from 'rxjs' ; import { mergeMap , finalize } from 'rxjs/operators' ; import { MatDialog } from '@angular/material/dialog' ; import { ActionDialogComponent } from '../shared/component/action-dialog/action-dialog.component' ; import { ComponentType } from '@angular/cdk/portal' ; export class Action { public running : boolean ; public get displayTitle ( ) : string { return this . running ? this . runningTitle : this . title ; } constructor ( public name , public title , public runningTitle , protected execute , public canRun , public isAdvanced = false ) { this . running = false ; } public run ( ... params ) { this . runInternal ( ( ) => null , ( ) => null , params ) . subscribe ( ) ; } public runWithCallbacks ( success , error , ... params ) { return this . runInternal ( success , error , params ) ; } protected runInternal ( success , error , ... params ) { if ( this . canRun ( ) ) { this . running = true ; const executing = this . execute ( ) ; return executing . pipe ( finalize ( ( ) => { this . running = false ; } ) ) ; } } } export class ActionWithDialog extends Action { param name param title param runningTitle param execute param canRun param modalSettings param beforeOpen template = ActionDialogComponent ; constructor ( public dialog , public name , public title , public runningTitle , public execute , public canRun , public beforeOpen ? ) { super ( name , title , runningTitle , execute , canRun ) ; } protected runInternal ( success , error , ... params ) { if ( this . canRun ( ) ) { return of ( this . beforeOpen ? this . beforeOpen ( ) : true ) . pipe ( mergeMap ( ( ) => { const dialogRef = this . dialog . open ( this . template , { data : this , panelClass : 'mat-dialog-container-wrapper' } ) ; return dialogRef . afterClosed ( ) . pipe ( mergeMap ( ( data ) => { if ( data ) { return super . runInternal ( success , error , params ) ; } return of ( null ) ; } ) ) ; } ) ) ; } else { return of ( null ) ; } } } export class ActionWithConfirmationDialog extends ActionWithDialog { constructor ( public dialog , public name , public title , public runningTitle , public execute , public canRun , public confirmationDialogTitle ? , public confirmationDialogMessage ? , public confirmationKeyword ? ) { super ( dialog , name , title , runningTitle , execute , canRun ) ; } } export class IsolatedAction extends Action { constructor ( public dialog , public name , public title , public runningTitle , public data , public template , public canRun , public beforeOpen ? ) { super ( name , title , runningTitle , null , canRun ) ; } protected runInternal ( success , error , ... params ) { if ( this . canRun ( ) ) { return ( ! ! this . beforeOpen ? this . beforeOpen ( ) : of ( null ) ) . pipe ( mergeMap ( ( ) => { const dialogRef = this . dialog . open ( this . template , { data : this , panelClass : 'mat-dialog-container-wrapper' } ) ; return dialogRef . afterClosed ( ) ; } ) ) ; } else { return of ( null ) ; } } }