-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
212 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Field; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface; | ||
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\EmbedType; | ||
|
||
final class EmbedField implements FieldInterface | ||
{ | ||
use FieldTrait; | ||
|
||
public static function new(string $propertyName, ?string $label = null): self | ||
{ | ||
return (new self()) | ||
->setProperty($propertyName) | ||
->setLabel($label) | ||
->setTemplateName('crud/field/embed') | ||
->setFormType(EmbedType::class) | ||
->setFormTypeOption('mapped', false) | ||
->onlyWhenUpdating() | ||
; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Form\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | ||
|
||
class EmbedType extends AbstractType | ||
{ | ||
public function getBlockPrefix(): string | ||
{ | ||
return 'ea_embedded_collection'; | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return CollectionType::class; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #} | ||
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #} | ||
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #} | ||
{% set has_footer = entities|length != 0 %} | ||
{% set has_batch_actions = false %} | ||
{% set some_results_are_hidden = false %} | ||
{% set sort_field_name = app.request.get('sort')|keys|first %} | ||
{% set sort_order = app.request.get('sort')|first %} | ||
|
||
{{ block("content", "@EasyAdmin/crud/index.html.twig") }} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #} | ||
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #} | ||
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #} | ||
{% set current_url = ea_url() %} | ||
{% set field = form.vars.ea_vars.field %} | ||
{% set entity = form.vars.ea_vars.entity %} | ||
{% set target_entity = field.doctrineMetadata.get('targetEntity') %} | ||
{% set crudControllers = ea.crudControllers %} | ||
{% set target_entity_crud_fqcn = crudControllers.findCrudFqcnByEntityFqcn(target_entity) %} | ||
|
||
{% set url = ea_url().unset('entityId').setController(target_entity_crud_fqcn).setAction('index').set('embedContext', { | ||
mappedBy: field.doctrineMetadata.get('mappedBy'), | ||
embeddedIn: entity.primaryKeyValue | ||
}) %} | ||
|
||
{% set id_suffix = '-'~field.property %} | ||
|
||
<div id="embed{{ id_suffix }}" class="position-relative embed-loading"> | ||
<div class="position-absolute text-center embed-spinner"> | ||
<div class="spinner-border text-primary spinner-border-lg mt-2"></div> | ||
</div> | ||
<div class="embed-content"></div> | ||
</div> | ||
|
||
<style> | ||
.embed-spinner { | ||
display: none; | ||
top: 50%; | ||
left: 50%; | ||
margin-top: -1rem; | ||
margin-left: -1rem; | ||
z-index: 10; | ||
} | ||
.embed-loading .embed-spinner { | ||
display: block; | ||
} | ||
.embed-loading .embed-content { | ||
opacity: 0.5; | ||
pointer-events: none; | ||
} | ||
</style> | ||
|
||
<script> | ||
window.addEventListener('load', () => { | ||
const referrer = window.location.href; | ||
const initialUrl = '{{ url|raw }}' | ||
const embed = document.querySelector('#embed{{ id_suffix }}'); | ||
const embedContent = embed.querySelector('.embed-content'); | ||
const load = (url) => { | ||
embed.classList.add('embed-loading'); | ||
fetch(url) | ||
.then(it => it.text()) | ||
.then(it => embedContent.innerHTML = it) | ||
.then(() => { | ||
// override referrer of actions, so we get back to the "main" view afterwards, not the embed | ||
embedContent.querySelectorAll('.actions a').forEach(action => { | ||
const target = new URL(action.href) | ||
target.searchParams.set('referrer', referrer); | ||
action.href = target.toString(); | ||
}); | ||
// override referrer of actions, so we get back to the "main" view afterwards, not the embed | ||
embedContent.querySelectorAll('.global-actions a').forEach(action => { | ||
const target = new URL(action.href) | ||
const current = new URL(referrer); | ||
target.searchParams.set('relatedEntityId', current.searchParams.get('entityId')); | ||
target.searchParams.set('referrer', referrer); | ||
action.href = target.toString(); | ||
}) | ||
embedContent.querySelectorAll('.action-delete').forEach((actionElement) => { | ||
actionElement.addEventListener('click', (event) => { | ||
event.preventDefault(); | ||
document.querySelector('#modal-delete-button').addEventListener('click', () => { | ||
const deleteFormAction = new URL(actionElement.getAttribute('formaction')); | ||
const deleteForm = document.querySelector('#delete-form'); | ||
deleteFormAction.searchParams.set('referrer', referrer); | ||
deleteForm.setAttribute('action', deleteFormAction.toString()); | ||
deleteForm.submit(); | ||
}); | ||
}); | ||
}); | ||
// intercept sort and pagination | ||
embedContent.querySelectorAll('thead a, .pagination a').forEach(link => { | ||
link.addEventListener('click', evt => { | ||
evt.preventDefault(); | ||
load(link.href) | ||
}) | ||
}) | ||
// intercept search | ||
// embedContent.querySelector('.form-action-search form').addEventListener('submit', evt => { | ||
// evt.preventDefault(); | ||
// const data = new FormData(evt.target); | ||
// const params = new URLSearchParams(data).toString() | ||
// const target = new URL(url); | ||
// target.search = params.toString(); | ||
// load(target.toString()) | ||
// }) | ||
// highlight results | ||
// const searchQuery = new URL(url).searchParams.get('query'); | ||
// if(searchQuery) { | ||
// $(embedContent).find('table tbody td:not(.actions)').highlight($.merge([searchQuery], searchQuery.split(' '))); | ||
// } | ||
// can be used to re-initialize dynamic content | ||
document.dispatchEvent(new Event('ea.embed.content-loaded')) | ||
}) | ||
.finally(() => embed.classList.remove('embed-loading')) | ||
; | ||
} | ||
load(initialUrl); | ||
}) | ||
</script> |
This file contains 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
This file contains 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