11import { AfterViewInit , Component , ElementRef , EventEmitter , Input , Optional , Output , Renderer } from '@angular/core' ;
22
3+ import { NotifierAnimationService } from './../services/notifier-animation.service' ;
4+ import { NoifierAnimationData } from './../models/notifier-animation.model' ;
35import { NotifierConfigGlobal } from './../models/notifier-config-global.model' ;
46import { NotifierNotification } from './../models/notifier-notification.model' ;
57
@@ -38,15 +40,30 @@ export class NotifierNotificationComponent implements AfterViewInit {
3840 @Output ( )
3941 public dismiss : EventEmitter < NotifierNotification > ;
4042
43+ /**
44+ * Global configuration
45+ */
46+ public config : NotifierConfigGlobal ;
47+
4148 /**
4249 * Current notification height, calculated and cached here (#perfmatters)
4350 */
44- public currentElementHeight : number ;
51+ public elementHeight : number ;
4552
4653 /**
47- * Global configuration
54+ * Current notification width, calculated and cached here (#perfmatters)
55+ */
56+ public elementWidth : number ;
57+
58+ /**
59+ * Current notification shift, calculated and cached here (#perfmatters)
4860 */
49- private config : NotifierConfigGlobal ;
61+ public elementShift : number ;
62+
63+ /**
64+ * Notifier animation service
65+ */
66+ private animationService : NotifierAnimationService ;
5067
5168 /**
5269 * Native element reference, used for manipulating DOM properties
@@ -58,23 +75,22 @@ export class NotifierNotificationComponent implements AfterViewInit {
5875 */
5976 private renderer : Renderer ;
6077
61- /**
62- * Current notification shift, calculated and cached here (#perfmatters)
63- */
64- private currentElementShift : number ;
65-
6678 /**
6779 * Constructor
6880 * @param {ElementRef } elementRef Component element reference
6981 * @param {Renderer } renderer Angular rendering service
7082 */
71- public constructor ( elementRef : ElementRef , renderer : Renderer , @Optional ( ) config : NotifierConfigGlobal ) {
83+ public constructor ( animationService : NotifierAnimationService ,
84+ elementRef : ElementRef ,
85+ renderer : Renderer ,
86+ @Optional ( ) config : NotifierConfigGlobal ) {
7287 this . config = config === null ? new NotifierConfigGlobal ( ) : config ;
7388 this . init = new EventEmitter < NotifierNotificationComponent > ( ) ;
7489 this . dismiss = new EventEmitter < NotifierNotification > ( ) ;
7590 this . element = elementRef . nativeElement ;
91+ this . animationService = animationService ;
7692 this . renderer = renderer ;
77- this . currentElementShift = 0 ;
93+ this . elementShift = 0 ;
7894 }
7995
8096 /**
@@ -83,7 +99,8 @@ export class NotifierNotificationComponent implements AfterViewInit {
8399 */
84100 public ngAfterViewInit ( ) : void {
85101 this . setup ( ) ;
86- this . currentElementHeight = this . element . offsetHeight ; // Calculate current notification height and cache it (#perfmatters)
102+ this . elementHeight = this . element . offsetHeight ;
103+ this . elementWidth = this . element . offsetWidth ;
87104 this . init . emit ( this ) ;
88105 }
89106
@@ -92,20 +109,40 @@ export class NotifierNotificationComponent implements AfterViewInit {
92109 * @returns {Promise<null> } Empty promise, resolved when finished
93110 */
94111 public show ( ) : Promise < null > {
95- return new Promise < null > ( ( resolve : ( value ?: null ) => { } , reject : ( value ?: null ) => { } ) => {
112+
113+ // Decision: Are animations enabled?
114+ if ( this . config . animations . enabled ) {
115+ const animationData : NoifierAnimationData = this . animationService . getAnimationData ( 'in' , this . notification ) ;
116+ for ( let attribute in animationData . keyframes [ 0 ] ) { // Set pre-animation styles to prevent flickering
117+ this . renderer . setElementStyle ( this . element , attribute , animationData . keyframes [ 0 ] [ attribute ] ) ;
118+ }
96119 this . renderer . setElementStyle ( this . element , 'visibility' , 'visible' ) ;
97- resolve ( ) ;
98- } ) ;
120+ return this . element . animate ( animationData . keyframes , animationData . options ) . finished ; // Returns a promise
121+ } else {
122+ return new Promise < null > ( ( resolve : ( value ?: null ) => { } , reject : ( value ?: null ) => { } ) => {
123+ this . renderer . setElementStyle ( this . element , 'visibility' , 'visible' ) ;
124+ resolve ( ) ;
125+ } ) ;
126+ }
127+
99128 }
100129
101130 /**
102131 * Hide (animate out) notification
103132 * @returns {Promise<null> } Empty promise, resolved when finished
104133 */
105134 public hide ( ) : Promise < null > {
106- return new Promise < null > ( ( resolve : ( value ?: null ) => { } , reject : ( value ?: null ) => { } ) => {
107- resolve ( ) ;
108- } ) ;
135+
136+ // Decision: Are animations enabled?
137+ if ( this . config . animations . enabled ) {
138+ const animationData : NoifierAnimationData = this . animationService . getAnimationData ( 'out' , this . notification ) ;
139+ return this . element . animate ( animationData . keyframes , animationData . options ) . finished ; // Returns a promise
140+ } else {
141+ return new Promise < null > ( ( resolve : ( value ?: null ) => { } , reject : ( value ?: null ) => { } ) => {
142+ resolve ( ) ;
143+ } ) ;
144+ }
145+
109146 }
110147
111148 /**
@@ -119,23 +156,23 @@ export class NotifierNotificationComponent implements AfterViewInit {
119156 switch ( this . config . position . verticalPosition ) { // TODO: Maybe if-else ??
120157 case 'top' :
121158 if ( shiftToMakePlace ) {
122- newElementShift = this . currentElementShift + distance + this . config . position . verticalGap ;
159+ newElementShift = this . elementShift + distance + this . config . position . verticalGap ;
123160 } else {
124- newElementShift = this . currentElementShift - distance - this . config . position . verticalGap ;
161+ newElementShift = this . elementShift - distance - this . config . position . verticalGap ;
125162 }
126163 break ;
127164 case 'bottom' :
128165 if ( shiftToMakePlace ) {
129- newElementShift = this . currentElementShift - distance - this . config . position . verticalGap ;
166+ newElementShift = this . elementShift - distance - this . config . position . verticalGap ;
130167 } else {
131- newElementShift = this . currentElementShift + distance + this . config . position . verticalGap ;
168+ newElementShift = this . elementShift + distance + this . config . position . verticalGap ;
132169 }
133170 }
134171 let horizontalPosition : string = this . config . position . horizontalPosition === 'middle' ? '-50%' : '0' ;
135172
136173 // Shift
137174 this . renderer . setElementStyle ( this . element , 'transform' , `translate3d( ${ horizontalPosition } , ${ newElementShift } px, 0 )` ) ;
138- this . currentElementShift = newElementShift ;
175+ this . elementShift = newElementShift ;
139176 resolve ( ) ; // DONE
140177
141178 } ) ;
0 commit comments