(function(){ function Commands(){ var $$ = this.$$; this.commands = linux_commands || []; this.elm_query = $$('query'); this.elm_btn = $$('search_btn'); this.elm_result = $$('result'); this.elm_search_result = $$('search_list_result'); // 获取根路径 this.root_path = (function(){ var elm_path = $$('current_path'); var url = window.location.origin+window.location.pathname; return elm_path?url.replace(elm_path.value,'').replace(/\/$/,''):''; })(); this.query = ''; // this.query_size = 5; //搜索框结果显示5条 this.page_size = 50; //每页显示20条 this.init() this.goToIndex() } Commands.prototype = { $$:function(id){ return document.getElementById(id) }, goToIndex:function(){ var elma = document.getElementsByTagName('A'); for (var i = 0; i < elma.length; i++) { if(elma[i].pathname==='/') elma[i].href = this.root_path+'/'; } }, bindEvent:function(elm,type,handle){ if (elm.addEventListener) { elm.addEventListener(type, handle, false); } else if (elm.attachEvent) { elm.attachEvent('on'+type, handle); } }, isSreachIndexOF:function(oldstr,kw){ var istrue = false; if(oldstr&&toString.call(oldstr) === '[object Array]'){ for (var i = 0; i < oldstr.length; i++) { oldstr[i].toLowerCase()===kw.toLowerCase()?istrue=true:null; } return istrue; } if(!oldstr || !kw) return false; return oldstr.toLowerCase().indexOf(kw.toLowerCase()) > -1 ? true : false; }, //获取URL上面的参数 getQueryString:function(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = decodeURIComponent(window.location.hash.replace(/^(\#\!|\#)/,'')).match(reg); if (r != null) return unescape(r[2]); return null; }, pushState:function(){ if(window.history&&window.history.pushState) this.query ? history.pushState({},"linux_commands","#!kw="+this.query): history.pushState({},"linux_commands",window.location.pathname); }, //简单模版 simple:function(str,obj){ return str.replace(/\$\w+\$/gi, function(matchs) { var returns = obj[matchs.replace(/\$/g, "")]; return typeof returns === "undefined" ? "" : returns; }) }, createKeyworldsHTML:function(json,keywolds,islist){ var name = json.n,des = json.d,self = this, reg = new RegExp("("+keywolds+")","ig"), str = ''; if(keywolds){ name = json.n.replace(reg,''+"$1"+""); des = json.d.replace(reg,''+"$1"+"") || ''; } var rootp = this.root_path.replace(/\/$/,''); str = islist ? '$name$ - $des$
' : '$name$ - $des$'; return this.simple(str,{ name:name, url:json.p, des:des }); }, /** * [searchResult ] * @param {[type]} islist [是否为列表] */ searchResult:function(islist){ var arr = this.commands,self = this,i=0, page_size = arr.length, arrResultHTML = [], show_list_count = islist?this.page_size:this.query_size; if(arr&&arr.length&&toString.call(arr).indexOf('Array')>-1){ var count = 0 for (; i < page_size; i++) { if(!arr[i]) break; if(self.isSreachIndexOF(arr[i].n,self.query) || self.isSreachIndexOF(arr[i].d,self.query) ){ if(count