1- import { Component , Input , OnDestroy , OnInit } from '@angular/core' ;
1+ import { ChangeDetectorRef , Component , Input , OnDestroy , OnInit } from '@angular/core' ;
22
33import { TranslateService } from '@ngx-translate/core' ;
44
55import { Subscription } from 'rxjs' ;
66import { hasValue } from '../empty.util' ;
7+ import { environment } from '../../../environments/environment' ;
8+ import { AlertType } from '../alert/alert-type' ;
9+
10+ enum MessageType {
11+ LOADING = 'loading' ,
12+ WARNING = 'warning' ,
13+ ERROR = 'error'
14+ }
715
816@Component ( {
917 selector : 'ds-loading' ,
@@ -15,29 +23,74 @@ export class LoadingComponent implements OnDestroy, OnInit {
1523 @Input ( ) message : string ;
1624 @Input ( ) showMessage = true ;
1725
26+ @Input ( ) enableFallbackMessages = environment . loader . enableFallbackMessagesByDefault ;
27+ @Input ( ) warningMessage : string ;
28+ @Input ( ) warningMessageDelay = environment . loader . warningMessageDelay ;
29+ @Input ( ) errorMessage : string ;
30+ @Input ( ) errorMessageDelay = environment . loader . errorMessageDelay ;
31+
1832 /**
1933 * Show a more compact spinner animation instead of the default one
2034 */
2135 @Input ( ) spinner = false ;
2236
23- private subscription : Subscription ;
37+ readonly MessageType = MessageType ;
38+ messageToShow : MessageType = this . showMessage ? MessageType . LOADING : undefined ;
39+
40+ private subscriptions : Subscription [ ] = [ ] ;
41+
42+ warningTimeout : any ;
43+ errorTimeout : any ;
44+
45+ readonly AlertTypeEnum = AlertType ;
2446
25- constructor ( private translate : TranslateService ) {
47+ constructor ( private translate : TranslateService , private changeDetectorRef : ChangeDetectorRef ) {
2648
2749 }
2850
2951 ngOnInit ( ) {
30- if ( this . message === undefined ) {
31- this . subscription = this . translate . get ( 'loading.default' ) . subscribe ( ( message : string ) => {
52+ if ( this . showMessage && this . message === undefined ) {
53+ this . subscriptions . push ( this . translate . get ( 'loading.default' ) . subscribe ( ( message : string ) => {
3254 this . message = message ;
33- } ) ;
55+ } ) ) ;
56+ }
57+ if ( this . enableFallbackMessages ) {
58+ if ( ! this . warningMessage ) {
59+ this . subscriptions . push ( this . translate . get ( 'loading.warning' ) . subscribe ( ( warningMessage : string ) => {
60+ this . warningMessage = warningMessage ;
61+ } ) ) ;
62+ }
63+ if ( ! this . errorMessage ) {
64+ this . subscriptions . push ( this . translate . get ( 'loading.error' ) . subscribe ( ( errorMessage : string ) => {
65+ this . errorMessage = errorMessage ;
66+ } ) ) ;
67+ }
68+ if ( this . warningMessageDelay > 0 ) {
69+ this . warningTimeout = setTimeout ( ( ) => {
70+ this . messageToShow = MessageType . WARNING ;
71+ this . changeDetectorRef . detectChanges ( ) ;
72+ } , this . warningMessageDelay ) ;
73+ }
74+ if ( this . errorMessageDelay > 0 ) {
75+ this . errorTimeout = setTimeout ( ( ) => {
76+ this . messageToShow = MessageType . ERROR ;
77+ this . changeDetectorRef . detectChanges ( ) ;
78+ } , this . errorMessageDelay ) ;
79+ }
3480 }
3581 }
3682
3783 ngOnDestroy ( ) {
38- if ( hasValue ( this . subscription ) ) {
39- this . subscription . unsubscribe ( ) ;
84+ if ( this . subscriptions . length > 0 ) {
85+ this . subscriptions . forEach ( ( sub ) => {
86+ sub . unsubscribe ( ) ;
87+ } ) ;
88+ }
89+ if ( hasValue ( this . warningTimeout ) ) {
90+ clearTimeout ( this . warningTimeout ) ;
91+ }
92+ if ( hasValue ( this . errorTimeout ) ) {
93+ clearTimeout ( this . errorTimeout ) ;
4094 }
4195 }
42-
4396}
0 commit comments