(function init(notifyLib) { //(function initWebNotification(notifyLib) { 'use strict'; app.factory("deskNotification", function onCreateService() { var service = {}; service.allowRequest = true; Object.defineProperty(service, 'permissionGranted', { get: function getPermission() { var permission = notifyLib.permissionLevel(); var permissionGranted = false; if (permission === notifyLib.PERMISSION_GRANTED) { permissionGranted = true; } return permissionGranted; } }); var isEnabled = function () { var enabled = notifyLib.isSupported; if (enabled) { enabled = service.permissionGranted; } return enabled; }; var createAndDisplayNotification = function (title, options) { var autoClose = 0; if (options && options.autoClose && (typeof options.autoClose === 'number')) { autoClose = options.autoClose; } notifyLib.config({ autoClose: autoClose }); var notification = notifyLib.createNotification(title, options); //add onclick handler if (options.onClick && notification && notification.webNotification) { notification.webNotification.onclick = options.onClick; } return function hideNotification() { notification.close(); }; }; var parseInput = function (argumentsArray) { //callback is always the last argument var 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 }; }; 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; }); }(window.notify));