forked from ngrx/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dd18a17
Showing
19 changed files
with
778 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Users Environment Variables | ||
.lock-wscript | ||
|
||
# OS generated files # | ||
.DS_Store | ||
ehthumbs.db | ||
Icon? | ||
Thumbs.db | ||
|
||
# Node Files # | ||
/node_modules | ||
/bower_components | ||
|
||
# Typing TSD # | ||
/src/typings/tsd/ | ||
/typings/ | ||
/tsd_typings/ | ||
|
||
# Dist # | ||
/dist | ||
/public/__build__/ | ||
/src/*/__build__/ | ||
__build__/** | ||
.webpack.json | ||
|
||
#doc | ||
/doc | ||
|
||
# IDE # | ||
.idea/ | ||
*.swp | ||
!/typings/custom.d.ts | ||
|
||
# Build Artifacts # | ||
release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Brandon Roberts, Mike Ryan, Rob Wormald | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# @ngrx/notify | ||
### Web Notifications Powered by RxJS for Angular 2 | ||
[![Join the chat at https://gitter.im/ngrx/notify](https://badges.gitter.im/ngrx/notify.svg)](https://gitter.im/ngrx/notify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
|
||
Easily create and handle desktop notifications in Angular 2 | ||
|
||
### Installation | ||
Install @ngrx/notify from npm: | ||
```bash | ||
npm install @ngrx/notify --save | ||
``` | ||
|
||
Setup the providers, optionally providing global notification options: | ||
```ts | ||
import { NOTIFY_PROVIDERS, NOTIFY_GLOBAL_OPTIONS } from '@ngrx/notify'; | ||
|
||
bootstrap(App, [ | ||
NOTIFY_PROVIDERS, | ||
{ provide: NOTIFY_GLOBAL_OPTIONS, multi: true, useValue: { /* global options here */ } } | ||
]) | ||
``` | ||
|
||
## Usage | ||
### Requesting Notification Permission | ||
Before creating notifications, you must resolve the app's notification permission: | ||
|
||
```ts | ||
class AppComponent { | ||
constructor(notify: Notify) { | ||
notify.requestPermission().subscribe(permission => { | ||
if (permission) { | ||
// continue | ||
} | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
### Creating a Notification | ||
To create a notification observable, call the `open()` method with a title and optional config. The notification will be opened when you subscribe to the observable and will close after you unsubscribe from it. The observable will emit the instance of the notification every time it is clicked on: | ||
|
||
```ts | ||
notify.open('Hello world!', options) | ||
// Automatically close the notification after 5 seconds | ||
.takeUntil(Observable.timer(5000)) | ||
// Close the notification after it has been clicked once | ||
.take(1) | ||
.subscribe(notification => { | ||
|
||
}); | ||
``` | ||
|
||
See the [documentation on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification) for available options. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
var path = require('path'); | ||
|
||
module.exports = function(karma) { | ||
'use strict'; | ||
|
||
karma.set({ | ||
basePath: __dirname, | ||
|
||
frameworks: ['jasmine'], | ||
|
||
files: [ | ||
{ pattern: 'tests.js', watched: false } | ||
], | ||
|
||
exclude: [], | ||
|
||
preprocessors: { | ||
'tests.js': ['coverage', 'webpack', 'sourcemap'] | ||
}, | ||
|
||
reporters: ['mocha', 'coverage'], | ||
|
||
coverageReporter: { | ||
dir: 'coverage/', | ||
subdir: '.', | ||
reporters: [ | ||
{ type: 'text-summary' }, | ||
{ type: 'json' }, | ||
{ type: 'html' } | ||
] | ||
}, | ||
|
||
browsers: ['Chrome'], | ||
|
||
port: 9018, | ||
runnerPort: 9101, | ||
colors: true, | ||
logLevel: karma.LOG_INFO, | ||
autoWatch: true, | ||
singleRun: false, | ||
|
||
webpack: { | ||
devtool: 'inline-source-map', | ||
resolve: { | ||
root: __dirname, | ||
extensions: ['', '.ts', '.js'] | ||
}, | ||
module: { | ||
loaders: [ | ||
{ | ||
test: /\.ts?$/, | ||
exclude: /(node_modules)/, | ||
loader: 'ts-loader?target=es5&module=commonjs' | ||
} | ||
], | ||
postLoaders: [ | ||
{ | ||
test: /\.(js|ts)$/, loader: 'istanbul-instrumenter-loader', | ||
include: path.resolve(__dirname, 'lib'), | ||
exclude: [ | ||
/\.(e2e|spec)\.ts$/, | ||
/node_modules/ | ||
] | ||
} | ||
] | ||
}, | ||
ts: { | ||
configFileName: './spec/tsconfig.json' | ||
} | ||
}, | ||
|
||
webpackServer: { | ||
noInfo: true | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { Notify } from './notify'; | ||
export { NOTIFY_PROVIDERS, NOTIFY_GLOBAL_OPTIONS } from './ng2'; | ||
export { NotificationInstance, NotificationOptions } from './notification'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { OpaqueToken } from '@angular/core'; | ||
|
||
import { Notify } from './notify'; | ||
import { NotificationStatic, NotificationOptions } from './notification'; | ||
import { NotificationPermission } from './notification-permission'; | ||
|
||
declare var Notification: NotificationStatic; | ||
|
||
export const NOTIFY_GLOBAL_OPTIONS = new OpaqueToken('[@ngrx/notify] Global Options'); | ||
export const NOTIFICATION_STATIC = new OpaqueToken('[@ngrx/notify] Notification Static Constructor'); | ||
|
||
|
||
const NOTIFICATION_STATIC_PROVIDER = { | ||
provide: NOTIFICATION_STATIC, | ||
useValue: Notification | ||
}; | ||
|
||
const NOTIFICATION_PERMISSION_PROVIDER = { | ||
provide: NotificationPermission, | ||
deps: [ NOTIFICATION_STATIC ], | ||
useFactory(Notification: NotificationStatic) { | ||
return new NotificationPermission(Notification); | ||
} | ||
}; | ||
|
||
const NOTIFY_PROVIDER = { | ||
provide: Notify, | ||
deps: [ NOTIFICATION_STATIC, NOTIFY_GLOBAL_OPTIONS, NotificationPermission ], | ||
useFactory(Notification: NotificationStatic, options: NotificationOptions[], permission$: NotificationPermission) { | ||
return new Notify(Notification, options, permission$); | ||
} | ||
}; | ||
|
||
const DEFAULT_GLOBAL_OPTIONS_PROVIDER = { | ||
provide: NOTIFY_GLOBAL_OPTIONS, | ||
multi: true, | ||
useValue: {} | ||
}; | ||
|
||
export const NOTIFY_PROVIDERS: any[] = [ | ||
NOTIFICATION_STATIC_PROVIDER, | ||
NOTIFICATION_PERMISSION_PROVIDER, | ||
NOTIFY_PROVIDER, | ||
DEFAULT_GLOBAL_OPTIONS_PROVIDER | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Observable } from 'rxjs/Observable'; | ||
import { Subscriber } from 'rxjs/Subscriber'; | ||
|
||
import { NotificationStatic } from './notification'; | ||
|
||
export class NotificationPermission extends Observable<boolean> { | ||
constructor(notification?: NotificationStatic) { | ||
super((subscriber: Subscriber<boolean>) => { | ||
if (!notification || notification.permission === 'denied') { | ||
subscriber.next(false); | ||
subscriber.complete(); | ||
} | ||
else if (notification.permission === 'granted') { | ||
subscriber.next(true); | ||
subscriber.complete(); | ||
} | ||
else { | ||
notification.requestPermission().then(permission => { | ||
subscriber.next(permission === 'granted'); | ||
subscriber.complete(); | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export type NotificationPermissionStatus = 'denied' | 'granted' | 'default'; | ||
|
||
export interface NotificationOptions { | ||
dir?: 'auto' | 'ltr' | 'rtl'; | ||
lang?: string; | ||
body?: string; | ||
tag?: string; | ||
icon?: string; | ||
data?: any; | ||
vibrate?: number[]; | ||
renotify?: boolean; | ||
silent?: boolean; | ||
sound?: string; | ||
noscreen?: boolean; | ||
sticky?: boolean; | ||
} | ||
|
||
export interface NotificationInstance extends NotificationOptions { | ||
onclick: () => void; | ||
onerror: () => void; | ||
onclose: () => void; | ||
timestamp: string; | ||
close(): void; | ||
} | ||
|
||
export interface NotificationStatic { | ||
new (title: string, options?: NotificationOptions): NotificationInstance; | ||
permission: NotificationPermissionStatus; | ||
requestPermission(): Promise<NotificationPermissionStatus>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'rxjs/add/operator/cache'; | ||
import 'rxjs/add/operator/mergeMap'; | ||
import 'rxjs/add/observable/throw'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import { Subscriber } from 'rxjs/Subscriber'; | ||
|
||
import { NotificationStatic, NotificationOptions, NotificationInstance } from './notification'; | ||
|
||
|
||
export class Notify { | ||
private _permission$: Observable<boolean>; | ||
|
||
constructor( | ||
private notificationConstructor: NotificationStatic, | ||
private globalOptions: NotificationOptions[], | ||
permission$: Observable<boolean> | ||
) { | ||
this._permission$ = permission$.cache(); | ||
} | ||
|
||
requestPermission(): Observable<boolean> { | ||
return this._permission$; | ||
} | ||
|
||
private _createNotificationObservable(title: string, _options?: NotificationOptions) { | ||
const options: NotificationOptions = Object.assign({}, ...this.globalOptions, _options); | ||
|
||
return new Observable((subscriber: Subscriber<NotificationInstance>) => { | ||
const notification = new this.notificationConstructor(title, options); | ||
const CLOSE_NOTIFICATION = notification.close.bind(notification); | ||
|
||
notification.close = () => { CLOSE_NOTIFICATION(); subscriber.complete(); }; | ||
notification.onclick = () => subscriber.next(notification); | ||
notification.onerror = () => subscriber.error(notification); | ||
notification.onclose = () => subscriber.complete(); | ||
|
||
return CLOSE_NOTIFICATION; | ||
}); | ||
} | ||
|
||
open(title: string, options?: NotificationOptions): Observable<NotificationInstance> { | ||
return this.requestPermission().mergeMap(permission => { | ||
if (!permission) { | ||
return Observable.throw(new Error( | ||
'Permission Denied: You do not have permission to open a notification.' | ||
)); | ||
} | ||
|
||
return this._createNotificationObservable(title, options); | ||
}); | ||
} | ||
} |
Oops, something went wrong.