forked from fanruan/fineui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
7 years ago
|
require([
|
||
|
'gitbook',
|
||
|
'jquery'
|
||
|
], function(gitbook, $) {
|
||
|
// Global search objects
|
||
|
var engine = null;
|
||
|
var initialized = false;
|
||
|
|
||
|
// Set a new search engine
|
||
|
function setEngine(Engine, config) {
|
||
|
initialized = false;
|
||
|
engine = new Engine(config);
|
||
|
|
||
|
init(config);
|
||
|
}
|
||
|
|
||
|
// Initialize search engine with config
|
||
|
function init(config) {
|
||
|
if (!engine) throw new Error('No engine set for research. Set an engine using gitbook.research.setEngine(Engine).');
|
||
|
|
||
|
return engine.init(config)
|
||
|
.then(function() {
|
||
|
initialized = true;
|
||
|
gitbook.events.trigger('search.ready');
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Launch search for query q
|
||
|
function query(q, offset, length) {
|
||
|
if (!initialized) throw new Error('Search has not been initialized');
|
||
|
return engine.search(q, offset, length);
|
||
|
}
|
||
|
|
||
|
// Get stats about search
|
||
|
function getEngine() {
|
||
|
return engine? engine.name : null;
|
||
|
}
|
||
|
|
||
|
function isInitialized() {
|
||
|
return initialized;
|
||
|
}
|
||
|
|
||
|
// Initialize gitbook.search
|
||
|
gitbook.search = {
|
||
|
setEngine: setEngine,
|
||
|
getEngine: getEngine,
|
||
|
query: query,
|
||
|
isInitialized: isInitialized
|
||
|
};
|
||
|
});
|