Skip to content

Commit

Permalink
Fixed the paginator.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed May 13, 2024
1 parent 4cf44bf commit 6eb5e28
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 55 deletions.
9 changes: 4 additions & 5 deletions src/App/CallableClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Jaxon\Plugin\Request\CallableClass\CallableClassHelper;
use Jaxon\Plugin\Response\DataBag\DataBagContext;
use Jaxon\Plugin\Response\JQuery\DomSelector;
use Jaxon\Request\Call\Call;
use Jaxon\Plugin\Response\Pagination\Paginator;
use Jaxon\Request\Factory\RequestFactory;
use Jaxon\Request\TargetInterface;
use Jaxon\Response\Response;
Expand Down Expand Up @@ -129,15 +129,14 @@ public function bag(string $sBagName): DataBagContext
/**
* Render an HTML pagination control.
*
* @param Call $xCall
* @param int $nCurrentPage The current page number
* @param int $nItemsPerPage The number of items per page
* @param int $nTotalItems The total number of items
*
* @return void
* @return Paginator
*/
public function paginate(Call $xCall, int $nCurrentPage, int $nItemsPerPage, int $nTotalItems)
public function paginator(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator
{
$this->response->paginate($xCall, $nCurrentPage, $nItemsPerPage, $nTotalItems);
return $this->response->paginator($nCurrentPage, $nItemsPerPage, $nTotalItems);
}
}
2 changes: 2 additions & 0 deletions src/Plugin/Manager/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
use Jaxon\Plugin\Response\Dialog\DialogPlugin;
use Jaxon\Plugin\Response\JQuery\JQueryPlugin;
use Jaxon\Plugin\Response\Pagination\PaginatorPlugin;
use Jaxon\Plugin\ResponsePlugin;
use Jaxon\Plugin\ResponsePluginInterface;
use Jaxon\Request\Handler\ParameterReader;
Expand Down Expand Up @@ -221,6 +222,7 @@ public function registerPlugins()
$this->registerPlugin(JQueryPlugin::class, JQueryPlugin::NAME, 700);
$this->registerPlugin(DataBagPlugin::class, DataBagPlugin::NAME, 700);
$this->registerPlugin(DialogPlugin::class, DialogPlugin::NAME, 750);
$this->registerPlugin(PaginatorPlugin::class, PaginatorPlugin::NAME, 800);
}

/**
Expand Down
30 changes: 19 additions & 11 deletions src/Plugin/Response/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

namespace Jaxon\Plugin\Response\Pagination;

use Jaxon\Request\Call\Call;

use function array_walk;
use function ceil;
use function floor;
Expand Down Expand Up @@ -87,32 +89,38 @@ class Paginator
*/
protected $sEllipsysText = '...';

/**
* @var PaginatorPlugin
*/
private $xPlugin;

/**
* The constructor.
*
* @param PaginatorPlugin $xPlugin
* @param int $nCurrentPage The current page number
* @param int $nItemsPerPage The number of items per page
* @param int $nTotalItems The total number of items
*/
public function __construct(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems)
public function __construct(PaginatorPlugin $xPlugin, int $nCurrentPage, int $nItemsPerPage, int $nTotalItems)
{
$this->setup($nCurrentPage, $nItemsPerPage, $nTotalItems);
$this->xPlugin = $xPlugin;
$this->setCurrentPage($nCurrentPage)
->setItemsPerPage($nItemsPerPage)
->setTotalItems($nTotalItems);
}

/**
* Setup the paginator
* Render the paginator
*
* @param int $nCurrentPage The current page number
* @param int $nItemsPerPage The number of items per page
* @param int $nTotalItems The total number of items
* @param Call $xCall
* @param string $sWrapperId
*
* @return Paginator
* @return string
*/
public function setup(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator
public function paginate(Call $xCall, string $sWrapperId)
{
return $this->setCurrentPage($nCurrentPage)
->setItemsPerPage($nItemsPerPage)
->setTotalItems($nTotalItems);
$this->xPlugin->render($this, $xCall, $sWrapperId);
}

/**
Expand Down
63 changes: 29 additions & 34 deletions src/Plugin/Response/Pagination/PaginatorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@
/**
* Usage
*
* Step 1: Create a wrapper for the pagination.
* Step 1: Render a template containing a wrapper for the pagination.
*
* $html = $this->render($pageTemplate, [
* // ...
* 'pagination' => $this->response->pg->wrapper($wrapperId),
* ]);
*
* Step 2: Render the pagination into the wrapper.
* Step 2: Create a paginator and render the pagination into the wrapper.
*
* $this->response->pg->render($this->rq()->page(), $pageNumber, $perPage, $total);
* $this->response->pg->paginator($pageNumber, $perPage, $total)
* ->paginate($this->rq()->page(), $wrapperId);
* // Or, using the response shortcut
* $this->response->paginate($this->rq()->page(), $pageNumber, $perPage, $total);
* $this->response->paginator($pageNumber, $perPage, $total)
* ->paginate($this->rq()->page(), $wrapperId);
* // Or, in a class that inherits from CallableClass
* $this->paginate($this->rq()->page(), $pageNumber, $perPage, $total);
* $this->paginator($pageNumber, $perPage, $total)
* ->paginate($this->rq()->page(), $wrapperId);
*/
class PaginatorPlugin extends ResponsePlugin
{
Expand All @@ -53,11 +55,6 @@ class PaginatorPlugin extends ResponsePlugin
*/
protected $xRenderer;

/**
* @var string
*/
protected $sWrapperId = '';

/**
* The constructor.
*
Expand Down Expand Up @@ -85,19 +82,6 @@ public function getHash(): string
return '5.0.0';
}

/**
* Get the pagination wrapper HTML
*
* @param string $sWrapperId The pagination wrapper id
*
* @return string
*/
public function wrapper(string $sWrapperId): string
{
$this->sWrapperId = trim($sWrapperId);
return $this->sWrapperId;
}

/**
* @param array<Page> $aPages
*
Expand All @@ -122,18 +106,30 @@ private function _render(array $aPages): ?Store
}

/**
* Render an HTML pagination control.
* Create a paginator
*
* @param Call $xCall
* @param int $nCurrentPage The current page number
* @param int $nItemsPerPage The number of items per page
* @param int $nTotalItems The total number of items
*
* @return Paginator
*/
public function paginator(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator
{
return new Paginator($this, $nCurrentPage, $nItemsPerPage, $nTotalItems);
}

/**
* Render an HTML pagination control.
*
* @param Paginator $xPaginator
* @param Call $xCall
* @param string $sWrapperId
*
* @return void
*/
public function render(Call $xCall, int $nCurrentPage, int $nItemsPerPage, int $nTotalItems)
public function render(Paginator $xPaginator, Call $xCall, string $sWrapperId)
{
$xPaginator = new Paginator($nCurrentPage, $nItemsPerPage, $nTotalItems);
$aPages = $xPaginator->pages();
if(count($aPages) === 0)
{
Expand All @@ -150,15 +146,14 @@ public function render(Call $xCall, int $nCurrentPage, int $nItemsPerPage, int $
{
$xCall->addParameter(Parameter::PAGE_NUMBER, 0);
}

// Show the pagination links
$this->response()->html($this->sWrapperId, $xStore->__toString());
$sWrapperId = trim($sWrapperId);
$this->response()->html($sWrapperId, $xStore->__toString());
// Set click handlers on the pagination links
$this->addCommand('pg.paginate', [
'id' => $this->sWrapperId,
'call' => $xCall->toArray(),
// 'pages' => array_map(function(Page $xPage) {
// return ['type' => $xPage->sType, 'number' => $xPage->nNumber];
// }, $aPages),
'id' => $sWrapperId,
'func' => $xCall->toArray(),
]);
}
}
9 changes: 4 additions & 5 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
use Jaxon\Plugin\Response\JQuery\DomSelector;
use Jaxon\Plugin\Response\JQuery\JQueryPlugin;
use Jaxon\Plugin\Response\Pagination\Paginator;
use Jaxon\Plugin\Response\Pagination\PaginatorPlugin;
use Jaxon\Plugin\ResponsePlugin;
use Jaxon\Request\Call\Call;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Stream;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
Expand Down Expand Up @@ -180,18 +180,17 @@ public function bag(string $sName): DataBagContext
/**
* Render an HTML pagination control.
*
* @param Call $xCall
* @param int $nCurrentPage The current page number
* @param int $nItemsPerPage The number of items per page
* @param int $nTotalItems The total number of items
*
* @return void
* @return Paginator
*/
public function paginate(Call $xCall, int $nCurrentPage, int $nItemsPerPage, int $nTotalItems)
public function paginator(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator
{
/** @var PaginatorPlugin */
$xPlugin = $this->plugin('pg');
$xPlugin->render($xCall, $nCurrentPage, $nItemsPerPage, $nTotalItems);
return $xPlugin->paginator($nCurrentPage, $nItemsPerPage, $nTotalItems);
}

/**
Expand Down

0 comments on commit 6eb5e28

Please sign in to comment.