|
| 1 | +import { Component, OnInit, OnDestroy, Optional } from '@angular/core'; |
| 2 | +import { Subscription } from 'rxjs/Subscription'; |
| 3 | + |
| 4 | +import { NotifierQueueService } from './../services/notifier-queue.service'; |
| 5 | +import { NotifierNotificationComponent } from './notifier-notification.component'; |
| 6 | +import { NotifierConfigGlobal } from './../models/notifier-config-global.model'; |
| 7 | +import { NotifierNotification } from './../models/notifier-notification.model'; |
| 8 | +import { NotifierAction, NotifierActionType } from './../models/notifier-action.model'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Notifier container component |
| 12 | + */ |
| 13 | +@Component( { |
| 14 | + host: { |
| 15 | + class: 'x-notifier__container' |
| 16 | + }, |
| 17 | + selector: 'x-notifier-container', |
| 18 | + template: ` |
| 19 | + <ul> |
| 20 | + <li class="x-notifier__container-list" *ngFor="let notification of notifications"> |
| 21 | + <x-notifier-notification |
| 22 | + [notification]="notification" |
| 23 | + (init)="onComponentInit( $event )" |
| 24 | + (dismiss)="onClickDismiss( $event )"> |
| 25 | + </x-notifier-notification> |
| 26 | + </li> |
| 27 | + </ul>` |
| 28 | +} ) |
| 29 | +export class NotifierContainerComponent implements OnInit, OnDestroy { |
| 30 | + |
| 31 | + /** |
| 32 | + * Global configuration |
| 33 | + */ |
| 34 | + private config: NotifierConfigGlobal; |
| 35 | + |
| 36 | + /** |
| 37 | + * Notifier queue service |
| 38 | + */ |
| 39 | + private queueService: NotifierQueueService; |
| 40 | + |
| 41 | + /** |
| 42 | + * List of currently visible notifications |
| 43 | + */ |
| 44 | + private notifications: Array<NotifierNotification>; |
| 45 | + |
| 46 | + /** |
| 47 | + * Queue service observable subscription (used for cleanup later on) |
| 48 | + */ |
| 49 | + private queueServiceSubscription: Subscription; |
| 50 | + |
| 51 | + /** |
| 52 | + * Promise resolve function reference, temporarily used while the notification child component gets created |
| 53 | + */ |
| 54 | + private tempPromiseResolver: ( value?: null ) => {}; |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructor |
| 58 | + */ |
| 59 | + public constructor( notifierQueueService: NotifierQueueService, @Optional() config: NotifierConfigGlobal ) { |
| 60 | + this.config = config === null ? new NotifierConfigGlobal() : config; |
| 61 | + this.queueService = notifierQueueService; |
| 62 | + this.notifications = []; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Component initialization lifecycle hook |
| 67 | + * Handles the incoming stream of actions, and also communicates back when an action is finished |
| 68 | + */ |
| 69 | + public ngOnInit(): void { |
| 70 | + this.queueServiceSubscription = this.queueService.actionStream.subscribe( ( action: NotifierAction ) => { |
| 71 | + this.handleNewAction( action ).then( () => { |
| 72 | + this.queueService.continue(); |
| 73 | + } ); |
| 74 | + } ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Component destroy lifecycle hook |
| 79 | + * Cleans up observable subsciption |
| 80 | + */ |
| 81 | + public ngOnDestroy(): void { |
| 82 | + this.queueServiceSubscription.unsubscribe(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Handle click on a notification dismiss button |
| 87 | + * @param {NotifierNotification} notification |
| 88 | + */ |
| 89 | + public onClickDismiss( notification: NotifierNotification ): void { |
| 90 | + this.queueService.push( { |
| 91 | + payload: notification, |
| 92 | + type: NotifierActionType.CLEAR |
| 93 | + } ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Redirect ... event handler |
| 98 | + */ |
| 99 | + public onComponentInit( componentRef: NotifierNotificationComponent ): void { |
| 100 | + let currentNotification: NotifierNotification = this.notifications[ this.notifications.length - 1 ]; |
| 101 | + currentNotification.componentRef = componentRef; // Save component Ref |
| 102 | + this.showNotification( currentNotification ); |
| 103 | + }; |
| 104 | + |
| 105 | + /** |
| 106 | + * Maps action types to component methods, and then runs the correct one |
| 107 | + */ |
| 108 | + private handleNewAction( action: NotifierAction ): Promise<any> { |
| 109 | + switch ( action.type ) { |
| 110 | + case NotifierActionType.SHOW: |
| 111 | + return this.addNotification( action ); |
| 112 | + case NotifierActionType.CLEAR: |
| 113 | + return this.removeNotification( action ); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Add a new notification to the list of notifications |
| 119 | + * @param {NotifierAction} action Action object |
| 120 | + * @returns {Promise<null>} Empty Promise, resolved when finished |
| 121 | + */ |
| 122 | + private addNotification( action: NotifierAction ): Promise<null> { |
| 123 | + return new Promise<null>( ( resolve: ( value?: null ) => {}, reject: ( value?: null ) => {} ) => { |
| 124 | + let notification: NotifierNotification = new NotifierNotification( action.payload ); |
| 125 | + this.tempPromiseResolver = resolve; // Gets resolved when the notification is finally visible |
| 126 | + this.notifications.push( notification ); |
| 127 | + } ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Remove notification |
| 132 | + */ |
| 133 | + private removeNotification( action: NotifierAction ): Promise<null> { |
| 134 | + return new Promise<null>( ( resolve: ( value?: null ) => {}, reject: ( value?: null ) => {} ) => { |
| 135 | + |
| 136 | + // 1. Decision: Only one notification in the list? |
| 137 | + if ( this.notifications.length === 1 ) { |
| 138 | + this.hideNotification( action.payload ).then( resolve ); // DONE |
| 139 | + } else { |
| 140 | + this.hideNotification( action.payload ); |
| 141 | + const notificationIndex: number = this.notifications.findIndex( ( notification: NotifierNotification ) => { |
| 142 | + return notification.componentRef === action.payload.componentRef; |
| 143 | + } ); |
| 144 | + const otherNotifications: Array<NotifierNotification> = this.notifications.slice( 0, notificationIndex ); |
| 145 | + this.shiftNotifications( otherNotifications, action.payload.componentRef.currentElementHeight, false ).then( resolve ); |
| 146 | + } |
| 147 | + |
| 148 | + } ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Show notification |
| 153 | + */ |
| 154 | + private showNotification( notification: NotifierNotification ): void { |
| 155 | + |
| 156 | + // 1. Decision: First notification in the list? |
| 157 | + if ( this.notifications.length === 1 ) { |
| 158 | + notification.componentRef.show().then( this.tempPromiseResolver ); |
| 159 | + } else { |
| 160 | + |
| 161 | + // 2. Decision: Stacking enabled? (same as stacking value below 2) |
| 162 | + if ( this.config.behaviour.stacking === false || this.config.behaviour.stacking < 2 ) { |
| 163 | + this.hideNotification( this.notifications[ 0 ] ).then( () => { |
| 164 | + notification.componentRef.show().then( this.tempPromiseResolver ); |
| 165 | + } ); |
| 166 | + } else { |
| 167 | + |
| 168 | + // 3. Decision: Are there now too many notifications? |
| 169 | + if ( this.notifications.length > this.config.behaviour.stacking ) { |
| 170 | + |
| 171 | + this.hideNotification( this.notifications[ 0 ] ); // Hide oldest notification |
| 172 | + const otherNotifications: Array<NotifierNotification> = this.notifications.slice( 1, this.notifications.length - 1 ); |
| 173 | + this.shiftNotifications( otherNotifications, notification.componentRef.currentElementHeight, true ); |
| 174 | + notification.componentRef.show().then( this.tempPromiseResolver ); // DONE |
| 175 | + |
| 176 | + } else { |
| 177 | + |
| 178 | + const otherNotifications: Array<NotifierNotification> = this.notifications.slice( 0, this.notifications.length - 1 ); |
| 179 | + this.shiftNotifications( otherNotifications, notification.componentRef.currentElementHeight, true ); |
| 180 | + notification.componentRef.show().then( this.tempPromiseResolver ); // DONE |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Hide notification |
| 192 | + */ |
| 193 | + private hideNotification( notification: NotifierNotification ): Promise<null> { |
| 194 | + return new Promise<null>( ( resolve: ( value?: null ) => {}, reject: ( value?: null ) => {} ) => { |
| 195 | + notification.componentRef.hide().then( () => { |
| 196 | + this.notifications = this.notifications.filter( ( currentNotification: NotifierNotification ) => { |
| 197 | + return currentNotification.componentRef !== notification.componentRef; |
| 198 | + } ); |
| 199 | + resolve(); // DONE |
| 200 | + } ); |
| 201 | + } ); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Shift notifications |
| 206 | + */ |
| 207 | + private shiftNotifications( notifications: Array<NotifierNotification>, distance: number, shiftToMakePlace: boolean ): Promise<null> { |
| 208 | + let shiftPromises: Array<Promise<null>> = []; |
| 209 | + for ( let notification of notifications ) { |
| 210 | + shiftPromises.push( notification.componentRef.shift( distance, shiftToMakePlace ) ); |
| 211 | + } |
| 212 | + return Promise.all( shiftPromises ); // DONE |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments