Skip to content

Commit

Permalink
Closes #156. Refactored getContainer to only get the container, unles…
Browse files Browse the repository at this point in the history
…s a 2nd boolean parameter is passed in in which case it will also create it if the container did not exist.
  • Loading branch information
johnpapa committed May 5, 2014
1 parent a0119a9 commit 871c2a6
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions toastr.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
options: {},
subscribe: subscribe,
success: success,
version: '2.0.2',
version: '2.0.3beta',
warning: warning
};

Expand All @@ -48,6 +48,18 @@
});
}

function getContainer(options, create) {
if (!options) { options = getOptions(); }
$container = $('#' + options.containerId);
if ($container.length) {
return $container;
}
if(create) {
$container = createContainer(options);
}
return $container;
}

function info(message, title, optionsOverride) {
return notify({
type: toastType.info,
Expand Down Expand Up @@ -124,6 +136,17 @@
return false;
}

function createContainer(options) {
$container = $('<div/>')
.attr('id', options.containerId)
.addClass(options.positionClass)
.attr('aria-live', 'polite')
.attr('role', 'alert');

$container.appendTo($(options.target));
return $container;
}

function getDefaults() {
return {
tapToDismiss: true,
Expand Down Expand Up @@ -159,15 +182,12 @@
}

function publish(args) {
if (!listener) {
return;
}
if (!listener) { return; }
listener(args);
}

function notify(map) {
var
options = getOptions(),
var options = getOptions(),
iconClass = map.iconClass || options.iconClass;

if (typeof (map.optionsOverride) !== 'undefined') {
Expand All @@ -177,9 +197,8 @@

toastId++;

$container = getContainer(options);
var
intervalId = null,
$container = getContainer(options, true);
var intervalId = null,
$toastElement = $('<div/>'),
$titleElement = $('<div/>'),
$messageElement = $('<div/>'),
Expand Down Expand Up @@ -222,14 +241,16 @@
$toastElement[options.showMethod](
{ duration: options.showDuration, easing: options.showEasing, complete: options.onShown }
);

if (options.timeOut > 0) {
intervalId = setTimeout(hideToast, options.timeOut);
}

$toastElement.hover(stickAround, delayedhideToast);
$toastElement.hover(stickAround, delayedHideToast);
if (!options.onclick && options.tapToDismiss) {
$toastElement.click(hideToast);
}

if (options.closeButton && $closeElement) {
$closeElement.click(function (event) {
if( event.stopPropagation ) {
Expand Down Expand Up @@ -275,7 +296,7 @@
});
}

function delayedhideToast() {
function delayedHideToast() {
if (options.timeOut > 0 || options.extendedTimeOut > 0) {
intervalId = setTimeout(hideToast, options.extendedTimeOut);
}
Expand All @@ -288,21 +309,6 @@
);
}
}
function getContainer(options) {
if (!options) { options = getOptions(); }
$container = $('#' + options.containerId);
if ($container.length) {
return $container;
}
$container = $('<div/>')
.attr('id', options.containerId)
.addClass(options.positionClass)
.attr('aria-live', 'polite')
.attr('role', 'alert');

$container.appendTo($(options.target));
return $container;
}

function getOptions() {
return $.extend({}, getDefaults(), toastr.options);
Expand Down

0 comments on commit 871c2a6

Please sign in to comment.