worker.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var base_path = 'function' === typeof importScripts ? '.' : '/search/';
  2. var allowSearch = false;
  3. var index;
  4. var documents = {};
  5. var lang = ['en'];
  6. var data;
  7. function getScript(script, callback) {
  8. console.log('Loading script: ' + script);
  9. $.getScript(base_path + script).done(function () {
  10. callback();
  11. }).fail(function (jqxhr, settings, exception) {
  12. console.log('Error: ' + exception);
  13. });
  14. }
  15. function getScriptsInOrder(scripts, callback) {
  16. if (scripts.length === 0) {
  17. callback();
  18. return;
  19. }
  20. getScript(scripts[0], function() {
  21. getScriptsInOrder(scripts.slice(1), callback);
  22. });
  23. }
  24. function loadScripts(urls, callback) {
  25. if( 'function' === typeof importScripts ) {
  26. importScripts.apply(null, urls);
  27. callback();
  28. } else {
  29. getScriptsInOrder(urls, callback);
  30. }
  31. }
  32. function onJSONLoaded () {
  33. data = JSON.parse(this.responseText);
  34. var scriptsToLoad = ['lunr.js'];
  35. if (data.config && data.config.lang && data.config.lang.length) {
  36. lang = data.config.lang;
  37. }
  38. if (lang.length > 1 || lang[0] !== "en") {
  39. scriptsToLoad.push('lunr.stemmer.support.js');
  40. if (lang.length > 1) {
  41. scriptsToLoad.push('lunr.multi.js');
  42. }
  43. for (var i=0; i < lang.length; i++) {
  44. if (lang[i] != 'en') {
  45. scriptsToLoad.push(['lunr', lang[i], 'js'].join('.'));
  46. }
  47. }
  48. }
  49. loadScripts(scriptsToLoad, onScriptsLoaded);
  50. }
  51. function onScriptsLoaded () {
  52. console.log('All search scripts loaded, building Lunr index...');
  53. if (data.config && data.config.separator && data.config.separator.length) {
  54. lunr.tokenizer.separator = new RegExp(data.config.separator);
  55. }
  56. if (data.index) {
  57. index = lunr.Index.load(data.index);
  58. data.docs.forEach(function (doc) {
  59. documents[doc.location] = doc;
  60. });
  61. console.log('Lunr pre-built index loaded, search ready');
  62. } else {
  63. index = lunr(function () {
  64. if (lang.length === 1 && lang[0] !== "en" && lunr[lang[0]]) {
  65. this.use(lunr[lang[0]]);
  66. } else if (lang.length > 1) {
  67. this.use(lunr.multiLanguage.apply(null, lang)); // spread operator not supported in all browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#Browser_compatibility
  68. }
  69. this.field('title');
  70. this.field('text');
  71. this.ref('location');
  72. for (var i=0; i < data.docs.length; i++) {
  73. var doc = data.docs[i];
  74. this.add(doc);
  75. documents[doc.location] = doc;
  76. }
  77. });
  78. console.log('Lunr index built, search ready');
  79. }
  80. allowSearch = true;
  81. postMessage({config: data.config});
  82. postMessage({allowSearch: allowSearch});
  83. }
  84. function init () {
  85. var oReq = new XMLHttpRequest();
  86. oReq.addEventListener("load", onJSONLoaded);
  87. var index_path = base_path + '/search_index.json';
  88. if( 'function' === typeof importScripts ){
  89. index_path = 'search_index.json';
  90. }
  91. oReq.open("GET", index_path);
  92. oReq.send();
  93. }
  94. function search (query) {
  95. if (!allowSearch) {
  96. console.error('Assets for search still loading');
  97. return;
  98. }
  99. var resultDocuments = [];
  100. var results = index.search(query);
  101. for (var i=0; i < results.length; i++){
  102. var result = results[i];
  103. doc = documents[result.ref];
  104. doc.summary = doc.text.substring(0, 200);
  105. resultDocuments.push(doc);
  106. }
  107. return resultDocuments;
  108. }
  109. if( 'function' === typeof importScripts ) {
  110. onmessage = function (e) {
  111. if (e.data.init) {
  112. init();
  113. } else if (e.data.query) {
  114. postMessage({ results: search(e.data.query) });
  115. } else {
  116. console.error("Worker - Unrecognized message: " + e);
  117. }
  118. };
  119. }