Skip to content

Commit

Permalink
Merge pull request #60 from cal-smith/master
Browse files Browse the repository at this point in the history
fix(toast): add toast to banner.service and enable the close event
  • Loading branch information
zvonimirfras authored Sep 14, 2018
2 parents e0c261c + 97132d9 commit 30b35a5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 35 deletions.
10 changes: 8 additions & 2 deletions src/banner/banner.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { TranslateModule, TranslateLoader, TranslateFakeLoader } from "@ngx-tran

import { StaticIconModule } from "./../icon/static-icon.module";

import { Banner } from "./banner.component";
import { BannerService } from "./banner.service";
import { Banner, BannerService } from "./banner.module";


describe("Banner", () => {
Expand Down Expand Up @@ -32,6 +31,7 @@ describe("Banner", () => {
const fixture = TestBed.createComponent(Banner);
fixture.componentInstance.bannerObj = {
type: "info",
title: "sample",
message: "sample message"
};
fixture.detectChanges();
Expand All @@ -44,6 +44,7 @@ describe("Banner", () => {
const fixture = TestBed.createComponent(Banner);
fixture.componentInstance.bannerObj = {
type: "danger",
title: "sample",
message: "sample message"
};
fixture.detectChanges();
Expand All @@ -56,6 +57,7 @@ describe("Banner", () => {
const fixture = TestBed.createComponent(Banner);
fixture.componentInstance.bannerObj = {
type: "warning",
title: "sample",
message: "sample message"
};
fixture.detectChanges();
Expand All @@ -68,6 +70,7 @@ describe("Banner", () => {
const fixture = TestBed.createComponent(Banner);
fixture.componentInstance.bannerObj = {
type: "success",
title: "sample",
message: "sample message"
};
fixture.detectChanges();
Expand All @@ -80,6 +83,7 @@ describe("Banner", () => {
const fixture = TestBed.createComponent(Banner);
fixture.componentInstance.bannerObj = {
type: "success",
title: "sample",
message: "sample message"
};
fixture.detectChanges();
Expand All @@ -93,6 +97,7 @@ describe("Banner", () => {
const fixture = TestBed.createComponent(Banner);
fixture.componentInstance.bannerObj = {
type: "success",
title: "sample",
message: "sample message"
};
fixture.detectChanges();
Expand All @@ -109,6 +114,7 @@ describe("Banner", () => {
const fixture = TestBed.createComponent(Banner);
fixture.componentInstance.bannerObj = {
type: "info",
title: "sample",
message: "sample message"
};
fixture.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion src/banner/banner.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Banner {

@ViewChild("banner") banner;

constructor(private bannerService: BannerService) {}
constructor(protected bannerService: BannerService) {}

/**
* Emits close event.
Expand Down
10 changes: 6 additions & 4 deletions src/banner/banner.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ import { TranslateModule } from "@ngx-translate/core";

import { StaticIconModule } from "./../icon/static-icon.module";

import { Banner } from "./banner.component";
import { Toast } from "./toast.component";
import { Banner } from "./banner.component";
import { BannerService } from "./banner.service";

export { BannerService } from "./banner.service";
export { Banner } from "./banner.component";
export { Toast } from "./toast.component";

@NgModule({
declarations: [
Banner,
Toast
Toast,
Banner
],
exports: [
Banner,
Toast
],
entryComponents: [Banner, Toast],
imports: [CommonModule, TranslateModule, StaticIconModule]
imports: [CommonModule, TranslateModule, StaticIconModule],
providers: [BannerService]
})
export class BannerModule {}
31 changes: 11 additions & 20 deletions src/banner/banner.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
OnDestroy
} from "@angular/core";

import { Banner } from "./banner.component";
import { BannerContent, NotificationContent, ToastContent } from "./banner-content.interface";
import { Banner, Toast } from "./banner.module";

/**
* Provides a way to use the banner component.
Expand All @@ -31,15 +31,6 @@ export class BannerService implements OnDestroy {
public bannerRefs = new Array<ComponentRef<any>>();
public onClose: EventEmitter<any> = new EventEmitter();

/**
* Used to create banners.
*
* @private
* @type {ComponentFactory<any>}
* @memberof BannerService
*/
private componentFactory: ComponentFactory<any>;

/**
* Constructs BannerService.
*
Expand Down Expand Up @@ -82,18 +73,14 @@ export class BannerService implements OnDestroy {
* }
* ```
*
* @param {any} [bannerComp=null] If provided, used to resolve component factory
* @param {any} [bannerComp=Banner] If provided, used to resolve component factory
* @memberof BannerService
*/
showBanner(bannerObj: BannerContent | NotificationContent | ToastContent, bannerComp = null): Banner {
if (!bannerComp) {
this.componentFactory = this.componentFactoryResolver.resolveComponentFactory(Banner);
} else {
this.componentFactory = this.componentFactoryResolver.resolveComponentFactory(bannerComp);
}
showBanner(bannerObj: BannerContent | NotificationContent | ToastContent, bannerComp = Banner) {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(bannerComp);

let bannerRef = this.componentFactory.create(this.injector);
bannerRef.instance.bannerObj = bannerObj;
let bannerRef = componentFactory.create(this.injector);
bannerRef.instance.bannerObj = bannerObj as any; // typescript isn't being very smart here, so we type to any
this.bannerRefs.push(bannerRef);

this.onClose = bannerRef.instance.close;
Expand All @@ -106,7 +93,7 @@ export class BannerService implements OnDestroy {

// get or create a container for alert list
let bannerClassName = "banner-overlay";
let bannerList = body.querySelector("." + bannerClassName);
let bannerList = body.querySelector(`.${bannerClassName}`);
if (!bannerList) {
bannerList = document.createElement("div");
bannerList.className = bannerClassName;
Expand Down Expand Up @@ -142,6 +129,10 @@ export class BannerService implements OnDestroy {
return bannerRef.instance;
}

showToast(bannerObj: BannerContent | NotificationContent | ToastContent, bannerComp = Toast) {
return this.showBanner(bannerObj, bannerComp);
}

/**
* Programatically closes banner based on `bannerRef`.
*
Expand Down
36 changes: 31 additions & 5 deletions src/banner/banner.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { Component } from "@angular/core";

import { TranslateModule } from "@ngx-translate/core";

import { BannerModule, BannerService } from "../";

import { BannerModule, BannerService } from "./banner.module";

@Component({
selector: "app-banner-story",
Expand All @@ -19,7 +18,6 @@ import { BannerModule, BannerService } from "../";
providers: [BannerService]
})
class BannnerStory {

constructor(private bannerService: BannerService) { }

showBanner() {
Expand All @@ -32,11 +30,34 @@ class BannnerStory {
}
}

@Component({
selector: "app-toast-story",
template: `
<button class="bx--btn bx--btn--primary" (click)="showToast()">Show info toast</button>
<div class="banner-container"></div>
`,
providers: [BannerService]
})
class ToastStory {
constructor(private bannerService: BannerService) { }

showToast() {
this.bannerService.showToast({
type: "info",
title: "Sample toast",
subtitle: "Sample subtitle message",
caption: "Sample caption",
target: ".banner-container"
});
}
}

storiesOf("Banner", module)
.addDecorator(
moduleMetadata({
declarations: [
BannnerStory
BannnerStory,
ToastStory
],
imports: [
BannerModule,
Expand All @@ -62,9 +83,14 @@ storiesOf("Banner", module)
template: `
<ibm-toast [bannerObj]="{
type: 'error',
title: 'Sample banner',
title: 'Sample toast',
subtitle: 'Sample subtitle message',
caption: 'Sample caption'
}"></ibm-toast>
`
}))
.add("Dynamic toast", () => ({
template: `
<app-toast-story></app-toast-story>
`
}));
7 changes: 5 additions & 2 deletions src/banner/toast.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Component, Input } from "@angular/core";

import { Banner } from "./banner.component";
import { BannerService } from "./banner.service";
import { ToastContent } from "./banner-content.interface";
import { Banner } from "./banner.component";

/**
* Banner messages are displayed toward the top of the UI and do not interrupt user’s work.
Expand All @@ -22,7 +22,10 @@ import { ToastContent } from "./banner-content.interface";
<p class="bx--toast-notification__subtitle" [innerHTML]="bannerObj.subtitle"></p>
<p class="bx--toast-notification__caption" [innerHTML]="bannerObj.caption"></p>
</div>
<button class="bx--toast-notification__close-button" type="button">
<button
class="bx--toast-notification__close-button"
type="button"
(click)="onClose()">
<svg
class="bx--toast-notification-icon"
aria-label="close"
Expand Down
2 changes: 1 addition & 1 deletion webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = [{
},
plugins: [
new HtmlWebpackPlugin({
template: '<html><body></body></html>'
templateContent: '<html><body></body></html>'
})
],
}];

0 comments on commit 30b35a5

Please sign in to comment.