1function JCTitleSearch(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':'nav-live-search'}, true); 41 if(tbl) 42 { 43 th = BX.findChild(tbl, {'tag':'th'}, true); 44 } 45 46 if(th) 47 { 48 var tbl_pos = BX.pos(tbl); 49 tbl_pos.width = tbl_pos.right - tbl_pos.left; 50 51 var th_pos = BX.pos(th); 52 th_pos.width = th_pos.right - th_pos.left; 53 th.style.width = th_pos.width + 'px'; 54 55 _this.RESULT.style.width = (pos.width + th_pos.width) + 'px'; 56 57 //Move table to left by width of the first column 58 _this.RESULT.style.left = (pos.left - th_pos.width - 1)+ 'px'; 59 60 //Shrink table when it's too wide 61 if((tbl_pos.width - th_pos.width) > pos.width) 62 _this.RESULT.style.width = (pos.width + th_pos.width -1) + 'px'; 63 64 //Check if table is too wide and shrink result div to it's width 65 tbl_pos = BX.pos(tbl); 66 res_pos = BX.pos(_this.RESULT); 67 if(res_pos.right > tbl_pos.right) 68 { 69 _this.RESULT.style.width = (tbl_pos.right - tbl_pos.left) + 'px'; 70 } 71 } 72 73 var fade; 74 if(tbl) fade = BX.findChild(_this.RESULT, {'class':'title-search-fader'}, true); 75 if(fade && th) 76 { 77 res_pos = BX.pos(_this.RESULT); 78 fade.style.left = (res_pos.right - res_pos.left - 18) + 'px'; 79 fade.style.width = 18 + 'px'; 80 fade.style.top = 0 + 'px'; 81 fade.style.height = (res_pos.bottom - res_pos.top) + 'px'; 82 fade.style.display = 'block'; 83 } 84 }; 85 86 this.onKeyPress = function(keyCode) 87 { 88 var tbl = BX.findChild(_this.CONTAINER, {'tag':'div','class':'search-res'}, true); 89 if(!tbl) 90 return false; 91 92 var i; 93 var cnt = tbl.childNodes.length; 94 95 //console.log(keyCode) 96 if(keyCode == '13') { 97 console.log(_this.CONTAINER.style.display) 98 //if(_this.CONTAINER.style.display == 'block') { 99 for(i = 0; i < cnt; i++) 100 { 101 if(_this.currentRow == i) 102 { 103 //console.log(tbl.childNodes[i]) 104 if(!BX.findChild(tbl.childNodes[i], {'class':'title-search-separator'}, true)) 105 { 106 var a = BX.findChild(tbl.childNodes[i], {'tag':'a'}, true); 107 if(a) 108 { 109 window.location = a.href; 110 return true; 111 } 112 } 113 } 114 } 115 //} 116 return false; 117 } 118 /*var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'nav-live-search'}, true); 119 if(!tbl) 120 return false; 121 122 var i; 123 var cnt = tbl.rows.length;*/ 124 125 switch (keyCode) 126 { 127 case 27: // escape key - close search div 128 _this.RESULT.style.display = 'none'; 129 _this.currentRow = -1; 130 _this.UnSelectAll(); 131 return true; 132 133 case 40: // down key - navigate down on search results 134 if(_this.RESULT.style.display == 'none') 135 _this.RESULT.style.display = 'block'; 136 137 var first = -1; 138 for(i = 0; i < cnt; i++) 139 { 140 if(!BX.findChild(tbl.childNodes[i], {'class':'title-search-separator'}, true)) 141 { 142 if(first == -1) 143 first = i; 144 145 if(_this.currentRow < i) 146 { 147 _this.currentRow = i; 148 break; 149 } 150 else if(tbl.childNodes[i].className == 'title-search-selected') 151 { 152 tbl.childNodes[i].backgroundColor = 'transparent'; 153 tbl.childNodes[i].className = 'live-search-result nav-live-search'; 154 } 155 //console.log(tbl) 156 console.log(tbl.childNodes[i]) 157 } 158 } 159 160 if(i == cnt && _this.currentRow != i) 161 _this.currentRow = first; 162 163 tbl.childNodes[_this.currentRow].style.backgroundColor = 'rgba(0,0,0,0.12)';
164 tbl.childNodes[_this.currentRow].style.boxShadow = 'none'; 165 tbl.childNodes[_this.currentRow].className = 'live-search-result nav-live-search title-search-selected'; 166 return true; 167 168 case 38: // up key - navigate up on search results 169 if(_this.RESULT.style.display == 'none') 170 _this.RESULT.style.display = 'block'; 171 172 var last = -1; 173 for(i = cnt-1; i >= 0; i--) 174 { 175 if(!BX.findChild(tbl.childNodes[i], {'class':'title-search-separator'}, true)) 176 { 177 if(last == -1) 178 last = i; 179 180 if(_this.currentRow > i) 181 { 182 _this.currentRow = i; 183 break; 184 } 185 else if(tbl.childNodes[i].className == 'title-search-selected') 186 { 187 tbl.childNodes[i].backgroundColor = 'transparent'; 188 189 tbl.childNodes[i].className = 'live-search-result nav-live-search'; 190 } 191 } 192 } 193 194 if(i < 0 && _this.currentRow != i) 195 _this.currentRow = last; 196 197 tbl.childNodes[_this.currentRow].style.backgroundColor = 'rgba(0,0,0,0.12)'; 198 tbl.childNodes[_this.currentRow].style.boxShadow = 'none'; 199 200 tbl.childNodes[_this.currentRow].className = 'live-search-result nav-live-search title-search-selected'; 201 return true; 202 203 case '13': // enter key - choose current search result 204 console.log('eneter') 205 if(_this.RESULT.style.display == 'block') 206 { 207 for(i = 0; i < cnt; i++) 208 { 209 if(_this.currentRow == i) 210 { 211 console.log(tbl.childNodes[i]) 212 if(!BX.findChild(tbl.childNodes[i], {'class':'title-search-separator'}, true)) 213 { 214 var a = BX.findChild(tbl.childNodes[i], {'tag':'a'}, true); 215 if(a) 216 { 217 window.location = a.href; 218 return true; 219 } 220 } 221 } 222 } 223 } 224 return false; 225 } 226 227 return false; 228 }; 229 230 this.onTimeout = function() 231 { 232 _this.onChange(function(){ 233 setTimeout(_this.onTimeout, 500); 234 }); 235 }; 236 237 this.onChange = function(callback) 238 { 239 if (_this.running) 240 return; 241 _this.running = true; 242 243 if(_this.INPUT.value != _this.oldValue && _this.INPUT.value != _this.startText) 244 { 245 _this.oldValue = _this.INPUT.value; 246 if(_this.INPUT.value.length >= _this.arParams.MIN_QUERY_LEN) 247 { 248 _this.cache_key = _this.arParams.INPUT_ID + '|' + _this.INPUT.value; 249 if(_this.cache[_this.cache_key] == null) 250 { 251 if(_this.WAIT) 252 { 253 var pos = BX.pos(_this.INPUT); 254 var height = (pos.bottom - pos.top)-2; 255 _this.WAIT.style.top = (pos.top+1) + 'px'; 256 _this.WAIT.style.height = height + 'px'; 257 _this.WAIT.style.width = height + 'px'; 258 _this.WAIT.style.left = (pos.right - height + 2) + 'px'; 259 _this.WAIT.style.display = 'block'; 260 } 261 262 BX.ajax.post( 263 _this.arParams.AJAX_PAGE, 264 { 265 'ajax_call':'y', 266 'INPUT_ID':_this.arParams.INPUT_ID, 267 'q':_this.INPUT.value, 268 'l':_this.arParams.MIN_QUERY_LEN 269 }, 270 function(result) 271 { 272 _this.cache[_this.cache_key] = result; 273 _this.ShowResult(result); 274 _this.currentRow = -1; 275 _this.EnableMouseEvents(); 276 if(_this.WAIT) 277 _this.WAIT.style.display = 'none'; 278 if (!!callback) 279 callback(); 280 _this.running = false; 281 } 282 ); 283 return; 284 } 285 else 286 { 287 _this.ShowResult(_this.cache[_this.cache_key]); 288 _this.currentRow = -1; 289 _this.EnableMouseEvents(); 290 } 291 } 292 else 293 { 294 _this.RESULT.style.display = 'none'; 295 _this.currentRow = -1; 296 _this.UnSelectAll(); 297 } 298 } 299 if (!!callback) 300 callback(); 301 _this.running = false; 302 }; 303 304 this.onScroll = function () 305 { 306 if(BX.type.isElementNode(_this.RESULT) 307 && _this.RESULT.style.display !== "none" 308 && _this.RESULT.innerHTML !== '' 309 ) 310 { 311 _this.adjustResultNode(); 312 } 313 }; 314 315 this.UnSelectAll = function() 316 { 317 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'nav-live-search'}, true); 318 if(tbl) 319 { 320 var cnt = tbl.rows.length; 321 for(var i = 0; i < cnt; i++) 322 tbl.rows[i].className = ''; 323 } 324 }; 325 326 this.EnableMouseEvents = function() 327 { 328 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'nav-live-search'}, true); 329 if(tbl) 330 { 331 var cnt = tbl.rows.length; 332 for(var i = 0; i < cnt; i++) 333 if(!BX.findChild(tbl.rows[i], {'class':'title-search-separator'}, true)) 334 { 335 tbl.rows[i].id = 'row_' + i; 336 tbl.rows[i].onmouseover = function (e) { 337 if(_this.currentRow != this.id.substr(4)) 338 { 339 _this.UnSelectAll(); 340 this.className = 'title-search-selected'; 341 _this.currentRow = this.id.substr(4); 342 } 343 }; 344 tbl.rows[i].onmouseout = function (e) { 345 this.className = ''; 346 _this.currentRow = -1; 347 }; 348 } 349 } 350 }; 351 352 this.onFocusLost = function(hide) 353 { 354 setTimeout(function(){_this.RESULT.style.display = 'none';}, 250); 355 }; 356 357 this.onFocusGain = function() 358 { 359 if(_this.RESULT.innerHTML.length) 360 _this.ShowResult(); 361 }; 362 363 this.onKeyDown = function(e) 364 { 365 if(!e) 366 e = window.event; 367 368 if (_this.RESULT.style.display == 'block') 369 { 370 if(_this.onKeyPress(e.keyCode)) 371 return BX.PreventDefault(e); 372 } 373 }; 374 375 this.adjustResultNode = function() 376 { 377 if(!(BX.type.isElementNode(_this.RESULT) 378 && BX.type.isElementNode(_this.CONTAINER)) 379 ) 380 { 381 return { top: 0, right: 0, bottom: 0, left: 0, width: 0, height: 0 }; 382 } 383 384 var pos = BX.pos(_this.CONTAINER); 385 386 _this.RESULT.style.position = 'absolute'; 387 _this.RESULT.style.top = (pos.bottom + 2) + 'px'; 388 _this.RESULT.style.left = pos.left + 'px'; 389 _this.RESULT.style.width = pos.width + 'px'; 390 391 return pos; 392 }; 393 394 this._onContainerLayoutChange = function() 395 { 396 if(BX.type.isElementNode(_this.RESULT) 397 && _this.RESULT.style.display !== "none" 398 && _this.RESULT.innerHTML !== '' 399 ) 400 { 401 _this.adjustResultNode(); 402 } 403 }; 404 this.Init = function() 405 { 406 this.CONTAINER = document.getElementById(this.arParams.CONTAINER_ID); 407 BX.addCustomEvent(this.CONTAINER, "OnNodeLayoutChange", this._onContainerLayoutChange); 408 409 //this.RESULT = document.body.appendChild(document.createElement("DIV")); 410 411 this.RESULT = this.CONTAINER.appendChild(document.createElement("DIV")); 412 413 this.RESULT.className = 'nav-live-search search-res'; 414 this.INPUT = document.getElementById(this.arParams.INPUT_ID); 415 this.startText = this.oldValue = this.INPUT.value; 416 BX.bind(this.INPUT, 'focus', function() {_this.onFocusGain()}); 417 BX.bind(this.INPUT, 'blur', function() {_this.onFocusLost()}); 418 this.INPUT.onkeydown = this.onKeyDown; 419 420 if(this.arParams.WAIT_IMAGE) 421 { 422 this.WAIT = document.body.appendChild(document.createElement("DIV")); 423 this.WAIT.style.backgroundImage = "url('" + this.arParams.WAIT_IMAGE + "')"; 424 if(!BX.browser.IsIE()) 425 this.WAIT.style.backgroundRepeat = 'none'; 426 this.WAIT.style.display = 'none'; 427 this.WAIT.style.position = 'absolute'; 428 this.WAIT.style.zIndex = '1100'; 429 } 430 431 BX.bind(this.INPUT, 'bxchange', function() {_this.onChange()}); 432 433 var fixedParent = BX.findParent(this.CONTAINER, BX.is_fixed); 434 if(BX.type.isElementNode(fixedParent)) 435 { 436 BX.bind(window, 'scroll', BX.throttle(this.onScroll, 100, this)); 437 } 438 }; 439 BX.ready(function (){_this.Init(arParams)}); 440}
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.