Skip to content

Commit f2aef10

Browse files
author
Tomas Kirda
committed
Merge branch 'develop'
Conflicts: readme.md
2 parents 5db721d + f7716b6 commit f2aef10

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
3333
* `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element.
3434
* `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element.
3535
* `onSearchError`: `function (query, jqXHR, textStatus, errorThrown) {}` called if ajax request fails. `this` is bound to input element.
36+
* `beforeRender`: `function (container) {}` called before displaying the suggestions. You may manipulate suggestions DOM before it is displayed.
3637
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
3738
* `paramName`: Default `query`. The name of the request parameter that contains the query.
3839
* `transformResult`: `function(response, originalQuery) {}` called after the result of the query is ready. Converts the result into response.suggestions format.

scripts/demo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ $(function () {
5656
// Initialize autocomplete with local lookup:
5757
$('#autocomplete').autocomplete({
5858
lookup: countriesArray,
59+
minChars: 0,
5960
onSelect: function (suggestion) {
6061
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
6162
}

spec/autocompleteBehavior.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,27 @@ describe('Autocomplete', function () {
473473

474474
expect(width).toBeGreaterThan(0);
475475
});
476+
477+
it('Should call beforeRender and pass container jQuery object', function () {
478+
var element = document.createElement('input'),
479+
input = $(element),
480+
instance,
481+
elementCount,
482+
context;
483+
484+
input.autocomplete({
485+
lookup: [{ value: 'Jamaica', data: 'B' }],
486+
beforeRender: function (container) {
487+
context = this;
488+
elementCount = container.length;
489+
}
490+
});
491+
492+
input.val('Jam');
493+
instance = input.autocomplete();
494+
instance.onValueChange();
495+
496+
expect(context).toBe(element);
497+
expect(elementCount).toBe(1);
498+
});
476499
});

src/jquery.autocomplete.js

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
escapeRegExChars: function (value) {
3030
return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
3131
},
32-
createNode: function (html) {
32+
createNode: function (containerClass) {
3333
var div = document.createElement('div');
34-
div.innerHTML = html;
35-
return div.firstChild;
34+
div.className = containerClass;
35+
div.style.position = 'absolute';
36+
div.style.display = 'none';
37+
return div;
3638
}
3739
};
3840
}()),
@@ -140,7 +142,7 @@
140142
}
141143
};
142144

143-
that.suggestionsContainer = Autocomplete.utils.createNode('<div class="' + options.containerClass + '" style="position: absolute; display: none;"></div>');
145+
that.suggestionsContainer = Autocomplete.utils.createNode(options.containerClass);
144146

145147
container = $(that.suggestionsContainer);
146148

@@ -175,15 +177,23 @@
175177
}
176178
};
177179

178-
$(window).on('resize', that.fixPositionCapture);
180+
$(window).on('resize.autocomplete', that.fixPositionCapture);
179181

180182
that.el.on('keydown.autocomplete', function (e) { that.onKeyPress(e); });
181183
that.el.on('keyup.autocomplete', function (e) { that.onKeyUp(e); });
182184
that.el.on('blur.autocomplete', function () { that.onBlur(); });
183-
that.el.on('focus.autocomplete', function () { that.fixPosition(); });
185+
that.el.on('focus.autocomplete', function () { that.onFocus(); });
184186
that.el.on('change.autocomplete', function (e) { that.onKeyUp(e); });
185187
},
186188

189+
onFocus: function () {
190+
var that = this;
191+
that.fixPosition();
192+
if (that.options.minChars <= that.el.val().length) {
193+
that.onValueChange();
194+
}
195+
},
196+
187197
onBlur: function () {
188198
this.enableKillerFn();
189199
},
@@ -220,7 +230,11 @@
220230
},
221231

222232
disable: function () {
223-
this.disabled = true;
233+
var that = this;
234+
that.disabled = true;
235+
if (that.currentRequest) {
236+
that.currentRequest.abort();
237+
}
224238
},
225239

226240
enable: function () {
@@ -229,7 +243,8 @@
229243

230244
fixPosition: function () {
231245
var that = this,
232-
offset;
246+
offset,
247+
styles;
233248

234249
// Don't adjsut position if custom container has been specified:
235250
if (that.options.appendTo !== 'body') {
@@ -238,10 +253,16 @@
238253

239254
offset = that.el.offset();
240255

241-
$(that.suggestionsContainer).css({
256+
styles = {
242257
top: (offset.top + that.el.outerHeight()) + 'px',
243258
left: offset.left + 'px'
244-
});
259+
};
260+
261+
if (that.options.width === 'auto') {
262+
styles.width = (that.el.outerWidth() - 2) + 'px';
263+
}
264+
265+
$(that.suggestionsContainer).css(styles);
245266
},
246267

247268
enableKillerFn: function () {
@@ -260,7 +281,7 @@
260281
that.intervalId = window.setInterval(function () {
261282
that.hide();
262283
that.stopKillSuggestions();
263-
}, 300);
284+
}, 50);
264285
},
265286

266287
stopKillSuggestions: function () {
@@ -431,15 +452,16 @@
431452
if ($.isFunction(options.serviceUrl)) {
432453
serviceUrl = options.serviceUrl.call(that.element, q);
433454
}
434-
if(this.currentRequest != null) {
435-
this.currentRequest.abort();
455+
if (that.currentRequest) {
456+
that.currentRequest.abort();
436457
}
437-
this.currentRequest = $.ajax({
458+
that.currentRequest = $.ajax({
438459
url: serviceUrl,
439460
data: options.ignoreParams ? null : options.params,
440461
type: options.type,
441462
dataType: options.dataType
442463
}).done(function (data) {
464+
that.currentRequest = null;
443465
that.processResponse(data, q);
444466
options.onSearchComplete.call(that.element, q);
445467
}).fail(function (jqXHR, textStatus, errorThrown) {
@@ -481,6 +503,7 @@
481503
className = that.classes.suggestion,
482504
classSelected = that.classes.selected,
483505
container = $(that.suggestionsContainer),
506+
beforeRender = that.options.beforeRender,
484507
html = '',
485508
width;
486509

@@ -498,15 +521,21 @@
498521
container.width(width > 0 ? width : 300);
499522
}
500523

501-
container.html(html).show();
502-
that.visible = true;
524+
container.html(html);
503525

504526
// Select first value by default:
505527
if (that.options.autoSelectFirst) {
506528
that.selectedIndex = 0;
507529
container.children().first().addClass(classSelected);
508530
}
509531

532+
if ($.isFunction(beforeRender)) {
533+
beforeRender.call(that.element, container);
534+
}
535+
536+
container.show();
537+
that.visible = true;
538+
510539
that.findBestHint();
511540
},
512541

@@ -703,7 +732,7 @@
703732
var that = this;
704733
that.el.off('.autocomplete').removeData('autocomplete');
705734
that.disableKillerFn();
706-
$(window).off('resize', that.fixPositionCapture);
735+
$(window).off('resize.autocomplete', that.fixPositionCapture);
707736
$(that.suggestionsContainer).remove();
708737
}
709738
};

0 commit comments

Comments
 (0)