Skip to content

Commit

Permalink
showMoreCharsNotice setting
Browse files Browse the repository at this point in the history
  • Loading branch information
glowka committed Mar 30, 2015
1 parent a82c33f commit 3830e1d
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
},
showNoSuggestionNotice: false,
noSuggestionNotice: 'No results',
showMoreCharsNotice: false,
moreCharsNotice: 'Keep writing...',
orientation: 'bottom',
forceFixPosition: false
};
Expand All @@ -108,6 +110,7 @@
that.isLocal = false;
that.suggestionsContainer = null;
that.noSuggestionsContainer = null;
that.moreCharsContainer = null;
that.options = $.extend({}, defaults, options);
that.classes = {
selected: 'autocomplete-selected',
Expand Down Expand Up @@ -148,6 +151,7 @@
selected = that.classes.selected,
options = that.options,
noSuggestionNotice = this.options.noSuggestionNotice,
moreCharsNotice = this.options.moreCharsNotice,
container;

// Remove autocomplete attribute to prevent native suggestions:
Expand All @@ -167,6 +171,10 @@
that.noSuggestionsContainer = $('<div class="autocomplete-no-suggestion"></div>')
.html(noSuggestionNotice).get(0);

if(typeof moreCharsNotice !== 'string')
moreCharsNotice = $(moreCharsNotice).clone(true);
that.moreCharsContainer = $('<div class="autocomplete-more-chars"></div>').html(moreCharsNotice).get(0);

that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass);

container = $(that.suggestionsContainer);
Expand Down Expand Up @@ -483,7 +491,14 @@
}

if (query.length < options.minChars) {
that.hide();
var showMoreCharsNotice = that.options.showMoreCharsNotice;
if(typeof that.options.showMoreCharsNotice === 'function')
showMoreCharsNotice = that.options.showMoreCharsNotice(query);

if(showMoreCharsNotice)
that.getMoreCharsNotice();
else
that.hide();
} else {
that.getSuggestions(query);
}
Expand Down Expand Up @@ -603,6 +618,27 @@
}
},

getMoreCharsNotice: function() {
var that = this,
container = $(that.suggestionsContainer),
moreCharsContainer = $(that.moreCharsContainer),
noSuggestionsContainer = $(that.noSuggestionsContainer);

this.adjustContainerWidth();

// Some explicit steps. Be careful here as it easy to get
// noSuggestionsContainer removed from DOM if not detached properly.
moreCharsContainer.detach();
noSuggestionsContainer.detach();
container.empty(); // clean suggestions if any
container.append(moreCharsContainer);

that.fixPosition();

container.show();
that.visible = true;
},

isBadQuery: function (q) {
if (!this.options.preventBadQueries){
return false;
Expand Down Expand Up @@ -658,6 +694,7 @@
classSelected = that.classes.selected,
container = $(that.suggestionsContainer),
noSuggestionsContainer = $(that.noSuggestionsContainer),
moreCharsContainer = $(that.moreCharsContainer),
beforeRender = options.beforeRender,
html = '',
category,
Expand Down Expand Up @@ -695,6 +732,7 @@

// Detach noSuggestions not to have it removed when filling container with new suggestions
noSuggestionsContainer.detach();
moreCharsContainer.detach();
container.html(html);

// If showNoSuggestionNotice is a function, call it to see
Expand Down Expand Up @@ -725,13 +763,15 @@
noSuggestions: function() {
var that = this,
container = $(that.suggestionsContainer),
moreCharsContainer = $(that.moreCharsContainer),
noSuggestionsContainer = $(that.noSuggestionsContainer);

this.adjustContainerWidth();

// Some explicit steps. Be careful here as it easy to get
// noSuggestionsContainer removed from DOM if not detached properly.
noSuggestionsContainer.detach();
moreCharsContainer.detach();
container.empty(); // clean suggestions if any
container.append(noSuggestionsContainer);

Expand Down

0 comments on commit 3830e1d

Please sign in to comment.