PageSourceSearch

https://impa.br/wp-content/themes/impa-2024/snippets/loadMoreSearchAjax/script.js?v=1790846424&ver=7.1.2

js impa.br collected 2026-10-01 09:24:17 UTC 5,734 bytes, 204 lines download raw bytes

1(function ($) {
2	function load_more_post_type_search(element) {
3		let grids = element.find("[load_more_grid]");
4		let button = element.find("[load_more_search_btn]");
5		let btn_search_redirect = $("[js-send-to-search]").hide();
6
7		for (let index = 0; index < grids.length; index++) {
8			const type_grid = $(grids[index]);
9			let argsJson = type_grid.attr("data-argsJson");
10			let templatepart = type_grid.attr("data-template_part");
11			let text_not_found = type_grid.attr("data-text_not_found");
12			let argsJsonParse = JSON.parse(argsJson);
13
14			let has_more_posts = type_grid.attr("data-has_more_posts");
15
16			if (!has_more_posts) {
17				continue;
18			}
19
20			if (button) {
21				button.hide();
22			}
23
24			let pagination = type_grid.attr("data-pagination");
25
26			if (!pagination) {
27				pagination = "1";
28				type_grid.attr("data-pagination", pagination);
29			}
30
31			let hasMoreItens = type_grid.attr("data-hasMoreItens");
32
33			let requestData = {
34				action: "load_more_search",
35				args: argsJson,
36				pagination: pagination, // Paginação atual
37				templatePart: templatepart,
38			};
39
40			// Aborta requisição anterior se existir
41			if (type_grid.data("xhr")) {
42				type_grid.data("xhr").abort();
43			}
44
45			let xhr = $.ajax({
46				type: "POST",
47				url: ajax_object.ajax_url, // URL do admin-ajax.php
48				data: requestData, // O jQuery converte isso para x-www-form-urlencoded
49				dataType: "json", // Continuamos esperando HTML como resposta
50				success: function (response) {
51					let data = response.data;
52					try {
53						console.log(data);
54
55						if (type_grid.html() == '<div class="loader"></div>') {
56							type_grid.html("");
57						}
58
59						if (data.success) {
60							let haveLoader = type_grid.find("div.loader");
61
62							if (haveLoader.length > 0) {
63								haveLoader.remove();
64							}
65
66							type_grid.append(data.html);
67
68							console.log(btn_search_redirect);
69
70							if (data.has_more_posts) {
71								button.fadeIn();
72
73								if (btn_search_redirect.length) {
74									btn_search_redirect.fadeIn();
75								}
76							}
77
78							let atualPagination = Number(pagination) + 1;
79							type_grid.attr("data-pagination", atualPagination);
80							type_grid.attr("data-has_more_posts", data.has_more_posts);
81						} else {
82							console.error("Nenhum conteúdo retornado.");
83							console.log(data);
84
85							let gridContent = type_grid.html();
86							if (gridContent.length == 0) {
87								type_grid.html("<p>" + text_not_found + "</p>");
88							}
89						}
90					} catch (error) {
91						if (type_grid.html() == '<div class="loader"></div>') {
92							type_grid.html("");
93						}
94
95						console.error("Erro na requisição: ", error);
96					}
97				},
98				error: function (jqXHR, textStatus, errorThrown) {
99					if (textStatus !== 'abort') {
100						console.error(
101							"Erro na requisição:",
102							jqXHR.responseText,
103							textStatus,
104							errorThrown
105						);
106					}
107				},
108				complete: function() {
109					type_grid.removeData("xhr");
110				}
111			});
112			
113			type_grid.data("xhr", xhr);
114		}
115	}
116
117	$("[load_more_search_btn]").on("click", function (e) {
118		e.preventDefault();
119
120		let elemento = $(this).parents("[js-search-ajax]");
121		load_more_post_type_search(elemento);
122	});
123
124	$("[js-search-ajax] form").on("submit", function (e) {
125		e.preventDefault();
126
127		let search = $(this).find("[name=s]").val();
128		let load_more_container = $(this).parents("[js-search-ajax]");
129		let load_more_grid = load_more_container.find("[load_more_grid]");
130
131		for (let index = 0; index < load_more_grid.length; index++) {
132			const element = $(load_more_grid[index]);
133
134			let argsJsonString = element.attr("data-argsjson");
135
136			// Inicializa argsJson como um objeto vazio caso o atributo seja inválido ou inexistente
137			let argsJson = {};
138
139			if (argsJsonString) {
140				try {
141					argsJson = JSON.parse(argsJsonString); // Mantém o JSON original
142				} catch (error) {
143					console.error("Erro ao analisar JSON: ", error);
144				}
145			}
146
147			argsJson.s = search;
148
149			console.log("Updated argsJson:", argsJson);
150
151			// Atualiza o atributo data-argsJson no container sem sobrescrever outros campos
152			element.attr("data-argsJson", JSON.stringify(argsJson));
153
154			element.attr("data-pagination", 0);
155
156			element.html("<div class='loader'></div>");
157		}
158
159		let box_busca__resultados = load_more_grid.parents(
160			".box-busca__resultados"
161		);
162
163		if (box_busca__resultados.length > 0) {
164			box_busca__resultados.show();
165		}
166
167		load_more_post_type_search(load_more_container);
168	});
169
170	let typingTimer; // Variável para armazenar o timeout
171	const typingDelay = 800; // 3 segundos
172
173	$("[js-search-ajax] form [name=s]").on('input', function () {
174		let input = $(this);
175
176		clearTimeout(typingTimer); // Limpa o timeout anterior
177
178		// Configura um novo timeout para 3 segundos
179		typingTimer = setTimeout(function () {
180			let form = input.parents("form");
181			form.trigger("submit"); // Submete o formulário
182		}, typingDelay); // 3000 ms = 3 segundos
183	});
184
185	$("[js-limpar-busca]").on("click", function (e) {
186		e.preventDefault();
187		let input = document.querySelector("[js-busca] input");
188		input.value = "";
189		input.focus();
190
191		$(this).parents("form").trigger("submit");
192	});
193
194	$(document).on("click", "[js-send-to-search]", function (e) {
195		let s = $(this).parents("[js-search-ajax]").find("input[name=s]").val();
196		let siteUrl = $(this)
197			.parents("[js-search-ajax]")
198			.find("input[name=siteUrl]")
199			.val();
200
201		e.preventDefault(); // Evita que o evento padrão do elemento (se for um link) seja acionado
202		window.location.href = siteUrl + "/?s=" + s;
203	});
204})(jQuery);

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.