11/**
22 * push.js
3- * --------
3+ * =======
44 * A compact, cross-browser solution for Javascript desktop notifications
55 *
6+ * Credits
7+ * -------
8+ * Tsvetan Tsvetkov (ttsvetko)
9+ * Alex Gibson (alexgibson)
10+ *
11+ * License
12+ * -------
13+ *
614 * The MIT License (MIT)
715 *
816 * Copyright (c) 2015 Tyler Nickerson
2533 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2634 * THE SOFTWARE.
2735 *
28- * Inspired by the work of
29- * Tsvetan Tsvetkov (ttsvetko) and Alex Gibson (alexgibson)
3036 */
3137
3238// Window root
@@ -61,12 +67,18 @@ var root = (window !== 'undefined' ? window : self);
6167 isString = function ( obj ) { return obj && obj . constructor === String ; } ,
6268 isFunction = function ( obj ) { return obj && obj . constructor === Function ; } ,
6369
70+ /* Whether Push has permission to notify */
71+ hasPermission = false ,
72+
6473 /**
6574 * Callback function for the 'create' method
6675 * @return {void }
6776 */
6877 create_callback = function ( title , options ) {
6978
79+ /* Set empty settings if none are specified */
80+ options = options || { } ;
81+
7082 /* Safari 6+, Chrome 23+ */
7183 if ( w . Notification ) {
7284
@@ -135,18 +147,10 @@ var root = (window !== 'undefined' ? window : self);
135147 } ;
136148
137149 /* Autoclose timeout */
138- if ( notification &&
139- notification . addEventListener &&
140- options . timeout ) {
141-
142- notification . addEventListener ( 'show' , function ( ) {
143-
144- setTimeout ( function ( ) {
145- wrapper . close ( ) ;
146- } , options . timeout ) ;
147-
148- } ) ;
149-
150+ if ( options . timeout ) {
151+ setTimeout ( function ( ) {
152+ wrapper . close ( ) ;
153+ } , options . timeout ) ;
150154 }
151155
152156 /* Notification callbacks */
@@ -189,13 +193,29 @@ var root = (window !== 'undefined' ? window : self);
189193 * @param {Function } callback - Function to execute once permission is granted
190194 * @return {void }
191195 */
192- self . Permission . request = function ( callback ) {
196+ self . Permission . request = function ( onGranted , onDenied ) {
193197
194198 /* Return if Push not supported */
195199 if ( ! self . isSupported ) { return ; }
196200
197- /* Set an empty callback if an invalid one is specified */
198- callback = isFunction ( callback ) ? callback : function ( ) { } ;
201+ /* Default callback */
202+ callback = function ( result ) {
203+
204+ switch ( result ) {
205+
206+ case self . Permission . GRANTED :
207+ hasPermission = true ;
208+ if ( onGranted ) onGranted ( ) ;
209+ break ;
210+
211+ case self . Permission . DENIED :
212+ hasPermission = false ;
213+ if ( onDenied ) onDenied ( ) ;
214+ break ;
215+
216+ }
217+
218+ } ;
199219
200220 /* Legacy webkit browsers */
201221 if ( w . webkitNotifications && w . webkitNotifications . checkPermission ) {
@@ -208,6 +228,14 @@ var root = (window !== 'undefined' ? window : self);
208228
209229 } ;
210230
231+ /**
232+ * Returns whether Push has been granted permission to run
233+ * @return {Boolean }
234+ */
235+ self . Permission . has = function ( ) {
236+ return hasPermission ;
237+ } ;
238+
211239 /**
212240 * Gets the permission level
213241 * @return {Permission } The permission level
@@ -290,15 +318,24 @@ var root = (window !== 'undefined' ? window : self);
290318
291319 /* Fail if the browser is not supported */
292320 if ( ! self . isSupported ) {
293- console . error ( 'push.js is incompatible with self browser.' ) ;
321+ console . error ( 'PushError: push.js is incompatible with self browser.' ) ;
294322 return ;
295323 }
296324
325+ /* Fail if no or an invalid title is provided */
326+ if ( typeof title !== 'string' ) {
327+ throw 'PushError: Title of notification must be a string' ;
328+ }
329+
330+ console . log ( self . Permission . has ( ) ) ;
331+
297332 /* Request permission if it isn't granted */
298- if ( self . Permission . get ( ) !== self . Permission . GRANTED ) {
333+ if ( ! self . Permission . has ( ) ) {
299334 self . Permission . request ( function ( ) {
300335 create_callback ( title , options ) ;
301336 } ) ;
337+ } else {
338+ create_callback ( title , options ) ;
302339 }
303340
304341 } ;
0 commit comments