daux.io/tipuesearch/tipuesearch.min.js
2016-02-15 12:14:48 -08:00

1 ligne
7.3 KiB
JavaScript

!function(e){e.fn.tipuesearch=function(t){var i=e.extend({show:7,newWindow:!1,showURL:!0,showTitleCount:!0,minimumLength:3,descriptiveWords:25,highlightTerms:!0,highlightEveryTerm:!1,mode:"static",liveDescription:"*",liveContent:"*",contentLocation:"tipuesearch/tipuesearch_content.json",debug:!1},t),r=function(){var e,t={},i=window.location.href.split("?")[1];if(i){i=i.split("&");for(var r=0;r<i.length;r++)e=i[r].split("="),t[decodeURIComponent(e[0])]=decodeURIComponent(e[1])}return t},s=function(e){var t=window.location.href,i=t.split("?")[0],r=[];for(var s in e)e.hasOwnProperty(s)&&r.push(encodeURIComponent(s)+"="+encodeURIComponent(e[s]));r.length&&(r.sort(),i+="?"+r.join("&")),window.location.href=i};return this.each(function(){function t(){var t=e.extend({search:"",start:"0"},r());e("#tipue_search_input").val(t.search),a(parseInt(t.start,10),!0)}function a(t,r){e("#tipue_search_content").hide();var h="",p=!1,u=!1,l=!0,_=0;found=[];var g=e("#tipue_search_input").val().toLowerCase();if(g=e.trim(g),!g)return void e(".doc_content").show();if(e("#tipue_search_content").html('<div class="tipue_search_spinner"><div class="tipue_search_rect1"></div><div class="tipue_search_rect2"></div><div class="rect3"></div></div>'),e("#tipue_search_content").show(),(g.match('^"')&&g.match('"$')||g.match("^'")&&g.match("'$"))&&(l=!1),l){var d=g.split(" ");g="";for(var v=0;v<d.length;v++){for(var f=!0,w=0;w<tipuesearch_stop_words.length;w++)d[v]==tipuesearch_stop_words[w]&&(f=!1,u=!0);f&&(g=g+" "+d[v])}g=e.trim(g),d=g.split(" ")}else g=g.substring(1,g.length-1);if(g.length>=i.minimumLength){if(l){if(r){for(var m=g,v=0;v<d.length;v++)for(var w=0;w<tipuesearch_replace.words.length;w++)d[v]==tipuesearch_replace.words[w].word&&(g=g.replace(d[v],tipuesearch_replace.words[w].replace_with),p=!0);d=g.split(" ")}for(var x=g,v=0;v<d.length;v++)for(var w=0;w<tipuesearch_stem.words.length;w++)d[v]==tipuesearch_stem.words[w].word&&(x=x+" "+tipuesearch_stem.words[w].stem);d=x.split(" ");for(var v=0;v<n.pages.length;v++){for(var b=0,L=n.pages[v].text,w=0;w<d.length;w++){var C=new RegExp(d[w],"gi");if(-1!=n.pages[v].title.search(C)){var E=n.pages[v].title.match(C).length;b+=20*E}if(-1!=n.pages[v].text.search(C)){var E=n.pages[v].text.match(C).length;b+=20*E}if(i.highlightTerms){if(i.highlightEveryTerm)var R=new RegExp("("+d[w]+")","gi");else var R=new RegExp("("+d[w]+")","i");L=L.replace(R,'<span class="h01">$1</span>')}if(-1!=n.pages[v].tags.search(C)){var E=n.pages[v].tags.match(C).length;b+=10*E}if(-1!=n.pages[v].url.search(C)&&(b+=20),0!=b)for(var y=0;y<tipuesearch_weight.weight.length;y++)n.pages[v].url==tipuesearch_weight.weight[y].url&&(b+=tipuesearch_weight.weight[y].score);d[w].match("^-")&&(C=new RegExp(d[w].substring(1),"i"),(-1!=n.pages[v].title.search(C)||-1!=n.pages[v].text.search(C)||-1!=n.pages[v].tags.search(C))&&(b=0))}0!=b&&(found.push({score:b,title:n.pages[v].title,desc:L,url:n.pages[v].url}),_++)}}else for(var v=0;v<n.pages.length;v++){var b=0,L=n.pages[v].text,C=new RegExp(g,"gi");if(-1!=n.pages[v].title.search(C)){var E=n.pages[v].title.match(C).length;b+=20*E}if(-1!=n.pages[v].text.search(C)){var E=n.pages[v].text.match(C).length;b+=20*E}if(i.highlightTerms){if(i.highlightEveryTerm)var R=new RegExp("("+g+")","gi");else var R=new RegExp("("+g+")","i");L=L.replace(R,'<span class="h01">$1</span>')}if(-1!=n.pages[v].tags.search(C)){var E=n.pages[v].tags.match(C).length;b+=10*E}if(-1!=n.pages[v].url.search(C)&&(b+=20),0!=b)for(var y=0;y<tipuesearch_weight.weight.length;y++)n.pages[v].url==tipuesearch_weight.weight[y].url&&(b+=tipuesearch_weight.weight[y].score);0!=b&&(found.push({score:b,title:n.pages[v].title,desc:L,url:n.pages[v].url}),_++)}if(0!=_){if(i.showTitleCount&&0==c){var T=document.title;document.title="("+_+") "+T,c++}1==p&&(h+='<div id="tipue_search_warning">'+tipuesearch_string_2+" "+g+". "+tipuesearch_string_3+' <a id="tipue_search_replaced">'+m+"</a></div>"),1==_?h+='<div id="tipue_search_results_count">'+tipuesearch_string_4+"</div>":(c_c=_.toString().replace(/\B(?=(\d{3})+(?!\d))/g,","),h+='<div id="tipue_search_results_count">'+c_c+" "+tipuesearch_string_5+"</div>"),found.sort(function(e,t){return t.score-e.score});for(var U=0,v=0;v<found.length;v++){if(U>=t&&U<i.show+t){if(h+='<div class="tipue_search_content_title"><a href="'+found[v].url+'"'+o+">"+found[v].title+"</a></div>",i.debug&&(h+='<div class="tipue_search_content_debug">Score: '+found[v].score+"</div>"),i.showURL){var j=found[v].url.toLowerCase();0==j.indexOf("http://")&&(j=j.slice(7)),h+='<div class="tipue_search_content_url"><a href="'+i.base_url+found[v].url+'"'+o+">"+j+"</a></div>"}if(found[v].desc){var k=found[v].desc,I="",O=k.split(" ");if(O.length<i.descriptiveWords)I=k;else for(var w=0;w<i.descriptiveWords;w++)I+=O[w]+" ";I=e.trim(I),"."!=I.charAt(I.length-1)&&(I+=" ..."),h+='<div class="tipue_search_content_text">'+I+"</div>"}}U++}if(_>i.show){var W=Math.ceil(_/i.show),S=t/i.show;if(h+='<div id="tipue_search_foot"><ul id="tipue_search_foot_boxes">',t>0&&(h+='<li><a class="tipue_search_foot_box" id="'+(t-i.show)+"_"+r+'">'+tipuesearch_string_6+"</a></li>"),2>=S){var $=W;W>3&&($=3);for(var w=0;$>w;w++)h+=w==S?'<li class="current">'+(w+1)+"</li>":'<li><a class="tipue_search_foot_box" id="'+w*i.show+"_"+r+'">'+(w+1)+"</a></li>"}else{var $=S+2;$>W&&($=W);for(var w=S-1;$>w;w++)h+=w==S?'<li class="current">'+(w+1)+"</li>":'<li><a class="tipue_search_foot_box" id="'+w*i.show+"_"+r+'">'+(w+1)+"</a></li>"}S+1!=W&&(h+='<li><a class="tipue_search_foot_box" id="'+(t+i.show)+"_"+r+'">'+tipuesearch_string_7+"</a></li>"),h+="</ul></div>"}}else h+='<div id="tipue_search_warning">'+tipuesearch_string_8+"</div>"}else u?h+='<div id="tipue_search_warning">'+tipuesearch_string_8+". "+tipuesearch_string_9+"</div>":(h+='<div id="tipue_search_warning">'+tipuesearch_string_10+"</div>",h+=1==i.minimumLength?'<div id="tipue_search_warning">'+tipuesearch_string_11+"</div>":'<div id="tipue_search_warning">'+tipuesearch_string_12+" "+i.minimumLength+" "+tipuesearch_string_13+"</div>");e(".doc_content").hide(),e("#tipue_search_content").hide(),e("#tipue_search_content").html(h),e("#tipue_search_content").slideDown(200),e("#tipue_search_replaced").click(function(){a(0,!1)}),e(".tipue_search_foot_box").click(function(){var t=e(this).attr("id"),i=t.split("_");s({search:e("#tipue_search_input").val(),start:i[0]})})}var n={pages:[]};e.ajaxSetup({async:!1});var c=0;if("live"==i.mode)for(var h=0;h<tipuesearch_pages.length;h++)e.get(tipuesearch_pages[h]).done(function(t){var r=e(i.liveContent,t).text();r=r.replace(/\s+/g," ");var s=e(i.liveDescription,t).text();s=s.replace(/\s+/g," ");var a=t.toLowerCase().indexOf("<title>"),c=t.toLowerCase().indexOf("</title>",a+7);if(-1!=a&&-1!=c)var o=t.slice(a+7,c);else var o=tipuesearch_string_1;n.pages.push({title:o,text:s,tags:r,url:tipuesearch_pages[h]})});"json"==i.mode&&e.getJSON(i.base_url+i.contentLocation).done(function(t){n=e.extend({},t)}),"static"==i.mode&&(n=e.extend({},tipuesearch));var o="";i.newWindow&&(o=' target="_blank"'),t(),"onsearch"in document.documentElement?e("#tipue_search_input")[0].addEventListener("search",function(t){s({search:e("#tipue_search_input").val(),start:0})}):(e("#tipue_search_input")[0].addEventListener("input",function(t){var i=e("#tipue_search_input").val();""===i&&s({search:i,start:0})}),e(this).keyup(function(t){"13"==t.keyCode&&s({search:e("#tipue_search_input").val(),start:0})}))})}}(jQuery);