|
| 1 | +{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #} |
| 2 | +{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #} |
| 3 | +{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #} |
| 4 | +{% set current_url = ea_url() %} |
| 5 | + |
| 6 | +{% set target_entity = field.doctrineMetadata.get('targetEntity') %} |
| 7 | +{% set crud_id = ea.crudControllers.findCrudIdByCrudFqcn(ea.crudControllers.findCrudFqcnByEntityFqcn(target_entity)) %} |
| 8 | + |
| 9 | +{% set url = ea_url().unset('entityId').setCrudId(crud_id).setAction('index').set('embedContext', { |
| 10 | + mappedBy: field.doctrineMetadata.get('mappedBy'), |
| 11 | + embeddedIn: entity.primaryKeyValue, |
| 12 | + crud_id: ea.crudControllers.findCrudIdByCrudFqcn(ea.crud.controllerFqcn), |
| 13 | + fieldName: field.property |
| 14 | +}) %} |
| 15 | + |
| 16 | +{% set id_suffix = '-'~field.property %} |
| 17 | + |
| 18 | +<div id="embed{{ id_suffix }}" class="position-relative embed-loading"> |
| 19 | + <div class="position-absolute text-center embed-spinner"> |
| 20 | + <div class="spinner-border text-primary spinner-border-lg mt-2"></div> |
| 21 | + </div> |
| 22 | + <div class="embed-content"></div> |
| 23 | +</div> |
| 24 | + |
| 25 | +{{ include('@EasyAdmin/crud/includes/_delete_form.html.twig', {crud_id: crud_id, referrer: current_url, id_suffix: id_suffix}, with_context = false) }} |
| 26 | + |
| 27 | +<style> |
| 28 | + .embed-spinner { |
| 29 | + display: none; |
| 30 | + top: 50%; |
| 31 | + left: 50%; |
| 32 | + margin-top: -1rem; |
| 33 | + margin-left: -1rem; |
| 34 | + z-index: 10; |
| 35 | + } |
| 36 | +
|
| 37 | + .embed-loading .embed-spinner { |
| 38 | + display: block; |
| 39 | + } |
| 40 | +
|
| 41 | + .embed-loading .embed-content { |
| 42 | + opacity: 0.5; |
| 43 | + pointer-events: none; |
| 44 | + } |
| 45 | +</style> |
| 46 | + |
| 47 | +<script> |
| 48 | + window.addEventListener('load', () => { |
| 49 | + const referrer = '{{ current_url|raw }}#embed{{ id_suffix }}' |
| 50 | + const initialUrl = '{{ url|raw }}' |
| 51 | +
|
| 52 | + const embed = document.querySelector('#embed{{ id_suffix }}'); |
| 53 | + const embedContent = embed.querySelector('.embed-content'); |
| 54 | +
|
| 55 | + const load = (url) => { |
| 56 | + embed.classList.add('embed-loading'); |
| 57 | + fetch(url) |
| 58 | + .then(it => it.text()) |
| 59 | + .then(it => embedContent.innerHTML = it) |
| 60 | + .then(() => { |
| 61 | + // override referrer of actions, so we get back to the "main" view afterwards, not the embed |
| 62 | + embedContent.querySelectorAll('.actions a').forEach(action => { |
| 63 | + const target = new URL(action.href) |
| 64 | + target.searchParams.set('referrer', referrer); |
| 65 | + action.href = target.toString(); |
| 66 | + }) |
| 67 | +
|
| 68 | + // remove "index" delete form/modal |
| 69 | + embedContent.querySelectorAll('#modal-delete, #delete-form').forEach(it => it.remove()); |
| 70 | + // initialize correct delete modal |
| 71 | + embedContent.querySelectorAll('.action-delete').forEach(action => { |
| 72 | + action.addEventListener('click', function (e) { |
| 73 | + e.preventDefault(); |
| 74 | + const id = $(this).parents('tr').first().data('id'); |
| 75 | +
|
| 76 | + $('#modal-delete{{ id_suffix }}').modal({backdrop: true, keyboard: true}) |
| 77 | + .off('click', '#modal-delete-button{{ id_suffix }}') |
| 78 | + .on('click', '#modal-delete-button{{ id_suffix }}', function () { |
| 79 | + let deleteForm = $('#delete-form{{ id_suffix }}'); |
| 80 | + deleteForm.attr('action', deleteForm.attr('action').replace('__entityId_placeholder__', id)); |
| 81 | + deleteForm.trigger('submit'); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }) |
| 85 | +
|
| 86 | + // intercept sort and pagination |
| 87 | + embedContent.querySelectorAll('thead a, .pagination a').forEach(link => { |
| 88 | + link.addEventListener('click', evt => { |
| 89 | + evt.preventDefault(); |
| 90 | + load(link.href) |
| 91 | + }) |
| 92 | + }) |
| 93 | +
|
| 94 | + // intercept search |
| 95 | + embedContent.querySelector('.form-action-search form').addEventListener('submit', evt => { |
| 96 | + evt.preventDefault(); |
| 97 | + const data = new FormData(evt.target); |
| 98 | + const params = new URLSearchParams(data).toString() |
| 99 | + const target = new URL(url); |
| 100 | + target.search = params.toString(); |
| 101 | + load(target.toString()) |
| 102 | + }) |
| 103 | +
|
| 104 | + // highlight results |
| 105 | + const searchQuery = new URL(url).searchParams.get('query'); |
| 106 | + if(searchQuery) { |
| 107 | + $(embedContent).find('table tbody td:not(.actions)').highlight($.merge([searchQuery], searchQuery.split(' '))); |
| 108 | + } |
| 109 | +
|
| 110 | + // can be used to re-initialize dynamic content |
| 111 | + document.dispatchEvent(new Event('ea.embed.content-loaded')) |
| 112 | + }) |
| 113 | + .finally(() => embed.classList.remove('embed-loading')) |
| 114 | + ; |
| 115 | + } |
| 116 | +
|
| 117 | + load(initialUrl); |
| 118 | + }) |
| 119 | +</script> |
0 commit comments