-
-
Save gnarf/1323953 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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() { | |
clearTimeout( notify.timer ); | |
div.fadeOut(function() { | |
div.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(), | |
data = notify.data( "notify" ); | |
elem.prepend(notify.hide()); | |
//set a timeout to display the notification. | |
window.setTimeout(function(){ | |
notify.fadeIn(); | |
//automatically clear the timeout | |
if(options.timeOut){ | |
data.updateTimeout = function() { | |
if(--options.timeOut) { | |
notify.find('.countDown').text(options.timeOut); | |
data.timer = setTimeout( data.updateTimeout, 1000 ); | |
} else { | |
data.destroy(); | |
} | |
}; | |
data.timer = setTimeout( data.updateTimeout, 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