Source code for website arcusiridis.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.3 KiB

5 years ago
  1. "use strict";
  2. {{ $isRel := and $.Site.Params.ref_type (eq $.Site.Params.ref_type "relative") }}
  3. {{ if $isRel }}
  4. // JavaScript trickery to make relative paths work
  5. var lunrJSON = "";
  6. var pathToRoot = "";
  7. var scripts = document.getElementsByTagName('script');
  8. for (var i = 0; i < scripts.length; i++ ) {
  9. var scr = scripts[i];
  10. if (scr.hasAttribute("src") && ((scr.getAttribute("src")[0] === '/' && scr.getAttribute("src")[1] !== '/') || scr.getAttribute("src")[0] === '.')) {
  11. pathToRoot = scr.getAttribute("src").replace(/\/js\/.+$/, "");
  12. lunrJSON = pathToRoot + "/index.json";
  13. break;
  14. }
  15. }
  16. {{ else }}
  17. var lunrJSON = "{{ partial "make_link" (dict "URL" "/index.json" "Root" $) }}";
  18. {{ end }}
  19. var lunrIndex = undefined;
  20. var rawIndex = [];
  21. var sbox = document.getElementById("searchbox");
  22. var sresults = document.getElementById("search-results");
  23. var searchAnalytics = undefined;
  24. function getJSON(url) {
  25. return new Promise(function(resolve, reject) {
  26. var req = new XMLHttpRequest();
  27. req.responseType = 'json';
  28. req.open('GET', url, true);
  29. req.addEventListener("load", function() {
  30. var status = req.status;
  31. if (status === 200)
  32. resolve(req.response);
  33. else
  34. reject(status);
  35. });
  36. console.log("Loading search index");
  37. req.send();
  38. });
  39. }
  40. function loadIndex(url) {
  41. return new Promise(function(resolve, reject) {
  42. getJSON(url).then(
  43. function(json) {
  44. rawIndex = json;
  45. lunrIndex = lunr(function () {
  46. this.ref('id');
  47. // These are all fields searched for results
  48. this.field('url');
  49. this.field('name');
  50. this.field('description');
  51. this.field('body');
  52. for (var i = 0; i < rawIndex.length; i++) {
  53. // Adding id allows to access other properties later
  54. rawIndex[i].id = i;
  55. this.add(rawIndex[i]);
  56. }
  57. });
  58. resolve(lunrIndex);
  59. },
  60. function(status) {
  61. console.error("Unable to load search index. Status: " + status);
  62. reject(status);
  63. }
  64. );
  65. });
  66. }
  67. function populateSearchbox() {
  68. // Trailing asterisk wildcards a potentially incomplete word
  69. var content = sbox.value;
  70. if (content.length > 0 && content[content.length - 1] !== " ")
  71. content += "*";
  72. // Empty previous results
  73. sresults.innerHTML = "";
  74. if (lunrIndex !== undefined) {
  75. var results = lunrIndex.search(content);
  76. // Add each result to a <a href="url"><div>Name</div></a>
  77. // and append to the results
  78. results.forEach(function(data) {
  79. var result = document.createElement("div");
  80. result.setAttribute("class", "search-result cell button");
  81. result.innerHTML = rawIndex[data.ref].name;
  82. var link = document.createElement("a");
  83. {{ if $isRel }}
  84. link.setAttribute("href", pathToRoot + rawIndex[data.ref].url);
  85. {{ else }}
  86. link.setAttribute("href", rawIndex[data.ref].url);
  87. {{ end }}
  88. // Add event handler to results to log search if using piwik
  89. if (typeof _paq !== "undefined") {
  90. link.addEventListener("click", function() {
  91. searchAnalytics = {
  92. keyword: sbox.value,
  93. numResults: results.length
  94. };
  95. });
  96. }
  97. link.appendChild(result);
  98. sresults.appendChild(link);
  99. });
  100. // Hide if no content (only asterisk, thanks to above line)
  101. sresults.setAttribute("class", results.length === 0 ? "hide" : "");
  102. }
  103. }
  104. sbox.addEventListener("keyup", populateSearchbox);
  105. window.addEventListener("beforeunload", function(){
  106. if (typeof _paq !== "undefined" && searchAnalytics !== undefined) {
  107. _paq.push(['trackSiteSearch',
  108. // Search keyword searched for
  109. searchAnalytics.keyword,
  110. // Search category selected in your search engine. If you do not need this, set to false
  111. false,
  112. // Number of results on the Search results page. Zero indicates a 'No Result Search Keyword'. Set to false if you don't know
  113. searchAnalytics.numResults
  114. ]);
  115. }
  116. });
  117. var lunrIndex = undefined;
  118. loadIndex(lunrJSON).then(
  119. function(index) { lunrIndex = index; },
  120. function(status){}
  121. );