1
0
mirror of synced 2024-11-25 14:26:03 +03:00

ability to provide custom matchers. closes #86

This commit is contained in:
Igor Vaynberg 2012-06-07 20:47:33 -07:00
parent 4b451fee81
commit 4ba25b8095

View File

@ -278,13 +278,13 @@
} }
return function (query) { return function (query) {
var t = query.term.toUpperCase(), filtered = {}; var t = query.term, filtered = {};
if (t === "") { if (t === "") {
query.callback({results: data}); query.callback({results: data});
return; return;
} }
filtered.results = $(data) filtered.results = $(data)
.filter(function () {return text(this).toUpperCase().indexOf(t) >= 0;}) .filter(function () {return query.matcher(t, text(this));})
.get(); .get();
query.callback(filtered); query.callback(filtered);
}; };
@ -301,11 +301,11 @@
// if not a function we assume it to be an array // if not a function we assume it to be an array
return function (query) { return function (query) {
var t = query.term.toUpperCase(), filtered = {results: []}; var t = query.term, filtered = {results: []};
$(data).each(function () { $(data).each(function () {
var isObject = this.text !== undefined, var isObject = this.text !== undefined,
text = isObject ? this.text : this; text = isObject ? this.text : this;
if (t === "" || text.toUpperCase().indexOf(t) >= 0) { if (t === "" || query.matcher(t, text)) {
filtered.results.push(isObject ? this : {id: this, text: this}); filtered.results.push(isObject ? this : {id: this, text: this});
} }
}); });
@ -470,7 +470,10 @@
formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; }, formatInputTooShort: function (input, min) { return "Please enter " + (min - input.length) + " more characters"; },
minimumResultsForSearch: 0, minimumResultsForSearch: 0,
minimumInputLength: 0, minimumInputLength: 0,
id: function (e) { return e.id; } id: function (e) { return e.id; },
matcher: function(term, text) {
return text.toUpperCase().indexOf(term.toUpperCase()) >= 0;
}
}, opts); }, opts);
if (typeof(opts.id) !== "function") { if (typeof(opts.id) !== "function") {
@ -481,7 +484,7 @@
if (select) { if (select) {
opts.query = this.bind(function (query) { opts.query = this.bind(function (query) {
var data = {results: [], more: false}, var data = {results: [], more: false},
term = query.term.toUpperCase(), term = query.term,
placeholder = this.getPlaceholder(); placeholder = this.getPlaceholder();
element.find("option").each(function (i) { element.find("option").each(function (i) {
var e = $(this), var e = $(this),
@ -489,7 +492,7 @@
if (i === 0 && placeholder !== undefined && text === "") return true; if (i === 0 && placeholder !== undefined && text === "") return true;
if (text.toUpperCase().indexOf(term) >= 0) { if (query.matcher(term, text)) {
data.results.push({id: e.attr("value"), text: text}); data.results.push({id: e.attr("value"), text: text});
} }
}); });
@ -676,6 +679,7 @@
term: this.search.val(), term: this.search.val(),
page: page, page: page,
context: self.context, context: self.context,
matcher: self.opts.matcher,
callback: this.bind(function (data) { callback: this.bind(function (data) {
var parts = [], self = this; var parts = [], self = this;
$(data.results).each(function () { $(data.results).each(function () {
@ -726,6 +730,7 @@
term: search.val(), term: search.val(),
page: this.resultsPage, page: this.resultsPage,
context: null, context: null,
matcher: opts.matcher,
callback: this.bind(function (data) { callback: this.bind(function (data) {
var parts = [], // html parts var parts = [], // html parts
def; // default choice def; // default choice