angular-web-notification
Advanced tools
Comparing version 1.0.26 to 1.1.27
/** | ||
* 'showNotification' callback. | ||
* | ||
* @callback ShowNotificationCallback | ||
* @param {error} [error] - The error object in case of any error | ||
* @param {function} [hide] - The hide notification function | ||
*/ | ||
/** | ||
* @ngdoc method | ||
@@ -19,5 +11,5 @@ * @function | ||
* | ||
* @param {Object} NotifyLib - The HTML5 notification library instance | ||
* @param {Object} webNotificationAPI - The simplified web notification API | ||
*/ | ||
(function initWebNotification(NotifyLib) { | ||
(function initWebNotification(webNotificationAPI) { | ||
'use strict'; | ||
@@ -35,248 +27,39 @@ | ||
* @description | ||
* The web notification service wraps the HTML 5 Web Notifications API as an angular service. | ||
* The web notification service wraps the HTML 5 Web Notifications API as an angular service.<br> | ||
* See [simple-web-notification](https://github.com/sagiegurari/simple-web-notification/blob/master/docs/api.md) for detailed API. | ||
* | ||
* @example | ||
* ```js | ||
* angular.module('exampleApp').directive('showButton', ['webNotification', function (webNotification) { | ||
* return { | ||
* link: function (scope, element) { | ||
* element.on('click', function onClick() { | ||
* webNotification.showNotification('Example Notification', { | ||
* body: 'Notification Text...', | ||
* icon: 'my-icon.ico', | ||
* onClick: function onNotificationClicked() { | ||
* console.log('Notification clicked.'); | ||
* }, | ||
* autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function) | ||
* }, function onShow(error, hide) { | ||
* if (error) { | ||
* window.alert('Unable to show notification: ' + error.message); | ||
* } else { | ||
* console.log('Notification Shown.'); | ||
* | ||
* setTimeout(function hideNotification() { | ||
* console.log('Hiding notification....'); | ||
* hide(); //manually close the notification (you can skip this if you use the autoClose option) | ||
* }, 5000); | ||
* } | ||
* }); | ||
* }); | ||
* } | ||
* }; | ||
* }]); | ||
* ``` | ||
*/ | ||
webNotification.factory('webNotification', function onCreateService() { | ||
var service = {}; | ||
/** | ||
* The internal Notification library used by this angular module. | ||
* | ||
* @memberof! webNotification | ||
* @alias webNotification.lib | ||
* @private | ||
*/ | ||
service.lib = NotifyLib; | ||
/** | ||
* True to enable automatic requesting of permissions if needed. | ||
* | ||
* @memberof! webNotification | ||
* @alias webNotification.allowRequest | ||
* @public | ||
*/ | ||
service.allowRequest = true; //true to enable automatic requesting of permissions if needed | ||
/*eslint-disable func-name-matching*/ | ||
Object.defineProperty(service, 'permissionGranted', { | ||
/** | ||
* Returns the permission granted value. | ||
* | ||
* @function | ||
* @memberof! webNotification | ||
* @private | ||
* @returns {Boolean} True if permission is granted, else false | ||
*/ | ||
get: function getPermission() { | ||
var permission = NotifyLib.permission; | ||
/** | ||
* True if permission is granted, else false. | ||
* | ||
* @memberof! webNotification | ||
* @alias webNotification.permissionGranted | ||
* @public | ||
*/ | ||
var permissionGranted = false; | ||
if (permission === 'granted') { | ||
permissionGranted = true; | ||
} | ||
return permissionGranted; | ||
} | ||
}); | ||
/*eslint-enable func-name-matching*/ | ||
/** | ||
* @ngdoc method | ||
* @function | ||
* @memberof! webNotification | ||
* @alias webNotification.noop | ||
* @private | ||
* | ||
* @description | ||
* Empty function | ||
* | ||
* @returns {undefined} Undefined | ||
*/ | ||
var noop = function () { | ||
return undefined; | ||
}; | ||
/** | ||
* @ngdoc method | ||
* @function | ||
* @memberof! webNotification | ||
* @alias webNotification.isEnabled | ||
* @private | ||
* | ||
* @description | ||
* Checks if web notifications are permitted. | ||
* | ||
* @returns {Boolean} True if allowed to show web notifications | ||
*/ | ||
var isEnabled = function () { | ||
return service.permissionGranted; | ||
}; | ||
/** | ||
* @ngdoc method | ||
* @function | ||
* @memberof! webNotification | ||
* @alias webNotification.createAndDisplayNotification | ||
* @private | ||
* | ||
* @description | ||
* Displays the web notification and returning a 'hide' notification function. | ||
* | ||
* @param {String} title - The notification title text (defaulted to empty string if null is provided) | ||
* @param {Object} options - Holds the notification data (web notification API spec for more info) | ||
* @param {String} [options.icon=/favicon.ico] - The notification icon (defaults to the website favicon.ico) | ||
* @param {Number} [options.autoClose] - Auto closes the notification after the provided amount of millies (0 or undefined for no auto close) | ||
* @param {function} [options.onClick] - An optional onclick event handler | ||
* @returns {function} The hide notification function | ||
*/ | ||
var createAndDisplayNotification = function (title, options) { | ||
var autoClose = 0; | ||
if (options.autoClose && (typeof options.autoClose === 'number')) { | ||
autoClose = options.autoClose; | ||
} | ||
//defaults the notification icon to the website favicon.ico | ||
if (!options.icon) { | ||
options.icon = '/favicon.ico'; | ||
} | ||
var notification = new NotifyLib(title, options); | ||
//add onclick handler | ||
if (options.onClick && notification) { | ||
notification.onclick = options.onClick; | ||
} | ||
var hideNotification = function () { | ||
notification.close(); | ||
}; | ||
if (autoClose) { | ||
setTimeout(hideNotification, autoClose); | ||
} | ||
return hideNotification; | ||
}; | ||
/** | ||
* @ngdoc method | ||
* @function | ||
* @memberof! webNotification | ||
* @alias webNotification.parseInput | ||
* @private | ||
* | ||
* @description | ||
* Returns an object with the show notification input. | ||
* | ||
* @param {Array} argumentsArray - An array of all arguments provided to the show notification function | ||
* @returns {Object} The parsed data | ||
*/ | ||
var parseInput = function (argumentsArray) { | ||
//callback is always the last argument | ||
var callback = noop; | ||
if (argumentsArray.length && (typeof argumentsArray[argumentsArray.length - 1] === 'function')) { | ||
callback = argumentsArray.pop(); | ||
} | ||
var title = null; | ||
var options = null; | ||
if (argumentsArray.length === 2) { | ||
title = argumentsArray[0]; | ||
options = argumentsArray[1]; | ||
} else if (argumentsArray.length === 1) { | ||
var value = argumentsArray.pop(); | ||
if (typeof value === 'string') { | ||
title = value; | ||
options = {}; | ||
} else { | ||
title = ''; | ||
options = value; | ||
} | ||
} | ||
//set defaults | ||
title = title || ''; | ||
options = options || {}; | ||
return { | ||
callback: callback, | ||
title: title, | ||
options: options | ||
}; | ||
}; | ||
/** | ||
* Shows the notification based on the provided input.<br> | ||
* The callback invoked will get an error object (in case of an error, null in | ||
* case of no errors) and a 'hide' function which can be used to hide the notification. | ||
* | ||
* @function | ||
* @memberof! webNotification | ||
* @alias webNotification.showNotification | ||
* @public | ||
* @param {String} [title] - The notification title text (defaulted to empty string if null is provided) | ||
* @param {Object} [options] - Holds the notification data (web notification API spec for more info) | ||
* @param {String} [options.icon=/favicon.ico] - The notification icon (defaults to the website favicon.ico) | ||
* @param {Number} [options.autoClose] - Auto closes the notification after the provided amount of millies (0 or undefined for no auto close) | ||
* @param {function} [options.onClick] - An optional onclick event handler | ||
* @param {ShowNotificationCallback} [callback] - Called after the show is handled. | ||
* @example | ||
* ```js | ||
* webNotification.showNotification('Example Notification', { | ||
* body: 'Notification Text...', | ||
* icon: 'my-icon.ico', | ||
* onClick: function onNotificationClicked() { | ||
* console.log('Notification clicked.'); | ||
* }, | ||
* autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function) | ||
* }, function onShow(error, hide) { | ||
* if (error) { | ||
* window.alert('Unable to show notification: ' + error.message); | ||
* } else { | ||
* setTimeout(function hideNotification() { | ||
* hide(); | ||
* }, 5000); | ||
* } | ||
* }); | ||
* ``` | ||
*/ | ||
service.showNotification = function () { | ||
//convert to array to enable modifications | ||
var argumentsArray = Array.prototype.slice.call(arguments, 0); | ||
if ((argumentsArray.length >= 1) && (argumentsArray.length <= 3)) { | ||
var data = parseInput(argumentsArray); | ||
//get values | ||
var callback = data.callback; | ||
var title = data.title; | ||
var options = data.options; | ||
var hideNotification = null; | ||
if (isEnabled()) { | ||
hideNotification = createAndDisplayNotification(title, options); | ||
callback(null, hideNotification); | ||
} else if (service.allowRequest) { | ||
NotifyLib.requestPermission(function onRequestDone() { | ||
if (isEnabled()) { | ||
hideNotification = createAndDisplayNotification(title, options); | ||
callback(null, hideNotification); | ||
} else { | ||
callback(new Error('Notifications are not enabled.'), null); | ||
} | ||
}); | ||
} else { | ||
callback(new Error('Notifications are not enabled.'), null); | ||
} | ||
} | ||
}; | ||
return service; | ||
return webNotificationAPI; | ||
}); | ||
}(window.Notification)); | ||
}(window.webNotification)); |
{ | ||
"name": "angular-web-notification", | ||
"version": "1.0.26", | ||
"version": "1.1.27", | ||
"description": "AngularJS service for displaying web notifications.", | ||
@@ -27,3 +27,3 @@ "authors": [ | ||
"dependencies": { | ||
"html5-desktop-notifications2": "~3", | ||
"simple-web-notification": "latest", | ||
"angular": "~1" | ||
@@ -30,0 +30,0 @@ }, |
110
docs/api.md
@@ -1,21 +0,6 @@ | ||
## Objects | ||
<dl> | ||
<dt><a href="#webNotification">webNotification</a> ⇒ <code>Object</code></dt> | ||
<dd><p>The web notification service wraps the HTML 5 Web Notifications API as an angular service.</p> | ||
</dd> | ||
</dl> | ||
## Typedefs | ||
<dl> | ||
<dt><a href="#ShowNotificationCallback">ShowNotificationCallback</a> : <code>function</code></dt> | ||
<dd><p>'showNotification' callback.</p> | ||
</dd> | ||
</dl> | ||
<a name="webNotification"></a> | ||
## webNotification ⇒ <code>Object</code> | ||
The web notification service wraps the HTML 5 Web Notifications API as an angular service. | ||
The web notification service wraps the HTML 5 Web Notifications API as an angular service.<br> | ||
See [simple-web-notification](https://github.com/sagiegurari/simple-web-notification/blob/master/docs/api.md) for detailed API. | ||
@@ -26,68 +11,31 @@ **Kind**: global namespace | ||
**Author:** Sagie Gur-Ari | ||
* [webNotification](#webNotification) ⇒ <code>Object</code> | ||
* [.allowRequest](#webNotification.allowRequest) | ||
* [.permissionGranted](#webNotification.permissionGranted) | ||
* [.showNotification([title], [options], [callback])](#webNotification.showNotification) | ||
<a name="webNotification.allowRequest"></a> | ||
### webNotification.allowRequest | ||
True to enable automatic requesting of permissions if needed. | ||
**Access:** public | ||
<a name="webNotification.permissionGranted"></a> | ||
### webNotification.permissionGranted | ||
True if permission is granted, else false. | ||
**Access:** public | ||
<a name="webNotification.showNotification"></a> | ||
### webNotification.showNotification([title], [options], [callback]) | ||
Shows the notification based on the provided input.<br> | ||
The callback invoked will get an error object (in case of an error, null in | ||
case of no errors) and a 'hide' function which can be used to hide the notification. | ||
**Access:** public | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [title] | <code>String</code> | | The notification title text (defaulted to empty string if null is provided) | | ||
| [options] | <code>Object</code> | | Holds the notification data (web notification API spec for more info) | | ||
| [options.icon] | <code>String</code> | <code>/favicon.ico</code> | The notification icon (defaults to the website favicon.ico) | | ||
| [options.autoClose] | <code>Number</code> | | Auto closes the notification after the provided amount of millies (0 or undefined for no auto close) | | ||
| [options.onClick] | <code>function</code> | | An optional onclick event handler | | ||
| [callback] | <code>[ShowNotificationCallback](#ShowNotificationCallback)</code> | | Called after the show is handled. | | ||
**Example** | ||
```js | ||
webNotification.showNotification('Example Notification', { | ||
body: 'Notification Text...', | ||
icon: 'my-icon.ico', | ||
onClick: function onNotificationClicked() { | ||
console.log('Notification clicked.'); | ||
}, | ||
autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function) | ||
}, function onShow(error, hide) { | ||
if (error) { | ||
window.alert('Unable to show notification: ' + error.message); | ||
} else { | ||
setTimeout(function hideNotification() { | ||
hide(); | ||
}, 5000); | ||
} | ||
}); | ||
angular.module('exampleApp').directive('showButton', ['webNotification', function (webNotification) { | ||
return { | ||
link: function (scope, element) { | ||
element.on('click', function onClick() { | ||
webNotification.showNotification('Example Notification', { | ||
body: 'Notification Text...', | ||
icon: 'my-icon.ico', | ||
onClick: function onNotificationClicked() { | ||
console.log('Notification clicked.'); | ||
}, | ||
autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function) | ||
}, function onShow(error, hide) { | ||
if (error) { | ||
window.alert('Unable to show notification: ' + error.message); | ||
} else { | ||
console.log('Notification Shown.'); | ||
setTimeout(function hideNotification() { | ||
console.log('Hiding notification....'); | ||
hide(); //manually close the notification (you can skip this if you use the autoClose option) | ||
}, 5000); | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
}]); | ||
``` | ||
<a name="ShowNotificationCallback"></a> | ||
## ShowNotificationCallback : <code>function</code> | ||
'showNotification' callback. | ||
**Kind**: global typedef | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| [error] | <code>error</code> | The error object in case of any error | | ||
| [hide] | <code>function</code> | The hide notification function | | ||
| Date | Version | Description | | ||
| ----------- | ------- | ----------- | | ||
| 2017-01-22 | v1.1.27 | Split the internal web notification API into a new project: simple-web-notification | | ||
| 2017-01-13 | v1.0.26 | Maintenance | | ||
@@ -4,0 +5,0 @@ | 2016-11-23 | v1.0.19 | Use forked version of html5-desktop-notifications in order to resolve few issues | |
{ | ||
"name": "angular-web-notification", | ||
"version": "1.0.26", | ||
"version": "1.1.27", | ||
"description": "AngularJS service for displaying web notifications.", | ||
@@ -41,2 +41,5 @@ "author": { | ||
}, | ||
"dependencies": { | ||
"simple-web-notification": "latest" | ||
}, | ||
"devDependencies": { | ||
@@ -43,0 +46,0 @@ "chai": "latest", |
@@ -19,2 +19,3 @@ /*global module: false, require: false */ | ||
'test/helpers/**/*.js', | ||
'bower_components/simple-web-notification/web-notification.js', | ||
mainJSFile, | ||
@@ -21,0 +22,0 @@ 'test/spec/**/*.js' |
@@ -36,2 +36,3 @@ # {"gitdown": "gitinfo", "name": "name"} | ||
<script type="text/javascript" src="html5-desktop-notifications2/dist/Notification.js"></script> | ||
<script type="text/javascript" src="simple-web-notification/web-notification.js"></script> | ||
<script type="text/javascript" src="angular-web-notification.js"></script> | ||
@@ -38,0 +39,0 @@ ``` |
@@ -36,2 +36,3 @@ # angular-web-notification | ||
<script type="text/javascript" src="html5-desktop-notifications2/dist/Notification.js"></script> | ||
<script type="text/javascript" src="simple-web-notification/web-notification.js"></script> | ||
<script type="text/javascript" src="angular-web-notification.js"></script> | ||
@@ -112,2 +113,3 @@ ``` | ||
| ----------- | ------- | ----------- | | ||
| 2017-01-22 | v1.1.27 | Split the internal web notification API into a new project: simple-web-notification | | ||
| 2017-01-13 | v1.0.26 | Maintenance | | ||
@@ -114,0 +116,0 @@ | 2016-11-23 | v1.0.19 | Use forked version of html5-desktop-notifications in order to resolve few issues | |
@@ -6,8 +6,2 @@ /*global describe: false, assert: false, inject: false, it: false, beforeEach: false */ | ||
var emptyValuesValidation = function (title, options) { | ||
assert.equal(title, ''); | ||
assert.deepEqual(options, { | ||
icon: '/favicon.ico' | ||
}); | ||
}; | ||
var validShowValidation = function (error, hide, done) { | ||
@@ -23,2 +17,3 @@ assert.isNull(error); | ||
}; | ||
var errorValidation = function (error, hide, done) { | ||
@@ -42,5 +37,5 @@ assert.isDefined(error); | ||
describe('showNotification tests', function () { | ||
describe('showNotification allowed tests', function () { | ||
it('showNotification all info test', function (done) { | ||
describe('showNotification', function () { | ||
describe('allowed', function () { | ||
it('valid', function (done) { | ||
inject(function (webNotification) { | ||
@@ -65,3 +60,3 @@ assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
it('showNotification with auto close test', function (done) { | ||
it('auto close', function (done) { | ||
inject(function (webNotification) { | ||
@@ -88,125 +83,5 @@ assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
it('showNotification no params test', function (done) { | ||
it('first time permissions', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(emptyValuesValidation); | ||
webNotification.showNotification(function onShow(error, hide) { | ||
validShowValidation(error, hide, done); | ||
}); | ||
}); | ||
}); | ||
it('showNotification no input test', function () { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(emptyValuesValidation); | ||
webNotification.showNotification(); | ||
}); | ||
}); | ||
it('showNotification too many args test', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(emptyValuesValidation); | ||
webNotification.showNotification(1, 2, 3, 4, function () { | ||
assert.fail(); | ||
}); | ||
setTimeout(function () { | ||
done(); | ||
}, 50); | ||
}); | ||
}); | ||
it('showNotification null info test', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(emptyValuesValidation); | ||
webNotification.showNotification(null, null, function onShow(error, hide) { | ||
validShowValidation(error, hide, done); | ||
}); | ||
}); | ||
}); | ||
it('showNotification no callback test', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(function (title, options) { | ||
assert.equal(title, 'Example Notification'); | ||
assert.deepEqual(options, { | ||
body: 'Notification Text...', | ||
icon: 'my-icon.ico' | ||
}); | ||
}); | ||
webNotification.showNotification('Example Notification', { | ||
body: 'Notification Text...', | ||
icon: 'my-icon.ico' | ||
}); | ||
setTimeout(done, 50); | ||
}); | ||
}); | ||
it('showNotification no title test', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(function (title, options) { | ||
assert.equal(title, ''); | ||
assert.deepEqual(options, { | ||
body: 'no title', | ||
icon: '/favicon.ico' | ||
}); | ||
}); | ||
webNotification.showNotification({ | ||
body: 'no title' | ||
}, function onShow(error, hide) { | ||
validShowValidation(error, hide, done); | ||
}); | ||
}); | ||
}); | ||
it('showNotification with no icon test', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(function (title, options) { | ||
assert.equal(title, 'Example Notification'); | ||
assert.deepEqual(options, { | ||
body: 'Notification Text...', | ||
icon: '/favicon.ico' | ||
}); | ||
}); | ||
webNotification.showNotification('Example Notification', { | ||
body: 'Notification Text...' | ||
}, function onShow(error, hide) { | ||
validShowValidation(error, hide, done); | ||
}); | ||
}); | ||
}); | ||
it('showNotification no options test', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(function (title, options) { | ||
assert.equal(title, 'no options'); | ||
assert.deepEqual(options, { | ||
icon: '/favicon.ico' | ||
}); | ||
}); | ||
webNotification.showNotification('no options', function onShow(error, hide) { | ||
validShowValidation(error, hide, done); | ||
}); | ||
}); | ||
}); | ||
it('showNotification first time permissions test', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setNotAllowed(function (title, options) { | ||
@@ -233,26 +108,6 @@ assert.equal(title, 'first time'); | ||
}); | ||
it('showNotification with onClick', function (done) { | ||
inject(function (webNotification) { | ||
assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
window.Notification.setAllowed(function (title, options) { | ||
assert.equal(title, 'Example Notification'); | ||
assert.isFunction(options.onClick); | ||
}); | ||
webNotification.showNotification('Example Notification', { | ||
body: 'Notification Text...', | ||
icon: 'my-icon.ico', | ||
onClick: function () { | ||
done(); | ||
} | ||
}, function onShow(error, hide) { | ||
validShowValidation(error, hide); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('showNotification not allowed tests', function () { | ||
it('showNotification not allowed test', function (done) { | ||
describe('not allowed', function () { | ||
it('not allowed', function (done) { | ||
inject(function (webNotification) { | ||
@@ -268,3 +123,3 @@ assert.isTrue(webNotification.lib.MOCK_NOTIFY); | ||
it('showNotification not allowed and not allowed to ask permissions test', function (done) { | ||
it('not allowed and not allowed to ask permissions', function (done) { | ||
inject(function (webNotification) { | ||
@@ -271,0 +126,0 @@ assert.isTrue(webNotification.lib.MOCK_NOTIFY); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
139
54776
1
615
+ Addedsimple-web-notification@2.0.1(transitive)