UNPKG

11.7 kBMarkdownView Raw
1# Angular2-Notifications
2
3> A light and easy to use notifications library for Angular 2. ~~It features both regular page notifications (toasts) and push notifications.~~
4
5[![Build Status](https://travis-ci.org/flauc/angular2-notifications.svg?branch=master)](https://travis-ci.org/flauc/angular2-notifications)
6[![NPM Version](https://img.shields.io/npm/v/angular2-notifications.svg)](https://www.npmjs.com/package/angular2-notifications)
7[![NPM Downloads](https://img.shields.io/npm/dt/angular2-notifications.svg)](https://www.npmjs.com/package/angular2-notifications)
8
9**Push Notifications have been moved to a separate library [ng-push](https://github.com/flauc/ng-push)**
10
11## Table of Contents
12
13 - [Example](#example)
14 - [Setup](#setup)
15 - [SystemJS](#systemjs)
16 - [Webpack](#webpack)
17 - [Setup](#setup)
18 - [Creating Notifications](#crating-notifications)
19 - [Example using TemplateRef](#example-using-templateref)
20 - [Subscribing to clicks](#subscribing-to-clicks)
21 - [Options](#options)
22
23## Example
24
25Take a look at the live demo here: [Live Demo](https://stackblitz.com/edit/angular2-notifications-example)
26
27## Setup
28
29Install the library
30
31```sh
32npm install --save angular2-notifications
33# Or using Yarn for a faster installation
34yarn add angular2-notifications
35```
36
37### SystemJS
38
39Map the library in your `system.config.js` if you're using SystemJs.
40
41```js
42var map = {
43 'angular2-notifications': 'node_modules/angular2-notifications'
44}
45
46var packages = {
47 'angular2-notifications': { main: './dist/index.js', defaultExtension: 'js' }
48}
49```
50
51### Webpack
52
53If you're using Webpack >= 2, just include the library in your `main.ts` or `vendor.ts` file
54
55```ts
56import 'angular2-notifications';
57```
58
59## Setup
60
61Import the `SimpleNotificationsModule` in to your root `AppModule` (it will not work if you try to import it into a shared module)
62```ts
63import { SimpleNotificationsModule } from 'angular2-notifications';
64
65@NgModule({
66 imports: [
67 BrowserModule,
68 // Animations need to be imported in to your project to use the library
69 BrowserAnimationsModule,
70 SimpleNotificationsModule.forRoot()
71 ],
72 declarations: [AppComponent],
73 bootstrap: [AppComponent]
74})
75export class AppModule { }
76```
77
78
79Add the SimpleNotificationsComponent in to the component where you want to use the notifications. Or in your top level component for use in child components.
80```js
81...
82template: '<simple-notifications></simple-notifications>'
83...
84```
85
86You will also need to use the NotificationsService in your component to create or remove the notifications.
87```js
88...
89constructor( private _service: NotificationsService ) {}
90...
91```
92
93The create and destroy Event Emitters emit the notification that was created or destroyed you can utilise this functionality like this:
94```js
95<simple-notifications [options]="options" (create)="created($event)" (destroy)="destroyed($event)"></simple-notifications>
96```
97
98**If your app cannot find the built JS files for this package,** you may need to tell your build script to scan the `angular2-notifications` directory. See the related issue [#25](https://github.com/flauc/angular2-notifications/issues/25). Example:
99
100```js
101'angular2-notifications/*.+(js|js.map)',
102'angular2-notifications/lib/*.+(js|js.map)'
103```
104
105## Creating Notifications
106
107This are the currently available access methods:
108
109* The access methods return the constructed notification along with the created id.
110
111| Method | Description
112---| ---
113`success(title: any, content?: any, override?: any, context?: any)` | Creates a success notification with the provided title and content.
114`error(title: any, content?: any, override?: any, context?: any)` | Creates an error notification with the provided title and content.
115`alert(title: any, content?: any, override?: any, context?: any)` | Creates an alert notification with the provided title and content.
116`warn(title: any, content?: any, override?: any, context?: any)` | Creates a warn notification with the provided title and content.
117`info(title: any, content?: any, override?: any, context?: any)` | Creates an info notification with the provided title and content.
118`bare(title: any, content?: any, override?: any, context?: any)` | Creates a bare notification with the provided title and content. This notification type is best used when adding custom html.
119`create(title: any, content: any = '', type: string = 'success', override?: any, context?: any)` | Use this method to create any notification type ['success', 'error', 'alert', 'info', 'bare'].
120`html(html: any, type: string = 'success', override?: any, icon: string = 'bare', context?: any)` | Use this method to create a notification with custom html. By specifying an icon (success, error, alert, info or warn) you can use the default icons in addition to your custom html. If you do not explicitly pass an icon param no icon will be shown by default.
121`remove(id?: string)` | Removes the notification that has the provided id or removes all currently open notifications if no id was provided.
122
123The `title`, `content` and `html` arguments can be a string, html string or `TemplateRef`. Now it's also possible to pass the datas (context) to the `TemplateRef` by using the optional `context` argument.
124
125### Example using `TemplateRef`
126
127To use a `TemplateRef` in the title or content you need to create it in a component template:
128
129```html
130<ng-template #example let-title="title">
131 <p>{{title}}</p>
132</ng-template>
133```
134
135Then you need to somehow get it to the component:
136
137```ts
138 title: string = 'Winter is coming';
139 @ViewChild('example') example: TemplateRef<any>;
140
141 open() {
142 let context: any = {title: this.title};
143 this._service.html(this.example, null, null, null, context);
144 }
145```
146
147You could also pass the template through the `open()` method:
148
149```ts
150 open(temp: TemplateRef<any>) {
151 this._service.html(temp, null, null, null, context);
152 }
153```
154
155## Subscribing to clicks
156If you are interested in the clicks that happen on a notification you have
157the possibility to subscribe to a EventEmitter. The methods (success, error, alert, warn, info, bare, create and html) from the
158NotificationsService return an Object of type Notification.
159
160```js
161const toast = this.notificationsService.success('Item created!', 'Click to undo...', {
162 timeOut: 3000,
163 showProgressBar: true,
164 pauseOnHover: true,
165 clickToClose: true
166 });
167```
168
169The returned object has a click property with an EventEmitter on it which you can subscribe to. Your callback then gets notified with the click event
170at each click that happens on your Notification.
171
172```
173toast.click.subscribe((event) => {
174 doSomething(event)
175});
176```
177
178If you have configured the notification to close when the icon is clicked, an EventEmitter exists to listen for those clicks as well.
179
180```js
181const toast = this.notificationsService.success('Item created!', 'Click to undo...', {
182 timeOut: 3000,
183 showProgressBar: true,
184 pauseOnHover: true,
185 clickToClose: false,
186 clickIconToClose: true
187 });
188```
189
190With the corresponding clickIcon property as above.
191
192```
193toast.clickIcon.subscribe((event) => {
194 doSomething(event)
195});
196```
197
198## Options
199
200Global options can be added in two ways. They can be passed through the `forRoot()` method on the module.
201
202```js
203SimpleNotificationsModule.forRoot({
204 ...options
205})
206```
207
208You can also pass them in to the root component.
209
210```js
211<simple-notifications [options]="options"></simple-notifications>
212```
213
214This are the current options that can be set globally:
215
216Option | Type | Default | Description |
217------------ | ------------- | ------------- | -------------
218__position__ | ["top" or "bottom" or "middle", "right" or "left" or "center"] | ["bottom", "right"] | Set the position on the screen where the notifications should display. Pass an array with two values example: ["top", "left"].
219__timeOut__ | int | 0 | Determine how long a notification should wait before closing. If set to 0 a notification won't close it self.
220__showProgressBar__ | boolean | true | Determine if a progress bar should be shown or not.
221__pauseOnHover__ | boolean | true | Determines if the timeOut should be paused when the notification is hovered.
222__lastOnBottom__ | boolean | true | Determines if new notifications should appear at the bottom or top of the list.
223__clickToClose__ | boolean | true | Determines if notifications should close on click.
224__clickIconToClose__ | boolean | false | Determines if notifications should close when user clicks the icon.
225__maxLength__ | int | 0 | Set the maximum allowed length of the content string. If set to 0 or not defined there is no maximum length.
226__maxStack__ | int | 8 | Set the maximum number of notifications that can be on the screen at once.
227__preventDuplicates__ | boolean | false | If true prevents duplicates of open notifications.
228__preventLastDuplicates__ | boolean or string | false | If set to "all" prevents duplicates of the latest notification shown ( even if it isn't on screen any more ). If set to "visible" only prevents duplicates of the last created notification if the notification is currently visible.
229__theClass__ | string | null | A class that should be attached to the notification. (It doesn't exactly get attached to the selector but to the first div of the template.)
230__rtl__ | boolean | false | Adds the class `.rtl-mode` to the notification aligning the icon to the left and adding `direction: rtl`
231__animate__ | "fade" or "fromTop" or "fromRight" or "fromBottom" or "fromLeft" or "scale" or "rotate" or null | "fromRight" | Choose the type of animation or set the value to null not to display animations.
232__icons__ | Icons | DefaultIcons | Overrides the default icons
233
234## Icons
235
236Option | Type | Default | Description |
237------------ | ------------- | ------------- | -------------
238__alert__ | string | Clock | html string for alert icon
239__error__ | string | Exclamation Point | html string for alert icon
240__info__ | string | Info | html string for alert icon
241__warn__ | string | Warning | html string for warning icon
242__success__ | string | Check | html string for alert icon
243
244Here is an example of passing the options to the component. You only pass the options you want changed.
245Options passed to the component are global. They apply to all of the notifications the get created.
246
247```js
248...
249template: '<simple-notifications [options]="options"></simple-notifications>'
250...
251public options = {
252 position: ["bottom", "left"],
253 timeOut: 5000,
254 lastOnBottom: true
255 ...
256}
257```
258
259If you want a specific notification to have different options you can override them when calling any of the access methods by passing them to the override object.
260The following options can be overridden:
261* id
262* animate
263* timeOut
264* showProgressBar
265* pauseOnHover
266* clickToClose
267* clickIconToClose
268* maxLength
269* theClass
270* icon
271
272This is an example of overriding global options:
273```js
274this._notificationsService.success(
275 'Some Title',
276 'Some Content',
277 {
278 timeOut: 5000,
279 showProgressBar: true,
280 pauseOnHover: false,
281 clickToClose: false,
282 maxLength: 10
283 }
284)
285```
286
287## Development
288
289To generate all `*.js`, `*.d.ts` and `*.metadata.json` files:
290
291```bash
292$ npm run build
293```
294
295To lint all `*.ts` files:
296
297```bash
298$ npm run lint
299```
300
301## License
302
303MIT © [Filip Lauc](mailto:filip.lauc93@gmail.com)
\No newline at end of file