From 4d35cd7646e869523228a9b9249f5117b3526a86 Mon Sep 17 00:00:00 2001 From: Amandio Magalhaes Date: Thu, 4 Jan 2018 15:35:02 -0200 Subject: [PATCH] Events are called twice --- README.md | 58 +++++++++++++++++++++++++++++++++- index.js | 20 +++++++----- package.json | 2 +- vue-native-notification.min.js | 2 +- 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 612ed0a..382ad7d 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ export default { // https://developer.mozilla.org/en-US/docs/Web/API/Notification/Notification#Parameters this.$notification.show('Hello World', { body: 'This is an example!' - }) + }, {}) } } } @@ -61,6 +61,62 @@ this.$notification.requestPermission() .then(console.log) ``` +### Events + +https://developer.mozilla.org/en-US/docs/Web/API/Notification + +We now supports all notifications events + +#### onerror + +https://developer.mozilla.org/en-US/docs/Web/API/Notification/onerror + +Is an empty function. Nothing will be executed + +#### onclick + +https://developer.mozilla.org/en-US/docs/Web/API/Notification/onclick + +When notification is clicked, we set the focus on the context browser and close the notification + +#### onclose + +https://developer.mozilla.org/en-US/docs/Web/API/Notification/onclose + +Is an empty function. Nothing will be executed + +#### onshow + +https://developer.mozilla.org/en-US/docs/Web/API/Notification/onshow + +Is an empty function. Nothing will be executed + +#### Usage + +```javascript +const notification = { + title: 'Your title', + options: { + body: 'This is an example!' + }, + events: { + onerror: function () { + console.log('Custom error event was called'); + }, + onclick: function () { + console.log('Custom click event was called'); + }, + onclose: function () { + console.log('Custom close event was called'); + }, + onshow: function () { + console.log('Custom show event was called'); + } + } +} +this.$notification.show(notification.title, notification.options, notification.events) +``` + ## License [MIT](LICENSE.md) diff --git a/index.js b/index.js index 9b78143..4ae41a6 100644 --- a/index.js +++ b/index.js @@ -2,18 +2,22 @@ var Notification = window.Notification || window.webkitNotification const onerror = function onerror (event) { + // console.log('On error event was called') } const onclick = function onclick (event) { + // console.log('On click event was called') event.preventDefault() window.focus() event.target.close() } const onclose = function onclose (event) { + // console.log('On close event was called') } const onshow = function onshow (event) { + // console.log('On show event was called') } const defaultEvents = { @@ -41,10 +45,10 @@ const VueNativeNotification = { // Show function var show = function (title, opts, { - onerror = defaultEvents.onerror, - onclick = defaultEvents.onclick, - onclose = defaultEvents.onclose, - onshow = defaultEvents.onshow + onerror = function () { }, + onclick = function () { }, + onclose = function () { }, + onshow = function () { } }) { return Promise.resolve() .then(function () { @@ -67,25 +71,25 @@ const VueNativeNotification = { const bindOnError = function (event) { 'use strict' defaultEvents.onerror(event) - onerror(event) + onerror() } const bindOnClick = function (event) { 'use strict' defaultEvents.onclick(event) - onclick(event) + onclick() } const bindOnClose = function (event) { 'use strict' defaultEvents.onclose(event) - onclose(event) + onclose() } const bindOnShow = function (event) { 'use strict' defaultEvents.onshow(event) - onshow(event) + onshow() } notification.onerror = bindOnError diff --git a/package.json b/package.json index 4967fba..8cd6070 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-native-notification", - "version": "1.0.1", + "version": "1.0.2", "description": "Vue.js plugin for native notifications", "main": "index.js", "repository": "https://github.com/dennisbruner/vue-native-notification.git", diff --git a/vue-native-notification.min.js b/vue-native-notification.min.js index 3c87e7c..54cf363 100644 --- a/vue-native-notification.min.js +++ b/vue-native-notification.min.js @@ -1 +1 @@ -(function n(o,r,e){function i(u,c){if(!r[u]){if(!o[u]){var f=typeof require=="function"&&require;if(!c&&f)return f(u,!0);if(t)return t(u,!0);var s=new Error("Cannot find module '"+u+"'");throw s.code="MODULE_NOT_FOUND",s}var a=r[u]={exports:{}};o[u][0].call(a.exports,function(n){var r=o[u][1][n];return i(r?r:n)},a,a.exports,n,o,r,e)}return r[u].exports}var t=typeof require=="function"&&require;for(var u=0;u