|
| 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 | +{% set field = form.vars.ea_vars.field %} |
| 6 | +{% set entity = form.vars.ea_vars.entity %} |
| 7 | +{% set target_entity = field.doctrineMetadata.get('targetEntity') %} |
| 8 | +{% set crudControllers = ea.crudControllers %} |
| 9 | +{% set target_entity_crud_fqcn = crudControllers.findCrudFqcnByEntityFqcn(target_entity) %} |
| 10 | + |
| 11 | +{% set url = ea_url().unset('entityId').setController(target_entity_crud_fqcn).setAction('index').set('embedContext', { |
| 12 | + mappedBy: field.doctrineMetadata.get('mappedBy'), |
| 13 | + embeddedIn: entity.primaryKeyValue |
| 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 | +<style> |
| 26 | + .embed-spinner { |
| 27 | + display: none; |
| 28 | + top: 50%; |
| 29 | + left: 50%; |
| 30 | + margin-top: -1rem; |
| 31 | + margin-left: -1rem; |
| 32 | + z-index: 10; |
| 33 | + } |
| 34 | +
|
| 35 | + .embed-loading .embed-spinner { |
| 36 | + display: block; |
| 37 | + } |
| 38 | +
|
| 39 | + .embed-loading .embed-content { |
| 40 | + opacity: 0.5; |
| 41 | + pointer-events: none; |
| 42 | + } |
| 43 | +</style> |
| 44 | + |
| 45 | +<script> |
| 46 | + window.addEventListener('load', () => { |
| 47 | + const referrer = window.location.href; |
| 48 | + const initialUrl = '{{ url|raw }}' |
| 49 | +
|
| 50 | + const embed = document.querySelector('#embed{{ id_suffix }}'); |
| 51 | + const embedContent = embed.querySelector('.embed-content'); |
| 52 | +
|
| 53 | + const load = (url) => { |
| 54 | + embed.classList.add('embed-loading'); |
| 55 | + fetch(url) |
| 56 | + .then(it => it.text()) |
| 57 | + .then(it => embedContent.innerHTML = it) |
| 58 | + .then(() => { |
| 59 | + // override referrer of actions, so we get back to the "main" view afterwards, not the embed |
| 60 | + embedContent.querySelectorAll('.actions a').forEach(action => { |
| 61 | + const target = new URL(action.href) |
| 62 | + target.searchParams.set('referrer', referrer); |
| 63 | + action.href = target.toString(); |
| 64 | + }); |
| 65 | +
|
| 66 | + // override referrer of actions, so we get back to the "main" view afterwards, not the embed |
| 67 | + embedContent.querySelectorAll('.global-actions a').forEach(action => { |
| 68 | + const target = new URL(action.href) |
| 69 | + const current = new URL(referrer); |
| 70 | + target.searchParams.set('relatedEntityId', current.searchParams.get('entityId')); |
| 71 | + target.searchParams.set('referrer', referrer); |
| 72 | + action.href = target.toString(); |
| 73 | + }) |
| 74 | +
|
| 75 | + embedContent.querySelectorAll('.action-delete').forEach((actionElement) => { |
| 76 | + actionElement.addEventListener('click', (event) => { |
| 77 | + event.preventDefault(); |
| 78 | +
|
| 79 | + document.querySelector('#modal-delete-button').addEventListener('click', () => { |
| 80 | + const deleteFormAction = new URL(actionElement.getAttribute('formaction')); |
| 81 | + const deleteForm = document.querySelector('#delete-form'); |
| 82 | + deleteFormAction.searchParams.set('referrer', referrer); |
| 83 | + deleteForm.setAttribute('action', deleteFormAction.toString()); |
| 84 | + deleteForm.submit(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | +
|
| 89 | + // intercept sort and pagination |
| 90 | + embedContent.querySelectorAll('thead a, .pagination a').forEach(link => { |
| 91 | + link.addEventListener('click', evt => { |
| 92 | + evt.preventDefault(); |
| 93 | + load(link.href) |
| 94 | + }) |
| 95 | + }) |
| 96 | +
|
| 97 | + // intercept search |
| 98 | + // embedContent.querySelector('.form-action-search form').addEventListener('submit', evt => { |
| 99 | + // evt.preventDefault(); |
| 100 | + // const data = new FormData(evt.target); |
| 101 | + // const params = new URLSearchParams(data).toString() |
| 102 | + // const target = new URL(url); |
| 103 | + // target.search = params.toString(); |
| 104 | + // load(target.toString()) |
| 105 | + // }) |
| 106 | +
|
| 107 | + // highlight results |
| 108 | + // const searchQuery = new URL(url).searchParams.get('query'); |
| 109 | + // if(searchQuery) { |
| 110 | + // $(embedContent).find('table tbody td:not(.actions)').highlight($.merge([searchQuery], searchQuery.split(' '))); |
| 111 | + // } |
| 112 | +
|
| 113 | + // can be used to re-initialize dynamic content |
| 114 | + document.dispatchEvent(new Event('ea.embed.content-loaded')) |
| 115 | + }) |
| 116 | + .finally(() => embed.classList.remove('embed-loading')) |
| 117 | + ; |
| 118 | + } |
| 119 | +
|
| 120 | + load(initialUrl); |
| 121 | + }) |
| 122 | +</script> |
0 commit comments