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