-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* data table view `url_query_parameters` variable with an array of URL parameters for the specific data table * improved url generators for filter clearing buttons, column sorting and (new) pagination controls * new `state` Stimulus controller in place of previous `persistence` to load the state of the data table - currently it adds `url_query_parameters` to URL in the browser
- Loading branch information
Showing
25 changed files
with
543 additions
and
114 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
|
||
export default class extends Controller { | ||
static values = { | ||
urlQueryParameters: Object, | ||
} | ||
|
||
connect() { | ||
this.#appendUrlQueryParameters(); | ||
} | ||
|
||
#appendUrlQueryParameters() { | ||
const url = new URL(window.location.href); | ||
|
||
const parameters = this.#flattenParameters(this.urlQueryParametersValue); | ||
|
||
for (const [key, value] of Object.entries(parameters)) { | ||
if (!url.searchParams.has(key)) { | ||
url.searchParams.set(key, String(value)); | ||
} | ||
} | ||
|
||
window.history.replaceState(null, null, url); | ||
} | ||
|
||
#flattenParameters(input, keyName) { | ||
let result = {}; | ||
|
||
for (const key in input) { | ||
const newKey = keyName ? `${keyName}[${key}]` : key; | ||
|
||
if (typeof input[key] === "object" && !Array.isArray(input[key])) { | ||
result = {...result, ...this.#flattenParameters(input[key], newKey)} | ||
} else { | ||
result[newKey] = input[key]; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
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
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
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
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
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
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Pagination; | ||
|
||
use Kreyu\Bundle\DataTableBundle\DataTableView; | ||
use Kreyu\Bundle\DataTableBundle\Exception\LogicException; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
class PaginationUrlGenerator implements PaginationUrlGeneratorInterface | ||
{ | ||
public function __construct( | ||
private readonly RequestStack $requestStack, | ||
private readonly UrlGeneratorInterface $urlGenerator, | ||
) { | ||
} | ||
|
||
public function generate(DataTableView $dataTableView, int $page): string | ||
{ | ||
$request = $this->getRequest(); | ||
|
||
$route = $request->attributes->get('_route'); | ||
$routeParams = $request->attributes->get('_route_params', []); | ||
$queryParams = $request->query->all(); | ||
|
||
$parameters = [...$routeParams, ...$queryParams]; | ||
|
||
// Recursively replace/merge with the URL query parameters defined in the data table view. | ||
// This allows the user to define custom query parameters that should be preserved when changing pages. | ||
$parameters = array_replace_recursive($parameters, $dataTableView->vars['url_query_parameters'] ?? []); | ||
|
||
$parameters[$dataTableView->vars['page_parameter_name']] = $page; | ||
|
||
return $this->urlGenerator->generate($route, $parameters); | ||
} | ||
|
||
private function getRequest(): Request | ||
{ | ||
if (null === $request = $this->requestStack->getCurrentRequest()) { | ||
throw new LogicException('Unable to retrieve current request.'); | ||
} | ||
|
||
return $request; | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Pagination; | ||
|
||
use Kreyu\Bundle\DataTableBundle\DataTableView; | ||
|
||
interface PaginationUrlGeneratorInterface | ||
{ | ||
public function generate(DataTableView $dataTableView, int $page): string; | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
use Kreyu\Bundle\DataTableBundle\Pagination\PaginationUrlGenerator; | ||
use Kreyu\Bundle\DataTableBundle\Pagination\PaginationUrlGeneratorInterface; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
use function Symfony\Component\DependencyInjection\Loader\Configurator\service; | ||
|
||
return static function (ContainerConfigurator $configurator) { | ||
$services = $configurator->services(); | ||
|
||
$services | ||
->set('kreyu_data_table.pagination.url_generator', PaginationUrlGenerator::class) | ||
->args([ | ||
service('request_stack'), | ||
service(UrlGeneratorInterface::class), | ||
]) | ||
->alias(PaginationUrlGeneratorInterface::class, 'kreyu_data_table.pagination.url_generator') | ||
; | ||
}; |
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
Oops, something went wrong.