Skip to content

Instantly share code, notes, and snippets.

@ofmarconi
Last active September 21, 2023 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofmarconi/2e9e5facbec937362cef13a51e269e93 to your computer and use it in GitHub Desktop.
Save ofmarconi/2e9e5facbec937362cef13a51e269e93 to your computer and use it in GitHub Desktop.
JetEngine • Transformando o primeiro item do Repeater em um botão que adiciona um novo item no topo ao invés de no final.
// Inverte a lógica e ordem de criação de novo item do Repeater
add_action( 'admin_head', function () { ?>
<style>
/* CSS */
.cx-ui-repeater-item:first-child .cheryr-ui-repeater-content-box {
display: none;
}
.cx-ui-repeater-item:first-child .cx-ui-repeater-actions-box {
margin: 0!important;
}
.cx-ui-repeater-item:first-child {
padding: 0;
pointer-events: none;
display: flex!important;
position: inherit!important;
}
.cx-ui-repeater-item:first-child a.cx-ui-repeater-remove {
display: none;
}
.cx-ui-repeater-item:first-child span.cx-ui-repeater-title {
display: none;
}
.cx-ui-repeater-item:first-child a.cx-ui-repeater-toggle {
display: none;
}
.cx-ui-repeater-item:first-child a.cx-ui-repeater-copy {
right: auto;
pointer-events: auto;
}
.cx-ui-repeater-item:first-child a.cx-ui-repeater-copy:after {
content: 'Adicionar novo item';
position: absolute;
width: max-content;
left: 29px;
top: -2px;
}
/* botão padrão de adicionar novo item */
a.cx-ui-repeater-add:after {
content: 'Adicionar novos itens';
text-indent: 0;
display: block;
line-height: initial; /* New content takes up original line height */
}
a.cx-ui-repeater-add {
text-indent: -9999px;
line-height: 0; /* Collapse the original line */
}
</style>
<script>
var intervalId;
var counter = 0;
function executeScript() {
// Verifica se o objeto 'fn' e o plugin Sortable estão disponíveis
if ('fn' in jQuery && 'sortable' in jQuery.fn) {
// Função para atualizar o Sortable
function updateSortable() {
// Primeiro, destrói qualquer instância anterior
jQuery('.cx-ui-repeater-list').sortable("destroy");
// Em seguida, recria o Sortable, excluindo o primeiro item
jQuery('.cx-ui-repeater-list').sortable({
items: '.cx-ui-repeater-item:not(:first)'
});
}
// Inicialmente atualiza o Sortable
updateSortable();
// Atualiza o Sortable quando um item é adicionado ou removido
jQuery('.cx-ui-repeater-add, .cx-ui-repeater-remove').on('click', function() {
updateSortable();
});
} else {
console.error('jQuery UI Sortable não encontrado.');
}
}
// Executa a função a cada 1 segundo
intervalId = setInterval(function() {
executeScript();
counter++;
if (counter >= 2) {
clearInterval(intervalId);
}
}, 1000);
// Garante que o script seja executado após o documento estar completamente carregado
jQuery(document).ready(function() {
executeScript();
});
</script>
<?php } );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment