Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…usedown event in autocomplete used by the blur event to determine if it was triggered by a scroll click or an item selection, in which case we set focus back to the input instead of closing the autocomplete. Added a click listener to the document body that triggers the autocomplete to close when the click is not within the textext wrap element.
  • Loading branch information
adamayres committed Jan 16, 2012
1 parent a185c4f commit f312b04
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions src/js/textext.plugin.autocomplete.js
Expand Up @@ -226,6 +226,8 @@

POSITION_ABOVE = 'above',
POSITION_BELOW = 'below',

DATA_MOUSEDOWN_ON_AUTOCOMPLETE = "mousedownOnAutocomplete",

DEFAULT_OPTS = {
autocomplete : {
Expand Down Expand Up @@ -291,6 +293,7 @@

self.on(container, {
mouseover : self.onMouseOver,
mousedown : self.onMouseDown,
click : self.onClick
});

Expand All @@ -300,6 +303,12 @@
;

$(self).data('container', container);

$(document.body).click(function(e)
{
if (self.isDropdownVisible() && !self.withinWrapElement(e.target))
self.trigger(EVENT_HIDE_DROPDOWN);
});

self.positionDropdown();
}
Expand Down Expand Up @@ -345,7 +354,23 @@
target.addClass(CSS_SELECTED);
}
};


/**
* Reacts to the `mouseDown` event triggered by the TextExt core.
*
* @signature TextExtAutocomplete.onMouseDown(e)
*
* @param e {Object} jQuery event.
*
* @author adamayres
* @date 2012/01/13
* @id TextExtAutocomplete.onMouseDown
*/
p.onMouseDown = function(e)
{
this.containerElement().data(DATA_MOUSEDOWN_ON_AUTOCOMPLETE, true);
};

/**
* Reacts to the `click` event triggered by the TextExt core.
*
Expand Down Expand Up @@ -380,12 +405,18 @@
*/
p.onBlur = function(e)
{
var self = this;
var self = this,
container = self.containerElement(),
isBlurByMousedown = container.data(DATA_MOUSEDOWN_ON_AUTOCOMPLETE) === true
;

// use timeout here so that onClick has a chance to fire because if
// dropdown is hidden when clicked, onClick doesn't fire
// only trigger a close event if the blur event was
// not triggered by a mousedown event on the autocomplete
// otherwise set focus back back on the input
if(self.isDropdownVisible())
setTimeout(function() { self.trigger(EVENT_HIDE_DROPDOWN) }, 100);
isBlurByMousedown ? self.core().focusInput() : self.trigger(EVENT_HIDE_DROPDOWN);

container.removeData(DATA_MOUSEDOWN_ON_AUTOCOMPLETE);
};

/**
Expand Down Expand Up @@ -1049,9 +1080,25 @@
if(suggestion)
{
self.val(self.itemManager().itemToString(suggestion));
self.core().getFormData();
self.core().getFormData();
}

self.trigger(EVENT_HIDE_DROPDOWN);
};

/**
* Determines if the specified HTML element is within the TextExt core wrap HTML element.
*
* @signature TextExtAutocomplete.withinWrapElement(element)
*
* @param element {HTMLElement} element to check if contained by wrap element
*
* @author adamayres
* @date 2012/01/15
* @id TextExtAutocomplete.withinWrapElement
*/
p.withinWrapElement = function(element)
{
return this.core().wrapElement().find(element).size() > 0;
}
})(jQuery);

0 comments on commit f312b04

Please sign in to comment.