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.UnSelectAll = function() 263 { 264 var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'title-search-result'}, true); 265 if(tbl) 266 { 267 var cnt = tbl.rows.length; 268 for(var i = 0; i < cnt; i++) 269 tbl.rows[i].className = ''; 270 } 271 }; 272 273 this.EnableMouseEvents = 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 if(!BX.findChild(tbl.rows[i], {'class':'title-search-separator'}, true)) 281 { 282 tbl.rows[i].id = 'row_' + i; 283 tbl.rows[i].onmouseover = function (e) { 284 if(_this.currentRow != this.id.substr(4)) 285 { 286 _this.UnSelectAll(); 287 this.className = 'title-search-selected'; 288 _this.currentRow = this.id.substr(4); 289 } 290 }; 291 tbl.rows[i].onmouseout = function (e) { 292 this.className = ''; 293 _this.currentRow = -1; 294 }; 295 } 296 } 297 }; 298 299 this.onFocusLost = function(hide) 300 { 301 setTimeout(function(){_this.RESULT.style.display = 'none';}, 250); 302 }; 303 304 this.onFocusGain = function() 305 { 306 if(_this.RESULT.innerHTML.length) 307 _this.ShowResult(); 308 }; 309 310 this.onKeyDown = function(e) 311 { 312 if(!e) 313 e = window.event; 314 315 if (_this.RESULT.style.display == 'block') 316 { 317 if(_this.onKeyPress(e.keyCode)) 318 return BX.PreventDefault(e); 319 } 320 }; 321 322 this.adjustResultNode = function() 323 { 324 var pos; 325 var fixedParent = BX.findParent(_this.CONTAINER, BX.is_fixed); 326 if(!!fixedParent) 327 { 328 _this.RESULT.style.position = 'fixed'; 329 _this.RESULT.style.zIndex = BX.style(fixedParent, 'z-index') + 2; 330 pos = BX.pos(_this.CONTAINER, true); 331 } 332 else 333 { 334 _this.RESULT.style.position = 'absolute'; 335 pos = BX.pos(_this.CONTAINER); 336 } 337 pos.width = pos.right - pos.left; 338 _this.RESULT.style.top = (pos.bottom + 2) + 'px'; 339 _this.RESULT.style.left = pos.left + 'px'; 340 _this.RESULT.style.width = pos.width + 'px'; 341 return pos; 342 }; 343 344 this._onContainerLayoutChange = function() 345 { 346 if(_this.RESULT.style.display !== "none" && _this.RESULT.innerHTML !== '') 347 { 348 _this.adjustResultNode(); 349 } 350 }; 351 this.Init = function() 352 { 353 this.CONTAINER = document.getElementById(this.arParams.CONTAINER_ID); 354 BX.addCustomEvent(this.CONTAINER, "OnNodeLayoutChange", this._onContainerLayoutChange); 355 356 this.RESULT = document.body.appendChild(document.createElement("DIV")); 357 this.RESULT.className = 'title-search-result'; 358 this.INPUT = document.getElementById(this.arParams.INPUT_ID); 359 this.startText = this.oldValue = this.INPUT.value; 360 BX.bind(this.INPUT, 'focus', function() {_this.onFocusGain()}); 361 BX.bind(this.INPUT, 'blur', function() {_this.onFocusLost()}); 362 363 if(BX.browser.IsSafari() || BX.browser.IsIE()) 364 this.INPUT.onkeydown = this.onKeyDown; 365 else 366 this.INPUT.onkeypress = this.onKeyDown; 367 368 if(this.arParams.WAIT_IMAGE) 369 { 370 this.WAIT = document.body.appendChild(document.createElement("DIV")); 371 this.WAIT.style.backgroundImage = "url('" + this.arParams.WAIT_IMAGE + "')"; 372 if(!BX.browser.IsIE()) 373 this.WAIT.style.backgroundRepeat = 'none'; 374 this.WAIT.style.display = 'none'; 375 this.WAIT.style.position = 'absolute'; 376 this.WAIT.style.zIndex = '1100'; 377 } 378 379 BX.bind(this.INPUT, 'bxchange', function() {_this.onChange()}); 380 }; 381 BX.ready(function (){_this.Init(arParams)}); 382}
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.