Skip to content

Commit e1bdd4e

Browse files
erkezFoxandxss
authored andcommitted
Added preventOpenDuplicates global option. The option prevents toasts that are currently open from having duplicates. Also fixed typo in documentation: extendedTimeOut.
Closes #89
1 parent 52d0508 commit e1bdd4e

File tree

7 files changed

+61
-27
lines changed

7 files changed

+61
-27
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ app.config(function(toastrConfig) {
133133
onShown: null,
134134
positionClass: 'toast-top-right',
135135
preventDuplicates: false,
136+
preventOpenDuplicates: false,
136137
progressBar: false,
137138
tapToDismiss: true,
138139
target: 'body',
@@ -163,6 +164,7 @@ Those are the default values, you can pick what you need from it and override wi
163164
* **onShown**: A callback function called when a toast is shown.
164165
* **positionClass**: The position where the toasts are added.
165166
* **preventDuplicates**: Prevent duplicates of the last toast.
167+
* **preventOpenDuplicates**: Prevent duplicates of open toasts.
166168
* **progressBar**: A progress bar to see the timeout in real time.
167169
* **tapToDismiss**: Whether the toast should be dismissed when it is clicked.
168170
* **target**: The element to put the toastr container.
@@ -234,7 +236,7 @@ There you can override:
234236
* **allowHtml**: Whether to allow HTML or not in a concrete toast.
235237
* **closeButton**: Putting a close button on the toast.
236238
* **closeHtml**: If you need to override how the close button looks like.
237-
* **extendedTimeout**: The timeout after you hover it.
239+
* **extendedTimeOut**: The timeout after you hover it.
238240
* **iconClass**: For the type class you want to use for the toast.
239241
* **messageClass**: If you want to modify the message look.
240242
* **onHidden**: Function to call when the toast gets hidden.

dist/angular-toastr.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var toasts = [];
1313

1414
var previousToastMessage = '';
15+
var openToasts = {};
1516

1617
var containerDefer = $q.defer();
1718

@@ -69,6 +70,7 @@
6970
}
7071
toast.scope.$destroy();
7172
var index = toasts.indexOf(toast);
73+
delete openToasts[toast.scope.message];
7274
toasts.splice(index, 1);
7375
var maxOpened = toastrConfig.maxOpened;
7476
if (maxOpened && toasts.length >= maxOpened) {
@@ -225,7 +227,7 @@
225227

226228
function cleanOptionsOverride(options) {
227229
var badOptions = ['containerId', 'iconClasses', 'maxOpened', 'newestOnTop',
228-
'positionClass', 'preventDuplicates', 'templates'];
230+
'positionClass', 'preventDuplicates', 'preventOpenDuplicates', 'templates'];
229231
for (var i = 0, l = badOptions.length; i < l; i++) {
230232
delete options[badOptions[i]];
231233
}
@@ -245,14 +247,17 @@
245247
}
246248

247249
function shouldExit() {
248-
if (options.preventDuplicates) {
249-
if (map.message === previousToastMessage) {
250-
return true;
251-
} else {
252-
previousToastMessage = map.message;
253-
}
254-
return false;
250+
var isDuplicateOfLast = options.preventDuplicates && map.message === previousToastMessage;
251+
var isDuplicateOpen = options.preventOpenDuplicates && openToasts[map.message];
252+
253+
if (isDuplicateOfLast || isDuplicateOpen) {
254+
return true;
255255
}
256+
257+
previousToastMessage = map.message;
258+
openToasts[map.message] = true;
259+
260+
return false;
256261
}
257262
}
258263
}

dist/angular-toastr.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-toastr.tpls.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var toasts = [];
1313

1414
var previousToastMessage = '';
15+
var openToasts = {};
1516

1617
var containerDefer = $q.defer();
1718

@@ -69,6 +70,7 @@
6970
}
7071
toast.scope.$destroy();
7172
var index = toasts.indexOf(toast);
73+
delete openToasts[toast.scope.message];
7274
toasts.splice(index, 1);
7375
var maxOpened = toastrConfig.maxOpened;
7476
if (maxOpened && toasts.length >= maxOpened) {
@@ -225,7 +227,7 @@
225227

226228
function cleanOptionsOverride(options) {
227229
var badOptions = ['containerId', 'iconClasses', 'maxOpened', 'newestOnTop',
228-
'positionClass', 'preventDuplicates', 'templates'];
230+
'positionClass', 'preventDuplicates', 'preventOpenDuplicates', 'templates'];
229231
for (var i = 0, l = badOptions.length; i < l; i++) {
230232
delete options[badOptions[i]];
231233
}
@@ -245,14 +247,17 @@
245247
}
246248

247249
function shouldExit() {
248-
if (options.preventDuplicates) {
249-
if (map.message === previousToastMessage) {
250-
return true;
251-
} else {
252-
previousToastMessage = map.message;
253-
}
254-
return false;
250+
var isDuplicateOfLast = options.preventDuplicates && map.message === previousToastMessage;
251+
var isDuplicateOpen = options.preventOpenDuplicates && openToasts[map.message];
252+
253+
if (isDuplicateOfLast || isDuplicateOpen) {
254+
return true;
255255
}
256+
257+
previousToastMessage = map.message;
258+
openToasts[map.message] = true;
259+
260+
return false;
256261
}
257262
}
258263
}

0 commit comments

Comments
 (0)