File

src/app/core/notifications/notifications.component.ts

Implements

OnInit OnDestroy

Metadata

selector crm-notifications
styleUrls notifications.component.css
templateUrl ./notifications.component.html

Index

Properties
Methods

Constructor

constructor(notificationsService: NotificationsService)
Parameters :
Name Type Optional Description
notificationsService NotificationsService

Methods

clear
clear(id: number)
Parameters :
Name Type Optional Description
id number
Returns : void
close
close(notification: Notification)
Parameters :
Name Type Optional Description
notification Notification
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void
Private setTimeout
setTimeout(notification: Notification)
Parameters :
Name Type Optional Description
notification Notification
Returns : void

Properties

limit
limit: number
Type : number
Default value : 6
notifications
notifications: Notification[]
Type : Notification[]
Default value : []
Private notificationSubscription
notificationSubscription: Subscription
Type : Subscription
position
position: string
Type : string
Default value : 'bottom-right'
timeout
timeout: number
Type : number
Default value : 5000
import {Component, OnInit, OnDestroy} from '@angular/core';
import {Subscription} from "rxjs";
import {NotificationsService} from "./notifications.service";
import {Notification} from "./notification.model";

@Component({
  selector: 'crm-notifications',
  templateUrl: './notifications.component.html',
  styleUrls: ['./notifications.component.css']
})
export class NotificationsComponent implements OnInit, OnDestroy {

  //bottom-right, bottom-left, top-right, top-left, top-center, bottom-center, center-center
  position: string = 'bottom-right';
  timeout: number = 5000;
  limit: number = 6;

  notifications: Notification[] = [];

  private notificationSubscription: Subscription;

  constructor(private notificationsService: NotificationsService) {
  }

  ngOnInit() {
    this.notificationSubscription = this.notificationsService.notificationsState
      .subscribe((notification: Notification) => {
        if (this.notifications.length >= this.limit) {
          this.notifications.shift();
        }
        this.notifications.push(notification);
        this.setTimeout(notification);
      })
  }

  ngOnDestroy() {
    this.notificationSubscription.unsubscribe();
  }

  close(notification: Notification) {
    this.clear(notification.id);
  }

  clear(id: number) {
    if (id) {
      this.notifications.forEach((notification: Notification, index: number) => {
        if (notification.id === id) {
          this.notifications.splice(index, 1);
        }
      });
    } else {
      throw new Error('Notification without id')
    }
  }

  private setTimeout(notification: Notification) {
    window.setTimeout(() => {
      this.clear(notification.id);
    }, this.timeout);
  }
}
<div id="notifications" [ngClass]="[position]">
  <div class="notification" *ngFor="let notification of notifications" [ngClass]="[notification.type]">

    <div class="close-button" (click)="close(notification)"></div>
    <div *ngIf="notification.message" class="notification-text">
      <span *ngIf="notification.title" class="toast-title">{{notification.title}}</span>
      <br *ngIf="notification.title && notification.message"/>
      <span *ngIf="notification.message" class="notification-message">{{notification.message}}</span>
    </div>

  </div>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""