Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion dist/typeahead.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,9 @@
this.source = o.source;
this.displayFn = getDisplayFn(o.display || o.displayKey);
this.templates = getTemplates(o.templates, this.displayFn);
this.queryTokenizer = o.queryTokenizer || function(query) {
return [ query ];
};
this.$el = $(html.dataset.replace("%CLASS%", this.name));
}
Dataset.extractDatasetName = function extractDatasetName(el) {
Expand Down Expand Up @@ -1149,7 +1152,7 @@
$suggestions.append.apply($suggestions, nodes);
that.highlight && highlight({
node: $suggestions[0],
pattern: query
pattern: cleanPatterns(that.queryTokenizer(query))
});
return $suggestions;
function getSuggestionNode(suggestion) {
Expand Down Expand Up @@ -1225,6 +1228,13 @@
function isValidName(str) {
return /^[_a-zA-Z0-9-]+$/.test(str);
}
function cleanPatterns(patterns) {
var cleaned = [];
for (var i = 0; i < patterns.length; i++) {
if (patterns[i].length > 0) cleaned.push(patterns[i]);
}
return cleaned;
}
}();
var Dropdown = function() {
function Dropdown(o) {
Expand Down
2 changes: 1 addition & 1 deletion dist/typeahead.bundle.min.js

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion dist/typeahead.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@
this.source = o.source;
this.displayFn = getDisplayFn(o.display || o.displayKey);
this.templates = getTemplates(o.templates, this.displayFn);
this.queryTokenizer = o.queryTokenizer || function(query) {
return [ query ];
};
this.$el = $(html.dataset.replace("%CLASS%", this.name));
}
Dataset.extractDatasetName = function extractDatasetName(el) {
Expand Down Expand Up @@ -588,7 +591,7 @@
$suggestions.append.apply($suggestions, nodes);
that.highlight && highlight({
node: $suggestions[0],
pattern: query
pattern: cleanPatterns(that.queryTokenizer(query))
});
return $suggestions;
function getSuggestionNode(suggestion) {
Expand Down Expand Up @@ -664,6 +667,13 @@
function isValidName(str) {
return /^[_a-zA-Z0-9-]+$/.test(str);
}
function cleanPatterns(patterns) {
var cleaned = [];
for (var i = 0; i < patterns.length; i++) {
if (patterns[i].length > 0) cleaned.push(patterns[i]);
}
return cleaned;
}
}();
var Dropdown = function() {
function Dropdown(o) {
Expand Down
2 changes: 1 addition & 1 deletion dist/typeahead.jquery.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions doc/jquery_typeahead.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ Datasets can be configured using the following options.
function that transforms a suggestion object into a string. Defaults to
`value`.

* `queryTokenzier` – A function with the signature `(query)` that transforms a
query into an array of string tokens, used to create proper highlighting in
the suggestion box. If not set it returns the query wrapped in a single
element array. The signature is same as the `queryTokenzier` option in
Bloodhound, and therefore in most cases they should be the same if Bloodhound
is used.

* `templates` – A hash of templates to be used when rendering the dataset. Note
a precompiled template is a function that takes a JavaScript object as its
first argument and returns a HTML string.
Expand Down
16 changes: 15 additions & 1 deletion src/typeahead/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var Dataset = (function() {

this.templates = getTemplates(o.templates, this.displayFn);

this.queryTokenizer = o.queryTokenizer || function(query) {return [query];}

this.$el = $(html.dataset.replace('%CLASS%', this.name));
}

Expand Down Expand Up @@ -96,7 +98,10 @@ var Dataset = (function() {
nodes = _.map(suggestions, getSuggestionNode);
$suggestions.append.apply($suggestions, nodes);

that.highlight && highlight({ node: $suggestions[0], pattern: query });
that.highlight && highlight({
node: $suggestions[0],
pattern: cleanPatterns(that.queryTokenizer(query))
});

return $suggestions;

Expand Down Expand Up @@ -201,4 +206,13 @@ var Dataset = (function() {
// dashes, underscores, letters, and numbers
return (/^[_a-zA-Z0-9-]+$/).test(str);
}

function cleanPatterns(patterns) {
var cleaned = [];
// rid of empty patterns
for(var i = 0; i < patterns.length; i ++) {
if(patterns[i].length > 0) cleaned.push(patterns[i]);
}
return cleaned;
}
})();