1 line
3.8 KiB
JavaScript
1 line
3.8 KiB
JavaScript
const sortArray=function(e,t){return e.nIdx-t.nIdx};function indexOfCatch(e){return-1<e}new class{query="";query_size=5;page_size=50;$$(e){return document.getElementById(e)}constructor(){function e(e){return document.getElementById(e)}var t,n;this.commands=linux_commands||[],this.elm_query=e("query"),this.elm_btn=e("search_btn"),this.elm_result=e("result"),this.elm_search_result=e("search_list_result"),this.root_path=(t=e("current_path"),n=window.location.origin+window.location.pathname,t?n.replace(/\/(c\/)?(\w|-)+\.html/,"").replace(/\/$/,""):""),this.init(),this.goToIndex()}goToIndex(){var t=document.getElementsByTagName("A");for(let e=0;e<t.length;e++)"/"!==t[e].pathname||/^https?:/i.test(t[e].protocol)||(t[e].href=this.root_path+"/")}bindEvent(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&e.attachEvent("on"+t,n)}isSreachIndexOF(e,t){return e&&t?e.toLowerCase().indexOf(t.toLowerCase()):-1}getQueryString(e){e=new RegExp("(^|&)"+e+"=([^&]*)(&|$)","i"),e=decodeURIComponent(window.location.hash.replace(/^(\#\!|\#)/,"")).match(e);return null!=e?unescape(e[2]):null}pushState(){window.history&&window.history.pushState&&(this.query?history.pushState({},"linux_commands","#!kw="+this.query):history.pushState({},"linux_commands",window.location.pathname))}simple(e,t){return e.replace(/\$\w+\$/gi,function(e){e=t[e.replace(/\$/g,"")];return void 0===e?"":e})}createKeyworldsHTML(e,t,n){var r='<i class="kw">$1</i>';let s=e.n,i=e.d;var l=new RegExp(`(${t})`,"ig"),t=(t&&(s=e.n.replace(l,r),i=e.d.replace(l,r)||""),this.root_path.replace(/\/$/,""));return this.simple(`<a href="${t}/c$url$.html"><strong>$name$</strong> - $des$</a>`+(n?"<p></p>":""),{name:s,url:e.p,des:i})}searchResult(t=!1){var n=this.commands;const r=this;var s=n.length;let i=[];var e,l=t?this.page_size:this.query_size,a=[],c=[];if(indexOfCatch(n&&n.length&&toString.call(n).indexOf("Array")))for(let e=0;e<s&&n[e];e++){var h=r.isSreachIndexOF(n[e].n,r.query),o=r.isSreachIndexOF(n[e].d,r.query),u=n[e];indexOfCatch(h)?(u.nIdx=h,a.push(u)):indexOfCatch(o)&&(u.dIdx=o,c.push(u))}a.sort(sortArray),c.sort(sortArray),a.concat(c).slice(0,l).forEach(e=>{i.push(r.createKeyworldsHTML(e,r.query,t))});let d=t?this.elm_search_result:this.elm_result;d.innerHTML="",i.forEach((e,t)=>{var n=document.createElement("li");n.innerHTML=e,d.appendChild(n)}),i.length||(l=document.createElement("LI"),(e=document.createElement("span")).innerText=this.query?"没有搜索到任何内容,请尝试输入其它字符!":"请尝试输入一些字符,进行搜索!",l.appendChild(e),d.appendChild(l))}selectedResult(e){var t=this.elm_result.children;let n=0;for(var r=0;r<t.length;r++)if("ok"==t[r].className){t[r].className="",n="up"==e?r-1:r+1;break}t[n]&&(t[n].className="ok")}isSelectedResult(){var t=this.elm_result.children;let n=!1;for(let e=0;e<t.length;e++)if("ok"==t[e].className){n=t[e];break}return n}init(){function t(e){n.elm_result.style.display=e||"none"}let n=this;var e=n.getQueryString("kw");this.elm_query.value=e,this.query=e||"",this.elm_search_result&&n.searchResult(!0),this.bindEvent(this.elm_query,"input",function(e){n.query=e.target.value,n.pushState(),n.query?n.searchResult():t(),n.elm_search_result?n.elm_btn.click():t(n.query?"block":"none")}),this.bindEvent(this.elm_btn,"click",function(e){t(),n.elm_search_result?n.searchResult(!0):window.location.href=n.root_path+"/list.html#!kw="+n.query}),this.bindEvent(this.elm_query,"focus",function(e){n.searchResult(),n.query&&t("block")}),this.bindEvent(this.elm_query,"blur",function(e){setTimeout(function(){t()},300)}),this.bindEvent(document,"keyup",function(e){if(40===e.keyCode&&n.selectedResult("down"),38===e.keyCode&&n.selectedResult("up"),"Enter"==e.key){e=n.isSelectedResult();if(!e)return n.elm_btn.click();e.children[0]&&e.children[0].click()}}),e&&n.searchResult()}}; |