I modified the show_dropdown function to automatically determine if it should display above or below:
function show_dropdown() {
if (input_box.is(':focus')) {
var windowHeight = $(window).height();
var docViewTop = $(window).scrollTop();
var dropdownPosition = $(token_list).offset().top + $(token_list).outerHeight();
var availableSpace = windowHeight - (dropdownPosition - docViewTop)
var borderWidth = parseInt(dropdown.css('border-bottom-width'));
if (availableSpace >= (settings.maxHeight + 2 * borderWidth)) {
dropdown.css('bottom', '');
$('body').css('position', '');
dropdown.css({
'border-top-width': 0,
left: $(token_list).offset().left,
top: $(token_list).offset().top + $(token_list).outerHeight()
});
} else {
dropdown.css('top', '');
var bodyHeight = $('body').outerHeight();
var bodyTop = $('body').offset().top;
var bottom;
if ((bodyHeight + bodyTop) < dropdownPosition) {
bottom = dropdownPosition - (bodyTop + bodyHeight) ;
}
else {
bottom = (bodyTop + bodyHeight) - ($(token_list).offset().top + borderWidth);
}
dropdown.css({
'border-top-width': borderWidth,
'border-top-color': dropdown.css('border-bottom-color'),
'border-top-style': dropdown.css('border-bottom-style'),
'bottom': bottom,
'left': $(token_list).offset().left - parseInt($('body').css('margin-left')),
'position': 'absolute'
});
$('body').css({
'position': 'relative'
});
}
dropdown.css({
maxHeight: settings.maxHeight,
overflow: 'auto',
zIndex: settings.zIndex
}).show();
}
}
I've also added a maxHeight
property to tokenInput and use it to both check for space and set the size of the dropdown.
Try it out here: http://jsfiddle.net/colin_young/dvuHD/19/ (note that my version has some significant modifications from the stock version, I just haven't had a chance to create pull-requests for them).
No attempt is made to ensure there is space above however. The assumption is that if there is no space below and none above there isn't a lot you can do, although centering on the input element might be a good compromise.