Created
December 21, 2009 21:39
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
// 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