views:

37

answers:

3

In the jQuery autocomplete/menu widget (the autocomplete widget is based on the menu widget, which is a still unreleased widget), how is a click outside of the menu detected ? (A click outisde of the menu closes the menu)

I have added a srollbar (similar to the classic select element) to that menu in a custom combobox widget I am writing. The problem is that in IE8, a mousedown on the scrollbar is detected as a click outside the menu, which closes it, making the scrollbar useless. So, to work around this issue, I am first trying to understand how the menu widget works.

A: 

It would be easier to dismiss the menu not after a click but if the mouse leaves the menu (after a short grace-period).

Lorenzo
A: 

I would implement it by utilize event bubbling:

$(document).bind('mousedown', function(e){
     if(e.target.id === 'foobar'){
         // do something
     }
});

But what do I know...

jAndy
If the click target is a child of `#foobar` then your `// do something` code will run before it's bubbled up to `#foobar`. You'd want to do something like: `if(e.target.id == 'foobar' || $(e.target).parents('#foobar').length) {...}`
eyelidlessness
A: 

You can view the source here, it's basically just checked when it's blur event fires, and hiding 150 seconds after, if the click wasn't in the menu portion:

.bind( "blur.autocomplete", function( event ) {
  clearTimeout( self.searching );
  // clicks on the menu (or a button to trigger a search) will cause a blur event
  self.closing = setTimeout(function() {
    self.close( event );
    self._change( event );
  }, 150 );
});

Other areas of autocomplete, e.g. the selection menu itself clear this timeout, preventing the hide...a blur caused by something else doesn't, resulting it in being hidden. It's worth noting this is not the way for you to replicate the behavior, there are better ways by preventing bubbling, but if your goal is to understand this widget specifically...well that's what it does :)

Nick Craver
Thanks. I am not trying to replicate this behaviour, but to understand how it works. I have extended it and added (with other things) a scrollbar to it using the overflow css property. Now, in firefox, the click on the scrollbar is recognized as part of the menu and the closing timeout is canceled. So it works. in IE8 the mouse event in the scrollbar of the menu is not recognized as part of the widget and the menu closes after 150 ms. Any idea how to work around that (without rewriting the autocomplete widget code itself) ?
Pierre Henry
Okay... I updated from jquery-ui 1.8.1 to 1.8.2 and this issue is fixed. Anyway I am glad to have learnt a little something today. But no time to investigate further what exact change made it work !
Pierre Henry