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