Package detail

angular-toastr

Foxandxss44.6kMIT2.1.1
angularjs, toastr, popup

readme

Angular Toastr

Code Climate Build Status devDependency Status

NOTE: For angular 1.2.x support check angular-1.2 branch or download the 0.4.x release of angular-toastr.

angular-toastr was originally a port of CodeSeven/toastr. It could now show some differences with it.

The goal is to provide the same API than the original one but without jQuery and using all the angular power.

Demo

Demo

Installation

Use npm:

$ npm install angular-toastr

If you are not using npm (you should), you can use bower:

$ bower install angular-toastr

To use a CDN, you can include the next two lines:

<script src="https://npmcdn.com/angular-toastr/dist/angular-toastr.tpls.js"></script>
<link rel="stylesheet" href="https://npmcdn.com/angular-toastr/dist/angular-toastr.css" />

Or you can grab the latest release and add both the css and javascript file:

<link rel="stylesheet" type="text/css" href="angular-toastr.css" />
<script type="text/javascript" src="angular-toastr.tpls.js"></script>

Note: If you add a script tag for angular-toastr, keep in mind that you need the tpls version or the other depending if you want the default template or not (see below).

If you want animations, don't forget to add angular-animate.

Then add toastr to your modules dependencies:

angular.module('app', ['ngAnimate', 'toastr'])

Usage

Toastr usage is very simple, by default it comes with four types of notification messages:

Success:

app.controller('foo', function($scope, toastr) {
  toastr.success('Hello world!', 'Toastr fun!');
});

Success Image

Info:

app.controller('foo', function($scope, toastr) {
  toastr.info('We are open today from 10 to 22', 'Information');
});

Info Image

Error:

app.controller('foo', function($scope, toastr) {
  toastr.error('Your credentials are gone', 'Error');
});

Error Image

Warning:

app.controller('foo', function($scope, toastr) {
  toastr.warning('Your computer is about to explode!', 'Warning');
});

Warning Image

Apart from that you can customize your basic toasts:

No title:

app.controller('foo', function($scope, toastr) {
  toastr.success('I don\'t need a title to live');
});

No Title

Closing toasts programmatically:

app.controller('foo', function($scope, toastr) {
  toastr.clear([toast]);
});

If no toast is passed in, all toasts will be closed.

Getting active (open) toasts:

app.controller('foo', function($scope, toastr) {
  toastr.active();
});

Refreshing an opened toast:

app.controller('foo', function($scope, toastr) {
  var toast = toastr.error('You are not allowed to do this!');
  // after doing something...
  toastr.refreshTimer(toast, 5000);
});

The second parameter is optional and will fallback to the configured timeOut.

It return the number of active toasts in screen.

Other options

A toast has a isOpened flag to see whether it is opened or not.

Toastr customization

This library has two parts, a container and the toasts you put in it.

To configure the container you need to modify the toastrConfig, for example:

app.config(function(toastrConfig) {
  angular.extend(toastrConfig, {
    autoDismiss: false,
    containerId: 'toast-container',
    maxOpened: 0,    
    newestOnTop: true,
    positionClass: 'toast-top-right',
    preventDuplicates: false,
    preventOpenDuplicates: false,
    target: 'body'
  });
});

Those are the default values, you can pick what you need from it and override with your values.

  • autoDismiss If set, show only the most recent maxOpened toast(s)
  • containerId: The name of the container where you want to append your toasts (the container will be created for you).
  • maxOpened: Maximum number of toasts displayed at once.
  • newestOnTop: Add new toasts on top of the old one. Put on false to put them on the bottom.
  • positionClass: The position where the toasts are added.
  • preventDuplicates: Prevent duplicates of the last toast.
  • preventOpenDuplicates: Prevent duplicates of open toasts.
  • target: The element to put the toastr container.

To customize a toast you have two options. First, you can set a default option to be applied globally to all toasts in the same way you modified the container:

app.config(function(toastrConfig) {
  angular.extend(toastrConfig, {
    allowHtml: false,
    closeButton: false,
    closeHtml: '<button>&times;</button>',
    extendedTimeOut: 1000,
    iconClasses: {
      error: 'toast-error',
      info: 'toast-info',
      success: 'toast-success',
      warning: 'toast-warning'
    },  
    messageClass: 'toast-message',
    onHidden: null,
    onShown: null,
    onTap: null,
    progressBar: false,
    tapToDismiss: true,
    templates: {
      toast: 'directives/toast/toast.html',
      progressbar: 'directives/progressbar/progressbar.html'
    },
    timeOut: 5000,
    titleClass: 'toast-title',
    toastClass: 'toast'
  });
});
  • allowHtml: Your toast can use custom HTML here (See Issue 3)
  • closeButton: Whether to display an "X" close button on the toast.
  • closeHtml: Html element to be used as a close button.
  • extendedTimeOut: The timeout after you hover a toast.
  • extraData: If you override the template, you can pass global extra data to your toasts.
  • iconClasses: The default type classes for the different toasts.
  • messageClass: The class for the toast's message.
  • progressBar: A progress bar to see the timeout in real time.
  • tapToDismiss: Whether the toast should be dismissed when it is clicked.
  • templates: To override the default path of the templates.
  • timeOut: The timeout before the toasts disappear.
  • titleClass: The class for the toast's title.
  • toastClass: Base class for toasts.

Toasts have 3 different callbacks:

  • onHidden: A callback function called when a toast gets hidden.
    • First parameter: A boolean to see whether or not the toast was closed via click.
    • Second parameter: The whole toast that got hidden.
  • onShown: A callback function called when a toast is shown.
    • First parameter: The whole toast that got shown.
  • onTap: A callback function called when it is clicked.
    • First parameter: The whole toast that got clicked.

The second option is to pass a third parameter (or second if you don't need a title). Let see some examples:

Toast with custom HTML (available in both title and message):

toastr.info('<input type="checkbox" checked> Success!', 'With HTML', {
  allowHtml: true
});

Html Image

Toast with a close button:

toastr.success('What a nice button', 'Button spree', {
  closeButton: true
});

Html Image

Toast with a custom button for apple fans:

toastr.info('What a nice apple button', 'Button spree', {
  closeButton: true,
  closeHtml: '<button></button>'
});

Html Image

A pinky custom style (you can also create here new types with $decorate):

toastr.info('I am totally custom!', 'Happy toast', {
  iconClass: 'toast-pink'
});

toast-pink is a custom class created for the occasion:

.toast-pink {
    background-image: url(...) !important;
    background-color: #fa39c3;
}

Pink image

Toast template

If you want to use the built-in template, you can use the angular-toastr.tpls.js file.

If you decide that you don't want to use the built-in one, you can always use angular-toastr.js file and then providing your own template like this:

angular.module('yourApp').run(['$templateCache', function($templateCache) {
  $templateCache.put('directives/toast/toast.html',
    "<div>Your template here</div>"
  );
  $templateCache.put('directives/progressbar/progressbar.html',
    "<div>Your progressbar here</div>"
  );
}]);

The important part here is to have a key named templates/toastr/toastr.html. The module you run it is not important, you just need to do it after you load toastr.

NOTE: Due some limitations in Angular, you need to have your custom template cached before trying to use it.

Building

If you want to build from master, you need to:

$ npm install -g gulp
$ npm install
$ gulp production

Grab the compressed files under /dist and the dev files at /gen.


FAQ

Q: Why can't I override the positionClass in a toast? It gets ignored. A: The toasts don't have a position, they are attached to a container and is that container who has the position set on the page. This will be changed in a future version.

Libraries using angular-toastr

Credits

All the credits for the guys at CodeSeven/toastr for creating the original implementation.

License

Mit License: http://www.opensource.org/licenses/mit-license.php

changelog

Changelog

Version 2.1.1

  • Add /dist again for bower.

Version 2.1.0

  • Fix wrong z-index in production build.
  • Add refreshTimer method to refresh the timeout of shown toasts.

Version 2.0.0

Not the promised version, but should help with some unwanted bugs.

  • replace: true has been removed from the directives.
  • Preserve margin-bottom of each toasts.

BREAKING CHANGE:

replace: true has been removed. If you use a custom template, you will probably need to do some CSS changes. On the good part, it should help with some bugs.

Version 1.7.0

  • toastr service has an active() method to get all the opened toasts.

Version 1.6.0

  • onTap callback receives the whole toast as the first parameter.
  • onShown callback receives the whole toast as the first parameter.
  • onHidden callback receives the whole toast as the second parameter.

Version 1.5.0

  • Fix an issue where maxOpened with 2 or more was conflicting with autoDismiss.
  • Fix that when you try to close an undefined toast it won't close them all.
  • Toasts should be now more accessible.
  • You can now pass custom data to templates, useful for custom templates
  • New callback, onTap which is called when you click a toast (doesn't have to be closed for it to work).
  • Fix onHidden to have the wasClicked parameter to true when using a toast close button.

Version 1.4.1

  • Fix a typo on the toastr.less file that prevented some automated tools to work.
  • Add the license to bower.json.

Version 1.4.0

  • With preventOpenDuplicates you can prevent duplicates of opened toasts.
  • Webpack / Browserify support.
  • Now the bower package won't try to fetch the latest angular version.

Version 1.3.1

  • Add compatibility with Angular 1.4.x.

Version 1.3.0

  • An autoDismiss option to be used with maxOpened to dismiss the oldest toast.
  • Every toast has now an isOpened property to see whether they are opened or not.

Version 1.2.1

  • Remove a nasty console.log from the progress bar (yikes!).

Version 1.2.0

  • Support for a progress bar.
  • A config option to change the path of the templates.

BREAKING CHANGE:

If you were using a custom template using the default path, it changed from:

templates/toastr/toastr.html

to

directives/toast/toast.html

Version 1.1.0

  • Now you can prevent the last toast from being duplicated setting preventDuplicates to true.
  • Fix toasts options not working if the title parameter is set to null.
  • Prevent toasts to override global options.

Version 1.0.2

  • Fixed an issue where it wouldn't work anymore without ngAnimate.

Version 1.0.1

  • Hotfix for npm package.

Version 1.0.0

  • No changes since last beta

Version 1.0.0-beta.3

  • Be able to specify a concrete target for container.
  • Using $injector internally to avoid circular dependencies.
  • onHidden receives a parameter to see whether a toast was closed by timeout or click.
  • Fix an issue with toasts not closing up.

Version 1.0.0-beta.2

  • Fix maxOpened. Now toasts are queued when the max is reached.

Version 1.0.0-beta.1

  • Maximum opened toasts can be limited now.
  • Allows to attach an onShown and onHidden callback.
  • Allows toasts to override options without title 9013c4d

Version 0.5.2

  • Removed the support for IE 8 (in terms of CSS)
  • Changed $timeout to $interval so protractor tests won't fail.

Version 0.5.1

  • newestOnTop, with that you can choose whether to add new toasts on the top or bottom. Top by default.

Version 0.5.0

  • Angular 1.3.x support

Version 0.4.0

  • You can add HTML on the toastr titles.
  • You can now override the toast's template.
  • Fix issue using toastr with ionic.

Version 0.3.0

  • Now the toasts supports a close button.
  • Be able to disable to close on click.

Version 0.2.4

  • Fixes #2 where a toast could remain open for all eternity.

Version 0.2.0

  • You can make an sticky toast if you set the timeOut to 0. If you also set extendedTimeOut to 0 the sticky won't go away until you click on them.
  • Toasts accept custom HTML into them!

Version 0.1.2

  • Animations are now optional
  • Removed the possibility to add the toast container where you want to (that will be back in a future)

Version 0.1.1

  • The close method has been renamed to clear to match the original API

Version 0.1.0

  • Initial release