Skip to content

Commit

Permalink
feat(): Migrated to Angular 2.4.7
Browse files Browse the repository at this point in the history
  • Loading branch information
akserg committed Feb 14, 2017
1 parent a4abe7b commit ff826da
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 59 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
"typings": "index.d.ts",
"homepage": "https://github.com/akserg/ng2-toasty",
"peerDependencies": {
"@angular/core": "^2.4.4"
"@angular/core": "^2.4.7"
},
"devDependencies": {
"@angular/common": "^2.4.4",
"@angular/compiler": "^2.4.4",
"@angular/compiler-cli": "^2.4.4",
"@angular/core": "^2.4.4",
"@angular/platform-browser": "^2.4.4",
"@angular/platform-browser-dynamic": "^2.4.4",
"@angular/platform-server": "^2.4.4",
"@angular/common": "^2.4.7",
"@angular/compiler": "^2.4.7",
"@angular/compiler-cli": "^2.4.7",
"@angular/core": "^2.4.7",
"@angular/platform-browser": "^2.4.7",
"@angular/platform-browser-dynamic": "^2.4.7",
"@angular/platform-server": "^2.4.7",
"@types/hammerjs": "2.0.33",
"@types/jasmine": "2.5.37",
"@types/node": "6.0.46",
Expand All @@ -61,7 +61,7 @@
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.1",
"loader-utils": "~0.2.16",
"reflect-metadata": "0.1.8",
"reflect-metadata": "^0.1.8",
"rxjs": "^5.0.3",
"semantic-release": "4.3.5",
"source-map-loader": "0.1.5",
Expand Down
57 changes: 33 additions & 24 deletions src/toasty.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { Component, Input, OnInit } from '@angular/core';

import { isFunction } from './toasty.utils';
import { ToastyService, ToastData, ToastyConfig } from './toasty.service';
import { ToastyService, ToastData, ToastyConfig, ToastyEvent, ToastyEventType } from './toasty.service';

/**
* Toasty is container for Toast components
Expand Down Expand Up @@ -69,30 +69,20 @@ export class ToastyComponent implements OnInit {
* directive is instantiated.
*/
ngOnInit(): any {
// We listen our service to recieve new toasts from it
this.toastyService.getToasts().subscribe((toast: ToastData) => {
// If we've gone over our limit, remove the earliest
// one from the array
if (this.toasts.length >= this.config.limit) {
this.toasts.shift();
// We listen events from our service
this.toastyService.events.subscribe((event: ToastyEvent) => {
if (event.type === ToastyEventType.ADD) {
// Add the new one
let toast: ToastData = event.value;
this.add(toast);
} else if (event.type === ToastyEventType.CLEAR) {
// Clear the one by number
let id: number = event.value;
this.clear(id);
} else if (event.type === ToastyEventType.CLEAR_ALL) {
// Lets clear all toasts
this.clearAll();
}
// 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);
}
});
// We listen clear all comes from service here.
this.toastyService.getClear().subscribe((id: number) => {
if (id) {
this.clear(id);
} else {
// Lets clear all toasts
this.clearAll();
}
});
}

Expand All @@ -104,6 +94,25 @@ export class ToastyComponent implements OnInit {
this.clear(toast.id);
}

/**
* Add new Toast
*/
add(toast: ToastData) {
// 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
Expand Down
50 changes: 37 additions & 13 deletions src/toasty.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// This project is licensed under the terms of the MIT license.
// https://github.com/akserg/ng2-toasty

import { Injectable, EventEmitter } from '@angular/core';
import { Injectable } from '@angular/core';
import { isString, isNumber, isFunction } from './toasty.utils';

import { Observable } from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {Observable} from 'rxjs/Observable';

/**
* Options to configure specific Toast
Expand Down Expand Up @@ -60,6 +61,16 @@ export class ToastyConfig {
theme: 'default' | 'material' | 'bootstrap' = 'default';
}

export enum ToastyEventType {
ADD,
CLEAR,
CLEAR_ALL
}

export class ToastyEvent {
constructor(public type:ToastyEventType, public value?:any) {}
}

export function toastyServiceFactory(config: ToastyConfig): ToastyService {
return new ToastyService(config);
}
Expand All @@ -74,22 +85,25 @@ export class ToastyService {
// Init the counter
uniqueCounter: number = 0;
// ToastData event emitter
private toastsEmitter: EventEmitter<ToastData> = new EventEmitter<ToastData>();
// private toastsEmitter: EventEmitter<ToastData> = new EventEmitter<ToastData>();
// Clear event emitter
private clearEmitter: EventEmitter<number> = new EventEmitter<number>();
// private clearEmitter: EventEmitter<number> = new EventEmitter<number>();

private eventSource: Subject<ToastyEvent> = new Subject<ToastyEvent>();
public events: Observable<ToastyEvent> = this.eventSource.asObservable();

constructor(private config: ToastyConfig) {}

/**
* Get list of toats
*/
getToasts(): Observable<ToastData> {
return this.toastsEmitter.asObservable();
}
// getToasts(): Observable<ToastData> {
// return this.toastsEmitter.asObservable();
// }

getClear(): Observable<number> {
return this.clearEmitter.asObservable();
}
// getClear(): Observable<number> {
// return this.clearEmitter.asObservable();
// }

/**
* Create Toast of a default type
Expand Down Expand Up @@ -188,7 +202,8 @@ export class ToastyService {

// Push up a new toast item
// this.toastsSubscriber.next(toast);
this.toastsEmitter.next(toast);
// this.toastsEmitter.next(toast);
this.emitEvent(new ToastyEvent(ToastyEventType.ADD, toast));
// If we have a onAdd function, call it here
if (toastyOptions.onAdd && isFunction(toastyOptions.onAdd)) {
toastyOptions.onAdd.call(this, toast);
Expand All @@ -197,12 +212,14 @@ export class ToastyService {

// Clear all toasts
clearAll() {
this.clearEmitter.next(null);
// this.clearEmitter.next(null);
this.emitEvent(new ToastyEvent(ToastyEventType.CLEAR_ALL));
}

// Clear the specific one
clear(id: number) {
this.clearEmitter.next(id);
// this.clearEmitter.next(id);
this.emitEvent(new ToastyEvent(ToastyEventType.CLEAR, id));
}

// Checks whether the local option is set, if not,
Expand All @@ -216,4 +233,11 @@ export class ToastyService {
return true;
}
}

private emitEvent(event: ToastyEvent) {
if (this.eventSource) {
// Push up a new event
this.eventSource.next(event);
}
}
}
36 changes: 23 additions & 13 deletions tests/toasty.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { inject, TestBed }

import {Observable} from 'rxjs/Observable';

import {ToastyService, ToastData, ToastOptions, ToastyConfig} from '../src/toasty.service';
import {ToastyService, ToastData, ToastOptions, ToastyConfig, ToastyEvent} from '../src/toasty.service';

describe('ToastyService', () => {

Expand All @@ -22,7 +22,7 @@ describe('ToastyService', () => {

it('should return Observable from getToasts method',
inject([ToastyService], (service:ToastyService) => {
expect(service.getToasts instanceof Observable);
expect(service.events instanceof Observable);
})
);

Expand All @@ -31,7 +31,8 @@ describe('ToastyService', () => {
it('with string title',
inject([ToastyService], (service:ToastyService) => {
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe('Hi');
Expand All @@ -49,7 +50,8 @@ describe('ToastyService', () => {
it('with number title',
inject([ToastyService], (service:ToastyService) => {
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe('1000');
Expand All @@ -72,7 +74,8 @@ describe('ToastyService', () => {
msg: 'message',
};
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe(options.title);
Expand Down Expand Up @@ -106,7 +109,7 @@ describe('ToastyService', () => {
}
};
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {});
service.events.subscribe((event: ToastyEvent) => {});
service.default(options);
})
);
Expand All @@ -117,7 +120,8 @@ describe('ToastyService', () => {
it('of info type',
inject([ToastyService], (service:ToastyService) => {
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe('Hi');
Expand All @@ -135,7 +139,8 @@ describe('ToastyService', () => {
it('of success type',
inject([ToastyService], (service:ToastyService) => {
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe('Hi');
Expand All @@ -153,7 +158,8 @@ describe('ToastyService', () => {
it('of wait type',
inject([ToastyService], (service:ToastyService) => {
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe('Hi');
Expand All @@ -171,7 +177,8 @@ describe('ToastyService', () => {
it('of error type',
inject([ToastyService], (service:ToastyService) => {
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe('Hi');
Expand All @@ -189,7 +196,8 @@ describe('ToastyService', () => {
it('of warning type',
inject([ToastyService], (service:ToastyService) => {
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe('Hi');
Expand All @@ -214,7 +222,8 @@ describe('ToastyService', () => {
theme: 'material'
};
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe(options.title);
Expand All @@ -237,7 +246,8 @@ describe('ToastyService', () => {
theme: 'bootstrap'
};
// We listen our service to recieve new toasts from it
service.getToasts().subscribe((toast:ToastData) => {
service.events.subscribe((event: ToastyEvent) => {
let toast:ToastData = event.value;
expect(toast).not.toBeNull();
expect(toast.id).not.toBeNull();
expect(toast.title).toBe(options.title);
Expand Down

0 comments on commit ff826da

Please sign in to comment.