1function JCTitleSearch2(arParams) 2{ 3 var _this = this; 4 5 this.arParams = { 6 'AJAX_PAGE': arParams.AJAX_PAGE, 7 'CONTAINER_ID': arParams.CONTAINER_ID, 8 'INPUT_ID': arParams.INPUT_ID, 9 'MIN_QUERY_LEN': parseInt(arParams.MIN_QUERY_LEN) 10 }; 11 if(arParams.WAIT_IMAGE) 12 this.arParams.WAIT_IMAGE = arParams.WAIT_IMAGE; 13 if(arParams.MIN_QUERY_LEN <= 0) 14 arParams.MIN_QUERY_LEN = 1; 15 16 this.cache = []; 17 this.cache_key = null; 18 19 this.startText = ''; 20 this.running = false; 21 this.currentRow = -1; 22 this.RESULT = null; 23 this.CONTAINER = null; 24 this.INPUT = null; 25 this.WAIT = null; 26 27 this.ShowResult = function(result) 28 { 29 if(BX.type.isString(result)) 30 { 31 _this.RESULT.innerHTML = result; 32 } 33 34 _this.RESULT.style.display = _this.RESULT.innerHTML !== '' ? 'block' : 'none'; 35 var pos = _this.adjustResultNode(); 36 37 //adjust left column to be an outline 38 var res_pos; 39 var th; 40 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'title-search-result'}, true); 41 if(tbl) 42 { 43 th = BX.findChild(tbl, {'tag':'th'}, true); 44 } 45 if(th) 46 { 47 var tbl_pos = BX.pos(tbl); 48 tbl_pos.width = tbl_pos.right - tbl_pos.left; 49 50 var th_pos = BX.pos(th); 51 th_pos.width = th_pos.right - th_pos.left; 52 th.style.width = th_pos.width + 'px'; 53 54 _this.RESULT.style.width = (pos.width + th_pos.width) + 'px'; 55 56 //Move table to left by width of the first column 57 _this.RESULT.style.left = (pos.left - th_pos.width - 1)+ 'px'; 58 59 //Shrink table when it's too wide 60 if((tbl_pos.width - th_pos.width) > pos.width) 61 _this.RESULT.style.width = (pos.width + th_pos.width -1) + 'px'; 62 63 //Check if table is too wide and shrink result div to it's width 64 tbl_pos = BX.pos(tbl); 65 res_pos = BX.pos(_this.RESULT); 66 if(res_pos.right > tbl_pos.right) 67 { 68 _this.RESULT.style.width = (tbl_pos.right - tbl_pos.left) + 'px'; 69 } 70 } 71 72 var fade; 73 if(tbl) fade = BX.findChild(_this.RESULT, {'class':'title-search-fader'}, true); 74 if(fade && th) 75 { 76 res_pos = BX.pos(_this.RESULT); 77 fade.style.left = (res_pos.right - res_pos.left - 18) + 'px'; 78 fade.style.width = 18 + 'px'; 79 fade.style.top = 0 + 'px'; 80 fade.style.height = (res_pos.bottom - res_pos.top) + 'px'; 81 fade.style.display = 'block'; 82 } 83 }; 84 85 this.onKeyPress = function(keyCode) 86 { 87 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'title-search-result'}, true); 88 if(!tbl) 89 return false; 90 91 var i; 92 var cnt = tbl.rows.length; 93 94 switch (keyCode) 95 { 96 case 27: // escape key - close search div 97 _this.RESULT.style.display = 'none'; 98 _this.currentRow = -1; 99 _this.UnSelectAll(); 100 return true; 101 102 case 40: // down key - navigate down on search results 103 if(_this.RESULT.style.display == 'none') 104 _this.RESULT.style.display = 'block'; 105 106 var first = -1; 107 for(i = 0; i < cnt; i++) 108 { 109 if(!BX.findChild(tbl.rows[i], {'class':'title-search-separator'}, true)) 110 { 111 if(first == -1) 112 first = i; 113 114 if(_this.currentRow < i) 115 { 116 _this.currentRow = i; 117 break; 118 } 119 else if(tbl.rows[i].className == 'title-search-selected') 120 { 121 tbl.rows[i].className = ''; 122 } 123 } 124 } 125 126 if(i == cnt && _this.currentRow != i) 127 _this.currentRow = first; 128 129 tbl.rows[_this.currentRow].className = 'title-search-selected'; 130 return true; 131 132 case 38: // up key - navigate up on search results 133 if(_this.RESULT.style.display == 'none') 134 _this.RESULT.style.display = 'block'; 135 136 var last = -1; 137 for(i = cnt-1; i >= 0; i--) 138 { 139 if(!BX.findChild(tbl.rows[i], {'class':'title-search-separator'}, true)) 140 { 141 if(last == -1) 142 last = i; 143 144 if(_this.currentRow > i) 145 { 146 _this.currentRow = i; 147 break; 148 } 149 else if(tbl.rows[i].className == 'title-search-selected') 150 { 151 tbl.rows[i].className = ''; 152 } 153 } 154 } 155 156 if(i < 0 && _this.currentRow != i) 157 _this.currentRow = last; 158 159 tbl.rows[_this.currentRow].className = 'title-search-selected'; 160 return true; 161 162 case 13: // enter key - choose current search result 163 if(_this.RESULT.style.display == 'block') 164 { 165 for(i = 0; i < cnt; i++) 166 { 167 if(_this.currentRow == i) 168 { 169 if(!BX.findChild(tbl.rows[i], {'class':'title-search-separator'}, true)) 170 { 171 var a = BX.findChild(tbl.rows[i], {'tag':'a'}, true); 172 if(a) 173 { 174 window.location = a.href; 175 return true; 176 } 177 } 178 } 179 } 180 } 181 return false;
182 } 183 184 return false; 185 }; 186 187 this.onTimeout = function() 188 { 189 _this.onChange(function(){ 190 setTimeout(_this.onTimeout, 500); 191 }); 192 }; 193 194 this.onChange = function(callback) 195 { 196 if (_this.running) 197 return; 198 _this.running = true; 199 200 if(_this.INPUT.value != _this.oldValue && _this.INPUT.value != _this.startText) 201 { 202 _this.oldValue = _this.INPUT.value; 203 if(_this.INPUT.value.length >= _this.arParams.MIN_QUERY_LEN) 204 { 205 _this.cache_key = _this.arParams.INPUT_ID + '|' + _this.INPUT.value; 206 if(_this.cache[_this.cache_key] == null) 207 { 208 if(_this.WAIT) 209 { 210 var pos = BX.pos(_this.INPUT); 211 var height = (pos.bottom - pos.top)-2; 212 _this.WAIT.style.top = (pos.top+1) + 'px'; 213 _this.WAIT.style.height = height + 'px'; 214 _this.WAIT.style.width = height + 'px'; 215 _this.WAIT.style.left = (pos.right - height + 2) + 'px'; 216 _this.WAIT.style.display = 'block'; 217 } 218 219 BX.ajax.post( 220 _this.arParams.AJAX_PAGE, 221 { 222 'ajax_call':'y', 223 'INPUT_ID':_this.arParams.INPUT_ID, 224 'q':_this.INPUT.value, 225 'l':_this.arParams.MIN_QUERY_LEN 226 }, 227 function(result) 228 { 229 _this.cache[_this.cache_key] = result; 230 _this.ShowResult(result); 231 _this.currentRow = -1; 232 _this.EnableMouseEvents(); 233 if(_this.WAIT) 234 _this.WAIT.style.display = 'none'; 235 if (!!callback) 236 callback(); 237 _this.running = false; 238 } 239 ); 240 return; 241 } 242 else 243 { 244 _this.ShowResult(_this.cache[_this.cache_key]); 245 _this.currentRow = -1; 246 _this.EnableMouseEvents(); 247 } 248 } 249 else 250 { 251 _this.RESULT.style.display = 'none'; 252 _this.currentRow = -1; 253 _this.UnSelectAll(); 254 } 255 } 256 if (!!callback) 257 callback(); 258 _this.running = false; 259 }; 260 261 this.UnSelectAll = function() 262 { 263 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'title-search-result'}, true); 264 if(tbl) 265 { 266 var cnt = tbl.rows.length; 267 for(var i = 0; i < cnt; i++) 268 tbl.rows[i].className = ''; 269 } 270 }; 271 272 this.EnableMouseEvents = function() 273 { 274 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'title-search-result'}, true); 275 if(tbl) 276 { 277 var cnt = tbl.rows.length; 278 for(var i = 0; i < cnt; i++) 279 if(!BX.findChild(tbl.rows[i], {'class':'title-search-separator'}, true)) 280 { 281 tbl.rows[i].id = 'row_' + i; 282 tbl.rows[i].onmouseover = function (e) { 283 if(_this.currentRow != this.id.substr(4)) 284 { 285 _this.UnSelectAll(); 286 this.className = 'title-search-selected'; 287 _this.currentRow = this.id.substr(4); 288 } 289 }; 290 tbl.rows[i].onmouseout = function (e) { 291 this.className = ''; 292 _this.currentRow = -1; 293 }; 294 } 295 } 296 }; 297 298 this.onFocusLost = function(hide) 299 { 300 // setTimeout(function(){_this.RESULT.style.display = 'none';}, 250); 301 _this.RESULT.style.display = 'none'; 302 }; 303 304 this.onFocusGain = function() 305 { 306 if(_this.RESULT.innerHTML.length) 307 _this.ShowResult(); 308 }; 309 310 this.onKeyDown = function(e) 311 { 312 if(!e) 313 e = window.event; 314 315 if (_this.RESULT.style.display == 'block') 316 { 317 if(_this.onKeyPress(e.keyCode)) 318 return BX.PreventDefault(e); 319 } 320 }; 321 322 this.adjustResultNode = function() 323 { 324 var pos, pos_input; 325 var fixedParent = BX.findParent(_this.CONTAINER, BX.is_fixed); 326 if(!!fixedParent) 327 { 328 _this.RESULT.style.position = 'fixed'; 329 _this.RESULT.style.zIndex = BX.style(fixedParent, 'z-index') + 2; 330 pos = BX.pos(_this.CONTAINER, true); 331 } 332 else 333 { 334 _this.RESULT.style.position = 'absolute'; 335 pos = BX.pos(_this.CONTAINER); 336 } 337 pos_input = BX.pos(_this.INPUT); 338 339 pos.width = pos.right - pos.left; 340 _this.RESULT.style.top = (pos.bottom + 2) + 'px'; 341 _this.RESULT.style.left = pos_input.left + 'px'; 342 343 if($(_this.INPUT).closest('.inline-search-block.with-close').length) 344 _this.RESULT.style.width = pos_input.width + 'px'; 345 else 346 _this.RESULT.style.width = pos.width + 'px'; 347 return pos; 348 }; 349 350 this._onContainerLayoutChange = function() 351 { 352 if(_this.RESULT.style.display !== "none" && _this.RESULT.innerHTML !== '') 353 { 354 _this.adjustResultNode(); 355 } 356 }; 357 this.Init = function() 358 { 359 this.CONTAINER = document.getElementById(this.arParams.CONTAINER_ID); 360 BX.addCustomEvent(this.CONTAINER, "OnNodeLayoutChange", this._onContainerLayoutChange); 361 362 this.RESULT = document.body.appendChild(document.createElement("DIV")); 363 364 this.RESULT.className = 'title-search-result '+this.arParams.INPUT_ID; 365 this.INPUT = document.getElementById(this.arParams.INPUT_ID); 366 this.startText = this.oldValue = this.INPUT.value; 367 BX.bind(this.INPUT, 'focus', function() {_this.onFocusGain()}); 368 BX.bind(this.INPUT, 'blur', function() {_this.onFocusLost()}); 369 370 if(BX.browser.IsSafari() || BX.browser.IsIE()) 371 this.INPUT.onkeydown = this.onKeyDown; 372 else 373 this.INPUT.onkeypress = this.onKeyDown; 374 375 if(this.arParams.WAIT_IMAGE) 376 { 377 this.WAIT = document.body.appendChild(document.createElement("DIV")); 378 this.WAIT.style.backgroundImage = "url('" + this.arParams.WAIT_IMAGE + "')"; 379 if(!BX.browser.IsIE()) 380 this.WAIT.style.backgroundRepeat = 'none'; 381 this.WAIT.style.display = 'none'; 382 this.WAIT.style.position = 'absolute'; 383 this.WAIT.style.zIndex = '1100'; 384 } 385 386 BX.bind(this.INPUT, 'bxchange', function() {_this.onChange()}); 387 }; 388 BX.ready(function (){_this.Init(arParams)}); 389}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.