PageSourceSearch

https://bars.su/bitrix/components/bitrix/search.title/script.js

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

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.