|
85 | 85 | return typeof response === 'string' ? $.parseJSON(response) : response; |
86 | 86 | }, |
87 | 87 | showNoSuggestionNotice: false, |
88 | | - noSuggestionNotice: 'No results' |
| 88 | + noSuggestionNotice: 'No results', |
| 89 | + orientation: 'bottom' |
89 | 90 | }; |
90 | 91 |
|
91 | 92 | // Shared variables: |
|
173 | 174 | that.select($(this).data('index')); |
174 | 175 | }); |
175 | 176 |
|
176 | | - that.fixPosition(); |
177 | | - |
178 | 177 | that.fixPositionCapture = function () { |
179 | 178 | if (that.visible) { |
180 | 179 | that.fixPosition(); |
|
214 | 213 | options.lookup = that.verifySuggestionsFormat(options.lookup); |
215 | 214 | } |
216 | 215 |
|
| 216 | + options.orientation = that.validateOrientation(options.orientation, 'bottom'); |
| 217 | + |
217 | 218 | // Adjust height, width and z-index: |
218 | 219 | $(that.suggestionsContainer).css({ |
219 | 220 | 'max-height': options.maxHeight + 'px', |
|
222 | 223 | }); |
223 | 224 | }, |
224 | 225 |
|
| 226 | + |
225 | 227 | clearCache: function () { |
226 | 228 | this.cachedResponse = {}; |
227 | 229 | this.badQueries = []; |
|
246 | 248 | }, |
247 | 249 |
|
248 | 250 | 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' ) |
255 | 253 | return; |
256 | | - } |
257 | 254 |
|
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; |
264 | 281 |
|
265 | 282 | if (that.options.width === 'auto') { |
266 | 283 | styles.width = (that.el.outerWidth() - 2) + 'px'; |
267 | 284 | } |
268 | 285 |
|
269 | | - $(that.suggestionsContainer).css(styles); |
| 286 | + $container.css(styles); |
270 | 287 | }, |
271 | 288 |
|
272 | 289 | enableKillerFn: function () { |
|
589 | 606 | beforeRender.call(that.element, container); |
590 | 607 | } |
591 | 608 |
|
| 609 | + that.fixPosition(); |
| 610 | + |
592 | 611 | container.show(); |
593 | 612 | that.visible = true; |
594 | 613 |
|
|
600 | 619 | container = $(that.suggestionsContainer), |
601 | 620 | html = '', |
602 | 621 | width; |
603 | | - |
| 622 | + |
604 | 623 | html += '<div class="autocomplete-no-suggestion">' + this.options.noSuggestionNotice + '</div>'; |
605 | 624 |
|
606 | 625 | this.adjustContainerWidth(); |
607 | 626 | container.html(html); |
| 627 | + |
| 628 | + that.fixPosition(); |
| 629 | + |
608 | 630 | container.show(); |
609 | 631 | that.visible = true; |
610 | 632 | }, |
|
669 | 691 | return suggestions; |
670 | 692 | }, |
671 | 693 |
|
| 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 | + |
672 | 701 | processResponse: function (result, originalQuery, cacheKey) { |
673 | 702 | var that = this, |
674 | 703 | options = that.options; |
|
0 commit comments