PageSourceSearch

https://techmkt.pro/wp-content/plugins/floating-side-search/assets/js/floating-search.js?ver=1.0

js techmkt.pro collected 2026-10-07 18:12:14 UTC 9,310 bytes, 226 lines download raw bytes

1jQuery(document).ready(function($) {
2var lastSearchTerm = '';
3var lastResultsHtml = '';
4var lastResultsCount = 0;
5    var searchTimeout;
6    var currentCategory = 0;
7    var isMobileActive = false;
8    
9    var $searchContainer = $('.floating-side-search');
10    var $searchInput = $('.floating-side-search-input');
11    var $searchBtn = $('.floating-side-search-btn');
12    var $resultsContainer = $('.floating-side-search-results');
13    var $filterToggle = $('.floating-side-filter-toggle');
14    var $filterOptions = $('.floating-side-filter-options');
15    var $overlay = $('.floating-side-overlay');
16    var $mobileClose = $('.floating-side-mobile-close');
17    
18    // Фильтр категорий
19    $filterToggle.on('click', function(e) {
20        e.stopPropagation();
21        $filterOptions.toggleClass('active');
22    });
23    
24    $(document).on('click', function(e) {
25        if (!$(e.target).closest('.floating-side-filter-dropdown').length) {
26            $filterOptions.removeClass('active');
27        }
28    });
29    
30    $('.floating-side-filter-option').on('click', function() {
31        currentCategory = $(this).data('category-id');
32        var categoryName = $(this).text();
33        $('.floating-side-filter-current').text(categoryName);
34        $filterOptions.removeClass('active');
35        
36        if ($searchInput.val().trim().length >= 2) {
37            performSearch(true);
38        }
39    });
40    
41
42// Принудительно обновляем flex после открытия панели
43$('.floating-search-text').on('click', function() {
44    setTimeout(function() {
45        $('.floating-side-search-results').css('height', 'auto'); // на всякий случай
46    }, 350);
47});
48
49    // Мобильный режим
50function activateMobileMode() {
51    if (isMobileActive) return;
52    isMobileActive = true;
53    $searchContainer.addClass('mobile-active');
54    $searchBtn.hide();
55$('body').addClass('floating-search-no-scroll');
56    setTimeout(function() {
57        $searchInput.focus();
58    }, 100);
59}
60
61function deactivateMobileMode() {
62    if (!isMobileActive) return;
63    isMobileActive = false;
64    $searchContainer.removeClass('mobile-active');
65    $resultsContainer.hide();
66$('body').removeClass('floating-search-no-scroll');
67    $searchInput.blur();
68    $searchBtn.show();
69    $('.floating-search-handle').show(); // ← ВОЗВРАЩАЕМ РУЧКУ (лупу)
70    // Закрываем внешнюю панель виджета (если она открыта)
71    var $widget = $('.floating-search-widget');
72    $widget.removeClass('open').addClass('collapsed');
73}
74    
75    if (window.innerWidth <= 768) {
76        $searchInput.on('focus', function(e) {
77            if (!isMobileActive) {
78                e.preventDefault();
79                activateMobileMode();
80            }
81        });
82        
83        $mobileClose.on('click', function() {
84            deactivateMobileMode();
85        });
86        
87        $overlay.on('click', function() {
88            deactivateMobileMode();
89        });
90    }
91    
92    $(window).on('resize', function() {
93        if (window.innerWidth > 768 && isMobileActive) {
94            deactivateMobileMode();
95        }
96    });
97    
98    // Поиск
99function performSearch(force) {
100    var searchTerm = $searchInput.val().trim();
101    
102    if (searchTerm.length < 2) {
103        $resultsContainer.hide().empty();
104        lastSearchTerm = '';
105        lastResultsHtml = '';
106        return;
107    }
108    
109    // Если поисковый термин не изменился и force=false – показываем старые результаты (если они есть)
110    if (!force && searchTerm === lastSearchTerm && lastResultsHtml) {
111        $resultsContainer.html(lastResultsHtml).show();
112        return;
113    }
114    
115    clearTimeout(searchTimeout);
116    $resultsContainer.html('<div class="floating-side-search-loading">🔍 Поиск...</div>').show();
117    
118    searchTimeout = setTimeout(function() {
119        $.ajax({
120            url: floating_search.ajax_url,
121            type: 'POST',
122            data: {
123                action: 'floating_ajax_search',
124                search_term: searchTerm,
125                category_id: currentCategory,
126                nonce: floating_search.nonce
127            },
128            success: function(response) {
129                if (response.success && response.data.length > 0) {
130                    var html = '<div class="floating-side-results-count">Найдено: ' + response.data.length + '</div>';
131                    $.each(response.data, function(i, product) {
132                        var metaHtml = '';
133                        if (product.artikul) metaHtml += '<span class="floating-side-meta-item">Артикул: ' + product.artikul + '</span>';
134                        if (product.kod) metaHtml += '<span class="floating-side-meta-item">Код: ' + product.kod + '</span>';
135                        
136                        html += `
137                            <div class="floating-side-result-item" data-url="${product.url}">
138                                <div class="floating-side-result-image">
139                                    ${product.image ? `<img src="${product.image}" alt="${product.title}">` : '<span>📦</span>'}
140                                </div>
141                                <div class="floating-side-result-content">
142                                    <div class="floating-side-result-title">${product.title}</div>
143                                    ${metaHtml ? `<div class="floating-side-result-meta">${metaHtml}</div>` : ''}
144                                    <div class="floating-side-result-price">${product.price}</div>
145                                </div>
146                                <div class="floating-side-result-actions">
147                                    ${product.quick_view}
148                                </div>
149                            </div>
150                        `;
151                    });
152                    lastSearchTerm = searchTerm;
153                    lastResultsHtml = html;
154                    $resultsContainer.html(html).show();
155                } else {
156                    $resultsContainer.html('<div class="floating-side-search-no-results">😔 Ничего не найдено</div>').show();
157                }
158            },
159            error: function() {
160                $resultsContainer.html('<div class="floating-side-search-error">⚠️ Ошибка поиска</div>').show();
161            }
162        });
163    }, 400);
164}
165    
166    $searchInput.on('input', function() { performSearch(false); });
167    $searchInput.on('keypress', function(e) { if (e.which === 13) { e.preventDefault(); performSearch(true); } });
168    $searchBtn.on('click', function() { performSearch(true); });
169    
170// Скрываем клавиатуру при скролле результатов (мобильные)
171$resultsContainer.on('scroll', function() {
172    if (window.innerWidth <= 768) {
173        $searchInput.blur();
174    }
175});
176
177// Показываем кэшированные результаты при фокусе на поле (десктоп и мобильные)
178$searchInput.on('focus', function() {
179    var term = $searchInput.val().trim();
180    if (term.length >= 2 && lastResultsHtml) {
181        $resultsContainer.html(lastResultsHtml).show();
182    }
183});
184
185    $(document).on('click', function(e) {
186        if (!$(e.target).closest('.floating-side-search-wrapper').length && !isMobileActive) {
187            $resultsContainer.hide();
188        }
189    });
190    
191    // === ОБРАБОТЧИКИ КЛИКОВ (ИСПРАВЛЕННЫЕ) ===
192    
193    // Клик по карточке товара - переход на страницу (НЕ если кликнули на кнопку быстрого просмотра)
194    $(document).on('click', '.floating-side-result-item', function(e) {
195        // Если кликнули на кнопку быстрого просмотра — не переходим
196        if ($(e.target).closest('.woosq-btn').length) {
197            e.preventDefault();
198            e.stopPropagation();
199            return;
200        }
201        window.location.href = $(this).data('url');
202    });
203    
204    // Кнопка быстрого просмотра — только открытие попапа, без перехода
205    $(document).on('click', '.floating-side-result-actions .woosq-btn', function(e) {
206        e.stopPropagation();
207        e.preventDefault();
208        // Плагин WPC Smart Quick View сам обработает открытие попапа
209    });
210    
211    // Старый обработчик — отключаем
212    $(document).off('click', '.floating-side-quick-view');
213    $(document).on('click', '.floating-side-quick-view', function(e) {
214        e.stopPropagation();
215        e.preventDefault();
216    });
217    
218    $(document).on('keyup', function(e) {
219        if (e.key === 'Escape') {
220            if (isMobileActive) {
221                deactivateMobileMode();
222            }
223            $resultsContainer.hide();
224        }
225    });
226});

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.