angular-extended-notifications
Version:
Highly customizable notifications with AngularJS
7 lines • 3.07 kB
JavaScript
/*!
* angular-extended-notifications v1.0.5 - 2016-05-06
* (c) 2014-2016 L.systems SARL, Etienne Folio, Quentin Raynaud
* https://bitbucket.org/lsystems/angular-extended-notifications
* License: MIT
*/
angular.module("angular-extended-notifications",[]).provider("notifications",function(){"use strict";var a={info:"fa-info",error:"fa-exclamation-circle",success:"fa-check",warning:"fa-exclamation-triangle"};this.setFaIcons=function(b){for(var c in b)a[c]=b[c];return this};var b={attachTo:"body",className:"",duration:2500,webkitNotifications:!1,close:angular.noop,show:angular.noop,closeButton:!0,templateFile:"global-notification.html",templatesDir:"templates/"};this.setDefaults=function(a){for(var c in a)b[c]=a[c];return this},this.$get=["$timeout","$templateCache","$injector","$rootScope","$compile","$q",function(c,d,e,f,g,h){function i(a){var i;for(var j in b)void 0===a[j]&&(a[j]=b[j]);a.template&&(a.templateFile=a.template+".html");var l=window.webkitNotifications,m=h.all([h.when(a.message),h.when(a.title)]).then(function(b){if(a.message=b[0],a.title=b[1],a.webkitNotifications&&l&&!l.checkPermission()){if("object"!=typeof a.webkitNotifications)throw new Error("data.webkitNotifications should either be false or an object");return i=a.webkitNotifications.notificationUrl?l.createHTMLNotification(a.webkitNotifications.notificationUrl):l.createNotification(a.webkitNotifications.iconFile,a.title,a.content),i.ondisplay=a.show,i.close=a.close,i.show(),i}});if(a.webkitNotifications&&l&&!l.checkPermission())return m;var n=function(b){var d=f.$new(!0);if(d.close=i.close.bind(i),d.data=a,i.templateElement=g(b)(d),i.templateElement.addClass(a.className),"string"==typeof a.attachTo&&(a.attachTo=angular.element(document.querySelector(a.attachTo))),"object"==typeof a.attachTo&&a.attachTo.prepend)a.attachTo.prepend(i.templateElement);else{if("function"!=typeof a.attachTo)throw new Error("Invalid value for attachTo. It should be either a function or an object with a prepend method");a.attachTo(i.templateElement)}if(k.push(i),a.closeOnRouteChange){if(!angular.isString(a.closeOnRouteChange))throw new Error('Invalid property closeOnRouteChange. Should be a string, like "route" to match an event like "routeChangeStart"');var e=f.$on("$"+a.closeOnRouteChange+"ChangeStart",function(){i.close(),e()})}~a.duration&&(i.$timeoutPromise=c(i.close.bind(i),a.duration)),a.show()};i={data:a,close:function(){this.$timeoutPromise&&c.cancel(this.$timeoutPromise),a.close()!==!1&&(k.splice(k.indexOf(this),1),n=angular.noop,this.templateElement&&this.templateElement.remove())}};var o=e.get("$http");return o.get(a.templatesDir+a.templateFile,{cache:d}).success(function(a){n(a)}).error(function(a){throw new Error("Template specified for notifications ("+a.template+") could not be loaded. "+a)}),i}function j(b){return function(c,d,e){return e=e||{},"object"==typeof c?e=c:(e.title=c,e.message=d),e.type=b,e.faIcons&&b in a&&(e.faIcon=a[b]),i(e)}}var k=[];return{queue:k,info:j("info"),error:j("error"),success:j("success"),warning:j("warning"),notify:i}}]});