{% block body_javascript %}
<script type="text/javascript">
$(function() {
const toggles = document.querySelectorAll('.checkbox-switch input[type="checkbox"]');
for (i = 0; i < toggles.length; i++) {
toggles[i].addEventListener('change', function () {
const toggle = this;
const newValue = this.checked;
const oldValue = !newValue;
const propertyName = this.closest('.checkbox-switch').dataset.propertyname;
const toggleUrl = "{{ path('easyadmin', { action: 'edit', entity: _entity_config.name, view: 'list' })|raw }}"
+ "&id=" + this.closest('tr').dataset.id
+ "&property=" + propertyName
+ "&newValue=" + newValue.toString();
let toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });
toggleRequest.done(function(result) {});
toggleRequest.fail(function() {
// in case of error, restore the original value and disable the toggle
toggle.checked = oldValue;
toggle.disabled = true;
toggle.closest('.checkbox-switch').classList.add('disabled');
});
});
}
$('.action-delete').on('click', function(e) {
e.preventDefault();
const id = $(this).parents('tr').first().data('id');
$('#modal-delete').modal({ backdrop: true, keyboard: true })
.off('click', '#modal-delete-button')
.on('click', '#modal-delete-button', function () {
let deleteForm = $('#delete-form');
deleteForm.attr('action', deleteForm.attr('action').replace('__id__', id));
deleteForm.trigger('submit');
});
});
{% if _has_filters %}
// HTML5 specifies that a <script> tag inserted with innerHTML should not execute
// https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML#Security_considerations
// That's why we can't use just 'innerHTML'. See https://stackoverflow.com/a/47614491/2804294
var setInnerHTML = function(element, htmlContent) {
element.innerHTML = htmlContent;
Array.from(element.querySelectorAll('script')).forEach( oldScript => {
const newScript = document.createElement('script');
Array.from(oldScript.attributes)
.forEach(attr => newScript.setAttribute(attr.name, attr.value));
newScript.appendChild(document.createTextNode(oldScript.innerHTML));
oldScript.parentNode.replaceChild(newScript, oldScript);
});
};
document.querySelector('.action-filters-button').addEventListener('click', function(event) {
let filterButton = event.currentTarget;
let filterModal = document.querySelector(filterButton.dataset.modal);
let filterModalBody = filterModal.querySelector('.modal-body');
$(filterModal).modal({ backdrop: true, keyboard: true });
filterModalBody.innerHTML = '<div class="fa-3x px-3 py-3 text-muted text-center"><i class="fas fa-circle-notch fa-spin"></i></div>';
$.get(filterButton.getAttribute('href'), function (response) {
setInnerHTML(filterModalBody, response);
});
event.preventDefault();
event.stopPropagation();
});
{% endif %}
{% if _has_batch_actions %}
$(document).ready(function () {
const $content = $('.panel');
let $input = $content.find(':hidden#batch_form_ids');
let ids = $input.val() ? $input.val().split(',') : [];
showBatchAcions(ids, $content);
});
//const titleContent = $('.panel-heading > .panel-title').html();
$(document).on('click', '.deselect-batch-button', function () {
$(this).closest('.panel').find(':checkbox.form-batch-checkbox-all').prop('checked', false).trigger('change');
});
$(document).on('change', '.form-batch-checkbox-all', function () {
$(this).closest('.panel').find(':checkbox.form-batch-checkbox').prop('checked', $(this).prop('checked')).trigger('change');
});
$(document).on('change', '.form-batch-checkbox', function () {
const $content = $(this).closest('.panel');
let $input = $content.find(':hidden#batch_form_ids');
let ids = $input.val() ? $input.val().split(',') : [];
const id = $(this).val();
if ($(this).prop('checked')) {
if (-1 === ids.indexOf(id)) {
ids.push(id);
}
} else {
ids = ids.filter(function(value) { return value !== id });
$content.find(':checkbox.form-batch-checkbox-all').prop('checked', false);
}
showBatchAcions(ids, $content);
$input.val(ids.join(','));
//$content.find('.panel-heading > .panel-title').html(0 == ids.length ? titleContent : ' ');
});
let showBatchAcions = function (ids, $content) {
if (0 === ids.length) {
$content.find('.panel-control').show();
$content.find('.batch-actions').hide();
$content.find('table').removeClass('table-batch');
} else {
$content.find('.batch-actions').show();
$content.find('.panel-control').hide();
$content.find('table').addClass('table-batch');
}
};
$('button[name="batch_form[name]"][value="captureMail"]').on('click', function (event) {
event.preventDefault();
event.stopPropagation();
const $content = $('.panel');
let $input = $content.find(':hidden#batch_form_ids');
let ids = $input.val() ? $input.val().split(',') : [];
let emails = [];
for (const idsKey in ids)
{
emails.push($('table tbody tr[data-id=' + ids[idsKey] + ']').find('td.string:eq(1) > span').attr('title'));
}
$('.toast').text('(' + ids.length + ') Correos Copiados').toast('show');
copyTextToClipboard(emails.join(' '));
$('.deselect-batch-button').click();
});
let modalTitle = $('#batch-action-confirmation-title');
const titleContentWithPlaceholders = modalTitle.text();
$('button[name="batch_form[name]"].batch-action-requires-confirmation').on('click', function (event) {
event.preventDefault();
event.stopPropagation();
let $button = $(this);
const actionName = $button.text();
const numberOfSelectedItems = $('input[type="checkbox"].form-batch-checkbox:checked').length;
modalTitle.text(titleContentWithPlaceholders
.replace('%action_name%', actionName)
.replace('%num_items%', numberOfSelectedItems));
$('#modal-batch-action').modal({ backdrop : true, keyboard : true })
.off('click', '#modal-batch-action-button')
.on('click', '#modal-batch-action-button', function () {
//let body_mail = $('textarea[name="batch_form[body_mail]"]').clone().hide();
//$('form[name="batch_form"]').append(body_mail);
$button.unbind('click');
$button.trigger('click');
modalTitle.text(titleContentWithPlaceholders);
});
});
{% endif %}
});
</script>
{% if 'search' == app.request.get('action') %}
<script type="text/javascript">
const _search_query = "{{ app.request.get('query')|default('')|e('js') }}";
// the original query is prepended to allow matching exact phrases in addition to single words
$('#main').find('table tbody td:not(.actions)').highlight($.merge([_search_query], _search_query.split(' ')));
</script>
{% endif %}
{% endblock %}