Skip to content

Instantly share code, notes, and snippets.

@ryanneufeld
Created October 29, 2011 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanneufeld/1323944 to your computer and use it in GitHub Desktop.
Save ryanneufeld/1323944 to your computer and use it in GitHub Desktop.
//based on https://gist.github.com/07a297472f182f7a7132/79d3dea1e3dc7ce2d065a42316b83ec820671634
;(function($, undefined) {
$.notify = function(options) {
if(options.timeOut > 0)
{
//add the timer to the message
console.log($(options.message).find('.countDown'));
if($(options.message).find('.countDown').length > 0){
options.message = options.message + '(<strong class="countDown">'+options.timeOut+'</strong>s)';
}
}
options = $.extend({}, $.notify.defaults, options);
var div = $.tmpl($.notify.template, options),
notify = {
destroy : function() {
clearInterval($(this).data('clearTimer'));
div.fadeOut(function() {
$(this).remove();
if(typeof options.onClose === 'function'){
options.onClose(this);
}
});
}
};
div.click(notify.destroy);
div.data("notify", notify);
return div;
};
$.fn.notify = function(options) {
return this.map(function() {
var elem = $(this),
notify = $.notify(options).first();
elem.prepend(notify.hide());
//set a timeout to display the notification.
window.setTimeout(function(){
notify.fadeIn();
//automatically clear the timeout
if(options.timeOut){
notify.data('clearTimer', window.setInterval(function(){
if(--options.timeOut) {
notify.find('.countDown').text(options.timeOut);
} else {
notify.data('notify').destroy();
clearInterval(notify.data('clearTimer'));
}
}, 1000));
}
}, options.delay);
return notify;
});
};
$.notify.template = '<div class="msg ${type}" style="display:none"><span>{{html message}}</span><a href="#" class="close">x</a></div>';
$.notify.defaults = {
type : "notice",
message : "This is a notice",
delay : 0, //time in ms to waitbefore displaying
timeOut: false, //how long until it fades out on it's own in seconds
onClose: false //on close callback
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment