Skip to content

Commit

Permalink
Added preventOpenDuplicates global option. The option prevents toasts…
Browse files Browse the repository at this point in the history
… that are currently open from having duplicates. Also fixed typo in documentation: extendedTimeOut.

Closes #89
  • Loading branch information
erkez authored and Foxandxss committed May 20, 2015
1 parent 52d0508 commit e1bdd4e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 27 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ app.config(function(toastrConfig) {
onShown: null,
positionClass: 'toast-top-right',
preventDuplicates: false,
preventOpenDuplicates: false,
progressBar: false,
tapToDismiss: true,
target: 'body',
Expand Down Expand Up @@ -163,6 +164,7 @@ Those are the default values, you can pick what you need from it and override wi
* **onShown**: A callback function called when a toast is shown.
* **positionClass**: The position where the toasts are added.
* **preventDuplicates**: Prevent duplicates of the last toast.
* **preventOpenDuplicates**: Prevent duplicates of open toasts.
* **progressBar**: A progress bar to see the timeout in real time.
* **tapToDismiss**: Whether the toast should be dismissed when it is clicked.
* **target**: The element to put the toastr container.
Expand Down Expand Up @@ -234,7 +236,7 @@ There you can override:
* **allowHtml**: Whether to allow HTML or not in a concrete toast.
* **closeButton**: Putting a close button on the toast.
* **closeHtml**: If you need to override how the close button looks like.
* **extendedTimeout**: The timeout after you hover it.
* **extendedTimeOut**: The timeout after you hover it.
* **iconClass**: For the type class you want to use for the toast.
* **messageClass**: If you want to modify the message look.
* **onHidden**: Function to call when the toast gets hidden.
Expand Down
21 changes: 13 additions & 8 deletions dist/angular-toastr.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var toasts = [];

var previousToastMessage = '';
var openToasts = {};

var containerDefer = $q.defer();

Expand Down Expand Up @@ -69,6 +70,7 @@
}
toast.scope.$destroy();
var index = toasts.indexOf(toast);
delete openToasts[toast.scope.message];
toasts.splice(index, 1);
var maxOpened = toastrConfig.maxOpened;
if (maxOpened && toasts.length >= maxOpened) {
Expand Down Expand Up @@ -225,7 +227,7 @@

function cleanOptionsOverride(options) {
var badOptions = ['containerId', 'iconClasses', 'maxOpened', 'newestOnTop',
'positionClass', 'preventDuplicates', 'templates'];
'positionClass', 'preventDuplicates', 'preventOpenDuplicates', 'templates'];
for (var i = 0, l = badOptions.length; i < l; i++) {
delete options[badOptions[i]];
}
Expand All @@ -245,14 +247,17 @@
}

function shouldExit() {
if (options.preventDuplicates) {
if (map.message === previousToastMessage) {
return true;
} else {
previousToastMessage = map.message;
}
return false;
var isDuplicateOfLast = options.preventDuplicates && map.message === previousToastMessage;
var isDuplicateOpen = options.preventOpenDuplicates && openToasts[map.message];

if (isDuplicateOfLast || isDuplicateOpen) {
return true;
}

previousToastMessage = map.message;
openToasts[map.message] = true;

return false;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-toastr.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions dist/angular-toastr.tpls.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var toasts = [];

var previousToastMessage = '';
var openToasts = {};

var containerDefer = $q.defer();

Expand Down Expand Up @@ -69,6 +70,7 @@
}
toast.scope.$destroy();
var index = toasts.indexOf(toast);
delete openToasts[toast.scope.message];
toasts.splice(index, 1);
var maxOpened = toastrConfig.maxOpened;
if (maxOpened && toasts.length >= maxOpened) {
Expand Down Expand Up @@ -225,7 +227,7 @@

function cleanOptionsOverride(options) {
var badOptions = ['containerId', 'iconClasses', 'maxOpened', 'newestOnTop',
'positionClass', 'preventDuplicates', 'templates'];
'positionClass', 'preventDuplicates', 'preventOpenDuplicates', 'templates'];
for (var i = 0, l = badOptions.length; i < l; i++) {
delete options[badOptions[i]];
}
Expand All @@ -245,14 +247,17 @@
}

function shouldExit() {
if (options.preventDuplicates) {
if (map.message === previousToastMessage) {
return true;
} else {
previousToastMessage = map.message;
}
return false;
var isDuplicateOfLast = options.preventDuplicates && map.message === previousToastMessage;
var isDuplicateOpen = options.preventOpenDuplicates && openToasts[map.message];

if (isDuplicateOfLast || isDuplicateOpen) {
return true;
}

previousToastMessage = map.message;
openToasts[map.message] = true;

return false;
}
}
}
Expand Down
Loading

0 comments on commit e1bdd4e

Please sign in to comment.