Skip to content

Commit 70590b5

Browse files
committed
Adds the toast as a parameter in all events
Fixes #138
1 parent 82362e0 commit 70590b5

File tree

3 files changed

+34
-29
lines changed

3 files changed

+34
-29
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,23 @@ app.config(function(toastrConfig) {
176176
* **extraData**: If you override the template, you can pass global extra data to your toasts.
177177
* **iconClasses**: The default type classes for the different toasts.
178178
* **messageClass**: The class for the toast's message.
179-
* **onHidden**: A callback function called when a toast gets hidden. It receives a boolean parameter to see whether it was closed via click or not.
180-
* **onShown**: A callback function called when a toast is shown.
181-
* **onTap**: A callback function called when it is clicked.
182179
* **progressBar**: A progress bar to see the timeout in real time.
183180
* **tapToDismiss**: Whether the toast should be dismissed when it is clicked.
184181
* **templates**: To override the default path of the templates.
185182
* **timeOut**: The timeout before the toasts disappear.
186183
* **titleClass**: The class for the toast's title.
187184
* **toastClass**: Base class for toasts.
188185

186+
Toasts have 3 different events:
187+
188+
* **onHidden**: A callback function called when a toast gets hidden.
189+
* First parameter: A boolean to see whether or not the toast was closed via click.
190+
* Second parameter: The whole toast that got hidden.
191+
* **onShown**: A callback function called when a toast is shown.
192+
* First parameter: The whole toast that got shown.
193+
* **onTap**: A callback function called when it is clicked.
194+
* First parameter: The whole toast that got clicked.
195+
189196
The second option is to pass a third parameter (or second if you don't need a **title**). Let see some examples:
190197

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

src/toastr.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
toast.isOpened = false;
7070
$animate.leave(toast.el).then(function() {
7171
if (toast.scope.options.onHidden) {
72-
toast.scope.options.onHidden(wasClicked);
72+
toast.scope.options.onHidden(!!wasClicked, toast);
7373
}
7474
toast.scope.$destroy();
7575
var index = toasts.indexOf(toast);
@@ -101,8 +101,7 @@
101101
}
102102

103103
/* Internal functions */
104-
function _buildNotification(type, message, title, optionsOverride)
105-
{
104+
function _buildNotification(type, message, title, optionsOverride) {
106105
if (angular.isObject(title)) {
107106
optionsOverride = title;
108107
title = null;
@@ -201,8 +200,8 @@
201200
extendedTimeOut: options.extendedTimeOut,
202201
messageClass: options.messageClass,
203202
onHidden: options.onHidden,
204-
onShown: options.onShown,
205-
onTap: options.onTap,
203+
onShown: generateEvent('onShown'),
204+
onTap: generateEvent('onTap'),
206205
progressBar: options.progressBar,
207206
tapToDismiss: options.tapToDismiss,
208207
timeOut: options.timeOut,
@@ -213,6 +212,14 @@
213212
if (options.closeButton) {
214213
toast.scope.options.closeHtml = options.closeHtml;
215214
}
215+
216+
function generateEvent(event) {
217+
if (options[event]) {
218+
return function() {
219+
options[event](toast);
220+
};
221+
}
222+
}
216223
}
217224

218225
function createToast() {

test/toastr_spec.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -724,58 +724,49 @@ describe('toastr', function() {
724724
describe('callbacks', function() {
725725
it('calls the onShown callback when showing a toast', function() {
726726
var callback = jasmine.createSpy();
727-
openToasts(1, { onShown: callback });
728-
expect(callback).toHaveBeenCalled();
727+
var toast = openToast('success', 'A toast', { onShown: callback });
728+
expect(callback).toHaveBeenCalledWith(toast);
729729
});
730730

731731
it('calls the onHidden callback after a toast is closed on click', function() {
732732
var callback = jasmine.createSpy();
733-
openToasts(1, { onHidden: callback });
733+
var toast = openToast('success', 'A toast', { onHidden: callback });
734734
expect(callback).not.toHaveBeenCalled();
735735
clickToast();
736-
expect(callback).toHaveBeenCalled();
736+
expect(callback).toHaveBeenCalledWith(true, toast);
737737
});
738738

739739
it('calls the onHidden callback after a toast is closed by timeout', function() {
740740
var callback = jasmine.createSpy();
741-
openToasts(1, { onHidden: callback });
741+
var toast = openToast('success', 'A toast', { onHidden: callback });
742742
expect(callback).not.toHaveBeenCalled();
743743
intervalFlush();
744744
animationFlush();
745-
expect(callback).toHaveBeenCalled();
746-
});
747-
748-
it('calls the onHidden callback with "true" if was hidden by click', function() {
749-
var callback = jasmine.createSpy();
750-
openToasts(1, { onHidden: callback });
751-
expect(callback).not.toHaveBeenCalled();
752-
clickToast();
753-
animationFlush();
754-
expect(callback).toHaveBeenCalledWith(true);
745+
expect(callback).toHaveBeenCalledWith(false, toast);
755746
});
756747

757748
it('calls the onHidden callback with "true" if the button was clicked', function() {
758749
var callback = jasmine.createSpy();
759-
openToast('info', 'I have a button', {
750+
var toast = openToast('info', 'I have a button', {
760751
onHidden: callback,
761752
closeButton: true
762753
});
763754
clickToastCloseButton();
764-
expect(callback).toHaveBeenCalledWith(true);
755+
expect(callback).toHaveBeenCalledWith(true, toast);
765756
});
766757

767758
it('can call the callbacks even if the title is set to null', function() {
768759
var callback = jasmine.createSpy();
769-
openToast('success', 'some message', null, {onShown: callback});
770-
expect(callback).toHaveBeenCalled();
760+
var toast = openToast('success', 'some message', null, {onShown: callback});
761+
expect(callback).toHaveBeenCalledWith(toast);
771762
});
772763

773764
it('calls the onTap callback when toast is clicked', function() {
774765
var callback = jasmine.createSpy();
775-
openToasts(1, { onTap: callback });
766+
var toast = openToast('success', 'A toast', { onTap: callback });
776767
expect(callback).not.toHaveBeenCalled();
777768
clickToast();
778-
expect(callback).toHaveBeenCalled();
769+
expect(callback).toHaveBeenCalledWith(toast);
779770
});
780771
});
781772

0 commit comments

Comments
 (0)