/* # ----------------------------------------------------------------------------- # ~/assets/themes/j1/modules/lunr/js/jquery.lunr.search.js # jQuery plugin to use with J1LunrSearch # # Product/Info: # https://jekyll.one # # Copyright (C) 2022 Juergen Adams # # J1 Template is licensed under the MIT License. # See: https://github.com/jekyll-one-org/J1 Template/blob/master/LICENSE # ----------------------------------------------------------------------------- # TODO: # ----------------------------------------------------------------------------- */ "use strict";(function(c){var b=function(e){var f;var g=Array.prototype.slice;return function(){var i=g.call(arguments),h=this;clearTimeout(f);f=setTimeout(function(){e.apply(h,i)},100)}};var d=function(e){var f=e.match(/(\d+)/g);return new Date(f[0],f[1]-1,f[2])};var a=(function(){function e(g,f){this.$elem=g;this.$results=c(f.results);this.indexData=f.index_file;this.template=this.compileTemplate(c(f.template));this.titleMsg=f.titleMsg;this.emptyMsg=f.emptyMsg;this.onAfterResultShow=f.onAfterResultShow;this.initialize()}e.prototype.initialize=function(){var f=this;this.loadIndexData(function(g){f.docs=c.map(g.docs,f.createEntry);f.index=lunr.Index.load(g.index);f.populateSearchFromQuery();f.bindKeypress()})};e.prototype.compileTemplate=function(f){var g=f.text();Mustache.parse(g);return function(h,i){return Mustache.render(g,h,i)}};e.prototype.loadIndexData=function(f){c.getJSON(this.indexData,f)};e.prototype.createEntry=function(g,f){var h=c.extend({id:f+1},g);if(g.date){c.extend(h,{date:d(g.date),pubdate:function(){return dateFormat(d(g.date),"yyyy-mm-dd")},displaydate:function(){return dateFormat(d(g.date),"mmm dd, yyyy")}})}return h};e.prototype.bindKeypress=function(){var f=this;var g=this.$elem.val();this.$elem.bind("keyup",b(function(){var h=f.$elem.val();if(h!==g){f.search(h)}g=h}))};e.prototype.search=function(g){var h=this.docs;if(g.length<3){this.$results.hide().empty()}else{var f=c.map(this.index.search(g),function(i){return c.grep(h,function(j){return j.id===parseInt(i.ref,10)})[0]});this.displayResults(f);this.onAfterResultShow()}};e.prototype.displayResults=function(k){var g=this.$results;g.empty();if(k.length===0){g.append(this.emptyMsg)}else{if(this.titleMsg&&0!==this.titleMsg.length){g.append(this.titleMsg)}g.append(this.template({docs:k}))}g.show();if(k.length>0){var i=log4javascript.getLogger("j1.searcher");var h="\ncleanup tags and categories";var f;var j;i.info(h);c(".sr-tags").each(function(){j=c(this).html().replace(/^\s+(.*)(\s+·\s+$)/,"$1");c(this).text(j)});c(".sr-categories").each(function(){f=c(this).html().replace(/^(.*)(\s+·\s+$)/,"$1");c(this).text(f)})}};e.prototype.populateSearchFromQuery=function(){var f=new URI(window.location.search.toString());var g=f.search(true);if(g.hasOwnProperty("q")){this.$elem.val(g.q);this.search(g.q.toString())}};return e})();c.fn.lunrSearch=function(e){e=c.extend({},c.fn.lunrSearch.defaults,e);new a(this,e);return this};c.fn.lunrSearch.defaults={index_file:" /assets/data/lunr-index.json",results:"#search-results",template:"#search-results-template",titleMsg:"

Search results

",emptyMsg:"

Nothing found.

",onAfterResultShow:function(){}}})(jQuery);