Skip to content

Commit 46c1dd2

Browse files
committed
Added vertical orientation of suggestions.
1 parent 09c4e18 commit 46c1dd2

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
4646
* `dataType`: type of data returned from server. Either 'text' (default) or 'jsonp', which will cause the autocomplete to use jsonp. You may return a json object in your callback when using jsonp.
4747
* `showNoSuggestionNotice`: Default `false`. When no matching results, display a notification label.
4848
* `noSuggestionNotice`: Default `No results`. Text for no matching results label.
49+
* `orientation`: Default `bottom`. Vertical orientation of the displayed suggestions, available values are `auto`, `top`, `bottom`.
50+
If set to `auto`, the suggestions will be orientated it the way that place them closer to middle of the view port.
4951

5052
Autocomplete instance has following methods:
5153

src/jquery.autocomplete.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@
8585
return typeof response === 'string' ? $.parseJSON(response) : response;
8686
},
8787
showNoSuggestionNotice: false,
88-
noSuggestionNotice: 'No results'
88+
noSuggestionNotice: 'No results',
89+
orientation: 'bottom'
8990
};
9091

9192
// Shared variables:
@@ -173,8 +174,6 @@
173174
that.select($(this).data('index'));
174175
});
175176

176-
that.fixPosition();
177-
178177
that.fixPositionCapture = function () {
179178
if (that.visible) {
180179
that.fixPosition();
@@ -214,6 +213,8 @@
214213
options.lookup = that.verifySuggestionsFormat(options.lookup);
215214
}
216215

216+
options.orientation = that.validateOrientation(options.orientation, 'bottom');
217+
217218
// Adjust height, width and z-index:
218219
$(that.suggestionsContainer).css({
219220
'max-height': options.maxHeight + 'px',
@@ -222,6 +223,7 @@
222223
});
223224
},
224225

226+
225227
clearCache: function () {
226228
this.cachedResponse = {};
227229
this.badQueries = [];
@@ -246,27 +248,42 @@
246248
},
247249

248250
fixPosition: function () {
249-
var that = this,
250-
offset,
251-
styles;
252-
253-
// Don't adjsut position if custom container has been specified:
254-
if (that.options.appendTo !== 'body') {
251+
var that = this;
252+
if (that.options.appendTo != 'body' )
255253
return;
256-
}
257254

258-
offset = that.el.offset();
259-
260-
styles = {
261-
top: (offset.top + that.el.outerHeight()) + 'px',
262-
left: offset.left + 'px'
263-
};
255+
var orientation = that.options.orientation,
256+
$container = $(that.suggestionsContainer),
257+
containerHeight = $container.outerHeight(),
258+
height = that.el.outerHeight(),
259+
offset = that.el.offset(),
260+
styles = {
261+
'top': offset.top,
262+
'left': offset.left
263+
};
264+
265+
if (orientation == 'auto') {
266+
var viewPortHeight = $(window).height(),
267+
scrollTop = $(window).scrollTop(),
268+
top_overflow = -scrollTop + offset.top - containerHeight,
269+
bottom_overflow = scrollTop + viewPortHeight - (offset.top + height + containerHeight);
270+
271+
if (Math.max(top_overflow, bottom_overflow) === top_overflow)
272+
orientation = 'top';
273+
else
274+
orientation = 'bottom';
275+
}
276+
277+
if (orientation === 'bottom')
278+
styles.top += height;
279+
else
280+
styles.top += -containerHeight;
264281

265282
if (that.options.width === 'auto') {
266283
styles.width = (that.el.outerWidth() - 2) + 'px';
267284
}
268285

269-
$(that.suggestionsContainer).css(styles);
286+
$container.css(styles);
270287
},
271288

272289
enableKillerFn: function () {
@@ -589,6 +606,8 @@
589606
beforeRender.call(that.element, container);
590607
}
591608

609+
that.fixPosition();
610+
592611
container.show();
593612
that.visible = true;
594613

@@ -600,11 +619,14 @@
600619
container = $(that.suggestionsContainer),
601620
html = '',
602621
width;
603-
622+
604623
html += '<div class="autocomplete-no-suggestion">' + this.options.noSuggestionNotice + '</div>';
605624

606625
this.adjustContainerWidth();
607626
container.html(html);
627+
628+
that.fixPosition();
629+
608630
container.show();
609631
that.visible = true;
610632
},
@@ -669,6 +691,13 @@
669691
return suggestions;
670692
},
671693

694+
validateOrientation: function(orientation, fallback) {
695+
orientation = orientation.trim().toLowerCase();
696+
if(['auto', 'bottom', 'top'].indexOf(orientation) == '-1')
697+
orientation = fallback;
698+
return orientation
699+
},
700+
672701
processResponse: function (result, originalQuery, cacheKey) {
673702
var that = this,
674703
options = that.options;

0 commit comments

Comments
 (0)