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
5- import { Subscription } from 'rxjs' ;
65import { hasValue } from '../empty.util' ;
6+ import { environment } from '../../../environments/environment' ;
7+ import { AlertType } from '../alert/alert-type' ;
8+
9+ enum MessageType {
10+ LOADING = 'loading' ,
11+ WARNING = 'warning' ,
12+ ERROR = 'error'
13+ }
714
815@Component ( {
916 selector : 'ds-loading' ,
@@ -15,29 +22,57 @@ export class LoadingComponent implements OnDestroy, OnInit {
1522 @Input ( ) message : string ;
1623 @Input ( ) showMessage = true ;
1724
25+ @Input ( ) showFallbackMessages = environment . loader . showFallbackMessagesByDefault ;
26+ @Input ( ) warningMessage : string ;
27+ @Input ( ) warningMessageDelay = environment . loader . warningMessageDelay ;
28+ @Input ( ) errorMessage : string ;
29+ @Input ( ) errorMessageDelay = environment . loader . errorMessageDelay ;
30+
1831 /**
1932 * Show a more compact spinner animation instead of the default one
2033 */
2134 @Input ( ) spinner = false ;
2235
23- private subscription : Subscription ;
36+ readonly MessageType = MessageType ;
37+ messageToShow : MessageType = this . showMessage ? MessageType . LOADING : undefined ;
38+
39+ warningTimeout : any ;
40+ errorTimeout : any ;
2441
25- constructor ( private translate : TranslateService ) {
42+ readonly AlertTypeEnum = AlertType ;
43+
44+ constructor ( private translate : TranslateService , private changeDetectorRef : ChangeDetectorRef ) {
2645
2746 }
2847
2948 ngOnInit ( ) {
30- if ( this . message === undefined ) {
31- this . subscription = this . translate . get ( 'loading.default' ) . subscribe ( ( message : string ) => {
32- this . message = message ;
33- } ) ;
49+ if ( this . showMessage ) {
50+ this . message = this . message || this . translate . instant ( 'loading.default' ) ;
51+ }
52+ if ( this . showFallbackMessages ) {
53+ this . warningMessage = this . warningMessage || this . translate . instant ( 'loading.warning' ) ;
54+ this . errorMessage = this . errorMessage || this . translate . instant ( 'loading.error' ) ;
55+ if ( this . warningMessageDelay > 0 ) {
56+ this . warningTimeout = setTimeout ( ( ) => {
57+ this . messageToShow = MessageType . WARNING ;
58+ this . changeDetectorRef . detectChanges ( ) ;
59+ } , this . warningMessageDelay ) ;
60+ }
61+ if ( this . errorMessageDelay > 0 ) {
62+ this . errorTimeout = setTimeout ( ( ) => {
63+ this . messageToShow = MessageType . ERROR ;
64+ this . changeDetectorRef . detectChanges ( ) ;
65+ } , this . errorMessageDelay ) ;
66+ }
3467 }
3568 }
3669
3770 ngOnDestroy ( ) {
38- if ( hasValue ( this . subscription ) ) {
39- this . subscription . unsubscribe ( ) ;
71+ if ( hasValue ( this . warningTimeout ) ) {
72+ clearTimeout ( this . warningTimeout ) ;
73+ }
74+ if ( hasValue ( this . errorTimeout ) ) {
75+ clearTimeout ( this . errorTimeout ) ;
4076 }
4177 }
42-
4378}
0 commit comments