1function JCTitleSearch3(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.needRunAgain = false; 22 this.currentRow = -1; 23 this.RESULT = null; 24 this.CONTAINER = null; 25 this.INPUT = null; 26 this.TYPE = 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 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 let type = 'catalog'; 206 if (this.TYPE) { 207 type = this.TYPE.value; 208 } 209 210 if( 211 ( 212 _this.INPUT.value != _this.oldValue 213 && _this.INPUT.value != _this.startText 214 ) 215 || type != _this.oldType 216 ){ 217 _this.oldValue = _this.INPUT.value; 218 _this.oldType = type; 219 220 if(_this.INPUT.value.length >= _this.arParams.MIN_QUERY_LEN) 221 { 222 _this.cache_key = _this.arParams.INPUT_ID + '|' + _this.INPUT.value + '|' + type; 223 if(_this.cache[_this.cache_key] == null) 224 { 225 if(_this.WAIT) 226 { 227 var pos = BX.pos(_this.INPUT); 228 var height = (pos.bottom - pos.top)-2; 229 _this.WAIT.style.top = (pos.top+1) + 'px'; 230 _this.WAIT.style.height = height + 'px'; 231 _this.WAIT.style.width = height + 'px'; 232 _this.WAIT.style.left = (pos.right - height + 2) + 'px'; 233 _this.WAIT.style.display = 'block'; 234 } 235 236 BX.ajax.post( 237 _this.arParams.AJAX_PAGE, 238 { 239 'ajax_call':'y', 240 'INPUT_ID':_this.arParams.INPUT_ID, 241 'q':_this.INPUT.value, 242 'l':_this.arParams.MIN_QUERY_LEN, 243 'type': type, 244 }, 245 function(result) 246 { 247 _this.cache[_this.cache_key] = result; 248 _this.ShowResult(result); 249 _this.currentRow = -1; 250 _this.EnableMouseEvents(); 251 if(_this.WAIT) 252 _this.WAIT.style.display = 'none'; 253 if (!!callback) 254 callback(); 255 _this.running = false; 256 257 if(_this.needRunAgain){ 258 _this.needRunAgain = false; 259 _this.onChange(callback); 260 } 261 } 262 ); 263 return; 264 } 265 else 266 { 267 _this.ShowResult(_this.cache[_this.cache_key]); 268 _this.currentRow = -1; 269 _this.EnableMouseEvents(); 270 } 271 } 272 else 273 { 274 _this.RESULT.style.display = 'none'; 275 _this.currentRow = -1; 276 _this.UnSelectAll(); 277 } 278 } 279 if (!!callback) 280 callback(); 281 _this.running = false; 282 }; 283 284 this.UnSelectAll = function() 285 { 286 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'title-search-result'}, true); 287 if(tbl) 288 { 289 var cnt = tbl.rows.length; 290 for(var i = 0; i < cnt; i++) 291 tbl.rows[i].className = ''; 292 } 293 }; 294 295 this.EnableMouseEvents = function() 296 { 297 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'title-search-result'}, true); 298 if(tbl) 299 { 300 var cnt = tbl.rows.length; 301 for(var i = 0; i < cnt; i++) 302 if(!BX.findChild(tbl.rows[i], {'class':'title-search-separator'}, true)) 303 { 304 tbl.rows[i].id = 'row_' + i; 305 tbl.rows[i].onmouseover = function (e) { 306 if(_this.currentRow != this.id.substr(4)) 307 { 308 _this.UnSelectAll(); 309 this.className = 'title-search-selected'; 310 _this.currentRow = this.id.substr(4); 311 } 312 }; 313 tbl.rows[i].onmouseout = function (e) { 314 this.className = ''; 315 _this.currentRow = -1; 316 }; 317 } 318 } 319 }; 320 321 this.onFocusLost = function(hide) 322 { 323 setTimeout(function(){_this.RESULT.style.display = 'none';}, 250); 324 //_this.RESULT.style.display = 'none'; 325 }; 326 327 this.onFocusGain = function() 328 { 329 if(_this.RESULT.innerHTML.length) 330 _this.ShowResult(); 331 }; 332 333 this.onKeyDown = function(e) 334 { 335 if(!e) 336 e = window.event; 337 338 if (_this.RESULT.style.display == 'block') 339 { 340 if(_this.onKeyPress(e.keyCode)) 341 return BX.PreventDefault(e); 342 } 343 344 $('.title-search-result.'+_this.arParams.INPUT_ID).removeClass('closed'); 345 }; 346 347 this.adjustResultNode = function() 348 { 349 var pos, pos_input; 350 var fixedParent = BX.findParent(_this.CONTAINER, BX.is_fixed); 351 /*if(!!fixedParent) 352 { 353 _this.RESULT.style.position = 'fixed'; 354 _this.RESULT.style.zIndex = BX.style(fixedParent, 'z-index') + 2; 355 pos = BX.pos(_this.CONTAINER, true); 356 } 357 else 358 {*/ 359 _this.RESULT.style.position = 'absolute'; 360 pos = BX.pos(_this.CONTAINER, true); 361 //} 362 pos_input = BX.pos(_this.INPUT); 363 364 pos.width = pos.right - pos.left; 365 _this.RESULT.style.top = (pos.bottom + 2) + 'px'; 366 //_this.RESULT.style.left = pos_input.left + 'px'; 367 _this.RESULT.style.left = 0 + 'px'; 368 369 if($(_this.INPUT).closest('.inline-search-block.with-close').length) 370 _this.RESULT.style.width = pos_input.width + 'px'; 371 else 372 _this.RESULT.style.width = pos.width + 'px'; 373 return pos; 374 }; 375 376 this._onContainerLayoutChange = function() 377 { 378 if(_this.RESULT.style.display !== "none" && _this.RESULT.innerHTML !== '') 379 { 380 _this.adjustResultNode(); 381 } 382 }; 383 this.Init = function() 384 { 385 this.CONTAINER = document.getElementById(this.arParams.CONTAINER_ID); 386 BX.addCustomEvent(this.CONTAINER, "OnNodeLayoutChange", this._onContainerLayoutChange); 387 388 this.RESULT = this.CONTAINER.appendChild(document.createElement("DIV")); 389 390 this.RESULT.className = 'title-search-result '+this.arParams.INPUT_ID; 391 this.INPUT = document.getElementById(this.arParams.INPUT_ID); 392 this.startText = this.oldValue = this.INPUT.value; 393 BX.bind(this.INPUT, 'focus', function() {_this.onFocusGain()}); 394 BX.bind(this.INPUT, 'blur', function() { 395 if (window.matchMedia("(min-width: 993px)").matches) { 396 setTimeout(function(){ 397 if(!$(_this.INPUT).closest('form').find('.dropdown-select.searchtype .dropdown-select__title.opened').length){ 398 _this.onFocusLost(); 399 } 400 }, 100); 401 } 402 }); 403 404 this.TYPE = this.CONTAINER ? this.CONTAINER.querySelector('.dropdown-select.searchtype input[name="type"]') : null; 405 406 if(BX.browser.IsSafari() || BX.browser.IsIE()) 407 this.INPUT.onkeydown = this.onKeyDown; 408 else 409 this.INPUT.onkeypress = this.onKeyDown; 410 411 if(this.arParams.WAIT_IMAGE) 412 { 413 this.WAIT = document.body.appendChild(document.createElement("DIV")); 414 this.WAIT.style.backgroundImage = "url('" + this.arParams.WAIT_IMAGE + "')"; 415 if(!BX.browser.IsIE()) 416 this.WAIT.style.backgroundRepeat = 'none'; 417 this.WAIT.style.display = 'none'; 418 this.WAIT.style.position = 'absolute'; 419 this.WAIT.style.zIndex = '1100'; 420 } 421 422 BX.bind(this.INPUT, 'bxchange', function() {_this.onChange()}); 423
424 if (this.TYPE) { 425 BX.bind(this.TYPE, 'change', function() {_this.onChange()}); 426 } 427 }; 428 BX.ready(function (){_this.Init(arParams)}); 429}
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.