PageSourceSearch

https://erc.ua/bundles/vidoerc/3/js/classes/ajaxSearch.js

js erc.ua collected 2026-10-03 01:08:33 UTC 8,268 bytes, 193 lines download raw bytes

1// TODO_4562: зачистити класс public/3/classes/ajaxSearch.js
2var AjaxSearch = {
3    search_string_len_tmp: 0,
4    search_timer: null,
5    doNotShowAjaxResults: false,
6    cachedAjaxResultKey: false,
7    is_on_search_result: false,
8
9    init: function () {
10        $("#search_input").on("keyup", function (e) {
11            AjaxSearch.doNotShowAjaxResults = false;
12            return AjaxSearch.search(e, $(this));
13        });
14        $("#search_input").on("blur", function () {
15            return AjaxSearch.disableSearch();
16        });
17        $("#search_input").on("focus", function () {
18            return AjaxSearch.showCachedResults();
19        });
20
21        $("#search_result_container, #search_input").hover(function () {
22            AjaxSearch.is_on_search_result = true;
23        }, function () {
24            AjaxSearch.is_on_search_result = false;
25        });
26
27        $("body").click(function (e) {
28            if (!AjaxSearch.is_on_search_result) {
29                AjaxSearch.hideResultsContainer();
30            }
31        });
32    },
33
34    isAjaxSearchDisable: function () {
35        return $("#search_input_only_current_category").val() > 0;
36    },
37
38    search: function (e, obj) {
39
40        if (AjaxSearch.isAjaxSearchDisable()) {
41            return false;
42        }
43        var input = obj;
44        var container = $("#search_result_container");
45        // ------------------
46        // TODO_4562: прибрати код для UP / DOWN (перейшов з vido.com.ua) АБО реалізувати його для ERC
47        var res_selected_cnt = 0; // патч для вимкнення помилки
48
49        // var res_cnt = $("li", container).size();
50        // var res_selected_cnt = $("li.selected", container).size();
51        // var res_selected_index = $("li.selected", container).index();
52        // -------------------------------------------------------------------
53        var search_string_len = input.val().length;
54        AjaxSearch.log('search', input.val());
55
56        var is_search_change = false;
57        if (AjaxSearch.search_string_len_tmp !== search_string_len) {
58            is_search_change = true;
59        }
60        AjaxSearch.search_string_len_tmp = search_string_len;
61
62        if ((e.keyCode >= 48 && e.keyCode <= 90) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode == 32 || e.keyCode == 46 || e.keyCode == 8 || (e.keyCode == 40 && !container.is(":visible")) || (e.keyCode == 17)) {
63            AjaxSearch.log('search', search_string_len, is_search_change);
64            if (search_string_len >= 2) {
65                if (is_search_change) {
66                    clearTimeout(AjaxSearch.search_timer);
67                    AjaxSearch.search_timer = setTimeout(function () {
68                        var data = [];
69                        data.push({name: 'q', value: input.val()});
70                        data.push({name: 'search_type', value: $("#search_type").val()});
71
72                        var searchToken;
73                        if ($('#search-token').length > 0) {
74                            data.push({name: 'search_token', value: $('#search-token').val()});
75                        }
76
77                        if (($("#search_input_only_current_category").val() > 0)) {
78                            data.push({name: 'only_current_category', value: 1});
79                            data.push({
80                                name: 'current_category_id',
81                                value: $("#search_input_only_current_category").val() > 0
82                            });
83                        }
84                        $.ajax({
85                            url: "/ajax/search-auto-complete/",
86                            type: "post",
87                            dataType: "json",
88                            data: data,
89                            beforeSend: function () {
90                                AjaxSearch.showLoading();
91                            },
92                            success: function (data) {
93                                AjaxSearch.hideLoading();
94                                AjaxSearch.cachedAjaxResultKey = input.val();
95                                container.html(data.content)
96                                if (!AjaxSearch.isAjaxSearchDisable() && !AjaxSearch.doNotShowAjaxResults) {
97                                    container.show();
98                                }
99                            }
100                        });
101                    }, 250);
102                } else if (e.keyCode == 40) {
103                    container.show();
104                }
105            } else {
106                container.hide();
107            }
108        } else {
109            if (e.keyCode == 13) { // если ENTER
110                if (res_selected_cnt > 0) { // если есть выбранная позиция
111                    window.location.href = $("li.selected a", container).attr("href"); // переходим на нее
112                    return false;
113                }
114            } else if (e.keyCode == 27) // если ESC
115                container.hide();
116
117            // ------------------
118            // TODO_4562: прибрати код для UP / DOWN (перейшов з vido.com.ua) АБО реалізувати його для ERC
119            // if (res_cnt > 0) {
120            //     if (e.keyCode == 38) { // Если UP_ARR
121            //         if (res_selected_cnt > 0) {
122            //             $("li:eq(" + res_selected_index + ")", container).removeClass("selected");
123            //             if (res_selected_index > 0)
124            //                 $("li:eq(" + (res_selected_index - 1) + ")", container).addClass("selected");
125            //         } else {
126            //             $("li:last", container).addClass("selected");
127            //         }
128            //     } else if (e.keyCode == 40) { // Если DOWN_ARR
129            //         if (container.is(":not(:visible)")) {
130            //             $("li:eq(" + res_selected_index + ")", container).removeClass("selected");
131            //             $("li:first", container).addClass("selected");
132            //             container.show();
133            //         } else if (res_selected_cnt > 0) {
134            //             $("li:eq(" + res_selected_index + ")", container).removeClass("selected");
135            //             if (res_selected_index < res_cnt - 1)
136            //                 $("li:eq(" + (res_selected_index + 1) + ")", container).addClass("selected");
137            //         } else {
138            //             $("li:first", container).addClass("selected");
139            //         }
140            //     }
141            // }
142            // END TODO_4562: прибрати код для UP / DOWN (перейшов з vido.com.ua) АБО реалізувати його для ERC
143            // ------------------
144        }
145    },
146
147    disableSearch: function () {
148        if (!AjaxSearch.is_on_search_result) {
149            AjaxSearch.doNotShowAjaxResults = true;
150            setTimeout(function () {
151                AjaxSearch.hideResultsContainer();
152            }, 100);
153            AjaxSearch.hideLoading();
154        }
155    },
156
157    showCachedResults: function () {
158        if (0 == $("#search_input_only_current_category").val()
159            && $("#search_input").val()
160            && AjaxSearch.cachedAjaxResultKey == $("#search_input").val()) {
161            setTimeout(function () {
162                AjaxSearch.showResultsContainer();
163            }, 100);
164        }
165    },
166
167    showLoading: function () {
168        $('#search_loading').show();
169    },
170
171    hideLoading: function () {
172        $('#search_loading').hide();
173    },
174
175    showResultsContainer: function () {
176        AjaxSearch.log('showResultsContainer');
177        console.trace();
178        $('.popup-search').fadeIn(100).addClass('opened');
179    },
180
181    hideResultsContainer: function () {
182        if ($('.popup-search').is(':visible')) {
183            AjaxSearch.log('hideResultsContainer');
184            $('.popup-search').fadeOut(100).removeClass('opened');
185        }
186    },
187
188    log: function () {
189        if (window.console) {
190            window.console.log('[AjaxSearch] ', arguments[0], arguments[1] ? arguments[1] : '', arguments[2] ? arguments[2] : '');
191        }
192    },
193}

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.