2018-11-19 16:57:35 +02:00
|
|
|
import { Component } from '@angular/core';
|
|
|
|
|
import { ToasterConfig } from 'angular2-toaster';
|
|
|
|
|
|
|
|
|
|
import 'style-loader!angular2-toaster/toaster.css';
|
2019-07-02 16:18:09 +03:00
|
|
|
import {
|
|
|
|
|
NbComponentStatus,
|
|
|
|
|
NbGlobalLogicalPosition,
|
|
|
|
|
NbGlobalPhysicalPosition,
|
|
|
|
|
NbGlobalPosition,
|
|
|
|
|
NbToastrService,
|
|
|
|
|
} from '@nebular/theme';
|
2018-11-19 16:57:35 +02:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'ngx-toastr',
|
|
|
|
|
styleUrls: ['./toastr.component.scss'],
|
|
|
|
|
templateUrl: './toastr.component.html',
|
|
|
|
|
})
|
|
|
|
|
export class ToastrComponent {
|
|
|
|
|
constructor(private toastrService: NbToastrService) {}
|
|
|
|
|
|
|
|
|
|
config: ToasterConfig;
|
|
|
|
|
|
|
|
|
|
index = 1;
|
|
|
|
|
destroyByClick = true;
|
|
|
|
|
duration = 2000;
|
|
|
|
|
hasIcon = true;
|
|
|
|
|
position: NbGlobalPosition = NbGlobalPhysicalPosition.TOP_RIGHT;
|
|
|
|
|
preventDuplicates = false;
|
2019-07-02 16:18:09 +03:00
|
|
|
status: NbComponentStatus = 'primary';
|
2018-11-19 16:57:35 +02:00
|
|
|
|
|
|
|
|
title = 'HI there!';
|
|
|
|
|
content = `I'm cool toaster!`;
|
|
|
|
|
|
2019-07-02 16:18:09 +03:00
|
|
|
types: NbComponentStatus[] = [
|
|
|
|
|
'primary',
|
|
|
|
|
'success',
|
|
|
|
|
'info',
|
|
|
|
|
'warning',
|
|
|
|
|
'danger',
|
2018-11-19 16:57:35 +02:00
|
|
|
];
|
|
|
|
|
positions: string[] = [
|
|
|
|
|
NbGlobalPhysicalPosition.TOP_RIGHT,
|
|
|
|
|
NbGlobalPhysicalPosition.TOP_LEFT,
|
|
|
|
|
NbGlobalPhysicalPosition.BOTTOM_LEFT,
|
|
|
|
|
NbGlobalPhysicalPosition.BOTTOM_RIGHT,
|
|
|
|
|
NbGlobalLogicalPosition.TOP_END,
|
|
|
|
|
NbGlobalLogicalPosition.TOP_START,
|
|
|
|
|
NbGlobalLogicalPosition.BOTTOM_END,
|
|
|
|
|
NbGlobalLogicalPosition.BOTTOM_START,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
quotes = [
|
|
|
|
|
{ title: null, body: 'We rock at Angular' },
|
|
|
|
|
{ title: null, body: 'Titles are not always needed' },
|
|
|
|
|
{ title: null, body: 'Toastr rock!' },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
makeToast() {
|
|
|
|
|
this.showToast(this.status, this.title, this.content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
openRandomToast () {
|
|
|
|
|
const typeIndex = Math.floor(Math.random() * this.types.length);
|
|
|
|
|
const quoteIndex = Math.floor(Math.random() * this.quotes.length);
|
2019-07-02 16:18:09 +03:00
|
|
|
const type = this.types[typeIndex];
|
2018-11-19 16:57:35 +02:00
|
|
|
const quote = this.quotes[quoteIndex];
|
|
|
|
|
|
|
|
|
|
this.showToast(type, quote.title, quote.body);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-02 16:18:09 +03:00
|
|
|
private showToast(type: NbComponentStatus, title: string, body: string) {
|
2018-11-19 16:57:35 +02:00
|
|
|
const config = {
|
|
|
|
|
status: type,
|
|
|
|
|
destroyByClick: this.destroyByClick,
|
|
|
|
|
duration: this.duration,
|
|
|
|
|
hasIcon: this.hasIcon,
|
|
|
|
|
position: this.position,
|
|
|
|
|
preventDuplicates: this.preventDuplicates,
|
|
|
|
|
};
|
|
|
|
|
const titleContent = title ? `. ${title}` : '';
|
|
|
|
|
|
|
|
|
|
this.index += 1;
|
|
|
|
|
this.toastrService.show(
|
|
|
|
|
body,
|
|
|
|
|
`Toast ${this.index}${titleContent}`,
|
|
|
|
|
config);
|
|
|
|
|
}
|
|
|
|
|
}
|