From 767ee59fb808a92792ca70929b36e0244e4640f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Fri, 1 Nov 2019 22:04:30 +0100 Subject: [PATCH] Apply the filetype filter to accept attribute of file input. This way only files are shown which match the current attachment type and the user knows directly which file types are allowed or not. --- assets/ts_src/event_listeners.ts | 16 ++++++++++++++++ src/Form/AttachmentFormType.php | 1 + src/Form/Type/StructuralEntityType.php | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/assets/ts_src/event_listeners.ts b/assets/ts_src/event_listeners.ts index 2f19be50..3672011f 100644 --- a/assets/ts_src/event_listeners.ts +++ b/assets/ts_src/event_listeners.ts @@ -411,6 +411,22 @@ $(document).on("ajaxUI:start", function () { $(document).on("ajaxUI:reload", parseMarkdown); }); +$(document).on("ajaxUI:start ajaxUI:reload attachment:create", function() { + let updater = function() { + //@ts-ignore + let selected_option = $(this)[0].selectedOptions[0]; + let filter_string = $(selected_option).data('filetype_filter'); + //Find associated file input + + let $row = $(this).parents('tr'); + //Set accept filter + $('input[type="file"]', $row).prop('accept', filter_string); + }; + + //Register a change handler on all change listeners, and update it when the events are triggered + $('select.attachment_type_selector').change(updater).each(updater); +}); + //Need for proper body padding, with every navbar height $(window).resize(function () { let height : number = $('#navbar').height() + 10; diff --git a/src/Form/AttachmentFormType.php b/src/Form/AttachmentFormType.php index 4662d074..870b48b7 100644 --- a/src/Form/AttachmentFormType.php +++ b/src/Form/AttachmentFormType.php @@ -70,6 +70,7 @@ class AttachmentFormType extends AbstractType 'label' => $this->trans->trans('attachment.edit.attachment_type'), 'class' => AttachmentType::class, 'disable_not_selectable' => true, + 'attr' => ['class' => 'attachment_type_selector'] ]); $builder->add('showInTable', CheckboxType::class, ['required' => false, diff --git a/src/Form/Type/StructuralEntityType.php b/src/Form/Type/StructuralEntityType.php index d43f42ee..c0111486 100644 --- a/src/Form/Type/StructuralEntityType.php +++ b/src/Form/Type/StructuralEntityType.php @@ -23,6 +23,7 @@ namespace App\Form\Type; +use App\Entity\Attachments\AttachmentType; use App\Entity\Base\StructuralDBElement; use App\Entity\Parts\Storelocation; use App\Repository\StructuralDBElementRepository; @@ -114,6 +115,11 @@ class StructuralEntityType extends AbstractType if ($this->options['disable_not_selectable'] && $choice->isNotSelectable()) { $tmp += ['disabled' => 'disabled']; } + + if ($choice instanceof AttachmentType) { + $tmp += ['data-filetype_filter' => $choice->getFiletypeFilter()]; + } + return $tmp; }