Created
April 22, 2011 22:09
-
-
Save notbrain/937792 to your computer and use it in GitHub Desktop.
globalMessage.js WIP
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
(function($){ | |
var methods = { | |
init: function(options) { | |
log("init"); | |
var $this = this; | |
var opts = $.extend({}, $.fn.globalMessage.defaults, options); | |
var data = $this.data('globalMessage'); | |
// init global data | |
if ( ! data ) { | |
$this.data('globalMessage', { | |
settings : opts | |
}); | |
$(window).bind("scroll.globalMessage", function() { | |
$this.css("top", $(window).scrollTop()); | |
}); | |
$this.bind('click.globalMessage', methods.hide); | |
} | |
return $this; | |
}, | |
show: function(options) { | |
var $this = this; | |
var data = $this.data('globalMessage'); | |
var opts = $.extend({}, data.settings, options); | |
log("show - adding class " + opts.style); | |
$this.addClass(opts.style); | |
log("show: class = " + $this.attr("class")); | |
$this.html(opts.message); | |
log("show: msg = " + opts.message); | |
if(opts.sticky) { | |
$this.fadeIn(opts.inSpeed); | |
} else { | |
$this.fadeIn(opts.inSpeed) | |
.delay(opts.duration) | |
.fadeOut(opts.outSpeed, function() { | |
// reset classes | |
methods.hide(opts); | |
}); | |
} | |
return $this; | |
}, | |
hide: function(opts) { // internal, can't override options | |
log("hide - removing class " + opts.style); | |
$this.hide().removeClass(opts.style); | |
return $this; | |
}, | |
destroy: function() { | |
$(window).unbind("scroll.globalMessage"); | |
$this.unbind("click.globalMessage"); | |
} | |
}; | |
$.fn.globalMessage = function(method) { | |
// Method calling logic | |
if ( methods[method] ) { | |
// existing method found | |
log("invoked: existing method found - " + method); | |
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); | |
} else if ( typeof method === 'object' || ! method ) { | |
// no method passed/found, run init | |
log("invoked: no method found/passed - running init..."); | |
return methods.init.apply( this, arguments ); | |
} else { | |
// method not found | |
log("invoked: no method found"); | |
$.error( 'Method ' + method + ' does not exist on jQuery.globalMessage'); | |
} | |
}; | |
function log(msg) { | |
if (window.console && window.console.log) | |
window.console.log("globalMessage::" + msg); | |
}; | |
$.fn.globalMessage.defaults = { | |
style: "info", | |
message: "A message in a top-of-browser-clinging memo box.", | |
inSpeed: 200, | |
outSpeed: 500, | |
duration: 5000, | |
sticky: false, | |
debug: true | |
}; | |
$.fn.globalMessage.opts = {}; | |
// ideas for future: | |
// waiting for other msgs, multiple/stacked msgs | |
// tiny pulsation at beginning to grab attention | |
})(jQuery); | |
$(document).ready(function() { | |
// USAGE | |
// // init | |
// $(".globalMessageWrap").globalMessage(); | |
// | |
// // show | |
// $(".globalMessageWrap").globalMessage("show", { | |
// message: "This will show for 10 seconds, then disappear.", | |
// style: "info", | |
// sticky: false, | |
// inSpeed: 1000, | |
// outSpeed: 1000, | |
// duration: 10000 | |
// }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment