angular2-toaster-mod
Version:
An Angular 2 Toaster Notification library based on AngularJS-Toaster
83 lines • 3.21 kB
JavaScript
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/share';
import { Subject } from 'rxjs/Subject';
export var ToasterService = (function () {
/**
* Creates an instance of ToasterService.
*/
function ToasterService() {
var _this = this;
this.addToast = new Observable(function (observer) { return _this._addToast = observer; }).share();
this.clearToasts = new Observable(function (observer) { return _this._clearToasts = observer; }).share();
this._removeToastSubject = new Subject();
this.removeToast = this._removeToastSubject.share();
}
/**
* Synchronously create and show a new toast instance.
*
* @param {(string | Toast)} type The type of the toast, or a Toast object.
* @param {string=} title The toast title.
* @param {string=} body The toast body.
* @returns {Toast}
* The newly created Toast instance with a randomly generated GUID Id.
*/
ToasterService.prototype.pop = function (type, title, body) {
var toast = typeof type === 'string' ? { type: type, title: title, body: body } : type;
toast.toastId = Guid.newGuid();
if (!this._addToast) {
throw new Error("No Toaster Containers have been initialized to receive toasts.");
}
this._addToast.next(toast);
return toast;
};
/**
* Asynchronously create and show a new toast instance.
*
* @param {(string | Toast)} type The type of the toast, or a Toast object.
* @param {string=} title The toast title.
* @param {string=} body The toast body.
* @returns {Observable<Toast>}
* A hot Observable that can be subscribed to in order to receive the Toast instance
* with a randomly generated GUID Id.
*/
ToasterService.prototype.popAsync = function (type, title, body) {
var _this = this;
setTimeout(function () {
_this.pop(type, title, body);
}, 0);
return this.addToast;
};
/**
* Clears a toast by toastId and/or toastContainerId.
*
* @param {string} toastId The toastId to clear.
* @param {number=} toastContainerId
* The toastContainerId of the container to remove toasts from.
*/
ToasterService.prototype.clear = function (toastId, toastContainerId) {
var clearWrapper = {
toastId: toastId, toastContainerId: toastContainerId
};
this._clearToasts.next(clearWrapper);
};
ToasterService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
ToasterService.ctorParameters = function () { return []; };
return ToasterService;
}());
// http://stackoverflow.com/questions/26501688/a-typescript-guid-class
var Guid = (function () {
function Guid() {
}
Guid.newGuid = function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
return Guid;
}());
//# sourceMappingURL=toaster.service.js.map