Skip to content

Commit

Permalink
refactor: finalize the link modal (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 authored Apr 7, 2024
1 parent 759371e commit 0e0e31f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export default class LinkActionsView extends View {
'ck-link-actions__preview'
],
href: bind.to('href', href => self._getEmsUrl(href)),
target: '_blank',
target: bind.to('href', href => href.startsWith('#') ? '' : '_blank'),
rel: 'noopener noreferrer'
}
})
Expand Down
6 changes: 4 additions & 2 deletions EMS/admin-ui-bundle/assets/js/core/plugins/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class Select {
}

select2 (target) {
$(target).find('select.select2').select2({
const targetQuery = $(target)
targetQuery.find('select.select2').select2({
theme: 'bootstrap-5',
allowClear: true,
placeholder: '',
escapeMarkup: function (markup) { return markup }
escapeMarkup: function (markup) { return markup },
dropdownParent: targetQuery
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">{{ 'core.modal.link.title'|trans }}</h4>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
</div>
<div class="modal-body modal-loading">
<div class="text-center">
Expand All @@ -14,9 +14,6 @@
</div>
</div>
<div class="modal-body ajax-modal-body" style="display:none;"></div>
<div class="modal-footer ajax-modal-footer">
<button type="button" class="btn btn-sm btn-default" data-bs-dismiss="modal">{{ 'core.modal.close'|trans }}</button>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{{ form(form) }}
{% trans_default_domain 'ems-adminui-twigs' %}
{{ form_start(form) }}
{{ form_errors(form) }}
{% for field in form.children|filter(f => f.vars.name != 'submit') %}
{{ form_row(field) }}
{% endfor %}
<div class="btn-group">
{{ form_widget(form.submit) }}
<button type="button" class="btn btn-default" data-bs-dismiss="modal">{{ 'core.modal.close'|trans }}</button>
</div>
{{ form_end(form) }}

35 changes: 35 additions & 0 deletions EMS/core-bundle/src/Form/Form/AnchorChoiceLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace EMS\CoreBundle\Form\Form;

use Symfony\Component\Form\ChoiceList\Loader\AbstractChoiceLoader;

class AnchorChoiceLoader extends AbstractChoiceLoader
{
/**
* @param string[] $choices
*/
public function __construct(private array $choices)
{
}

public function addAnchor(string $anchor): void
{
if (\in_array($anchor, $this->choices)) {
return;
}
$label = $anchor;
if (\str_starts_with($label, '#')) {
$label = \substr($label, 1);
}
$this->choices = \array_merge([$label => $anchor], $this->choices);
}

/**
* @return array<string, string>
*/
protected function loadChoices(): iterable
{
return $this->choices;
}
}
17 changes: 16 additions & 1 deletion EMS/core-bundle/src/Form/Form/LoadLinkModalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EMS\CoreBundle\Form\Form;

use EMS\CoreBundle\EMSCoreBundle;
use EMS\CoreBundle\Entity\Form\LoadLinkModalEntity;
use EMS\CoreBundle\Form\Field\FileType;
use EMS\CoreBundle\Form\Field\ObjectPickerType;
use EMS\CoreBundle\Form\Field\SubmitEmsType;
Expand All @@ -15,6 +16,8 @@
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Validator\Constraints\Email;
Expand All @@ -38,6 +41,7 @@ class LoadLinkModalType extends AbstractType
public const FIELD_SUBMIT = 'submit';
public const WITH_TARGET_BLANK_FIELD = 'with_target_blank_field';
public const ANCHOR_TARGETS = 'anchor_targets';
private AnchorChoiceLoader $anchorLoader;

public function __construct(private readonly RouterInterface $router)
{
Expand All @@ -49,6 +53,7 @@ public function __construct(private readonly RouterInterface $router)
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$this->anchorLoader = new AnchorChoiceLoader($options[self::ANCHOR_TARGETS]);
$builder
->add(self::FIELD_LINK_TYPE, ChoiceType::class, [
'label' => 'link_modal.field.link_type',
Expand Down Expand Up @@ -148,7 +153,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add(self::FIELD_ANCHOR, ChoiceType::class, [
'label' => 'link_modal.field.anchor',
'attr' => ['data-tags' => true, 'class' => 'select2'],
'choices' => $options[self::ANCHOR_TARGETS],
'choice_loader' => $this->anchorLoader,
'multiple' => false,
'choice_translation_domain' => false,
'required' => false,
Expand All @@ -162,6 +167,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
]]),
],
]);
$builder->get(self::FIELD_ANCHOR)->resetViewTransformers();

if (true === ($options[self::WITH_TARGET_BLANK_FIELD] ?? false)) {
$builder->add(self::FIELD_TARGET_BLANK, CheckboxType::class, [
Expand Down Expand Up @@ -204,4 +210,13 @@ public function configureOptions(OptionsResolver $resolver): void
]);
parent::configureOptions($resolver);
}

public function buildView(FormView $view, FormInterface $form, array $options): void
{
$data = $options['data'] ?? null;
if ($data instanceof LoadLinkModalEntity and null !== $anchor = $data->getAnchor()) {
$this->anchorLoader->addAnchor($anchor);
}
parent::buildView($view, $form, $options);
}
}

0 comments on commit 0e0e31f

Please sign in to comment.