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