Skip to content

Commit

Permalink
see cl 1.0.0-beta128.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kunitzsch committed Feb 26, 2020
1 parent 2a0c9e3 commit 5cafc2b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.0.0-beta128.5] - 2020-02-26
- fixed reset of form for async submit

## [1.0.0-beta128.4] - 2020-02-25
- fixed replacement of filter for async submit

Expand Down
34 changes: 28 additions & 6 deletions src/Config/FilterConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -159,12 +160,10 @@ public function buildForm(array $data = [])
if ($this->getFilter()['asyncFormSubmit']) {
$options['attr']['data-async'] = 1;
$options['attr']['data-list'] = '#huh-list-'.$this->getFilter()['ajaxList'];
$this->container->get('huh.filter.util.filter_ajax')->updateData($this);
$data = $this->getData();
}

if(isset($data['reset'])) {
$this->resetData();
if($this->container->get('huh.request')->isXmlHttpRequest()){
$this->container->get('huh.filter.util.filter_ajax')->updateData($this);
$data = $this->getData();
}

Expand Down Expand Up @@ -292,8 +291,7 @@ public function handleForm($request = null): ?RedirectResponse
$this->setData($this->filter['mergeData'] ? array_merge($this->getData(), $data) : $data);

// allow reset, support different form configuration with same form name
if (null !== $form->getClickedButton() && \in_array($form->getClickedButton()->getName(),
$this->getResetNames(), true)) {
if ($this->isResetButtonClicked($form)) {
$this->resetData();
// redirect to referrer page without filter parameters
$url = $this->container->get('huh.utils.url')->removeQueryString([$form->getName()],
Expand All @@ -310,6 +308,30 @@ public function handleForm($request = null): ?RedirectResponse
return null;
}


protected function isResetButtonClicked(FormInterface $form): bool
{
if(!(null !== $form->getClickedButton() && \in_array($form->getClickedButton()->getName(),
$this->getResetNames(), true))) {
return $this->isAsyncResetButtonClicked();
}

return true;
}

protected function isAsyncResetButtonClicked(): bool
{
$request = $this->container->get('huh.request');

if(!$request->isXmlHttpRequest()) {
return false;
}

$data = 'GET' == $request->getMethod() ? $request->getGet($this->getFilter()['name']) : $request->getPost($this->getFilter()['name']);

return isset($data['reset']);
}

/**
* @return int|null
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/npm-package/js/contao-filter-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class FilterBundle {
clickedButton = document.createElement('div');
clickedButton.setAttribute('name', buttonName);

console.log(clickedButton);


FilterBundle.asyncSubmit(element.form, clickedButton);
});

Expand All @@ -45,6 +48,8 @@ class FilterBundle {
data.append(clickedButton.getAttribute('name'), '');
}



if ('get' === method || 'GET' === method) {
utilsBundle.ajax.get(action, data, config);
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/Util/FilterAjaxUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function updateData(FilterConfig &$form): void
return;
}

if(isset($updateData['reset'])) {
$form->resetData();
return;
}

$form->setData($updateData);
}

Expand Down

0 comments on commit 5cafc2b

Please sign in to comment.