Created
July 10, 2024 12:12
-
-
Save dantetesta/a9853a6e23a1f9ae5990c53b01eba545 to your computer and use it in GitHub Desktop.
Mostra e Conta Favoritos - Data Storage - JetEngine Data Store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Esse script serve para mostrar um menu de favoritos só quando houver itens no data store - do tipo local storage | |
Aplique a class .showinfo no item do menu | |
e coloque o código no elementor custom code all pages. | |
*/ | |
<script> | |
jQuery(document).ready(function() { | |
function getLocalStorageItem(key) { | |
return localStorage.getItem(key); | |
} | |
function checkFavorites() { | |
let favoritos = getLocalStorageItem('jet_engine_store_favoritos'); | |
console.log("Valor do localStorage 'jet_engine_store_favoritos':", favoritos); | |
if (favoritos && favoritos.trim() !== "") { | |
jQuery('.showinfo').css('display', 'block'); | |
} else { | |
jQuery('.showinfo').css('display', 'none'); | |
} | |
updateFavoritesCount(); | |
} | |
function updateFavoritesCount() { | |
let favoritos = getLocalStorageItem('jet_engine_store_favoritos'); | |
let count = 0; | |
if (favoritos && favoritos.trim() !== "") { | |
// Divide a string por vírgulas e conta os itens | |
let favoritosArray = favoritos.split(','); | |
count = favoritosArray.length; | |
} | |
jQuery('.showinfo .elementor-item').text('Favoritos (' + count + ')'); | |
} | |
// Verifica a variável 1.5 segundos após um clique em .jet-data-store-link | |
jQuery(document).on('click', '.jet-data-store-link', function() { | |
console.log("Clique detectado em .jet-data-store-link"); | |
setTimeout(checkFavorites, 1500); | |
}); | |
// Verifica a variável 1.5 segundos após um clique em .jet-remove-from-store usando delegação de eventos | |
jQuery(document).on('click', '.jet-remove-from-store', function() { | |
console.log("Clique detectado em .jet-remove-from-store"); | |
setTimeout(checkFavorites, 1500); | |
}); | |
// Inicializa o estado corretamente ao carregar a página | |
checkFavorites(); | |
// Verifica a variável 1.5 segundos após uma requisição AJAX bem-sucedida | |
jQuery(document).ajaxSuccess(function(event, xhr, settings) { | |
console.log("Requisição AJAX concluída com sucesso."); | |
setTimeout(checkFavorites, 1500); | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment