PageSourceSearch

https://kraskidoski.ru/bitrix/templates/aspro_max/components/bitrix/search.title/fixed/script.js

js kraskidoski.ru collected 2026-10-10 00:50:37 UTC 10,790 bytes, 432 lines download raw bytes

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.TYPE = null;
28	this.WAIT = null;
29
30	this.ShowResult = function(result)
31	{
32		if(BX.type.isString(result))
33		{
34			_this.RESULT.innerHTML = result;
35		}
36
37		_this.RESULT.style.display = _this.RESULT.innerHTML !== '' ? 'block' : 'none';
38		var pos = _this.adjustResultNode();
39
40		//adjust left column to be an outline
41		var res_pos;
42		var th;
43		var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'title-search-result'}, true);
44		if(tbl)
45		{
46			th = BX.findChild(tbl, {'tag':'th'}, true);
47		}
48		if(th)
49		{
50			var tbl_pos = BX.pos(tbl);
51			tbl_pos.width = tbl_pos.right - tbl_pos.left;
52
53			var th_pos = BX.pos(th);
54			th_pos.width = th_pos.right - th_pos.left;
55			th.style.width = th_pos.width + 'px';
56
57			_this.RESULT.style.width = (pos.width + th_pos.width) + 'px';
58
59			//Move table to left by width of the first column
60			_this.RESULT.style.left = (pos.left - th_pos.width - 1)+ 'px';
61
62			//Shrink table when it's too wide
63			if((tbl_pos.width - th_pos.width) > pos.width)
64				_this.RESULT.style.width = (pos.width + th_pos.width -1) + 'px';
65
66			//Check if table is too wide and shrink result div to it's width
67			tbl_pos = BX.pos(tbl);
68			res_pos = BX.pos(_this.RESULT);
69			if(res_pos.right > tbl_pos.right)
70			{
71				_this.RESULT.style.width = (tbl_pos.right - tbl_pos.left) + 'px';
72			}
73		}
74
75		var fade;
76		if(tbl) fade = BX.findChild(_this.RESULT, {'class':'title-search-fader'}, true);
77		if(fade && th)
78		{
79			res_pos = BX.pos(_this.RESULT);
80			fade.style.left = (res_pos.right - res_pos.left - 18) + 'px';
81			fade.style.width = 18 + 'px';
82			fade.style.top = 0 + 'px';
83			fade.style.height = (res_pos.bottom - res_pos.top) + 'px';
84			fade.style.display = 'block';
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_TMP,
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';}, 300);
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
345	this.adjustResultNode = function()
346	{
347		var pos, pos_input;
348		var fixedParent = BX.findParent(_this.CONTAINER, BX.is_fixed);
349		if(!!fixedParent)
350		{
351			_this.RESULT.style.position = 'fixed';
352			_this.RESULT.style.zIndex = BX.style(fixedParent, 'z-index') + 2;
353			pos = BX.pos(_this.CONTAINER, true);
354		}
355		else
356		{
357			_this.RESULT.style.position = 'absolute';
358			pos = BX.pos(_this.CONTAINER);
359		}
360		//console.log(pos);
361		//console.log(_this.CONTAINER);
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
368		if($(_this.INPUT).closest('.inline-search-block.with-close').length)
369			_this.RESULT.style.width = pos_input.width + 'px';
370		else
371			_this.RESULT.style.width = pos.width + 'px';
372		return pos;
373	};
374
375	this._onContainerLayoutChange = function()
376	{
377		if(_this.RESULT.style.display !== "none" && _this.RESULT.innerHTML !== '')
378		{
379			_this.adjustResultNode();
380		}
381	};
382	this.Init = function()
383	{
384		this.CONTAINER = document.getElementById(this.arParams.CONTAINER_ID);
385		BX.addCustomEvent(this.CONTAINER, "OnNodeLayoutChange", this._onContainerLayoutChange);
386
387		this.RESULT = document.body.appendChild(document.createElement("DIV"));
388
389		this.RESULT.className = 'title-search-result '+this.arParams.INPUT_ID;
390		if(this.arParams.INPUT_ID != 'title-search-input_fixed')
391			this.RESULT.className += ' '+arMaxOptions['THEME']['TYPE_SEARCH']+'_type';
392
393		this.INPUT = document.getElementById(this.arParams.INPUT_ID);
394		this.startText = this.oldValue = this.INPUT.value;
395
396		BX.bind(this.INPUT, 'focus', function() {_this.onFocusGain();});
397		BX.bind(this.INPUT, 'blur', function() {
398			if (window.matchMedia("(min-width: 993px)").matches) {
399				setTimeout(function(){
400					if(!$(_this.INPUT).closest('form').find('.dropdown-select.searchtype .dropdown-select__title.opened').length){
401						_this.onFocusLost();
402					}
403				}, 100);
404			} 
405		});
406
407		this.TYPE = this.CONTAINER ? this.CONTAINER.querySelector('.dropdown-select.searchtype input[name="type"]') : null;
408
409		if(BX.browser.IsSafari() || BX.browser.IsIE())
410			this.INPUT.onkeydown = this.onKeyDown;
411		else
412			this.INPUT.onkeypress = this.onKeyDown;
413
414		if(this.arParams.WAIT_IMAGE)
415		{
416			this.WAIT = document.body.appendChild(document.createElement("DIV"));
417			this.WAIT.style.backgroundImage = "url('" + this.arParams.WAIT_IMAGE + "')";
418			if(!BX.browser.IsIE())
419				this.WAIT.style.backgroundRepeat = 'none';
420			this.WAIT.style.display = 'none';
421			this.WAIT.style.position = 'absolute';
422			this.WAIT.style.zIndex = '1100';
423		}
424
425		BX.bind(this.INPUT, 'bxchange', function() {_this.onChange()});
426		
427		if (this.TYPE) {
428			BX.bind(this.TYPE, 'change', function() {_this.onChange()});
429		}
430	};
431	BX.ready(function (){_this.Init(arParams)});
432}

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.