PageSourceSearch

https://cdn.krepauto.ru/bitrix/templates/aspro-lite/components/arturgolubev/search.title/corp/script.js

js krepauto.ru collected 2026-10-07 15:27:53 UTC 11,473 bytes, 460 lines download raw bytes

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

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.