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