Skip to content

Commit 3830e1d

Browse files
committed
showMoreCharsNotice setting
1 parent a82c33f commit 3830e1d

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/jquery.autocomplete.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
},
9191
showNoSuggestionNotice: false,
9292
noSuggestionNotice: 'No results',
93+
showMoreCharsNotice: false,
94+
moreCharsNotice: 'Keep writing...',
9395
orientation: 'bottom',
9496
forceFixPosition: false
9597
};
@@ -108,6 +110,7 @@
108110
that.isLocal = false;
109111
that.suggestionsContainer = null;
110112
that.noSuggestionsContainer = null;
113+
that.moreCharsContainer = null;
111114
that.options = $.extend({}, defaults, options);
112115
that.classes = {
113116
selected: 'autocomplete-selected',
@@ -148,6 +151,7 @@
148151
selected = that.classes.selected,
149152
options = that.options,
150153
noSuggestionNotice = this.options.noSuggestionNotice,
154+
moreCharsNotice = this.options.moreCharsNotice,
151155
container;
152156

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

174+
if(typeof moreCharsNotice !== 'string')
175+
moreCharsNotice = $(moreCharsNotice).clone(true);
176+
that.moreCharsContainer = $('<div class="autocomplete-more-chars"></div>').html(moreCharsNotice).get(0);
177+
170178
that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass);
171179

172180
container = $(that.suggestionsContainer);
@@ -483,7 +491,14 @@
483491
}
484492

485493
if (query.length < options.minChars) {
486-
that.hide();
494+
var showMoreCharsNotice = that.options.showMoreCharsNotice;
495+
if(typeof that.options.showMoreCharsNotice === 'function')
496+
showMoreCharsNotice = that.options.showMoreCharsNotice(query);
497+
498+
if(showMoreCharsNotice)
499+
that.getMoreCharsNotice();
500+
else
501+
that.hide();
487502
} else {
488503
that.getSuggestions(query);
489504
}
@@ -603,6 +618,27 @@
603618
}
604619
},
605620

621+
getMoreCharsNotice: function() {
622+
var that = this,
623+
container = $(that.suggestionsContainer),
624+
moreCharsContainer = $(that.moreCharsContainer),
625+
noSuggestionsContainer = $(that.noSuggestionsContainer);
626+
627+
this.adjustContainerWidth();
628+
629+
// Some explicit steps. Be careful here as it easy to get
630+
// noSuggestionsContainer removed from DOM if not detached properly.
631+
moreCharsContainer.detach();
632+
noSuggestionsContainer.detach();
633+
container.empty(); // clean suggestions if any
634+
container.append(moreCharsContainer);
635+
636+
that.fixPosition();
637+
638+
container.show();
639+
that.visible = true;
640+
},
641+
606642
isBadQuery: function (q) {
607643
if (!this.options.preventBadQueries){
608644
return false;
@@ -658,6 +694,7 @@
658694
classSelected = that.classes.selected,
659695
container = $(that.suggestionsContainer),
660696
noSuggestionsContainer = $(that.noSuggestionsContainer),
697+
moreCharsContainer = $(that.moreCharsContainer),
661698
beforeRender = options.beforeRender,
662699
html = '',
663700
category,
@@ -695,6 +732,7 @@
695732

696733
// Detach noSuggestions not to have it removed when filling container with new suggestions
697734
noSuggestionsContainer.detach();
735+
moreCharsContainer.detach();
698736
container.html(html);
699737

700738
// If showNoSuggestionNotice is a function, call it to see
@@ -725,13 +763,15 @@
725763
noSuggestions: function() {
726764
var that = this,
727765
container = $(that.suggestionsContainer),
766+
moreCharsContainer = $(that.moreCharsContainer),
728767
noSuggestionsContainer = $(that.noSuggestionsContainer);
729768

730769
this.adjustContainerWidth();
731770

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

0 commit comments

Comments
 (0)