-
Notifications
You must be signed in to change notification settings - Fork 0
/
insight.js
1 lines (1 loc) · 3.79 KB
/
insight.js
1
!function(a,s){var e=a(".ins-search"),n=e.find(".ins-search-input"),r=e.find(".ins-section-wrapper"),o=e.find(".ins-section-container");function c(n,t,e,i,r){return a("<div>").addClass("ins-selectable").addClass("ins-search-item").append(a("<header>").append(a("<i>").addClass("fa").addClass("fa-"+n)).append(a("<span>").addClass("ins-title").text(null!=t&&""!==t?t:s.TRANSLATION.UNTITLED)).append(e?a("<span>").addClass("ins-slug").text(e):null)).append(i?a("<p>").addClass("ins-search-preview").text(i):null).attr("data-url",r)}function i(t,n){var e,i;if(0===n.length)return null;switch(e=s.TRANSLATION[t],t){case"POSTS":case"PAGES":i=n.map(function(n){return c("file",n.title,null,n.text.slice(0,150),n.link)});break;case"CATEGORIES":case"TAGS":i=n.map(function(n){return c("CATEGORIES"===t?"folder":"tag",n.name,n.slug,null,n.link)});break;default:return null}return function(n){return a("<section>").addClass("ins-section").append(a("<header>").addClass("ins-section-header").text(n))}(e).append(i)}function u(n){return n.split(" ").filter(function(n){return!!n}).map(function(n){return n.toUpperCase()})}function l(n,e,i){var t=u(n);return t.filter(function(t){return 0<i.filter(function(n){return!!e.hasOwnProperty(n)&&(-1<e[n].toUpperCase().indexOf(t)||void 0)}).length}).length===t.length}function f(n,r,t,a){var s=0;return u(n).forEach(function(n){var i=new RegExp(n,"img");t.forEach(function(n,t){if(r.hasOwnProperty(n)){var e=r[n].match(i);s+=e?e.length*a[t]:0}})}),s}function p(n,t){var e=function(t){return{POST:function(n){return f(t,n,["title","text"],[3,1])},PAGE:function(n){return f(t,n,["title","text"],[3,1])},CATEGORY:function(n){return f(t,n,["name","slug"],[1,1])},TAG:function(n){return f(t,n,["name","slug"],[1,1])}}}(t),i=function(t){return{POST:function(n){return l(t,n,["title","text"])},PAGE:function(n){return l(t,n,["title","text"])},CATEGORY:function(n){return l(t,n,["name","slug"])},TAG:function(n){return l(t,n,["name","slug"])}}}(t),r=n.posts,a=n.pages,s=n.tags,o=n.categories;return{posts:r.filter(i.POST).sort(function(n,t){return e.POST(t)-e.POST(n)}).slice(0,5),pages:a.filter(i.PAGE).sort(function(n,t){return e.PAGE(t)-e.PAGE(n)}).slice(0,5),categories:o.filter(i.CATEGORY).sort(function(n,t){return e.CATEGORY(t)-e.CATEGORY(n)}).slice(0,5),tags:s.filter(i.TAG).sort(function(n,t){return e.TAG(t)-e.TAG(n)}).slice(0,5)}}function t(n){var t=a.makeArray(o.find(".ins-selectable")),e=-1;t.forEach(function(n,t){a(n).hasClass("active")&&(e=t)});var i=(t.length+e+n)%t.length;a(t[e]).removeClass("active"),a(t[i]).addClass("active"),function(n){if(0!==n.length){var t=r[0].clientHeight,e=n.position().top-r.scrollTop(),i=n[0].clientHeight+n.position().top;i>t+r.scrollTop()&&r.scrollTop(i-r[0].clientHeight),e<0&&r.scrollTop(n.position().top)}}(a(t[i]))}function d(n){n&&n.length&&(location.href=n.attr("data-url"))}e.parent().remove(".ins-search"),a("body").append(e),a.getJSON(s.CONTENT_URL,function(t){"#ins-search"===location.hash.trim()&&e.addClass("show"),n.on("input",function(){var n=a(this).val();!function(n){for(var t in o.empty(),n)o.append(i(t.toUpperCase(),n[t]))}(p(t,n))}),n.trigger("input")});var h=!1;a(document).on("click focus",".navbar-main .search",function(){e.addClass("show"),e.find(".ins-search-input").focus()}).on("click touchend",".ins-search-item",function(n){("click"===n.type||h)&&(d(a(this)),h=!1)}).on("click touchend",".ins-close",function(n){("click"===n.type||h)&&(a(".navbar-main").css("pointer-events","none"),setTimeout(function(){a(".navbar-main").css("pointer-events","auto")},400),e.removeClass("show"),h=!1)}).on("keydown",function(n){if(e.hasClass("show"))switch(n.keyCode){case 27:e.removeClass("show");break;case 38:t(-1);break;case 40:t(1);break;case 13:d(o.find(".ins-selectable.active").eq(0))}}).on("touchstart",function(n){h=!0}).on("touchmove",function(n){h=!1})}(jQuery,window.INSIGHT_CONFIG);