Overview

angular-extended-notifications

Highly customizable notifications with Angular.js

Minimal setup

Retrieve with Bower:

bower install angular-extended-notifications

Then include the js and html files in your project:

<script src="bower_components/angular-extended-notifications/dist/angular-extended-notifications.min.js" type="text/javascript"></script>

Include the module in your app

angular.module("my-project", [
  // all my project dependencies…
  "angular-extended-notifications"
])
.run(function (notifications) {
  // set the directory containing the html templates
  notifications.useTemplatesDirectory("bower_components/angular-extended-notifications/dist/");
});

And use the service as you wish

angular.module("my-project").controller("my-controller", function(notifications) {
  "use strict";

  // use notifications
});

Documentation

Using the bundled template

With title and message arguments:

notifications.info("Hello title", "Message to the world!");

notifications.warning("Hello title", "Message to the world!");

notifications.error("Hello title", "Message to the world!");

notifications.success("Hello title", "Message to the world!");

notifications.info("Hello title", "Message to the world!");

With a configuration object:

notifications.info({
  title: "Hello title",
  message: "Message to the world!"
});

notifications.notify({
  type: "info"
  title: "Hello title",
  message: "Message to the world!"
});

Override the close button:

notifications.info("Hello title", "Message to the world!", {
  closeButton: false
});

Configure your CSS to style special notifications:

notifications.info("Hello title", "Message to the world!", {
  className: "custom_error_class"
});

Attach the notification anywhere you want (they are prepended on body by default):

notifications.info("Hello title", "Message to the world!", {
  attachTo: $("#my-div") // prepend
});

notifications.info("Hello title", "Message to the world!", {
  // describe how you want to attach the template by yourself
  attachTo: function(template) {
    $myDiv.append(template);
  }
});

The library uses font-awesome icons by default. This is how you override this behavior:

notifications.info("Hello title", "Message to the world!", {
  faIcon: false // no icon
});

notifications.info("Hello title", "Message to the world!", {
  faIcon: "fa-heart" // with a different icon
});

To disable font-awesome icons:

notifications.useFaIcons(false);

Change the notification duration on creation:

var notif = notifications.info("Hello title", "Message to the world!", {
  duration: 10000 // 10s
});

Or on the fly:

notif.changeDuration(5000); // 5s
notif.changeDuration(-1); // unlimited

Trigger callbacks when the notification appears or closes:

notifications.info("Hello title", "Message to the world!", {
  show: function () {
    console.log("The notification appears");
  },
  close: function () {
    console.log("This function executes when the notification disappears");
  }
});

Add custom actions (for example a "Dismiss" button):

var notif = notifications.info("Hello title", "Message to the world!", {
  actions: [{
    label: "Dismiss",
    fn: function () {
      console.log("This function executes when you click on "Dismiss"");
      notif.close();
    },
  }]
});

Or GMail's cancellation:

var notif = notifications.info("Sending…", "", {
  actions: [{
    label: "Cancel",
    fn: function () {
      notif.changeDuration(-1);
      notif.data.title = "Cancelling…";
      cancelEmailCall(function success() {
        notif.changeDuration(3000);
        notif.data.type = "success";
        notif.data.title = "Done";
      }, function error(code) {
        notif.changeDuration(3000);
        notif.data.type = "error";
        notif.data.title = "An error happened during the operation (" + code + ")";
      });
    },
  }]
});

Using a custom template

notifications.info("Hello title", "Message to the world!", {
  template: "my-custom-template" // by name (without extension)
});

notifications.info("Hello title", "Message to the world!", {
  templateFile: "my-custom-template.html" // by filename (with extension)
});

Override the template directory:

notifications.info("Hello title", "Message to the world!", {
  templatesDir: "/templates/"
});

The data object passed as parameter is available in the templates' scope:

notifications.info("Hello title", "Message to the world!", {
  username: myUser
});

See the bundled template as an example.

Using webkitNotifications (html5 notifications)

Webkit notifications are available as described in the draft spec.

To enhance the browser compatibilities, you can include the bundled shim for Firefox 22+ if necessary.

notifications.info("Hello title", "Message to the world!", {
  webkitNotifications: {
    iconFile: "favicon.ico"
  }
});

You can use HTMLNotifications if you want a custom template:

notifications.info("Hello title", "Message to the world!", {
  webkitNotifications: {
    notificationUrl: "url/to/the/notification/content"
  }
});

To enable the webkitNotifications by default:

notifications.useWebkitNotifications({
  // data.webkitNotifications object
  iconFile: "favicon.ico"
});

and to disable:

notifications.useWebkitNotifications(false);

License

Angulartics is freely distributable under the terms of the MIT license.

Copyright (c) 2013 Etienne Folio

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.