Skip to content

Instantly share code, notes, and snippets.

@documentcloud
Created December 21, 2009 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save documentcloud/261269 to your computer and use it in GitHub Desktop.
Save documentcloud/261269 to your computer and use it in GitHub Desktop.
// When the next click or keypress happens, anywhere on the screen, hide the
// element. 'clickable' makes the element and its contents clickable without
// hiding. The 'onHide' callback runs when the hide fires, and has a chance
// to cancel it.
autohide : function(options) {
var me = this;
options = _.extend({clickable : null, onHide : null}, options || {});
me._autoignore = true;
setTimeout(function(){ delete me._autoignore; }, 0);
if (!me._autohider) {
me.forceHide = function(e) {
if (!e && options.onHide) options.onHide();
me.hide();
$(document).unbind('click', me._autohider);
$(document).unbind('keypress', me._autohider);
me._autohider = null;
me.forceHide = null;
};
me._autohider = function(e) {
if (me._autoignore) return;
if (options.clickable && (me[0] == e.target || _.include($(e.target).parents(), me[0]))) return;
if (options.onHide && !options.onHide(e)) return;
me.forceHide(e);
};
$(document).bind('click', this._autohider);
$(document).bind('keypress', this._autohider);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment