|
6 | 6 | this.elm_btn = $$('search_btn'); |
7 | 7 | this.elm_info = $$('commands_info'); |
8 | 8 | this.elm_result = $$('result'); |
| 9 | + this.elm_search_result = $$('search_list_result'); |
9 | 10 |
|
10 | 11 | // 获取根路径 |
11 | 12 | this.root_path = (function(){ |
|
15 | 16 | })(); |
16 | 17 |
|
17 | 18 | this.query = ''; // |
18 | | - this.page_size = 10; //每页显示10条 |
| 19 | + this.query_size = 5; //搜索框结果显示5条 |
| 20 | + this.page_size = 20; //每页显示20条 |
19 | 21 |
|
20 | 22 | this.elm_info.innerHTML = this.commands.length; |
21 | 23 |
|
|
69 | 71 | return typeof returns === "undefined" ? "" : returns; |
70 | 72 | }) |
71 | 73 | }, |
72 | | - createKeyworldsHTML:function(json,keywolds){ |
| 74 | + createKeyworldsHTML:function(json,keywolds,islist){ |
73 | 75 | var name = json.n,des = json.d,self = this, |
74 | | - reg = new RegExp("("+keywolds+")","ig"); |
| 76 | + reg = new RegExp("("+keywolds+")","ig"), |
| 77 | + str = ''; |
75 | 78 | if(keywolds){ |
76 | 79 | name = json.n.replace(reg,'<i class="kw">'+"$1"+"</i>"); |
77 | 80 | des = json.d.replace(reg,'<i class="kw">'+"$1"+"</i>") || ''; |
78 | 81 | } |
79 | 82 |
|
80 | | - return this.simple('<a href="'+this.root_path+'/c$url$.html"><strong>$name$</strong> - $des$</a>',{ |
| 83 | + str = islist ? '<a href="'+this.root_path+'/c$url$.html"><strong>$name$</strong> - $des$</a><p></p>' : '<a href="'+this.root_path+'/c$url$.html"><strong>$name$</strong> - $des$</a>'; |
| 84 | + return this.simple(str,{ |
81 | 85 | name:name, |
82 | 86 | url:json.p, |
83 | 87 | des:des |
84 | | - }) |
85 | | - |
| 88 | + }); |
86 | 89 | }, |
87 | | - createListHTML:function(){ |
88 | | - var arr = this.commands,self = this,page_size = this.page_size,i=0; |
| 90 | + searchResult:function(islist){ |
| 91 | + var arr = this.commands,self = this,i=0, |
| 92 | + page_size = islist?this.page_size:this.query_size, |
| 93 | + arrResultHTML = []; |
89 | 94 | if(arr&&arr.length&&toString.call(arr).indexOf('Array')>-1){ |
90 | | - self.elm_result.innerHTML=''; |
91 | | - var relese = 0; |
92 | 95 | for (; i < page_size; i++) { |
93 | 96 | if(!arr[i]) break; |
94 | | - var myLi = document.createElement("LI"); |
95 | | - if(self.isSreachIndexOF(arr[i].n,self.query) |
96 | | - || self.isSreachIndexOF(arr[i].d,self.query) |
| 97 | + if(self.isSreachIndexOF(arr[i].n,self.query) |
| 98 | + || self.isSreachIndexOF(arr[i].d,self.query) |
97 | 99 | ){ |
98 | | - relese += 1 |
99 | | - myLi.innerHTML = self.createKeyworldsHTML(arr[i],self.query) |
100 | | - self.elm_result.appendChild(myLi); |
| 100 | + arrResultHTML.push(self.createKeyworldsHTML(arr[i],self.query,islist)); |
101 | 101 | } |
102 | 102 | } |
103 | | - if(relese ===0){ |
104 | | - myLi.innerHTML = this.query?'<span>没有搜索到任何内容,请尝试输入其它字符!</span>':'<span>请尝试输入一些字符,进行搜素!</span>'; |
105 | | - self.elm_result.innerHTML = myLi.outerHTML; |
106 | | - } |
| 103 | + } |
| 104 | + |
| 105 | + var elm = islist?this.elm_search_result:this.elm_result; |
| 106 | + elm.innerHTML=''; |
| 107 | + for (var i = 0; i < arrResultHTML.length; i++) { |
| 108 | + var myLi = document.createElement("LI"); |
| 109 | + myLi.innerHTML = arrResultHTML[i]; |
| 110 | + elm.appendChild(myLi); |
| 111 | + } |
| 112 | + if(arrResultHTML.length === 0){ |
| 113 | + var myLi = document.createElement("LI"); |
| 114 | + myLi.innerHTML = '<span>'+this.query?'请尝试输入一些字符,进行搜素!'+'</span>':'没有搜索到任何内容,请尝试输入其它字符!'; |
| 115 | + elm.appendChild(myLi); |
107 | 116 | } |
108 | 117 | }, |
109 | 118 | init:function(){ |
|
112 | 121 | var timer = null |
113 | 122 | this.elm_query.value = kw; |
114 | 123 | this.query = kw; |
| 124 | + if(this.elm_search_result) self.searchResult(true); |
115 | 125 | this.bindEvent(this.elm_query,'input',function(e){ |
116 | 126 | self.query = e.target.value; |
117 | 127 | self.pushState() |
118 | | - self.createListHTML(); |
| 128 | + self.searchResult(); |
119 | 129 | self.elm_result.style.display = self.query?'block':'none'; |
120 | 130 | }) |
121 | 131 | this.bindEvent(this.elm_btn,'click',function(e){ |
122 | | - self.createListHTML(); |
| 132 | + console.log("---->") |
| 133 | + if(self.elm_search_result){ |
| 134 | + self.searchResult(true); |
| 135 | + } |
| 136 | + else{ |
| 137 | + window.location.href = self.root_path + '/list.html#!kw='+self.query; |
| 138 | + } |
123 | 139 | }) |
124 | 140 | this.bindEvent(this.elm_query,'focus',function(e){ |
125 | | - self.createListHTML(); |
| 141 | + self.searchResult(); |
126 | 142 | self.elm_result.style.display = 'block'; |
127 | 143 | }) |
128 | 144 | this.bindEvent(this.elm_query,'blur',function(e){ |
129 | 145 | timer = setTimeout(function(){ |
130 | 146 | self.elm_result.style.display = 'none'; |
131 | 147 | },600) |
132 | 148 | }) |
133 | | - if(kw) self.createListHTML(); |
| 149 | + if(kw) self.searchResult(); |
134 | 150 | } |
135 | 151 | } |
136 | 152 |
|
|
0 commit comments