PageSourceSearch

https://app.waitry.net/bower_components/angular-web-notification/angular-web-notification.js

js waitry.net collected 2026-10-01 14:56:48 UTC 10,010 bytes, 280 lines download raw bytes

1/**
2 * 'showNotification' callback.
3 *
4 * @callback ShowNotificationCallback
5 * @param {error} [error] - The error object in case of any error
6 * @param {function} [hide] - The hide notification function
7 */
8
9/**
10 * @ngdoc method
11 * @function
12 * @memberof! webNotification
13 * @alias webNotification.initWebNotification
14 * @private
15 *
16 * @description
17 * Initializes the angular web notification service.
18 *
19 * @param {Object} NotifyLib - The HTML5 notification library instance
20 */
21(function initWebNotification(NotifyLib) {
22    'use strict';
23
24    var webNotification = window.angular.module('angular-web-notification', []);
25
26    /**
27     * @ngdoc service
28     * @name webNotification
29     * @namespace webNotification
30     * @author Sagie Gur-Ari
31     * @returns {Object} The service instance
32     *
33     * @description
34     * The web notification service wraps the HTML 5 Web Notifications API as an angular service.
35     */
36    webNotification.factory('webNotification', function onCreateService() {
37        var service = {};
38
39        /**
40         * The internal Notification library used by this angular module.
41         *
42         * @memberof! webNotification
43         * @alias webNotification.lib
44         * @private
45         */
46        service.lib = NotifyLib;
47
48        /**
49         * True to enable automatic requesting of permissions if needed.
50         *
51         * @memberof! webNotification
52         * @alias webNotification.allowRequest
53         * @public
54         */
55        service.allowRequest = true; //true to enable automatic requesting of permissions if needed
56
57        /*eslint-disable func-name-matching*/
58        Object.defineProperty(service, 'permissionGranted', {
59            /**
60             * Returns the permission granted value.
61             *
62             * @function
63             * @memberof! webNotification
64             * @private
65             * @returns {Boolean} True if permission is granted, else false
66             */
67            get: function getPermission() {
68                var permission = NotifyLib.permission;
69
70                /**
71                 * True if permission is granted, else false.
72                 *
73                 * @memberof! webNotification
74                 * @alias webNotification.permissionGranted
75                 * @public
76                 */
77                var permissionGranted = false;
78                if (permission === 'granted') {
79                    permissionGranted = true;
80                }
81
82                return permissionGranted;
83            }
84        });
85        /*eslint-enable func-name-matching*/
86
87        /**
88         * @ngdoc method
89         * @function
90         * @memberof! webNotification
91         * @alias webNotification.noop
92         * @private
93         *
94         * @description
95         * Empty function
96         *
97         * @returns {undefined} Undefined
98         */
99        var noop = function () {
100            return undefined;
101        };
102
103        /**
104         * @ngdoc method
105         * @function
106         * @memberof! webNotification
107         * @alias webNotification.isEnabled
108         * @private
109         *
110         * @description
111         * Checks if web notifications are permitted.
112         *
113         * @returns {Boolean} True if allowed to show web notifications
114         */
115        var isEnabled = function () {
116            return service.permissionGranted;
117        };
118
119        /**
120         * @ngdoc method
121         * @function
122         * @memberof! webNotification
123         * @alias webNotification.createAndDisplayNotification
124         * @private
125         *
126         * @description
127         * Displays the web notification and returning a 'hide' notification function.
128         *
129         * @param {String} title - The notification title text (defaulted to empty string if null is provided)
130         * @param {Object} options - Holds the notification data (web notification API spec for more info)
131         * @param {String} [options.icon=/favicon.ico] - The notification icon (defaults to the website favicon.ico)
132         * @param {Number} [options.autoClose] - Auto closes the notification after the provided amount of millies (0 or undefined for no auto close)
133         * @param {function} [options.onClick] - An optional onclick event handler
134         * @returns {function} The hide notification function
135         */
136        var createAndDisplayNotification = function (title, options) {
137            var autoClose = 0;
138            if (options.autoClose && (typeof options.autoClose === 'number')) {
139                autoClose = options.autoClose;
140            }
141
142            //defaults the notification icon to the website favicon.ico
143            if (!options.icon) {
144                options.icon = '/favicon.ico';
145            }
146
147            var notification = new NotifyLib(title, options);
148
149            //add onclick handler
150            if (options.onClick && notification) {
151                notification.onclick = options.onClick;
152            }
153
154            var hideNotification = function () {
155                notification.close();
156            };
157
158            if (autoClose) {
159                setTimeout(hideNotification, autoClose);
160            }
161
162            return hideNotification;
163        };
164
165        /**
166         * @ngdoc method
167         * @function
168         * @memberof! webNotification
169         * @alias webNotification.parseInput
170         * @private
171         *
172         * @description
173         * Returns an object with the show notification input.
174         *
175         * @param {Array} argumentsArray - An array of all arguments provided to the show notification function
176         * @returns {Object} The parsed data
177         */
178        var parseInput = function (argumentsArray) {
179            //callback is always the last argument
180            var callback = noop;
181            if (argumentsArray.length && (typeof argumentsArray[argumentsArray.length - 1] === 'function')) {
182                callback = argumentsArray.pop();
183            }
184
185            var title = null;
186            var options = null;
187            if (argumentsArray.length === 2) {
188                title = argumentsArray[0];
189                options = argumentsArray[1];
190            } else if (argumentsArray.length === 1) {
191                var value = argumentsArray.pop();
192                if (typeof value === 'string') {
193                    title = value;
194                    options = {};
195                } else {
196                    title = '';
197                    options = value;
198                }
199            }
200
201            //set defaults
202            title = title || '';
203            options = options || {};
204
205            return {
206                callback: callback,
207                title: title,
208                options: options
209            };
210        };
211
212        /**
213         * Shows the notification based on the provided input.<br>
214         * The callback invoked will get an error object (in case of an error, null in
215         * case of no errors) and a 'hide' function which can be used to hide the notification.
216         *
217         * @function
218         * @memberof! webNotification
219         * @alias webNotification.showNotification
220         * @public
221         * @param {String} [title] - The notification title text (defaulted to empty string if null is provided)
222         * @param {Object} [options] - Holds the notification data (web notification API spec for more info)
223         * @param {String} [options.icon=/favicon.ico] - The notification icon (defaults to the website favicon.ico)
224         * @param {Number} [options.autoClose] - Auto closes the notification after the provided amount of millies (0 or undefined for no auto close)
225         * @param {function} [options.onClick] - An optional onclick event handler
226         * @param {ShowNotificationCallback} [callback] - Called after the show is handled.
227         * @example
228         * ```js
229         * webNotification.showNotification('Example Notification', {
230         *    body: 'Notification Text...',
231         *    icon: 'my-icon.ico',
232         *    onClick: function onNotificationClicked() {
233         *      console.log('Notification clicked.');
234         *    },
235         *    autoClose: 4000 //auto close the notification after 4 seconds (you can manually close it via hide function)
236         * }, function onShow(error, hide) {
237         *    if (error) {
238         *        window.alert('Unable to show notification: ' + error.message);
239         *    } else {
240         *        setTimeout(function hideNotification() {
241         *            hide();
242         *        }, 5000);
243         *    }
244         * });
245         * ```
246         */
247        service.showNotification = function () {
248            //convert to array to enable modifications
249            var argumentsArray = Array.prototype.slice.call(arguments, 0);
250
251            if ((argumentsArray.length >= 1) && (argumentsArray.length <= 3)) {
252                var data = parseInput(argumentsArray);
253
254                //get values
255                var callback = data.callback;
256                var title = data.title;
257                var options = data.options;
258
259                var hideNotification = null;
260                if (isEnabled()) {
261                    hideNotification = createAndDisplayNotification(title, options);
262                    callback(null, hideNotification);
263                } else if (service.allowRequest) {
264                    NotifyLib.requestPermission(function onRequestDone() {
265                        if (isEnabled()) {
266                            hideNotification = createAndDisplayNotification(title, options);
267                            callback(null, hideNotification);
268                        } else {
269                            callback(new Error('Notifications are not enabled.'), null);
270                        }
271                    });
272                } else {
273                    callback(new Error('Notifications are not enabled.'), null);
274                }
275            }
276        };
277
278        return service;
279    });
280}(window.Notification));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.