import { Component, Input } from '@angular/core'; import { trigger, transition, state, animate, style } from '@angular/animations'; import { Router, RoutesRecognized } from '@angular/router'; import { Subscription } from 'rxjs'; export const enum TaBannerStatus { alert = 'alert', success = 'success', error = 'error', } export const enum TaBannerUseRouter { yes = 'yes', no = 'no', } @Component({ selector: 'app-notification-banner', templateUrl: './notification-banner.component.html', styleUrls: ['./notification-banner.component.scss'], animations: [ trigger('bannerAnimation', [ // States state('alert', style({ opacity: 1, backgroundColor: '#0069b4', // $ta-blue500 })), state('success', style({ opacity: 1, backgroundColor: '#007c86', // $ta-green700; })), state('error', style({ opacity: 1, backgroundColor: '#d61d58', // $ta-pink500; })), state('void', style({ height: '0px', opacity: 0, backgroundColor: 'white', })), // Initial transition transition('void => *', [ animate('0.5s'), ]), // Transition to close transition('alert => void', [ animate('0.3s'), ]), transition('success => void', [ animate('0.3s'), ]), transition('error => void', [ animate('0.3s'), ]), // Transition from close transition('void => alert', [ animate('0.5s'), ]), transition('void => success', [ animate('0.5s'), ]), transition('void => error', [ animate('0.5s'), ]), // transition biderection transition('alert <=> success', [ animate('0.5s'), ]), transition('error <=> success', [ animate('0.5s'), ]), transition('alert <=> error', [ animate('0.5s'), ]), ]), ], }) export class NotificationBannerComponent { public isOpen: boolean = true; public path: string; public secondPath: string; public firstPath: string; public splitPath: string[]; @Input() public message: string = 'This is just a default message. Please give the message as a parameter or variable.'; @Input() public status: TaBannerStatus = TaBannerStatus.alert; // alert success error; @Input() public conditional: string = TaBannerUseRouter.yes; // yes no; Default: no @Input() public sticky: boolean = false; // stick banner on top public subscription: Subscription; constructor(private router: Router) { } public ngOnInit (): void { this.subscription = this.router.events .subscribe((event: any) => { if (event instanceof RoutesRecognized) { this.path = event.url; if(this.conditional === TaBannerUseRouter.yes) { this.displayBanner(); } else { this.variableSet(true, this.status, this.message); } } }); } public ngOnDestroy (): void { this.subscription.unsubscribe(); } public variableSet(open: boolean = false , status: TaBannerStatus = TaBannerStatus.success , message: string= 'This is just a default message. Please give the message as a parameter or variable.'): void { this.isOpen = open; if(this.isOpen) { this.status = status; this.message = message; } } public closeToggle(): void { this.isOpen = !this.isOpen; console.log(this.isOpen); } public displayBanner(): void { this.sticky = true; this.splitPath = this.path.split('/'); this.secondPath = this.splitPath [2]; this.firstPath = this.splitPath [1]; switch(this.firstPath) { case 'cardinal': { this.variableSet(); break; } case 'implementation': { this.variableSet(); break; } case 'color': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } case 'iconography': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } case 'library': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } case 'typography': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } case 'responsive': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } case 'z-depth': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } default: { this.variableSet(); break; } } break; } case 'grid': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } case 'lists': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } case 'responsive': { this.variableSet(); break; } case 'style': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } case 'spacing': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } case 'buttons': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } case 'style': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } case 'hero': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } case 'responsive': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } case 'tabs': { switch(this.secondPath) { case 'usage': { this.variableSet(); break; } case 'responsive': { this.variableSet(); break; } default: case 'developer-reference': { this.variableSet(); break; } } break; } default: { this.variableSet(); break; } } } }