Skip to content

Commit

Permalink
Changed the confirm() method in the Response class.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed May 13, 2024
1 parent a5a8e29 commit 4cf44bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public function __construct(Psr17Factory $xPsr17Factory, PsrRequestInterface $xR
$this->xDialogManager = $xDialogManager;
}

/**
* @return DialogManager
*/
protected function dialog(): DialogManager
{
return $this->xDialogManager;
}

/**
* @inheritDoc
*/
Expand Down
13 changes: 7 additions & 6 deletions src/Response/ResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Jaxon\Plugin\ResponsePlugin;
use Jaxon\Request\Call\JsCall;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
use Closure;
use JsonSerializable;

interface ResponseInterface
Expand Down Expand Up @@ -287,16 +288,16 @@ public function sleep(int $tenths): ResponseInterface;
/**
* Response command that prompts user with [ok] [cancel] style message box
*
* If the user clicks cancel, the specified number of response commands
* following this one, will be skipped.
* The provided closure will be called with a response object as unique parameter.
* If the user clicks cancel, the response commands defined in the closure will be skipped.
*
* @param integer $nCommandCount The number of commands to skip upon cancel
* @param string $sQuestion The question to ask to the user
* @param array $aArgs The arguments for the placeholders in the question
* @param Closure $fCalls A closure that defines the commands that can be skipped
* @param string $sQuestion The question to ask to the user
* @param array $aArgs The arguments for the placeholders in the question
*
* @return ResponseInterface
*/
public function confirmCommands(int $nCommandCount, string $sMessage, array $aArgs = []): ResponseInterface;
public function confirm(Closure $fCalls, string $sQuestion, array $aArgs = []): ResponseInterface;

/**
* Add a command to display an alert message to the user
Expand Down
39 changes: 28 additions & 11 deletions src/Response/Traits/ScriptTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@

namespace Jaxon\Response\Traits;

use Jaxon\App\Dialog\DialogManager;
use Jaxon\Request\Call\JsCall;
use Jaxon\Response\ResponseInterface;
use Closure;
use JsonSerializable;

use function Jaxon\jaxon;
use function func_get_args;
use function array_shift;

Expand All @@ -40,6 +43,11 @@ abstract public function addCommand(string $sName, array|JsonSerializable $aOpti
*/
abstract protected function str($xData): string;

/**
* @return DialogManager
*/
abstract protected function dialog(): DialogManager;

/**
* Add a command to call the specified javascript function with the given (optional) parameters
*
Expand All @@ -57,21 +65,30 @@ public function call(string $sFunc): ResponseInterface
/**
* Response command that prompts user with [ok] [cancel] style message box
*
* If the user clicks cancel, the specified number of response commands
* following this one, will be skipped.
* The provided closure will be called with a response object as unique parameter.
* If the user clicks cancel, the response commands defined in the closure will be skipped.
*
* @param integer $nCommandCount The number of commands to skip upon cancel
* @param string $sQuestion The question to ask to the user
* @param array $aArgs The arguments for the placeholders in the question
* @param Closure $fCalls A closure that defines the commands that can be skipped
* @param string $sQuestion The question to ask to the user
* @param array $aArgs The arguments for the placeholders in the question
*
* @return ResponseInterface
*/
public function confirmCommands(int $nCommandCount, string $sQuestion, array $aArgs = []): ResponseInterface
public function confirm(Closure $fCalls, string $sQuestion, array $aArgs = []): ResponseInterface
{
return $this->addCommand('script.confirm', [
'count' => $nCommandCount,
'question' => $this->xDialogManager->confirm($this->str($sQuestion), $aArgs),
]);
$xResponse = jaxon()->newResponse();
$fCalls($xResponse);
if(($nCommandCount = $xResponse->getCommandCount()) > 0)
{
$this->addCommand('script.confirm', [
'count' => $nCommandCount,
'question' => $this->dialog()->confirm($this->str($sQuestion), $aArgs),
]);

// Append the provided commands
$this->appendResponse($xResponse);
}
return $this;
}

/**
Expand All @@ -84,7 +101,7 @@ public function confirmCommands(int $nCommandCount, string $sQuestion, array $aA
*/
public function alert(string $sMessage, array $aArgs = []): ResponseInterface
{
$this->addCommand('dialog.message', $this->xDialogManager->info($this->str($sMessage), $aArgs));
$this->addCommand('dialog.message', $this->dialog()->info($this->str($sMessage), $aArgs));
return $this;
}

Expand Down

0 comments on commit 4cf44bf

Please sign in to comment.