/*
-------------------------------------------------------
-- Tagui Notifications
-------------------------------------------------------
*/
(function($)
{
"use strict";
// ------------------------------------------------
// -- Author: RGB Group
// ------------------------------------------------
// Plugin name
var plugin_name = 'notification';
var plugin_version = '1.0.0';
// Invoker object
var obj = null;
var obj_name = null;
// Var for parsed options
var options = null;
// Default paramteres
var defaults =
{
animation: 'fade', // fade, scroll, slide
delay: 5000, // -1 - indefinitly
position: 'tr', // tr - top right, tl - top left, br - bottom right, bl - bottom left
clickEvent: null
};
// var for canceling timeout
var delayTimer;
// --------------------------------------------------------------------
// Class : PRIVATE
// --------------------------------------------------------------------
// private functions
var _private =
{
Init : function(user_options)
{
_private.SetOptions(user_options);
if ($('#taguiNotifications').length == 0)
{
$('body').append('\n
');
}
},
SetOptions(user_options)
{
options = $.extend({},defaults);
// Settings to the defaults.
if(localStorage.getItem('taguiNotifications') == null)
{
options = $.extend({},defaults);
}
else
{
$.extend( options, JSON.parse(localStorage.getItem('taguiNotifications')) );
}
if( user_options )
{
$.extend( options, user_options ); // If options exist, lets merge them with our default settings.
}
localStorage.setItem('taguiNotifications',JSON.stringify(options));
},
ShowNotification: function(message, type, currentNotificationOptions)
{
// Create notification element
var notification = $("", {'class': 'notification '+type, 'html':message});
// Extend options for this notification only
$.extend( options, currentNotificationOptions );
var clickEvent = options.clickEvent;
// Declare on click event
$(notification).click(function()
{
$(notification).fadeTo(300, .1).slideUp(300, function()
{
$(notification).remove();
});
if(typeof clickEvent === 'function')
{
clickEvent();
}
});
// Add notification to container
if (options.position == 'tr' || options.position == 'tl')
{
$('#taguiNotifications').append(notification);
}
else
{
$('#taguiNotifications').prepend(notification);
}
// animation
switch(options.animation)
{
default:
case 'fade':
$(notification).fadeIn(400);
break;
case 'scroll':
$(notification).slideDown(200);
break;
case 'slide':
if(options.position == 'tr' || options.position == 'br')
{
$(notification).css('display','block');
$(notification).css('right',$(notification).outerWidth() * -1 - 10);
$(notification).animate({right:'0px'},400, 'linear');
}
else
{
$(notification).css('display','block');
$(notification).css('left',$(notification).outerWidth() * -1 - 10);
$(notification).animate({left:'0px'},400, 'swing');
}
break;
}
// Add timer if defined
if (options.delay != -1)
{
setTimeout(function()
{
$(notification).fadeTo(300, .1).slideUp(300, function()
{
$(notification).remove();
});
},options.delay);
}
}
} // END _private
$[plugin_name] =
{
options: function(options)
{
_private.Init(options);
},
success: function(message, options)
{
_private.Init();
_private.ShowNotification(message, 'success', options);
},
warning: function(message, options)
{
_private.Init();
_private.ShowNotification(message, 'warning', options);
},
error: function(message, options)
{
_private.Init();
_private.ShowNotification(message, 'error', options);
},
info: function(message, options)
{
_private.Init();
_private.ShowNotification(message, 'info', options);
}
};
})(jQuery);