ng2-toasty
Version:
Angular2 Toasty component shows growl-style alerts and messages for your web app
166 lines (165 loc) • 5.82 kB
JavaScript
// Copyright (C) 2016-2017 Sergey Akopkokhyants
// This project is licensed under the terms of the MIT license.
// https://github.com/akserg/ng2-toasty
import { Component, Input } from '@angular/core';
import { isFunction } from './toasty.utils';
import { ToastyService, ToastyConfig, ToastyEventType } from './toasty.service';
/**
* Toasty is container for Toast components
*/
var ToastyComponent = (function () {
function ToastyComponent(config, toastyService) {
this.config = config;
this.toastyService = toastyService;
this._position = '';
// The storage for toasts.
this.toasts = [];
// Initialise position
this.position = '';
}
Object.defineProperty(ToastyComponent.prototype, "position", {
get: function () {
return this._position;
},
// The window position where the toast pops up. Possible values:
// - bottom-right (default value from ToastConfig)
// - bottom-left
// - top-right
// - top-left
// - top-center
// - bottom-center
// - center-center
set: function (value) {
if (value) {
var notFound = true;
for (var i = 0; i < ToastyComponent.POSITIONS.length; i++) {
if (ToastyComponent.POSITIONS[i] === value) {
notFound = false;
break;
}
}
if (notFound) {
// Position was wrong - clear it here to use the one from config.
value = this.config.position;
}
}
else {
value = this.config.position;
}
this._position = 'toasty-position-' + value;
},
enumerable: true,
configurable: true
});
/**
* `ngOnInit` is called right after the directive's data-bound properties have been checked for the
* first time, and before any of its children have been checked. It is invoked only once when the
* directive is instantiated.
*/
ToastyComponent.prototype.ngOnInit = function () {
var _this = this;
// We listen events from our service
this.toastyService.events.subscribe(function (event) {
if (event.type === ToastyEventType.ADD) {
// Add the new one
var toast = event.value;
_this.add(toast);
}
else if (event.type === ToastyEventType.CLEAR) {
// Clear the one by number
var id = event.value;
_this.clear(id);
}
else if (event.type === ToastyEventType.CLEAR_ALL) {
// Lets clear all toasts
_this.clearAll();
}
});
};
/**
* Event listener of 'closeToast' event comes from ToastyComponent.
* This method removes ToastComponent assosiated with this Toast.
*/
ToastyComponent.prototype.closeToast = function (toast) {
this.clear(toast.id);
};
/**
* Add new Toast
*/
ToastyComponent.prototype.add = function (toast) {
// If we've gone over our limit, remove the earliest
// one from the array
if (this.toasts.length >= this.config.limit) {
this.toasts.shift();
}
// Add toasty to array
this.toasts.push(toast);
//
// If there's a timeout individually or globally,
// set the toast to timeout
if (toast.timeout) {
this._setTimeout(toast);
}
};
/**
* Clear individual toast by id
* @param id is unique identifier of Toast
*/
ToastyComponent.prototype.clear = function (id) {
var _this = this;
if (id) {
this.toasts.forEach(function (value, key) {
if (value.id === id) {
if (value.onRemove && isFunction(value.onRemove)) {
value.onRemove.call(_this, value);
}
_this.toasts.splice(key, 1);
}
});
}
else {
throw new Error('Please provide id of Toast to close');
}
};
/**
* Clear all toasts
*/
ToastyComponent.prototype.clearAll = function () {
var _this = this;
this.toasts.forEach(function (value, key) {
if (value.onRemove && isFunction(value.onRemove)) {
value.onRemove.call(_this, value);
}
});
this.toasts = [];
};
/**
* Custom setTimeout function for specific setTimeouts on individual toasts.
*/
ToastyComponent.prototype._setTimeout = function (toast) {
var _this = this;
window.setTimeout(function () {
_this.clear(toast.id);
}, toast.timeout);
};
/**
* Set of constants defins position of Toasty on the page.
*/
ToastyComponent.POSITIONS = ['bottom-right', 'bottom-left', 'top-right', 'top-left', 'top-center', 'bottom-center', 'center-center'];
ToastyComponent.decorators = [
{ type: Component, args: [{
selector: 'ng2-toasty',
template: "\n <div id=\"toasty\" [ngClass]=\"[position]\">\n <ng2-toast *ngFor=\"let toast of toasts\" [toast]=\"toast\" (closeToast)=\"closeToast(toast)\"></ng2-toast>\n </div>"
},] },
];
/** @nocollapse */
ToastyComponent.ctorParameters = function () { return [
{ type: ToastyConfig, },
{ type: ToastyService, },
]; };
ToastyComponent.propDecorators = {
'position': [{ type: Input },],
};
return ToastyComponent;
}());
export { ToastyComponent };