worker.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if (lang.includes("ja") || lang.includes("jp")) {
  44. scriptsToLoad.push('tinyseg.js');
  45. }
  46. for (var i=0; i < lang.length; i++) {
  47. if (lang[i] != 'en') {
  48. scriptsToLoad.push(['lunr', lang[i], 'js'].join('.'));
  49. }
  50. }
  51. }
  52. loadScripts(scriptsToLoad, onScriptsLoaded);
  53. }
  54. function onScriptsLoaded () {
  55. console.log('All search scripts loaded, building Lunr index...');
  56. if (data.config && data.config.separator && data.config.separator.length) {
  57. lunr.tokenizer.separator = new RegExp(data.config.separator);
  58. }
  59. if (data.index) {
  60. index = lunr.Index.load(data.index);
  61. data.docs.forEach(function (doc) {
  62. documents[doc.location] = doc;
  63. });
  64. console.log('Lunr pre-built index loaded, search ready');
  65. } else {
  66. index = lunr(function () {
  67. if (lang.length === 1 && lang[0] !== "en" && lunr[lang[0]]) {
  68. this.use(lunr[lang[0]]);
  69. } else if (lang.length > 1) {
  70. 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
  71. }
  72. this.field('title');
  73. this.field('text');
  74. this.ref('location');
  75. for (var i=0; i < data.docs.length; i++) {
  76. var doc = data.docs[i];
  77. this.add(doc);
  78. documents[doc.location] = doc;
  79. }
  80. });
  81. console.log('Lunr index built, search ready');
  82. }
  83. allowSearch = true;
  84. postMessage({config: data.config});
  85. postMessage({allowSearch: allowSearch});
  86. }
  87. function init () {
  88. var oReq = new XMLHttpRequest();
  89. oReq.addEventListener("load", onJSONLoaded);
  90. var index_path = base_path + '/search_index.json';
  91. if( 'function' === typeof importScripts ){
  92. index_path = 'search_index.json';
  93. }
  94. oReq.open("GET", index_path);
  95. oReq.send();
  96. }
  97. function search (query) {
  98. if (!allowSearch) {
  99. console.error('Assets for search still loading');
  100. return;
  101. }
  102. var resultDocuments = [];
  103. var results = index.search(query);
  104. for (var i=0; i < results.length; i++){
  105. var result = results[i];
  106. doc = documents[result.ref];
  107. doc.summary = doc.text.substring(0, 200);
  108. resultDocuments.push(doc);
  109. }
  110. return resultDocuments;
  111. }
  112. if( 'function' === typeof importScripts ) {
  113. onmessage = function (e) {
  114. if (e.data.init) {
  115. init();
  116. } else if (e.data.query) {
  117. postMessage({ results: search(e.data.query) });
  118. } else {
  119. console.error("Worker - Unrecognized message: " + e);
  120. }
  121. };
  122. }