From 871c2a6e438bb6b996cfb80286720604a4cf00fd Mon Sep 17 00:00:00 2001 From: John Papa Date: Mon, 5 May 2014 07:31:29 -0400 Subject: [PATCH] Closes #156. Refactored getContainer to only get the container, unless a 2nd boolean parameter is passed in in which case it will also create it if the container did not exist. --- toastr.js | 58 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/toastr.js b/toastr.js index 71f872cb..e7eb78f2 100644 --- a/toastr.js +++ b/toastr.js @@ -31,7 +31,7 @@ options: {}, subscribe: subscribe, success: success, - version: '2.0.2', + version: '2.0.3beta', warning: warning }; @@ -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, @@ -124,6 +136,17 @@ return false; } + function createContainer(options) { + $container = $('
') + .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, @@ -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') { @@ -177,9 +197,8 @@ toastId++; - $container = getContainer(options); - var - intervalId = null, + $container = getContainer(options, true); + var intervalId = null, $toastElement = $('
'), $titleElement = $('
'), $messageElement = $('
'), @@ -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 ) { @@ -275,7 +296,7 @@ }); } - function delayedhideToast() { + function delayedHideToast() { if (options.timeOut > 0 || options.extendedTimeOut > 0) { intervalId = setTimeout(hideToast, options.extendedTimeOut); } @@ -288,21 +309,6 @@ ); } } - function getContainer(options) { - if (!options) { options = getOptions(); } - $container = $('#' + options.containerId); - if ($container.length) { - return $container; - } - $container = $('
') - .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);