main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 escapeHtml (value) {
  23. return value.replace(/&/g, '&amp;')
  24. .replace(/"/g, '&quot;')
  25. .replace(/</g, '&lt;')
  26. .replace(/>/g, '&gt;');
  27. }
  28. function formatResult (location, title, summary) {
  29. return '<article><h3><a href="' + joinUrl(base_url, location) + '">'+ escapeHtml(title) + '</a></h3><p>' + escapeHtml(summary) +'</p></article>';
  30. }
  31. function displayResults (results) {
  32. var search_results = document.getElementById("mkdocs-search-results");
  33. while (search_results.firstChild) {
  34. search_results.removeChild(search_results.firstChild);
  35. }
  36. if (results.length > 0){
  37. for (var i=0; i < results.length; i++){
  38. var result = results[i];
  39. var html = formatResult(result.location, result.title, result.summary);
  40. search_results.insertAdjacentHTML('beforeend', html);
  41. }
  42. } else {
  43. var noResultsText = search_results.getAttribute('data-no-results-text');
  44. if (!noResultsText) {
  45. noResultsText = "No results found";
  46. }
  47. search_results.insertAdjacentHTML('beforeend', '<p>' + noResultsText + '</p>');
  48. }
  49. }
  50. function doSearch () {
  51. var query = document.getElementById('mkdocs-search-query').value;
  52. if (query.length > min_search_length) {
  53. if (!window.Worker) {
  54. displayResults(search(query));
  55. } else {
  56. searchWorker.postMessage({query: query});
  57. }
  58. } else {
  59. // Clear results for short queries
  60. displayResults([]);
  61. }
  62. }
  63. function initSearch () {
  64. var search_input = document.getElementById('mkdocs-search-query');
  65. if (search_input) {
  66. search_input.addEventListener("keyup", doSearch);
  67. }
  68. var term = getSearchTermFromLocation();
  69. if (term) {
  70. search_input.value = term;
  71. doSearch();
  72. }
  73. }
  74. function onWorkerMessage (e) {
  75. if (e.data.allowSearch) {
  76. initSearch();
  77. } else if (e.data.results) {
  78. var results = e.data.results;
  79. displayResults(results);
  80. } else if (e.data.config) {
  81. min_search_length = e.data.config.min_search_length-1;
  82. }
  83. }
  84. if (!window.Worker) {
  85. console.log('Web Worker API not supported');
  86. // load index in main thread
  87. $.getScript(joinUrl(base_url, "search/worker.js")).done(function () {
  88. console.log('Loaded worker');
  89. init();
  90. window.postMessage = function (msg) {
  91. onWorkerMessage({data: msg});
  92. };
  93. }).fail(function (jqxhr, settings, exception) {
  94. console.error('Could not load worker.js');
  95. });
  96. } else {
  97. // Wrap search in a web worker
  98. var searchWorker = new Worker(joinUrl(base_url, "search/worker.js"));
  99. searchWorker.postMessage({init: true});
  100. searchWorker.onmessage = onWorkerMessage;
  101. }