Skip to content

Commit

Permalink
add support for an autocomplete.stopPrefixes config variable
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuels committed Jun 1, 2012
1 parent 23fd412 commit 128f5ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
5 changes: 4 additions & 1 deletion js/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,10 @@ Browser.prototype.createNavBox = function( parent, locLength ) {
};

Browser.prototype._makeLocationAutocompleteStore = function() {
return new JBrowse.Model.AutocompleteStore({ namesTrie: this.names });
return new JBrowse.Model.AutocompleteStore({
namesTrie: this.names,
stopPrefixes: (this.config.autocomplete||{}).stopPrefixes
});
};

/*
Expand Down
48 changes: 33 additions & 15 deletions js/Model/AutocompleteStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ dojo.declare( 'JBrowse.Model.AutocompleteStore', null,
throw "must provide a namesTrie argument";

this.namesTrie = args.namesTrie;

// generate stopPrefixes
var stopPrefixes = this.stopPrefixes = {};
// make our stopPrefixes an object as { prefix: true, ... }
// with all possible prefixes of our stop prefixes
if( args.stopPrefixes ) {
var prefixesInput = typeof args.stopPrefixes == 'string'
? [ args.stopPrefixes ] : args.stopPrefixes;

dojo.forEach( prefixesInput, function(prefix) {
while( prefix.length ) {
stopPrefixes[prefix] = true;
prefix = prefix.substr( 0, prefix.length - 1 );
}
});
}
},

// dojo.data.api.Read support
Expand All @@ -37,22 +53,24 @@ dojo.declare( 'JBrowse.Model.AutocompleteStore', null,
var gotTree = false; // stays false if tree isn't found
var matches = [];
var prefix = (request.query.name || '').replace(/\*$/,'');
this.namesTrie.mappingsFromPrefix(
prefix,
function(tree) {
gotTree = true;
// use dojo.some so that we can break out of the loop when we hit the limit
dojo.some( tree, function(node) {
if( matchesRemaining-- ) {
if( typeof node[0] == 'number' ) {
matches.push({ name: node[0] + " options for " + node[1] });
} else {
matches.push({ name: node[0] });
if( ! this.stopPrefixes[ prefix ] ) {
this.namesTrie.mappingsFromPrefix(
prefix,
function(tree) {
gotTree = true;
// use dojo.some so that we can break out of the loop when we hit the limit
dojo.some( tree, function(node) {
if( matchesRemaining-- ) {
if( typeof node[0] == 'number' ) {
matches.push({ name: node[0] + " options for " + node[1] });
} else {
matches.push({ name: node[0] });
}
}
}
return matchesRemaining < 0;
});
});
return matchesRemaining < 0;
});
});
}

// if we found more than the match limit
if( matchesRemaining < 0 )
Expand Down

0 comments on commit 128f5ac

Please sign in to comment.