Skip to content

Instantly share code, notes, and snippets.

@bastianallgeier
Created June 5, 2010 09:24
Show Gist options
  • Save bastianallgeier/426474 to your computer and use it in GitHub Desktop.
Save bastianallgeier/426474 to your computer and use it in GitHub Desktop.
var MouseContext = new Class({
Implements : Options,
options : {
contextClass : '.context',
delay : 300
},
initialize : function(elements, options) {
this.setOptions(options);
this.elements = elements;
var self = this;
$$(this.elements).addEvents({
'click' : function(e) {
e.stopPropagation();
},
'mousedown' : function(e) {
self.hideAll(this);
var context = this.getElement(self.options.contextClass);
if(context.getStyle('display') == 'block') {
if(self.isParent(e.target, context)) return false;
this.store('context', true); return false;
}
self.timer = self.show.delay(self.options.delay, self, this);
},
'mouseup' : function(event) {
if(this.retrieve('context')) self.hide.attempt(this, self);
$clear(self.timer);
}
});
document.addEvent('click', function(e) {
self.hideAll();
e.stopPropagation();
});
},
show : function(box) {
box.getElement(this.options.contextClass).setStyle('display', 'block');
},
hide : function(box) {
box.getElement(this.options.contextClass).setStyle('display', 'none');
box.store('context', false);
},
hideAll : function(but) {
$$(this.elements).erase(but).each(function(box) {
this.hide(box);
}.bind(this));
},
isParent : function(element, parent) {
element = $(element); parent = $(parent);
while(element.getParent()) {
if(element==parent) return true;
element=element.getParent();
} return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment