|
1 | 1 | /*global notify: false, angular: false */
|
| 2 | + |
2 | 3 | (function (notifyLib) {
|
3 | 4 | 'use strict';
|
4 | 5 |
|
|
39 | 40 | };
|
40 | 41 | };
|
41 | 42 |
|
42 |
| - return { |
| 43 | + var service = { |
| 44 | + /** |
| 45 | + * True to enable automatic requesting of permissions if needed. |
| 46 | + * |
| 47 | + * @memberof! webNotification |
| 48 | + * @public |
| 49 | + */ |
| 50 | + allowRequest: true,//true to enable automatic requesting of permissions if needed |
43 | 51 | /**
|
44 | 52 | * Shows the notification based on the provided input.<br>
|
45 | 53 | * The callback invoked will get an error object (in case of an error, null in
|
|
48 | 56 | * @function
|
49 | 57 | * @memberof! webNotification
|
50 | 58 | * @public
|
51 |
| - * @param {string} title - The notification title text |
52 |
| - * @param {object} options - Holds the notification data (see https://github.com/ttsvetko/HTML5-Desktop-Notifications.git for more info) |
| 59 | + * @param {string} [title] - The notification title text (defaulted to empty string if null is provided) |
| 60 | + * @param {object} [options] - Holds the notification data (web notification API spec for more info) |
53 | 61 | * @param {function} callback - Called after the show is handled.
|
54 | 62 | * @example
|
55 | 63 | * webNotification.showNotification('Example Notification', {
|
|
65 | 73 | * }
|
66 | 74 | * });
|
67 | 75 | */
|
68 |
| - showNotification: function (title, options, callback) { |
69 |
| - var hideNotification = null; |
70 |
| - if (isEnabled()) { |
71 |
| - hideNotification = createAndDisplayNotification(title, options); |
72 |
| - callback(null, hideNotification); |
73 |
| - } else { |
74 |
| - notifyLib.requestPermission(function onRequestDone() { |
75 |
| - if (isEnabled()) { |
76 |
| - hideNotification = createAndDisplayNotification(title, options); |
77 |
| - callback(null, hideNotification); |
| 76 | + showNotification: function () { |
| 77 | + //convert to array to enable modifications |
| 78 | + var argumentsArray = Array.prototype.slice.call(arguments, 0); |
| 79 | + |
| 80 | + if ((argumentsArray.length >= 1) && (argumentsArray.length <= 3)) { |
| 81 | + //callback is always the last argument |
| 82 | + var callback = argumentsArray.pop(); |
| 83 | + |
| 84 | + var title = null; |
| 85 | + var options = null; |
| 86 | + if (argumentsArray.length === 2) { |
| 87 | + title = argumentsArray[0]; |
| 88 | + options = argumentsArray[1]; |
| 89 | + } else if (argumentsArray.length === 1) { |
| 90 | + var value = argumentsArray.pop(); |
| 91 | + if (typeof value === 'string') { |
| 92 | + title = value || ''; |
| 93 | + options = {}; |
78 | 94 | } else {
|
79 |
| - callback(new Error('Notification is not enabled.'), null); |
| 95 | + title = ''; |
| 96 | + options = value; |
80 | 97 | }
|
81 |
| - }); |
| 98 | + } |
| 99 | + |
| 100 | + var hideNotification = null; |
| 101 | + if (isEnabled()) { |
| 102 | + hideNotification = createAndDisplayNotification(title, options); |
| 103 | + callback(null, hideNotification); |
| 104 | + } else if (service.allowRequest) { |
| 105 | + notifyLib.requestPermission(function onRequestDone() { |
| 106 | + if (isEnabled()) { |
| 107 | + hideNotification = createAndDisplayNotification(title, options); |
| 108 | + callback(null, hideNotification); |
| 109 | + } else { |
| 110 | + callback(new Error('Notifications are not enabled.'), null); |
| 111 | + } |
| 112 | + }); |
| 113 | + } else { |
| 114 | + callback(new Error('Notifications are not enabled.'), null); |
| 115 | + } |
82 | 116 | }
|
83 | 117 | }
|
84 | 118 | };
|
| 119 | + |
| 120 | + return service; |
85 | 121 | });
|
86 | 122 | }(notify));
|
0 commit comments