-
-
Notifications
You must be signed in to change notification settings - Fork 20
[POC] Add a modal action whose content is loaded via an HTTP call. #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a6ccbb
Add a modal action whose content is loaded via an HTTP call.
alexandre-castelain e23b71b
Make the modal work with batch actions
alexandre-castelain db1f834
Remove console.log()
alexandre-castelain 07af802
Accept relative URL in the batch controller
alexandre-castelain 86c1951
Use UrlGeneratorInterface and add translation
alexandre-castelain 68c8552
Merge branch 'main' into modal_action
alexandre-castelain 44bebc8
Fix service injection
alexandre-castelain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,27 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
import { Modal } from 'bootstrap'; // Import Bootstrap | ||
|
||
export default class extends Controller { | ||
static targets = ['modal']; | ||
|
||
static values = { | ||
url: String, | ||
} | ||
|
||
open(event) { | ||
event.preventDefault(); | ||
const modalContent = this.modalTarget; | ||
|
||
fetch(this.urlValue) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Error loading content.'); | ||
} | ||
return response.text(); | ||
}) | ||
.then(html => { | ||
modalContent.innerHTML = html; | ||
}) | ||
; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Action\Type; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Action\ActionInterface; | ||
use Kreyu\Bundle\DataTableBundle\Action\ActionView; | ||
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView; | ||
use Kreyu\Bundle\DataTableBundle\Exception\LogicException; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Routing\RouterInterface; | ||
|
||
final class ModalActionType extends AbstractActionType | ||
{ | ||
public function __construct( | ||
private readonly RouterInterface $router, | ||
) { | ||
} | ||
|
||
public function buildView(ActionView $view, ActionInterface $action, array $options): void | ||
{ | ||
if (null === $options['href'] && null === $options['route']) { | ||
throw new LogicException('No "route" or "href" provided.'); | ||
} | ||
|
||
if ($view->parent instanceof ColumnValueView) { | ||
$value = $view->parent->value; | ||
|
||
foreach (['href', 'route', 'route_params'] as $optionName) { | ||
if (isset($options[$optionName]) && is_callable($options[$optionName])) { | ||
$options[$optionName] = $options[$optionName]($value); | ||
} | ||
} | ||
} else { | ||
foreach (['href', 'route', 'route_params'] as $optionName) { | ||
if (isset($options[$optionName]) && is_callable($options[$optionName])) { | ||
throw new LogicException(sprintf('Callable used for option "%s", but it\'s only available for RowActions.', $optionName)); | ||
} | ||
} | ||
} | ||
|
||
$href = $options['href'] ?? $this->router->generate($options['route'], $options['route_params']); | ||
|
||
$view->vars = array_replace($view->vars, [ | ||
'href' => $href, | ||
]); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver | ||
->define('route') | ||
->default(null) | ||
->allowedTypes('null', 'string', 'callable') | ||
; | ||
|
||
$resolver | ||
->define('route_params') | ||
->allowedTypes('array', 'callable') | ||
->default([]) | ||
; | ||
|
||
$resolver | ||
->define('href') | ||
->default(null) | ||
->allowedTypes('null', 'string', 'callable') | ||
; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.