Skip to content

Commit 9cede02

Browse files
authored
Merge pull request #279 from cal-smith/notification
fix(notification): added NotificationDisplayService
2 parents 070d1d3 + bc65922 commit 9cede02

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
Injectable,
3+
ApplicationRef
4+
} from "@angular/core";
5+
6+
@Injectable()
7+
export class NotificationDisplayService {
8+
constructor(protected applicationRef: ApplicationRef) {}
9+
10+
/**
11+
* Programatically closes notification based on `notificationRef`. *
12+
*/
13+
close(notificationRef: any) {
14+
setTimeout(() => {
15+
this.applicationRef.detachView(notificationRef.hostView);
16+
notificationRef.destroy();
17+
}, 200);
18+
}
19+
}

src/notification/notification.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { TestBed } from "@angular/core/testing";
22

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

5-
import { Notification, NotificationService } from "./notification.module";
5+
import { Notification, NotificationService, NotificationDisplayService } from "./notification.module";
66
import { I18nModule } from "../i18n/i18n.module";
77

88

99
describe("Notification", () => {
1010
beforeEach(() => {
1111
TestBed.configureTestingModule({
1212
declarations: [Notification],
13-
providers: [NotificationService],
13+
providers: [NotificationDisplayService],
1414
imports: [
1515
StaticIconModule,
1616
I18nModule

src/notification/notification.component.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
HostBinding
99
} from "@angular/core";
1010

11-
import { NotificationService } from "./notification.service";
1211
import { NotificationContent } from "./notification-content.interface";
1312
import { I18n } from "./../i18n/i18n.module";
13+
import { NotificationDisplayService } from "./notification-display.service";
1414

1515
/**
1616
* Notification messages are displayed toward the top of the UI and do not interrupt user’s work.
@@ -44,8 +44,7 @@ import { I18n } from "./../i18n/i18n.module";
4444
<path d="M6.32 5L10 8.68 8.68 10 5 6.32 1.32 10 0 8.68 3.68 5 0 1.32 1.32 0 5 3.68 8.68 0 10 1.32 6.32 5z" fill-rule="nonzero"/>
4545
</svg>
4646
</button>
47-
`,
48-
providers: [NotificationService]
47+
`
4948
})
5049
export class Notification {
5150
/**
@@ -92,7 +91,7 @@ export class Notification {
9291
};
9392
protected _notificationObj: NotificationContent = Object.assign({}, this.defaultNotificationObj);
9493

95-
constructor(protected notificationService: NotificationService, protected i18n: I18n) {}
94+
constructor(protected notificationDisplayService: NotificationDisplayService, protected i18n: I18n) {}
9695

9796
/**
9897
* Emits close event.
@@ -104,6 +103,6 @@ export class Notification {
104103
}
105104

106105
destroy() {
107-
this.notificationService.close(this);
106+
this.notificationDisplayService.close(this);
108107
}
109108
}

src/notification/notification.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { StaticIconModule } from "./../icon/static-icon.module";
66
import { Toast } from "./toast.component";
77
import { Notification } from "./notification.component";
88
import { NotificationService } from "./notification.service";
9+
import { NotificationDisplayService } from "./notification-display.service";
910
import { I18nModule } from "./../i18n/i18n.module";
1011

1112
export { NotificationService } from "./notification.service";
13+
export { NotificationDisplayService } from "./notification-display.service";
1214
export { Notification } from "./notification.component";
1315
export { Toast } from "./toast.component";
1416

@@ -23,6 +25,6 @@ export { Toast } from "./toast.component";
2325
],
2426
entryComponents: [Notification, Toast],
2527
imports: [CommonModule, StaticIconModule, I18nModule],
26-
providers: [NotificationService]
28+
providers: [NotificationService, NotificationDisplayService]
2729
})
2830
export class NotificationModule {}

src/notification/notification.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
} from "@angular/core";
1111

1212
import { NotificationContent, ToastContent } from "./notification-content.interface";
13-
import { Notification, Toast } from "./notification.module";
13+
import { Notification } from "./notification.component";
14+
import { Toast } from "./toast.component";
1415

1516
/**
1617
* Provides a way to use the notification component.

src/notification/toast.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
HostBinding
66
} from "@angular/core";
77

8-
import { NotificationService } from "./notification.service";
98
import { NotificationContent, ToastContent } from "./notification-content.interface";
109
import { Notification } from "./notification.component";
1110

@@ -37,8 +36,7 @@ import { Notification } from "./notification.component";
3736
<path d="M6.32 5L10 8.68 8.68 10 5 6.32 1.32 10 0 8.68 3.68 5 0 1.32 1.32 0 5 3.68 8.68 0 10 1.32 6.32 5z" fill-rule="nonzero"/>
3837
</svg>
3938
</button>
40-
`,
41-
providers: [ NotificationService ]
39+
`
4240
})
4341
export class Toast extends Notification implements OnInit {
4442
/**

0 commit comments

Comments
 (0)