all files / services/ ng2-notify.ts

96.43% Statements 27/28
61.11% Branches 11/18
100% Functions 5/5
96.3% Lines 26/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                              
import {Injectable} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
 
@Injectable()
export class Ng2NotifyService {
	public notify: Observable<Object>;
	public notifyObserver: any;
	public corner: string = 'right-bottom';
	public delay: number = 2000;
 
	public positionTypes = ['right-bottom', 'left-bottom', 'left-top', 'right-top'];
 
	constructor() {
		this.notify = new Observable(observer => this.notifyObserver = observer).share();
	};
 
	public show(type: string, config) {
		if (!config.message) {
			console.error('ng2NotifyError: You must to set a message!!');
			return false;
		}
 
		Eif (config.delay && !Number(config.delay)) {
			console.error('ng2NotifyError: ', `The delay \"${config.delay}\" must be a number`);
			return false;
		}
 
		this.notifyObserver.next({
			type: type,
			message: config.message,
			corner: config.corner ? config.corner : this.corner,
			delay: config.delay ? config.delay : this.delay
		});
	}
 
	public config(config) {
		if (this.positionTypes.indexOf(config.corner) === -1) {
			console.error('ng2NotifyError: ', `The corner \"${config.corner}\" do not exist`);
		}
 
		if (!Number(config.delay)) {
			console.error('ng2NotifyError: ', `The delay \"${config.delay}\" must be a number`);
		}
		this.corner = config.corner || 'right-bottom';
		this.delay = config.delay || this.delay;
	}
}