|
262 | 262 |
|
263 | 263 |
|
264 | 264 |
|
| 265 | +<liclass="menu-item menu-item-search"> |
| 266 | + |
| 267 | +<ahref="#"diff-4beed07dd5ea8150831750611a298254de90113210435794d18f061524293353-264-268-0" data-selected="false" role="gridcell" tabindex="-1" valign="top"> | 268 | + |
| 269 | + |
| 270 | +<iclass="menu-item-icon fa fa-search fa-fw"></i><br/> |
| 271 | + |
| 272 | + 搜索 |
| 273 | +</a> |
| 274 | +</li> |
| 275 | + |
265 | 276 | </ul> |
266 | 277 |
|
267 | 278 |
|
268 | 279 |
|
| 280 | +<divclass="site-search"> |
| 281 | + |
| 282 | +<divclass="popup search-popup local-search-popup"> |
| 283 | +<divclass="local-search-header clearfix"> |
| 284 | +<spanclass="search-icon"> |
| 285 | +<iclass="fa fa-search"></i> |
| 286 | +</span> |
| 287 | +<spanclass="popup-btn-close"> |
| 288 | +<iclass="fa fa-times-circle"></i> |
| 289 | +</span> |
| 290 | +<divclass="local-search-input-wrapper"> |
| 291 | +<inputautocomplete="off" |
| 292 | +placeholder="搜索..."spellcheck="false" |
| 293 | +type="text"id="local-search-input"> |
| 294 | +</div> |
| 295 | +</div> |
| 296 | +<divid="local-search-result"></div> |
| 297 | +</div> |
| 298 | + |
| 299 | + |
| 300 | + |
| 301 | +</div> |
| 302 | + |
269 | 303 | </nav> |
270 | 304 |
|
271 | 305 |
|
@@ -618,25 +652,345 @@ <h2 class="post-title"> |
618 | 652 |
|
619 | 653 |
|
620 | 654 |
|
| 655 | + |
| 656 | +<script> |
| 657 | +varcloudTieConfig={ |
| 658 | +url:document.location.href, |
| 659 | +sourceId:"", |
| 660 | +productKey:"f2b9309d2b034863a3f5ea325458b6cb", |
| 661 | +target:"cloud-tie-wrapper" |
| 662 | +}; |
| 663 | +</script> |
| 664 | +<scriptsrc="https://img1.ws.126.net/f2e/tie/yun/sdk/loader.js"></script> |
| 665 | + |
621 | 666 |
|
622 | 667 |
|
623 | 668 |
|
624 | 669 |
|
625 | 670 |
|
626 | | - |
627 | 671 |
|
628 | 672 |
|
629 | 673 |
|
630 | 674 |
|
631 | 675 |
|
632 | 676 |
|
633 | 677 |
|
| 678 | +<scripttype="text/javascript"> |
| 679 | +// Popup Window; |
| 680 | +varisfetched=false; |
| 681 | +varisXml=true; |
| 682 | +// Search DB path; |
| 683 | +varsearch_path="search.xml"; |
| 684 | +if(search_path.length===0){ |
| 685 | +search_path="search.xml"; |
| 686 | +}elseif(search_path.endsWith("json")){ |
| 687 | +isXml=false; |
| 688 | +} |
| 689 | +varpath="/"+search_path; |
| 690 | +// monitor main search box; |
| 691 | + |
| 692 | +varonPopupClose=function(e){ |
| 693 | +$('.popup').hide(); |
| 694 | +$('#local-search-input').val(''); |
| 695 | +$('.search-result-list').remove(); |
| 696 | +$('#no-result').remove(); |
| 697 | +$(".local-search-pop-overlay").remove(); |
| 698 | +$('body').css('overflow',''); |
| 699 | +} |
634 | 700 |
|
| 701 | +functionproceedsearch(){ |
| 702 | +$("body") |
| 703 | +.append('<div class="search-popup-overlay local-search-pop-overlay"></div>') |
| 704 | +.css('overflow','hidden'); |
| 705 | +$('.search-popup-overlay').click(onPopupClose); |
| 706 | +$('.popup').toggle(); |
| 707 | +var$localSearchInput=$('#local-search-input'); |
| 708 | +$localSearchInput.attr("autocapitalize","none"); |
| 709 | +$localSearchInput.attr("autocorrect","off"); |
| 710 | +$localSearchInput.focus(); |
| 711 | +} |
635 | 712 |
|
| 713 | +// search function; |
| 714 | +varsearchFunc=function(path,search_id,content_id){ |
| 715 | +'use strict'; |
| 716 | + |
| 717 | +// start loading animation |
| 718 | +$("body") |
| 719 | +.append('<div class="search-popup-overlay local-search-pop-overlay">'+ |
| 720 | +'<div id="search-loading-icon">'+ |
| 721 | +'<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>'+ |
| 722 | +'</div>'+ |
| 723 | +'</div>') |
| 724 | +.css('overflow','hidden'); |
| 725 | +$("#search-loading-icon").css('margin','20% auto 0 auto').css('text-align','center'); |
| 726 | + |
| 727 | +$.ajax({ |
| 728 | +url:path, |
| 729 | +dataType:isXml ?"xml" :"json", |
| 730 | +async:true, |
| 731 | +success:function(res){ |
| 732 | +// get the contents from search data |
| 733 | +isfetched=true; |
| 734 | +$('.popup').detach().appendTo('.header-inner'); |
| 735 | +vardatas=isXml ?$("entry",res).map(function(){ |
| 736 | +return{ |
| 737 | +title:$("title",this).text(), |
| 738 | +content:$("content",this).text(), |
| 739 | +url:$("url",this).text() |
| 740 | +}; |
| 741 | +}).get() :res; |
| 742 | +varinput=document.getElementById(search_id); |
| 743 | +varresultContent=document.getElementById(content_id); |
| 744 | +varinputEventFunction=function(){ |
| 745 | +varsearchText=input.value.trim().toLowerCase(); |
| 746 | +varkeywords=searchText.split(/[\s\-]+/); |
| 747 | +if(keywords.length>1){ |
| 748 | +keywords.push(searchText); |
| 749 | +} |
| 750 | +varresultItems=[]; |
| 751 | +if(searchText.length>0){ |
| 752 | +// perform local searching |
| 753 | +datas.forEach(function(data){ |
| 754 | +varisMatch=false; |
| 755 | +varhitCount=0; |
| 756 | +varsearchTextCount=0; |
| 757 | +vartitle=data.title.trim(); |
| 758 | +vartitleInLowerCase=title.toLowerCase(); |
| 759 | +varcontent=data.content.trim().replace(/<[^>]+>/g,""); |
| 760 | +varcontentInLowerCase=content.toLowerCase(); |
| 761 | +vararticleUrl=decodeURIComponent(data.url); |
| 762 | +varindexOfTitle=[]; |
| 763 | +varindexOfContent=[]; |
| 764 | +// only match articles with not empty titles |
| 765 | +if(title!=''){ |
| 766 | +keywords.forEach(function(keyword){ |
| 767 | +functiongetIndexByWord(word,text,caseSensitive){ |
| 768 | +varwordLen=word.length; |
| 769 | +if(wordLen===0){ |
| 770 | +return[]; |
| 771 | +} |
| 772 | +varstartPosition=0,position=[],index=[]; |
| 773 | +if(!caseSensitive){ |
| 774 | +text=text.toLowerCase(); |
| 775 | +word=word.toLowerCase(); |
| 776 | +} |
| 777 | +while((position=text.indexOf(word,startPosition))>-1){ |
| 778 | +index.push({position:position,word:word}); |
| 779 | +startPosition=position+wordLen; |
| 780 | +} |
| 781 | +returnindex; |
| 782 | +} |
| 783 | + |
| 784 | +indexOfTitle=indexOfTitle.concat(getIndexByWord(keyword,titleInLowerCase,false)); |
| 785 | +indexOfContent=indexOfContent.concat(getIndexByWord(keyword,contentInLowerCase,false)); |
| 786 | +}); |
| 787 | +if(indexOfTitle.length>0||indexOfContent.length>0){ |
| 788 | +isMatch=true; |
| 789 | +hitCount=indexOfTitle.length+indexOfContent.length; |
| 790 | +} |
| 791 | +} |
| 792 | + |
| 793 | +// show search results |
| 794 | + |
| 795 | +if(isMatch){ |
| 796 | +// sort index by position of keyword |
| 797 | + |
| 798 | +[indexOfTitle,indexOfContent].forEach(function(index){ |
| 799 | +index.sort(function(itemLeft,itemRight){ |
| 800 | +if(itemRight.position!==itemLeft.position){ |
| 801 | +returnitemRight.position-itemLeft.position; |
| 802 | +}else{ |
| 803 | +returnitemLeft.word.length-itemRight.word.length; |
| 804 | +} |
| 805 | +}); |
| 806 | +}); |
| 807 | + |
| 808 | +// merge hits into slices |
| 809 | + |
| 810 | +functionmergeIntoSlice(text,start,end,index){ |
| 811 | +varitem=index[index.length-1]; |
| 812 | +varposition=item.position; |
| 813 | +varword=item.word; |
| 814 | +varhits=[]; |
| 815 | +varsearchTextCountInSlice=0; |
| 816 | +while(position+word.length<=end&&index.length!=0){ |
| 817 | +if(word===searchText){ |
| 818 | +searchTextCountInSlice++; |
| 819 | +} |
| 820 | +hits.push({position:position,length:word.length}); |
| 821 | +varwordEnd=position+word.length; |
| 822 | + |
| 823 | +// move to next position of hit |
| 824 | + |
| 825 | +index.pop(); |
| 826 | +while(index.length!=0){ |
| 827 | +item=index[index.length-1]; |
| 828 | +position=item.position; |
| 829 | +word=item.word; |
| 830 | +if(wordEnd>position){ |
| 831 | +index.pop(); |
| 832 | +}else{ |
| 833 | +break; |
| 834 | +} |
| 835 | +} |
| 836 | +} |
| 837 | +searchTextCount+=searchTextCountInSlice; |
| 838 | +return{ |
| 839 | +hits:hits, |
| 840 | +start:start, |
| 841 | +end:end, |
| 842 | +searchTextCount:searchTextCountInSlice |
| 843 | +}; |
| 844 | +} |
| 845 | + |
| 846 | +varslicesOfTitle=[]; |
| 847 | +if(indexOfTitle.length!=0){ |
| 848 | +slicesOfTitle.push(mergeIntoSlice(title,0,title.length,indexOfTitle)); |
| 849 | +} |
| 850 | + |
| 851 | +varslicesOfContent=[]; |
| 852 | +while(indexOfContent.length!=0){ |
| 853 | +varitem=indexOfContent[indexOfContent.length-1]; |
| 854 | +varposition=item.position; |
| 855 | +varword=item.word; |
| 856 | +// cut out 100 characters |
| 857 | +varstart=position-20; |
| 858 | +varend=position+80; |
| 859 | +if(start<0){ |
| 860 | +start=0; |
| 861 | +} |
| 862 | +if(end<position+word.length){ |
| 863 | +end=position+word.length; |
| 864 | +} |
| 865 | +if(end>content.length){ |
| 866 | +end=content.length; |
| 867 | +} |
| 868 | +slicesOfContent.push(mergeIntoSlice(content,start,end,indexOfContent)); |
| 869 | +} |
| 870 | + |
| 871 | +// sort slices in content by search text's count and hits' count |
| 872 | + |
| 873 | +slicesOfContent.sort(function(sliceLeft,sliceRight){ |
| 874 | +if(sliceLeft.searchTextCount!==sliceRight.searchTextCount){ |
| 875 | +returnsliceRight.searchTextCount-sliceLeft.searchTextCount; |
| 876 | +}elseif(sliceLeft.hits.length!==sliceRight.hits.length){ |
| 877 | +returnsliceRight.hits.length-sliceLeft.hits.length; |
| 878 | +}else{ |
| 879 | +returnsliceLeft.start-sliceRight.start; |
| 880 | +} |
| 881 | +}); |
| 882 | + |
| 883 | +// select top N slices in content |
| 884 | + |
| 885 | +varupperBound=parseInt('1'); |
| 886 | +if(upperBound>=0){ |
| 887 | +slicesOfContent=slicesOfContent.slice(0,upperBound); |
| 888 | +} |
| 889 | + |
| 890 | +// highlight title and content |
| 891 | + |
| 892 | +functionhighlightKeyword(text,slice){ |
| 893 | +varresult=''; |
| 894 | +varprevEnd=slice.start; |
| 895 | +slice.hits.forEach(function(hit){ |
| 896 | +result+=text.substring(prevEnd,hit.position); |
| 897 | +varend=hit.position+hit.length; |
| 898 | +result+='<b class="search-keyword">'+text.substring(hit.position,end)+'</b>'; |
| 899 | +prevEnd=end; |
| 900 | +}); |
| 901 | +result+=text.substring(prevEnd,slice.end); |
| 902 | +returnresult; |
| 903 | +} |
| 904 | + |
| 905 | +varresultItem=''; |
| 906 | + |
| 907 | +if(slicesOfTitle.length!=0){ |
| 908 | +resultItem+="<li><a href='"+articleUrl+"' class='search-result-title'>"+highlightKeyword(title,slicesOfTitle[0])+"</a>"; |
| 909 | +}else{ |
| 910 | +resultItem+="<li><a href='"+articleUrl+"' class='search-result-title'>"+title+"</a>"; |
| 911 | +} |
| 912 | + |
| 913 | +slicesOfContent.forEach(function(slice){ |
| 914 | +resultItem+="<a href='"+articleUrl+"'>"+ |
| 915 | +"<p class=\"search-result\">"+highlightKeyword(content,slice)+ |
| 916 | +"...</p>"+"</a>"; |
| 917 | +}); |
| 918 | + |
| 919 | +resultItem+="</li>"; |
| 920 | +resultItems.push({ |
| 921 | +item:resultItem, |
| 922 | +searchTextCount:searchTextCount, |
| 923 | +hitCount:hitCount, |
| 924 | +id:resultItems.length |
| 925 | +}); |
| 926 | +} |
| 927 | +}) |
| 928 | +}; |
| 929 | +if(keywords.length===1&&keywords[0]===""){ |
| 930 | +resultContent.innerHTML='<div id="no-result"><i class="fa fa-search fa-5x" /></div>' |
| 931 | +}elseif(resultItems.length===0){ |
| 932 | +resultContent.innerHTML='<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' |
| 933 | +}else{ |
| 934 | +resultItems.sort(function(resultLeft,resultRight){ |
| 935 | +if(resultLeft.searchTextCount!==resultRight.searchTextCount){ |
| 936 | +returnresultRight.searchTextCount-resultLeft.searchTextCount; |
| 937 | +}elseif(resultLeft.hitCount!==resultRight.hitCount){ |
| 938 | +returnresultRight.hitCount-resultLeft.hitCount; |
| 939 | +}else{ |
| 940 | +returnresultRight.id-resultLeft.id; |
| 941 | +} |
| 942 | +}); |
| 943 | +varsearchResultList='<ul class=\"search-result-list\">'; |
| 944 | +resultItems.forEach(function(result){ |
| 945 | +searchResultList+=result.item; |
| 946 | +}) |
| 947 | +searchResultList+="</ul>"; |
| 948 | +resultContent.innerHTML=searchResultList; |
| 949 | +} |
| 950 | +} |
636 | 951 |
|
| 952 | +if('auto'==='auto'){ |
| 953 | +input.addEventListener('input',inputEventFunction); |
| 954 | +}else{ |
| 955 | +$('.search-icon').click(inputEventFunction); |
| 956 | +input.addEventListener('keypress',function(event){ |
| 957 | +if(event.keyCode===13){ |
| 958 | +inputEventFunction(); |
| 959 | +} |
| 960 | +}); |
| 961 | +} |
637 | 962 |
|
| 963 | +// remove loading animation |
| 964 | +$(".local-search-pop-overlay").remove(); |
| 965 | +$('body').css('overflow',''); |
638 | 966 |
|
639 | | - |
| 967 | +proceedsearch(); |
| 968 | +} |
| 969 | +}); |
| 970 | +} |
| 971 | + |
| 972 | +// handle and trigger popup window; |
| 973 | +$('.popup-trigger').click(function(e){ |
| 974 | +e.stopPropagation(); |
| 975 | +if(isfetched===false){ |
| 976 | +searchFunc(path,'local-search-input','local-search-result'); |
| 977 | +}else{ |
| 978 | +proceedsearch(); |
| 979 | +}; |
| 980 | +}); |
| 981 | + |
| 982 | +$('.popup-btn-close').click(onPopupClose); |
| 983 | +$('.popup').click(function(e){ |
| 984 | +e.stopPropagation(); |
| 985 | +}); |
| 986 | +$(document).on('keyup',function(event){ |
| 987 | +varshouldDismissSearchPopup=event.which===27&& |
| 988 | +$('.search-popup').is(':visible'); |
| 989 | +if(shouldDismissSearchPopup){ |
| 990 | +onPopupClose(); |
| 991 | +} |
| 992 | +}); |
| 993 | +</script> |
640 | 994 |
|
641 | 995 |
|
642 | 996 |
|
|