var search_json; if (window.origen_search_id) { search_json = "search_" + window.origen_search_id + ".json"; } else { search_json = "search.json"; } $.getJSON("<%= path '' %>" + "/" + search_json, function(json) { window.index = lunr(function() { this.field("title", {boost: 10}); this.field("subtitle", {boost: 5}); this.field("body"); this.ref("id"); }); window.articles = json; $.each(json, function(id, content) { window.index.add({ id: id, title: content.title, subtitle: content.subtitle, body: content.body }); }); }); $(function() { $("#search button").click(function() { search(true); }); $("#search input").keypress(function(e) { if(e.which == 13) { e.preventDefault(); search(false); } }); }) function search(go) { var term = $("#search input").val(); // Wait for search index to populate while (!window.index) {}; var results = window.index.search(term); if(results && results.length == 1) { window.location.replace("<%= path "" %>" + results[0].ref + "?highlight=" + escape(term)); } else if(results && results.length > 1) { if (go) { window.location.replace("<%= path "" %>" + results[0].ref + "?highlight=" + escape(term)); } else { displayResults(results, term); } } else { alert("Found nothing"); } } function displayResults(results, term) { var resultTemplate = $("search-view-template"); var container = $("article"); container.empty(); container.append("
" + result.ref + "?highlight=" + escape(term) + "\">" + article.title + " - " + article.subtitle + "
"); }); } function getParams() { var query_string = {}; var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i