PageSourceSearch

https://rarus.ru/local/templates/rarus.light/components/bitrix/sea…/script.js?175518719713573

js rarus.ru collected 2026-09-24 09:34:56 UTC 13,573 bytes, 365 lines download raw bytes

1$(document).ready(function () {
2    //Удаляем лишние пробелы
3    $('.header-search__input').keydown(function () {
4        if ($(this).val().indexOf('  ') > -1) {
5            $(this).val($(this).val().replace('  ', ' '));
6        }
7    });
8
9    //скрытие предикативного поиска при клике за его пределом
10    $(document).click(function (event) {
11        if ($(event.target).closest(".title-search-result").length) {
12            return;
13        }
14        $(".title-search-result").fadeOut("slow");
15        event.stopPropagation();
16    });
17});
18
19// Перенесен скрипт из шаблона компонента в папке /bitrix/ для того, чтобы внести правки: предикативный поиск
20// скрывался при фокусе
21function JCTitleSearch(arParams) {
22    var _this = this;
23
24    this.arParams = {
25        'AJAX_PAGE': arParams.AJAX_PAGE,
26        'CONTAINER_ID': arParams.CONTAINER_ID,
27        'INPUT_ID': arParams.INPUT_ID,
28        'MIN_QUERY_LEN': parseInt(arParams.MIN_QUERY_LEN)
29    };
30    if (arParams.WAIT_IMAGE)
31        this.arParams.WAIT_IMAGE = arParams.WAIT_IMAGE;
32    if (arParams.MIN_QUERY_LEN <= 0)
33        arParams.MIN_QUERY_LEN = 1;
34
35    this.cache = [];
36    this.cache_key = null;
37
38    this.startText = '';
39    this.running = false;
40    this.currentRow = -1;
41    this.RESULT = null;
42    this.CONTAINER = null;
43    this.INPUT = null;
44    this.WAIT = null;
45
46    this.ShowResult = function (result) {
47        if (BX.type.isString(result)) {
48            _this.RESULT.innerHTML = result;
49        }
50
51        _this.RESULT.style.display = _this.RESULT.innerHTML !== '' ? 'block' : 'none';
52        var result = _this.adjustResultNode();
53
54        if (!result) {
55            _this.RESULT.style.display = 'none';
56            return;
57        }
58
59        //adjust left column to be an outline
60        var res_pos;
61        var th;
62        var tbl = BX.findChild(_this.RESULT, {'tag': 'table', 'class': 'title-search-result'}, true);
63        if (tbl) {
64            th = BX.findChild(tbl, {'tag': 'th'}, true);
65        }
66
67        if (th) {
68            var tbl_pos = BX.pos(tbl);
69            tbl_pos.width = tbl_pos.right - tbl_pos.left;
70
71            var th_pos = BX.pos(th);
72            th_pos.width = th_pos.right - th_pos.left;
73            th.style.width = th_pos.width + 'px';
74
75            _this.RESULT.style.width = (pos.width + th_pos.width) + 'px';
76
77            //Move table to left by width of the first column
78            _this.RESULT.style.left = (pos.left - th_pos.width - 1) + 'px';
79
80            //Shrink table when it's too wide
81            if ((tbl_pos.width - th_pos.width) > pos.width)
82                _this.RESULT.style.width = (pos.width + th_pos.width - 1) + 'px';
83
84            //Check if table is too wide and shrink result div to it's width
85            tbl_pos = BX.pos(tbl);
86            res_pos = BX.pos(_this.RESULT);
87            if (res_pos.right > tbl_pos.right) {
88                _this.RESULT.style.width = (tbl_pos.right - tbl_pos.left) + 'px';
89            }
90        }
91
92        var fade;
93        if (tbl) fade = BX.findChild(_this.RESULT, {'class': 'title-search-fader'}, true);
94        if (fade && th) {
95            res_pos = BX.pos(_this.RESULT);
96            fade.style.left = (res_pos.right - res_pos.left - 18) + 'px';
97            fade.style.width = 18 + 'px';
98            fade.style.top = 0 + 'px';
99            fade.style.height = (res_pos.bottom - res_pos.top) + 'px';
100            fade.style.display = 'block';
101        }
102    };
103
104    this.onKeyPress = function (keyCode) {
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            case 27: // escape key - close search div
114                _this.RESULT.style.display = 'none';
115                _this.currentRow = -1;
116                _this.UnSelectAll();
117                return true;
118
119            case 40: // down key - navigate down on search results
120                if (_this.RESULT.style.display == 'none')
121                    _this.RESULT.style.display = 'block';
122
123                var first = -1;
124                for (i = 0; i < cnt; i++) {
125                    if (!BX.findChild(tbl.rows[i], {'class': 'title-search-separator'}, true)) {
126                        if (first == -1)
127                            first = i;
128
129                        if (_this.currentRow < i) {
130                            _this.currentRow = i;
131                            break;
132                        } else if (tbl.rows[i].className == 'title-search-selected') {
133                            tbl.rows[i].className = '';
134                        }
135                    }
136                }
137
138                if (i == cnt && _this.currentRow != i)
139                    _this.currentRow = first;
140
141                tbl.rows[_this.currentRow].className = 'title-search-selected';
142                return true;
143
144            case 38: // up key - navigate up on search results
145                if (_this.RESULT.style.display == 'none')
146                    _this.RESULT.style.display = 'block';
147
148                var last = -1;
149                for (i = cnt - 1; i >= 0; i--) {
150                    if (!BX.findChild(tbl.rows[i], {'class': 'title-search-separator'}, true)) {
151                        if (last == -1)
152                            last = i;
153
154                        if (_this.currentRow > i) {
155                            _this.currentRow = i;
156                            break;
157                        } else if (tbl.rows[i].className == 'title-search-selected') {
158                            tbl.rows[i].className = '';
159                        }
160                    }
161                }
162
163                if (i < 0 && _this.currentRow != i)
164                    _this.currentRow = last;
165
166                tbl.rows[_this.currentRow].className = 'title-search-selected';
167                return true;
168
169            case 13: // enter key - choose current search result
170                if (_this.RESULT.style.display == 'block') {
171                    for (i = 0; i < cnt; i++) {
172                        if (_this.currentRow == i) {
173                            if (!BX.findChild(tbl.rows[i], {'class': 'title-search-separator'}, true)) {
174                                var a = BX.findChild(tbl.rows[i], {'tag': 'a'}, true);
175                                if (a) {
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        _this.onChange(function () {
191            setTimeout(_this.onTimeout, 500);
192        });
193    };
194
195    this.onChange = function (callback) {
196        if (_this.running) {
197            return;
198        }
199        _this.running = true;
200
201        if (_this.INPUT.value !== _this.oldValue && _this.INPUT.value !== _this.startText) {
202            _this.oldValue = _this.INPUT.value;
203            if (_this.INPUT.value.length >= _this.arParams.MIN_QUERY_LEN) {
204                _this.cache_key = _this.arParams.INPUT_ID + '|' + _this.INPUT.value;
205                if (_this.cache[_this.cache_key] == null) {
206                    if (_this.WAIT) {
207                        var pos = BX.pos(_this.INPUT);
208                        var height = (pos.bottom - pos.top) - 2;
209                        _this.WAIT.style.top = (pos.top + 1) + 'px';
210                        _this.WAIT.style.height = height + 'px';
211                        _this.WAIT.style.width = height + 'px';
212                        _this.WAIT.style.left = (pos.right - height + 2) + 'px';
213                        _this.WAIT.style.display = 'block';
214                    }
215
216                    BX.ajax.get(
217                        _this.arParams.AJAX_PAGE,
218                        {
219                            'ajax_call': 'y',
220                            'INPUT_ID': _this.arParams.INPUT_ID,
221                            'q': _this.INPUT.value,
222                            'l': _this.arParams.MIN_QUERY_LEN
223                        },
224                        function (result) {
225                            _this.cache[_this.cache_key] = result;
226                            _this.ShowResult(result);
227                            _this.currentRow = -1;
228                            _this.EnableMouseEvents();
229                            if (_this.WAIT)
230                                _this.WAIT.style.display = 'none';
231                            if (!!callback) {
232                                callback();
233                            }
234                            _this.running = false;
235                        }
236                    );
237                    return;
238                } else {
239                    _this.ShowResult(_this.cache[_this.cache_key]);
240                    _this.currentRow = -1;
241                    _this.EnableMouseEvents();
242                }
243            } else {
244                _this.RESULT.style.display = 'none';
245                _this.currentRow = -1;
246                _this.UnSelectAll();
247            }
248        }
249        if (!!callback)
250            callback();
251        _this.running = false;
252    };
253
254    this.UnSelectAll = function () {
255        var tbl = BX.findChild(_this.RESULT, {'tag': 'table', 'class': 'title-search-result'}, true);
256        if (tbl) {
257            var cnt = tbl.rows.length;
258            for (var i = 0; i < cnt; i++)
259                tbl.rows[i].className = '';
260        }
261    };
262
263    this.EnableMouseEvents = function () {
264        var tbl = BX.findChild(_this.RESULT, {'tag': 'table', 'class': 'title-search-result'}, true);
265        if (tbl) {
266            var cnt = tbl.rows.length;
267            for (var i = 0; i < cnt; i++)
268                if (!BX.findChild(tbl.rows[i], {'class': 'title-search-separator'}, true)) {
269                    tbl.rows[i].id = 'row_' + i;
270                    tbl.rows[i].onmouseover = function (e) {
271                        if (_this.currentRow != this.id.substr(4)) {
272                            _this.UnSelectAll();
273                            this.className = 'title-search-selected';
274                            _this.currentRow = this.id.substr(4);
275                        }
276                    };
277                    tbl.rows[i].onmouseout = function (e) {
278                        this.className = '';
279                        _this.currentRow = -1;
280                    };
281                }
282        }
283    };
284
285    this.onFocusLost = function (hide) {
286        setTimeout(function () {
287            _this.RESULT.style.display = 'none';
288        }, 250);
289    };
290
291    this.onFocusGain = function () {
292        if (_this.RESULT.innerHTML.length)
293            _this.ShowResult();
294    };
295
296    this.onKeyDown = function (e) {
297        if (!e)
298            e = window.event;
299
300        if (_this.RESULT.style.display == 'block') {
301            if (_this.onKeyPress(e.keyCode))
302                return BX.PreventDefault(e);
303        }
304    };
305
306    this.adjustResultNode = function () {
307        var pos;
308        var fixedParent = BX.findParent(_this.CONTAINER, BX.is_fixed);
309        if (!!fixedParent) {
310            _this.RESULT.style.position = 'fixed';
311            _this.RESULT.style.zIndex = BX.style(fixedParent, 'z-index') + 2;
312            pos = BX.pos(_this.CONTAINER, true);
313        } else {
314            _this.RESULT.style.position = 'absolute';
315            pos = BX.pos(_this.CONTAINER);
316        }
317        if (pos.width === 0) {
318            return false;
319        }
320        pos.width = pos.right - pos.left;
321        _this.RESULT.style.top = (pos.bottom + 2) + 'px';
322        _this.RESULT.style.left = pos.left + 'px';
323        _this.RESULT.style.width = pos.width + 'px';
324        return pos;
325    };
326
327    this._onContainerLayoutChange = function () {
328        if (_this.RESULT.style.display !== "none" && _this.RESULT.innerHTML !== '') {
329            _this.adjustResultNode();
330        }
331    };
332    this.Init = function () {
333        this.CONTAINER = document.getElementById(this.arParams.CONTAINER_ID);
334        BX.addCustomEvent(this.CONTAINER, "OnNodeLayoutChange", this._onContainerLayoutChange);
335
336        this.RESULT = document.body.appendChild(document.createElement("DIV"));
337        this.RESULT.className = 'title-search-result';
338        this.INPUT = document.getElementById(this.arParams.INPUT_ID);
339        this.startText = this.oldValue = this.INPUT.value;
340        //BX.bind(this.INPUT, 'focus', function() {_this.onFocusGain()});
341        //BX.bind(this.INPUT, 'blur', function() {_this.onFocusLost()});
342
343        if (BX.browser.IsSafari() || BX.browser.IsIE())
344            this.INPUT.onkeydown = this.onKeyDown;
345        else
346            this.INPUT.onkeypress = this.onKeyDown;
347
348        if (this.arParams.WAIT_IMAGE) {
349            this.WAIT = document.body.appendChild(document.createElement("DIV"));
350            this.WAIT.style.backgroundImage = "url('" + this.arParams.WAIT_IMAGE + "')";
351            if (!BX.browser.IsIE())
352                this.WAIT.style.backgroundRepeat = 'none';
353            this.WAIT.style.display = 'none';
354            this.WAIT.style.position = 'absolute';
355            this.WAIT.style.zIndex = '1100';
356        }
357
358        BX.bind(this.INPUT, 'bxchange', function () {
359            _this.onChange()
360        });
361    };
362    BX.ready(function () {
363        _this.Init(arParams);
364    });
365}

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.