main.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. function getSearchTermFromLocation() {
  2. var sPageURL = window.location.search.substring(1);
  3. var sURLVariables = sPageURL.split('&');
  4. for (var i = 0; i < sURLVariables.length; i++) {
  5. var sParameterName = sURLVariables[i].split('=');
  6. if (sParameterName[0] == 'q') {
  7. return decodeURIComponent(sParameterName[1].replace(/\+/g, '%20'));
  8. }
  9. }
  10. }
  11. function joinUrl (base, path) {
  12. if (path.substring(0, 1) === "/") {
  13. // path starts with `/`. Thus it is absolute.
  14. return path;
  15. }
  16. if (base.substring(base.length-1) === "/") {
  17. // base ends with `/`
  18. return base + path;
  19. }
  20. return base + "/" + path;
  21. }
  22. function formatResult (location, title, summary) {
  23. return '<article><h3><a href="' + joinUrl(base_url, location) + '">'+ title + '</a></h3><p>' + summary +'</p></article>';
  24. }
  25. function displayResults (results) {
  26. var search_results = document.getElementById("mkdocs-search-results");
  27. while (search_results.firstChild) {
  28. search_results.removeChild(search_results.firstChild);
  29. }
  30. if (results.length > 0){
  31. for (var i=0; i < results.length; i++){
  32. var result = results[i];
  33. var html = formatResult(result.location, result.title, result.summary);
  34. search_results.insertAdjacentHTML('beforeend', html);
  35. }
  36. } else {
  37. search_results.insertAdjacentHTML('beforeend', "<p>No results found</p>");
  38. }
  39. }
  40. function doSearch () {
  41. var query = document.getElementById('mkdocs-search-query').value;
  42. if (query.length > min_search_length) {
  43. if (!window.Worker) {
  44. displayResults(search(query));
  45. } else {
  46. searchWorker.postMessage({query: query});
  47. }
  48. } else {
  49. // Clear results for short queries
  50. displayResults([]);
  51. }
  52. }
  53. function initSearch () {
  54. var search_input = document.getElementById('mkdocs-search-query');
  55. if (search_input) {
  56. search_input.addEventListener("keyup", doSearch);
  57. }
  58. var term = getSearchTermFromLocation();
  59. if (term) {
  60. search_input.value = term;
  61. doSearch();
  62. }
  63. }
  64. function onWorkerMessage (e) {
  65. if (e.data.allowSearch) {
  66. initSearch();
  67. } else if (e.data.results) {
  68. var results = e.data.results;
  69. displayResults(results);
  70. } else if (e.data.config) {
  71. min_search_length = e.data.config.min_search_length-1;
  72. }
  73. }
  74. if (!window.Worker) {
  75. console.log('Web Worker API not supported');
  76. // load index in main thread
  77. $.getScript(joinUrl(base_url, "search/worker.js")).done(function () {
  78. console.log('Loaded worker');
  79. init();
  80. window.postMessage = function (msg) {
  81. onWorkerMessage({data: msg});
  82. };
  83. }).fail(function (jqxhr, settings, exception) {
  84. console.error('Could not load worker.js');
  85. });
  86. } else {
  87. // Wrap search in a web worker
  88. var searchWorker = new Worker(joinUrl(base_url, "search/worker.js"));
  89. searchWorker.postMessage({init: true});
  90. searchWorker.onmessage = onWorkerMessage;
  91. }