search-engine.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. (function(compodoc) {
  2. var engine = null;
  3. var initialized = false;
  4. function setEngine(Engine) {
  5. initialized = false;
  6. engine = new Engine();
  7. init();
  8. }
  9. function init() {
  10. if (!engine) throw new Error('No engine set for research. Set an engine using compodoc.search.setEngine(Engine).');
  11. return engine.init()
  12. .then(function() {
  13. initialized = true;
  14. compodoc.dispatchEvent({
  15. type: compodoc.EVENTS.SEARCH_READY
  16. });
  17. });
  18. }
  19. function query(q, offset, length) {
  20. if (!initialized) throw new Error('Search has not been initialized');
  21. return engine.search(q, offset, length);
  22. }
  23. function getEngine() {
  24. return engine? engine.name : null;
  25. }
  26. function isInitialized() {
  27. return initialized;
  28. }
  29. compodoc.search = {
  30. setEngine: setEngine,
  31. getEngine: getEngine,
  32. query: query,
  33. isInitialized: isInitialized
  34. };
  35. })(compodoc);