Skip to content

Commit

Permalink
Allow toasts to override options without title
Browse files Browse the repository at this point in the history
Now you can do:

toast.success('foo', { ... });

Instead of:

toast.success('foo', null, { ... });
  • Loading branch information
Foxandxss committed Dec 19, 2014
1 parent 758234f commit 9013c4d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
39 changes: 19 additions & 20 deletions src/toastr.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,42 +102,41 @@ angular.module('toastr', [])
}

function error(message, title, optionsOverride) {
return _notify({
iconClass: _getOptions().iconClasses.error,
message: message,
optionsOverride: optionsOverride,
title: title
});
var type = _getOptions().iconClasses.error;
return _buildNotification(type, message, title, optionsOverride);
}

function info(message, title, optionsOverride) {
return _notify({
iconClass: _getOptions().iconClasses.info,
message: message,
optionsOverride: optionsOverride,
title: title
});
var type = _getOptions().iconClasses.info;
return _buildNotification(type, message, title, optionsOverride);
}

function success(message, title, optionsOverride) {
return _notify({
iconClass: _getOptions().iconClasses.success,
message: message,
optionsOverride: optionsOverride,
title: title
});
var type = _getOptions().iconClasses.success;
return _buildNotification(type, message, title, optionsOverride);
}

function warning(message, title, optionsOverride) {
var type = _getOptions().iconClasses.warning;
return _buildNotification(type, message, title, optionsOverride);
}

/* Internal functions */
function _buildNotification(type, message, title, optionsOverride)
{
if (typeof title === 'object') {
optionsOverride = title;
title = null;
}

return _notify({
iconClass: _getOptions().iconClasses.warning,
iconClass: type,
message: message,
optionsOverride: optionsOverride,
title: title
});
}

/* Internal functions */
function _getOptions() {
return angular.extend({}, toastrConfig);
}
Expand Down
21 changes: 8 additions & 13 deletions test/toastr_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ describe('toastr', function() {
}

function openToast(type, message, title, options) {
var toast;
if (title) {
toast = toastr[type](message, title, options);
} else {
toast = toastr[type](message, null, options);
}
var toast = toastr[type](message, title, options);

$rootScope.$digest();
animationFlush();
Expand Down Expand Up @@ -312,13 +307,13 @@ describe('toastr', function() {
var options = {
timeOut: 0
};
openToast('info', 'I don\'t want to go...', null, options);
openToast('info', 'I don\'t want to go...', options);
intervalFlush();
expect($document).toHaveToastOpen(1);
clickToast();
expect($document).toHaveToastOpen(0);

openToast('info', 'I don\'t want to go...', null, options);
openToast('info', 'I don\'t want to go...', options);
intervalFlush();
expect($document).toHaveToastOpen(1);
hoverToast();
Expand All @@ -332,7 +327,7 @@ describe('toastr', function() {
timeOut: 0,
extendedTimeOut: 0
};
openToast('info', 'I don\'t want to go...', null, options);
openToast('info', 'I don\'t want to go...', options);
intervalFlush();
expect($document).toHaveToastOpen(1);
hoverToast();
Expand All @@ -343,7 +338,7 @@ describe('toastr', function() {
});

it('can show custom html on the toast message', function() {
var toast = openToast('success', 'I like to have a <button>button</button>', null, {
var toast = openToast('success', 'I like to have a <button>button</button>', {
allowHtml: true
});
expect(toast).toHaveA('button');
Expand All @@ -360,15 +355,15 @@ describe('toastr', function() {

describe('close button', function() {
it('should contain a close button with × if you add it', function() {
var toast = openToast('info', 'I have a button', null, {
var toast = openToast('info', 'I have a button', {
closeButton: true
});

expect(toast).toHaveButtonWith('×');
});

it('allows custom button text on the close button', function() {
var toast = openToast('info', 'I have a button', null, {
var toast = openToast('info', 'I have a button', {
closeButton: true,
closeHtml: '<button>1</button>'
});
Expand All @@ -377,7 +372,7 @@ describe('toastr', function() {
});

it('allows custom element as the close button', function() {
var toast = openToast('info', 'I have a button', null, {
var toast = openToast('info', 'I have a button', {
closeButton: true,
closeHtml: '<span>1</span>'
});
Expand Down

0 comments on commit 9013c4d

Please sign in to comment.