Skip to content

Instantly share code, notes, and snippets.

@notbrain
Created April 22, 2011 22:09
Show Gist options
  • Save notbrain/937792 to your computer and use it in GitHub Desktop.
Save notbrain/937792 to your computer and use it in GitHub Desktop.
globalMessage.js WIP
(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