A class that binds autocomplete functionality to a graph and text field. Autocomplete works by popping open a result set of queries that are performed each time a user enters text into the text field. These queries will hit the remote datasource of a graph and results will be returned, upon which, they will be rendered directly below the text field.
When using autocomplete, it is important to override the buildQuery and renderItem methods as they will not be suitable for most projects by default.
Also, one should add styling to the autocomplete list which uses the css class name _tm_autocomplete_
var graph = new thinkmap.graph.Graph();
// autocomplete should be used with a graph that has a remote datasource
graph.datasource('server.tas');
var autoComplete = new thinkmap.graph.autocomplete.Autocomplete(graph, document.getElementById('search') );
// when a user selects an item call this function
autoComplete.on('select', makeCenter);
// define the query that the autocomplete will use
autoComplete.buildQuery = function(search) {
if (search.length) {
if (!/["'*:?]/.test(search)) {
search = search.trim().split(/\W/).join(' AND ')+"*";
}
return {$t:'node', $search:search};
}
return null;
};
autoComplete.renderItem = function( itemSelection, result ) {
var data= result.entity.data;
var text = null;
if(result.entity.isType('movie')){
text = data.title + ' ('+data.year+')';
} else if(result.entity.isType('person')){
text = data.firstname + ' ' + data.lastname;
} else if(result.entity.isType('genre')){
text = data.genrename;
}
itemSelection.append('span').text(text );
};
the graph responsible for handling the search requests
a TextField Element that accepts the search text
var graph = new thinkmap.graph.Graph();
// autocomplete should be used with a graph that has a remote datasource
graph.datasource('server.tas');
var autoComplete = new thinkmap.graph.autocomplete.Autocomplete(graph, document.getElementById('search') );
// when a user selects an item call this function
autoComplete.on('select', makeCenter);
// define the query that the autocomplete will use
autoComplete.buildQuery = function(search) {
if (search.length) {
if (!/["'*:?]/.test(search)) {
search = search.trim().split(/\W/).join(' AND ')+"*";
}
return {$t:'node', $search:search};
}
return null;
};
autoComplete.renderItem = function( itemSelection, result ) {
var data= result.entity.data;
var text = null;
if(result.entity.isType('movie')){
text = data.title + ' ('+data.year+')';
} else if(result.entity.isType('person')){
text = data.firstname + ' ' + data.lastname;
} else if(result.entity.isType('genre')){
text = data.genrename;
}
itemSelection.append('span').text(text );
};
The query that is used for searching and will be sent to a Thinkmap datasource. This method should be overridden if additional behavior can be improved on.
The default functionality of this method takes the search text and appends a wildcard to it. Then a search object is returned using .
the string that is entered by the user
returns a JSON object that conforms to the Thinkmap Search Query Language. The default
object returned is {$t:'node',$search:search}
var graph = new thinkmap.graph.Graph();
// define the query that the autocomplete will use
autoComplete.buildQuery = function(search) {
if (search.length) {
if (!/["'*:?]/.test(search)) {
search = search.trim().split(/\W/).join(' AND ')+"*";
}
return {$t:'movie', $search:search};
}
return null;
};
Determines how a search result will be rendered to the list element. After each search query from the datasource is returned, this method is called for each result to render the result. This method should be overridden.
the list element for each item
a single result for the query
autoComplete.renderItem = function( itemSelection, result ) {
var data= result.entity.data;
var text = null;
if(result.entity.isType('movie')){
text = data.title + ' ('+data.year+')';
} else if(result.entity.isType('person')){
text = data.firstname + ' ' + data.lastname;
} else if(result.entity.isType('genre')){
text = data.genrename;
}
itemSelection.append('span').text(text );
};
This event is fired each time an item is selected in the autocomplete. The single argument is the entity.
This event is fired each time the autocomplete is to be updated by a new result set from a query
This event is fired when the autocomplete becomes hidden
This event is fired when the autocomplete's results have been cleared