PageSourceSearch

https://chelsosna.ru/bitrix/templates/simai.framework/components/bitrix/search.title/nav.search/script.js

js chelsosna.ru collected 2026-10-02 17:45:46 UTC 11,126 bytes, 444 lines download raw bytes

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':'nav-live-search'}, 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.CONTAINER, {'tag':'div','class':'search-res'}, true);
89		if(!tbl)
90			return false;
91
92		var i;
93		var cnt = tbl.childNodes.length;
94
95		//console.log(keyCode)
96		if(keyCode == '13') {
97
98			//if(_this.CONTAINER.style.display == 'block') {
99				for(i = 0; i < cnt; i++)
100				{
101					if(_this.currentRow == i)
102					{
103						//console.log(tbl.childNodes[i])
104						if(!BX.findChild(tbl.childNodes[i], {'class':'title-search-separator'}, true))
105						{
106							var a = BX.findChild(tbl.childNodes[i], {'tag':'a'}, true);
107							if(a)
108							{
109								window.location = a.href;
110								return true;
111							}
112						}
113					}
114				}
115			//}
116			return false;
117		}
118		/*var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'nav-live-search'}, true);
119		if(!tbl)
120			return false;
121
122		var i;
123		var cnt = tbl.rows.length;*/
124
125		switch (keyCode)
126		{
127		case 27: // escape key - close search div
128			_this.RESULT.style.display = 'none';
129			_this.currentRow = -1;
130			_this.UnSelectAll();
131		return true; 
132
133		case 40: // down key - navigate down on search results
134			if(_this.RESULT.style.display == 'none')
135				_this.RESULT.style.display = 'block';
136
137			var first = -1;
138			for(i = 0; i < cnt; i++)
139			{
140				if(!BX.findChild(tbl.childNodes[i], {'class':'title-search-separator'}, true))
141				{
142					if(first == -1)
143						first = i;
144
145					if(_this.currentRow < i)
146					{
147						_this.currentRow = i;
148						break;
149					}
150					else if(tbl.childNodes[i].className == 'title-search-selected')
151					{
152						tbl.childNodes[i].backgroundColor = 'transparent';
153						tbl.childNodes[i].className = 'live-search-result nav-live-search';
154					}
155					//console.log(tbl)
156				}
157			}
158
159			if(i == cnt && _this.currentRow != i)
160				_this.currentRow = first;
161
162			tbl.childNodes[_this.currentRow].style.backgroundColor = 'rgba(0,0,0,0.12)';
163			tbl.childNodes[_this.currentRow].style.boxShadow = 'none';
164			tbl.childNodes[_this.currentRow].className = 'live-search-result nav-live-search title-search-selected';
165		return true;
166
167		case 38: // up key - navigate up on search results
168			if(_this.RESULT.style.display == 'none')
169				_this.RESULT.style.display = 'block';
170
171			var last = -1;
172			for(i = cnt-1; i >= 0; i--)
173			{
174				if(!BX.findChild(tbl.childNodes[i], {'class':'title-search-separator'}, true))
175				{
176					if(last == -1)
177						last = i;
178
179					if(_this.currentRow > i)
180					{
181						_this.currentRow = i;
182						break;
183					}
184					else if(tbl.childNodes[i].className == 'title-search-selected')
185					{
186						tbl.childNodes[i].backgroundColor = 'transparent';
187
188						tbl.childNodes[i].className = 'live-search-result nav-live-search';
189					}
190				}
191			}
192
193			if(i < 0 && _this.currentRow != i)
194				_this.currentRow = last;
195
196			tbl.childNodes[_this.currentRow].style.backgroundColor = 'rgba(0,0,0,0.12)';
197			tbl.childNodes[_this.currentRow].style.boxShadow = 'none';
198
199			tbl.childNodes[_this.currentRow].className = 'live-search-result nav-live-search title-search-selected';
200		return true;
201
202		case '13': // enter key - choose current search result
203			if(_this.RESULT.style.display == 'block')
204			{
205				for(i = 0; i < cnt; i++)
206				{
207					if(_this.currentRow == i)
208					{
209						if(!BX.findChild(tbl.childNodes[i], {'class':'title-search-separator'}, true))
210						{
211							var a = BX.findChild(tbl.childNodes[i], {'tag':'a'}, true);
212							if(a)
213							{
214								window.location = a.href;
215								return true;
216							}
217						}
218					}
219				}
220			}
221		return false;
222		}
223
224		return false;
225	};
226
227	this.onTimeout = function()
228	{
229		_this.onChange(function(){
230			setTimeout(_this.onTimeout, 500);
231		});
232	};
233
234	this.onChange = function(callback)
235	{
236		if (_this.running)
237			return;
238		_this.running = true;
239
240		if(_this.INPUT.value != _this.oldValue && _this.INPUT.value != _this.startText)
241		{
242			_this.oldValue = _this.INPUT.value;
243			if(_this.INPUT.value.length >= _this.arParams.MIN_QUERY_LEN)
244			{
245				_this.cache_key = _this.arParams.INPUT_ID + '|' + _this.INPUT.value;
246				if(_this.cache[_this.cache_key] == null)
247				{
248					if(_this.WAIT)
249					{
250						var pos = BX.pos(_this.INPUT);
251						var height = (pos.bottom - pos.top)-2;
252						_this.WAIT.style.top = (pos.top+1) + 'px';
253						_this.WAIT.style.height = height + 'px';
254						_this.WAIT.style.width = height + 'px';
255						_this.WAIT.style.left = (pos.right - height + 2) + 'px';
256						_this.WAIT.style.display = 'block';
257					}
258
259					BX.ajax.post(
260						_this.arParams.AJAX_PAGE,
261						{
262							'ajax_call':'y',
263							'INPUT_ID':_this.arParams.INPUT_ID,
264							'q':_this.INPUT.value,
265							'l':_this.arParams.MIN_QUERY_LEN
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					);
280					return;
281				}
282				else
283				{
284					_this.ShowResult(_this.cache[_this.cache_key]);
285					_this.currentRow = -1;
286					_this.EnableMouseEvents();
287				}
288			}
289			else
290			{
291				_this.RESULT.style.display = 'none';
292				_this.currentRow = -1;
293				_this.UnSelectAll();
294			}
295		}
296		if (!!callback)
297			callback();
298		_this.running = false;
299	};
300
301	this.onScroll = function ()
302	{
303		if(BX.type.isElementNode(_this.RESULT)
304			&& _this.RESULT.style.display !== "none"
305			&& _this.RESULT.innerHTML !== ''
306		)
307		{
308			_this.adjustResultNode();
309		}
310	};
311
312	this.UnSelectAll = function()
313	{
314		var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'nav-live-search'}, true);
315		if(tbl)
316		{
317			var cnt = tbl.rows.length;
318			for(var i = 0; i < cnt; i++)
319				tbl.rows[i].className = '';
320		}
321	};
322
323	this.EnableMouseEvents = function()
324	{
325		var tbl = BX.findChild(_this.RESULT, {'tag':'table','class':'nav-live-search'}, true);
326		if(tbl)
327		{
328			var cnt = tbl.rows.length;
329			for(var i = 0; i < cnt; i++)
330				if(!BX.findChild(tbl.rows[i], {'class':'title-search-separator'}, true))
331				{
332					tbl.rows[i].id = 'row_' + i;
333					tbl.rows[i].onmouseover = function (e) {
334						if(_this.currentRow != this.id.substr(4))
335						{
336							_this.UnSelectAll();
337							this.className = 'title-search-selected';
338							_this.currentRow = this.id.substr(4);
339						}
340					};
341					tbl.rows[i].onmouseout = function (e) {
342						this.className = '';
343						_this.currentRow = -1;
344					};
345				}
346		}
347	};
348
349	this.onFocusLost = function(hide)
350	{
351		setTimeout(function(){_this.RESULT.style.display = 'none';}, 250);
352	};
353
354	this.onFocusGain = function()
355	{
356		if(_this.RESULT.innerHTML.length)
357			_this.ShowResult();
358	};
359
360	this.onKeyDown = function(e)
361	{
362		if(!e)
363			e = window.event;
364
365		if (_this.RESULT.style.display == 'block')
366		{
367			if(_this.onKeyPress(e.keyCode))
368			{
369				return BX.PreventDefault(e);
370			}
371				
372		}
373	};
374
375	this.adjustResultNode = function()
376	{
377		if(!(BX.type.isElementNode(_this.RESULT)
378			&& BX.type.isElementNode(_this.CONTAINER))
379		)
380		{
381			return { top: 0, right: 0, bottom: 0, left: 0, width: 0, height: 0 };
382		}
383
384		var pos = BX.pos(_this.CONTAINER);
385
386		_this.RESULT.style.position = 'absolute';
387		_this.RESULT.style.top = (pos.bottom + 2) + 'px';
388		_this.RESULT.style.left = pos.left + 'px';
389		if(pos.width)
390			_this.RESULT.style.width = pos.width + 'px';
391		else
392			this.RESULT.style.width = '1290px';
393
394		return pos;
395	};
396
397	this._onContainerLayoutChange = function()
398	{
399		if(BX.type.isElementNode(_this.RESULT)
400			&& _this.RESULT.style.display !== "none"
401			&& _this.RESULT.innerHTML !== ''
402		)
403		{
404			_this.adjustResultNode();
405		}
406	};
407	this.Init = function()
408	{
409		this.CONTAINER = document.getElementById(this.arParams.CONTAINER_ID);
410		BX.addCustomEvent(this.CONTAINER, "OnNodeLayoutChange", this._onContainerLayoutChange);
411
412		//this.RESULT = document.body.appendChild(document.createElement("DIV"));
413
414		this.RESULT = this.CONTAINER.appendChild(document.createElement("DIV"));
415
416		this.RESULT.className = 'nav-live-search search-res';
417		this.INPUT = document.getElementById(this.arParams.INPUT_ID);
418		this.startText = this.oldValue = this.INPUT.value;
419		//BX.bind(this.INPUT, 'focus', function() {_this.onFocusGain()});
420		//BX.bind(this.INPUT, 'blur', function() {_this.onFocusLost()});
421		this.INPUT.onkeydown = this.onKeyDown;
422
423		
424		if(this.arParams.WAIT_IMAGE)
425		{
426			this.WAIT = document.body.appendChild(document.createElement("DIV"));
427			this.WAIT.style.backgroundImage = "url('" + this.arParams.WAIT_IMAGE + "')";
428			if(!BX.browser.IsIE())
429				this.WAIT.style.backgroundRepeat = 'none';
430			this.WAIT.style.display = 'none';
431			this.WAIT.style.position = 'absolute';
432			this.WAIT.style.zIndex = '1100';
433		}
434
435		BX.bind(this.INPUT, 'bxchange', function() {_this.onChange()});
436
437		var fixedParent = BX.findParent(this.CONTAINER, BX.is_fixed);
438		if(BX.type.isElementNode(fixedParent))
439		{
440			BX.bind(window, 'scroll', BX.throttle(this.onScroll, 100, this));
441		}
442	};
443	BX.ready(function (){_this.Init(arParams)});
444}

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.