Skip to content

Commit

Permalink
Merge pull request #163 from SBero/master
Browse files Browse the repository at this point in the history
Prevent duplicates from piling up option.
  • Loading branch information
johnpapa committed Sep 4, 2014
2 parents 4f1d70a + 8f1b418 commit ccb377b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
30 changes: 30 additions & 0 deletions tests/unit/toastr-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,36 @@
clearContainerChildren();
});

test('event - prevent duplicate sequential toasts.', 1, function(){
toastr.options.preventDuplicates = true;

var $toast = [];
$toast[0] = toastr.info(sampleMsg, sampleTitle);
$toast[1] = toastr.info(sampleMsg, sampleTitle);
$toast[2] = toastr.info(sampleMsg + " 1", sampleTitle);
var $container = toastr.getContainer();

ok($container && $container.children().length === 2)

clearContainerChildren();
});

test('event - allow duplicate sequential toasts.', 1, function(){
toastr.options.preventDuplicates = false;

var $toast = [];
$toast[0] = toastr.info(sampleMsg, sampleTitle);
$toast[1] = toastr.info(sampleMsg, sampleTitle);
$toast[1] = toastr.info(sampleMsg, sampleTitle);
var $container = toastr.getContainer();

ok($container && $container.children().length === 3)

clearContainerChildren();
});



module('order of appearance');
test('Newest toast on top', 1, function () {
//Arrange
Expand Down
16 changes: 14 additions & 2 deletions toastr.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
warning: warning
};

var previousToast;

return toastr;

//#region Accessible Methods
Expand Down Expand Up @@ -177,7 +179,8 @@
messageClass: 'toast-message',
target: 'body',
closeHtml: '<button>&times;</button>',
newestOnTop: true
newestOnTop: true,
preventDuplicates: false
};
}

Expand All @@ -190,6 +193,15 @@
var options = getOptions(),
iconClass = map.iconClass || options.iconClass;

if(options.preventDuplicates){
if(map.message === previousToast){
return;
}
else{
previousToast = map.message;
}
}

if (typeof (map.optionsOverride) !== 'undefined') {
options = $.extend(options, map.optionsOverride);
iconClass = map.optionsOverride.iconClass || iconClass;
Expand Down Expand Up @@ -335,4 +347,4 @@
} else {
window['toastr'] = factory(window['jQuery']);
}
}));
}));

0 comments on commit ccb377b

Please sign in to comment.