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