Skip to content

Commit be39849

Browse files
jcford73Foxandxss
authored andcommitted
chore: add refreshTimer
1 parent 4591f46 commit be39849

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

src/directives/toast/toast.directive.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@
6363
}
6464
toastr.remove(scope.toastId, wasClicked);
6565
};
66+
67+
scope.refreshTimer = function(newTime) {
68+
if (timeout) {
69+
$interval.cancel(timeout);
70+
timeout = createTimeout(newTime || scope.options.timeOut);
71+
}
72+
};
6673

6774
element.on('mouseleave', function() {
6875
if (scope.options.timeOut === 0 && scope.options.extendedTimeOut === 0) { return; }

src/toastr.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
info: info,
2424
remove: remove,
2525
success: success,
26-
warning: warning
26+
warning: warning,
27+
refreshTimer: refreshTimer
2728
};
2829

2930
return toast;
@@ -66,6 +67,12 @@
6667
return _buildNotification(type, message, title, optionsOverride);
6768
}
6869

70+
function refreshTimer(toast, newTime) {
71+
if (toast && toast.isOpened && toasts.indexOf(toast) >= 0) {
72+
toast.scope.refreshTimer(newTime);
73+
}
74+
}
75+
6976
function remove(toastId, wasClicked) {
7077
var toast = findToast(toastId);
7178

test/toastr_spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,64 @@ describe('toastr', function() {
383383
animationFlush();
384384
expect(toastr.active()).toBe(0);
385385
});
386+
387+
it('allows to restart the timer, keeping the toast visible longer', function() {
388+
toastrConfig.timeOut = 5000;
389+
var toast = openToast('success', 'foo');
390+
expect($document).toHaveToastOpen(1);
391+
intervalFlush(2000);
392+
toastr.refreshTimer(toast);
393+
intervalFlush(3000);
394+
expect($document).toHaveToastOpen(1);
395+
intervalFlush(2000);
396+
expect($document).toHaveToastOpen(0);
397+
});
398+
399+
it('allows to restart the timer with a new duration', function() {
400+
toastrConfig.timeOut = 5000;
401+
var toast = openToast('success', 'foo');
402+
expect($document).toHaveToastOpen(1);
403+
intervalFlush(2000);
404+
toastr.refreshTimer(toast, 10000);
405+
intervalFlush(5000);
406+
expect($document).toHaveToastOpen(1);
407+
intervalFlush(5000);
408+
expect($document).toHaveToastOpen(0);
409+
});
410+
411+
it('ignores requests to restart the timer for manually-closed toasts', function() {
412+
toastrConfig.timeOut = 5000;
413+
var toast = openToast('success', 'foo');
414+
spyOn(toast.scope, 'refreshTimer');
415+
expect($document).toHaveToastOpen(1);
416+
intervalFlush(1000);
417+
toastr.clear(toast);
418+
intervalFlush(1000);
419+
toastr.refreshTimer(toast);
420+
expect(toast.scope.refreshTimer).not.toHaveBeenCalled();
421+
});
422+
423+
it('ignores requests to restart the timer for recently-expired toasts', function() {
424+
toastrConfig.timeOut = 5000;
425+
var toast = openToast('success', 'foo');
426+
spyOn(toast.scope, 'refreshTimer');
427+
expect($document).toHaveToastOpen(1);
428+
intervalFlush(5000);
429+
toastr.refreshTimer(toast);
430+
expect(toast.scope.refreshTimer).not.toHaveBeenCalled();
431+
});
432+
433+
it('ignores requests to restart the timer for old toasts', function() {
434+
toastrConfig.timeOut = 5000;
435+
var toast = openToast('success', 'foo');
436+
spyOn(toast.scope, 'refreshTimer');
437+
expect($document).toHaveToastOpen(1);
438+
intervalFlush(60000);
439+
expect($document).toHaveToastOpen(0);
440+
toastr.refreshTimer(toast);
441+
expect(toast.scope.refreshTimer).not.toHaveBeenCalled();
442+
});
443+
386444
});
387445

388446
describe('container', function() {

0 commit comments

Comments
 (0)