Skip to content

Commit

Permalink
Merge pull request #115 from jharding/61-configurable-value-key
Browse files Browse the repository at this point in the history
Make value key configurable
  • Loading branch information
jharding committed Mar 15, 2013
2 parents f57178e + 66a39c1 commit eac8d31
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 168 deletions.
67 changes: 19 additions & 48 deletions src/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var Dataset = (function() {
this.limit = o.limit || 5;
this.header = o.header;
this.footer = o.footer;
this.template = compileTemplate(o.template, o.engine);
this.valueKey = o.valueKey || 'value';
this.template = compileTemplate(o.template, o.engine, this.valueKey);

// used in #initialize
this.local = o.local;
Expand Down Expand Up @@ -95,18 +96,25 @@ var Dataset = (function() {
},

_processData: function(data) {
var itemHash = {}, adjacencyList = {};
var that = this, itemHash = {}, adjacencyList = {};

utils.each(data, function(i, item) {
var id;
utils.each(data, function(i, datum) {
var value = utils.isString(datum) ? datum : datum[that.valueKey],
tokens = datum.tokens || utils.tokenizeText(value),
item = { value: value, tokens: tokens },
id;

// convert string datums to datum objects
if (utils.isString(item)) {
item = { value: item, tokens: utils.tokenizeText(item) };
if (utils.isString(datum)) {
item.datum = {};
item.datum[that.valueKey] = datum;
}

else {
item.datum = datum;
}

// filter out falsy tokens
item.tokens = utils.filter(item.tokens || [], function(token) {
item.tokens = utils.filter(item.tokens, function(token) {
return !utils.isBlankString(token);
});

Expand Down Expand Up @@ -197,41 +205,6 @@ var Dataset = (function() {
return suggestions;
},

_compareItems: function(a, b, areLocalItems) {
var aScoreBoost = !a.score_boost ? 0 : a.score_boost,
bScoreBoost = !b.score_boost ? 0 : b.score_boost,
aScore = !a.score ? 0 : a.score,
bScore = !b.score ? 0 : b.score;

if(areLocalItems) {
return (b.weight + bScoreBoost) - (a.weight + aScoreBoost);
} else {
return (bScore + bScoreBoost) - (aScore + aScoreBoost);
}
},

_ranker: function(a, b) {
if (this._customRanker) {
return this._customRanker(a, b);
} else {
// Anything local should always be first (anything with a non-zero weight) and remote results (non-zero scores), and sort by weight/score within each category
var aIsLocal = a.weight && a.weight !== 0;
var bIsLocal = b.weight && b.weight !== 0;
if (aIsLocal && !bIsLocal) {
return -1;
} else if (bIsLocal && !aIsLocal) {
return 1;
} else {
return (aIsLocal && bIsLocal) ? this._compareItems(a, b, true) : this._compareItems(a, b, false);
}
}
},

_processRemoteSuggestions: function(callback, matchedItems) {
var that = this;

},

// public methods
// ---------------

Expand All @@ -256,9 +229,7 @@ var Dataset = (function() {
getSuggestions: function(query, cb) {
var that = this,
terms = utils.tokenizeQuery(query),
suggestions = this._getLocalSuggestions(terms)
.sort(this._ranker)
.slice(0, this.limit);
suggestions = this._getLocalSuggestions(terms).slice(0, this.limit);

cb && cb(suggestions);

Expand Down Expand Up @@ -301,7 +272,7 @@ var Dataset = (function() {

return Dataset;

function compileTemplate(template, engine) {
function compileTemplate(template, engine, valueKey) {
var wrapper = '<div class="tt-suggestion">%body</div>',
compiledTemplate;

Expand All @@ -314,7 +285,7 @@ var Dataset = (function() {
else {
compiledTemplate = {
render: function(context) {
return wrapper.replace('%body', '<p>' + context.value + '</p>');
return wrapper.replace('%body', '<p>' + context[valueKey] + '</p>');
}
};
}
Expand Down
14 changes: 7 additions & 7 deletions src/dropdown_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var DropdownView = (function() {

_handleSelection: function($e) {
var $suggestion = $($e.currentTarget);
this.trigger('suggestionSelected', getSuggestionData($suggestion));
this.trigger('suggestionSelected', extractSuggestion($suggestion));
},

_show: function() {
Expand Down Expand Up @@ -94,7 +94,7 @@ var DropdownView = (function() {
}

$underCursor = $suggestions.eq(nextIndex).addClass('tt-is-under-cursor');
this.trigger('cursorMoved', getSuggestionData($underCursor));
this.trigger('cursorMoved', extractSuggestion($underCursor));
},

_getSuggestions: function() {
Expand Down Expand Up @@ -166,16 +166,16 @@ var DropdownView = (function() {
.filter('.tt-is-under-cursor')
.first();

return $suggestion.length > 0 ? getSuggestionData($suggestion) : null;
return $suggestion.length > 0 ? extractSuggestion($suggestion) : null;
},

getFirstSuggestion: function() {
var $suggestion = this._getSuggestions().first();

return $suggestion.length > 0 ? getSuggestionData($suggestion) : null;
return $suggestion.length > 0 ? extractSuggestion($suggestion) : null;
},

renderSuggestions: function(query, dataset, suggestions) {
renderSuggestions: function(dataset, suggestions) {
var datasetClassName = 'tt-dataset-' + dataset.name,
$suggestionsList,
$dataset = this.$menu.find('.' + datasetClassName),
Expand Down Expand Up @@ -204,7 +204,7 @@ var DropdownView = (function() {
fragment = document.createDocumentFragment();

utils.each(suggestions, function(i, suggestion) {
elBuilder.innerHTML = dataset.template.render(suggestion);
elBuilder.innerHTML = dataset.template.render(suggestion.datum);

$el = $(elBuilder.firstChild)
.css(css.suggestion)
Expand Down Expand Up @@ -251,7 +251,7 @@ var DropdownView = (function() {
// helper functions
// ----------------

function getSuggestionData($el) {
function extractSuggestion($el) {
return $el.data('suggestion');
}
})();
21 changes: 7 additions & 14 deletions src/typeahead_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,32 +217,25 @@ var TypeaheadView = (function() {
byClick && utils.isMsie() ?
utils.defer(this.dropdownView.close) : this.dropdownView.close();

this.eventBus.trigger('selected', suggestion);
this.eventBus.trigger('selected', suggestion.datum);
}
},

_getSuggestions: function() {
var that = this,
query = this.inputView.getQuery();
var that = this, query = this.inputView.getQuery();

if (utils.isBlankString(query)) {
return;
}
if (utils.isBlankString(query)) { return; }

utils.each(this.datasets, function(i, dataset) {
dataset.getSuggestions(query, function(suggestions) {
that._renderSuggestions(query, dataset, suggestions);
// only render the suggestions if the query hasn't changed
if (query === that.inputView.getQuery()) {
that.dropdownView.renderSuggestions(dataset, suggestions);
}
});
});
},

_renderSuggestions: function(query, dataset, suggestions) {
if (query !== this.inputView.getQuery()) { return; }

suggestions = suggestions.slice(0, dataset.limit);
this.dropdownView.renderSuggestions(query, dataset, suggestions);
},

_autocomplete: function(e) {
var isCursorAtEnd, ignoreEvent, query, hint, suggestion;

Expand Down
Loading

0 comments on commit eac8d31

Please sign in to comment.