From e527efd3efddc6dc77116b2fd756e3d24dfbf8c7 Mon Sep 17 00:00:00 2001 From: Thierry Feuzeu Date: Wed, 15 May 2024 02:35:07 +0200 Subject: [PATCH] Convert JQuery selector and plugin to string. --- src/Di/Traits/PluginTrait.php | 3 +- src/Plugin/Response/JQuery/JQueryPlugin.php | 40 +-------------------- src/Request/Js/Call.php | 20 ++++++++--- src/Request/Js/Selector.php | 23 ++++++------ src/Request/Js/Selector/AttrGet.php | 3 +- src/Request/Js/Selector/AttrSet.php | 3 +- src/Request/Js/Selector/Event.php | 14 +++++++- src/start.php | 2 +- 8 files changed, 47 insertions(+), 61 deletions(-) diff --git a/src/Di/Traits/PluginTrait.php b/src/Di/Traits/PluginTrait.php index 2a44af9..0ab7c72 100644 --- a/src/Di/Traits/PluginTrait.php +++ b/src/Di/Traits/PluginTrait.php @@ -57,8 +57,7 @@ private function registerPlugins() // JQuery response plugin $this->set(JQueryPlugin::class, function($di) { - $jQueryNs = $di->g(ConfigManager::class)->getOption('core.jquery.no_conflict', false) ? 'jQuery' : '$'; - return new JQueryPlugin($jQueryNs); + return new JQueryPlugin(); }); // DataBag response plugin $this->set(DataBagPlugin::class, function($di) { diff --git a/src/Plugin/Response/JQuery/JQueryPlugin.php b/src/Plugin/Response/JQuery/JQueryPlugin.php index 2457e75..bd2e6ea 100644 --- a/src/Plugin/Response/JQuery/JQueryPlugin.php +++ b/src/Plugin/Response/JQuery/JQueryPlugin.php @@ -12,39 +12,6 @@ class JQueryPlugin extends ResponsePlugin */ const NAME = 'jquery'; - /** - * @var string - */ - protected $jQueryNs; - - /** - * True if the next selector is a command - * - * @var bool - */ - protected $bCommand = true; - - /** - * The class constructor - * - * @param string $jQueryNs - */ - public function __construct(string $jQueryNs) - { - $this->jQueryNs = $jQueryNs; - } - - /** - * @param bool $bCommand - * - * @return JQueryPlugin - */ - public function command(bool $bCommand): JQueryPlugin - { - $this->bCommand = $bCommand; - return $this; - } - /** * @inheritDoc */ @@ -76,12 +43,7 @@ public function getHash(): string public function selector(string $sPath = '', $xContext = null): Selector { $xSelector = new Selector($sPath, $xContext); - if($this->bCommand && $this->response() !== null) - { - $this->addCommand('jquery.call', ['selector' => $xSelector]); - } - // Reset the command value. - $this->bCommand = true; + $this->addCommand('jquery.call', ['selector' => $xSelector]); return $xSelector; } } diff --git a/src/Request/Js/Call.php b/src/Request/Js/Call.php index 13f7321..0767215 100644 --- a/src/Request/Js/Call.php +++ b/src/Request/Js/Call.php @@ -27,8 +27,10 @@ use function array_map; use function array_shift; use function func_get_args; +use function implode; +use function json_encode; -class Call +class Call implements JsonSerializable, Stringable { /** * @var DialogManager @@ -484,8 +486,6 @@ public function toArray(): array /** * Convert this call to array, when converting the response into json. * - * This is a method of the JsonSerializable interface. - * * @return array */ public function jsonSerialize(): array @@ -494,11 +494,21 @@ public function jsonSerialize(): array } /** - * Returns a string representation of the script output (javascript) from this request object + * Returns a call to jaxon as a string + * + * @return string + */ + public function __toString(): string + { + return 'jaxon.exec(' . json_encode($this->toArray()) . ')'; + } + + /** + * Returns the js code of the call * * @return string */ - public function __toString() + public function toJs(): string { $aParameters = array_map(function(Stringable $xParam) { return $xParam->__toString(); diff --git a/src/Request/Js/Selector.php b/src/Request/Js/Selector.php index 1e427a6..3fbb32c 100644 --- a/src/Request/Js/Selector.php +++ b/src/Request/Js/Selector.php @@ -21,6 +21,7 @@ namespace Jaxon\Request\Js; use function implode; +use function is_a; use function trim; class Selector implements ParameterInterface @@ -70,7 +71,7 @@ public function __construct(string $sPath, $xContext) */ public function getType(): string { - return 'selector'; + return 'expr'; } /** @@ -85,7 +86,6 @@ public function __call(string $sMethod, array $aArguments) { // Append the action into the array $this->aCalls[] = new Selector\Method($sMethod, $aArguments); - // Return $this so the calls can be chained return $this; } @@ -100,7 +100,6 @@ public function __get(string $sAttribute) { // Append the action into the array $this->aCalls[] = new Selector\AttrGet($sAttribute); - // Return $this so the calls can be chained return $this; } @@ -116,8 +115,7 @@ public function __set(string $sAttribute, $xValue) { // Append the action into the array $this->aCalls[] = new Selector\AttrSet($sAttribute, $xValue); - // No other call is allowed after a set - // return $this; + return $this; } /** @@ -126,11 +124,12 @@ public function __set(string $sAttribute, $xValue) * @param string $sName * @param Call $xHandler * - * @return void + * @return Selector */ - public function on(string $sName, Call $xHandler) + public function on(string $sName, Call $xHandler): Selector { $this->aCalls[] = new Selector\Event($sName, $xHandler); + return $this; } /** @@ -138,11 +137,12 @@ public function on(string $sName, Call $xHandler) * * @param Call $xHandler * - * @return void + * @return Selector */ - public function click(Call $xHandler) + public function click(Call $xHandler): Selector { $this->on('click', $xHandler); + return $this; } /** @@ -198,7 +198,8 @@ private function getPathAsArray() $aCall = ['_type' => 'select', '_name' => $sName]; if(($this->xContext)) { - $aCall['context'] = $this->xContext; + $aCall['context'] = is_a($this->xContext, self::class) ? + $this->xContext->jsonSerialize() :$this->xContext; } return $aCall; } @@ -221,7 +222,7 @@ public function toArray(): array 'args' => [[ '_type' => '_', '_name' => 'this' ]], ]; } - return ['_type' => 'expr', 'calls' => $aCalls]; + return ['_type' => $this->getType(), 'calls' => $aCalls]; } /** diff --git a/src/Request/Js/Selector/AttrGet.php b/src/Request/Js/Selector/AttrGet.php index 130aa9d..4c0d816 100644 --- a/src/Request/Js/Selector/AttrGet.php +++ b/src/Request/Js/Selector/AttrGet.php @@ -3,8 +3,9 @@ namespace Jaxon\Request\Js\Selector; use JsonSerializable; +use Stringable; -class AttrGet implements JsonSerializable +class AttrGet implements JsonSerializable, Stringable { /** * The attribute name diff --git a/src/Request/Js/Selector/AttrSet.php b/src/Request/Js/Selector/AttrSet.php index 8535311..6393685 100644 --- a/src/Request/Js/Selector/AttrSet.php +++ b/src/Request/Js/Selector/AttrSet.php @@ -5,8 +5,9 @@ use Jaxon\Request\Js\Parameter; use JsonSerializable; +use Stringable; -class AttrSet implements JsonSerializable +class AttrSet implements JsonSerializable, Stringable { /** * The attribute name diff --git a/src/Request/Js/Selector/Event.php b/src/Request/Js/Selector/Event.php index ecaddfd..c423669 100644 --- a/src/Request/Js/Selector/Event.php +++ b/src/Request/Js/Selector/Event.php @@ -3,8 +3,10 @@ namespace Jaxon\Request\Js\Selector; use Jaxon\Request\Js\Call; +use JsonSerializable; +use Stringable; -class Event +class Event implements JsonSerializable, Stringable { /** * @var string @@ -41,4 +43,14 @@ public function jsonSerialize(): array 'func' => $this->xHandler->jsonSerialize(), ]; } + + /** + * Returns a string representation of this call + * + * @return string + */ + public function __toString(): string + { + return ".on('{$this->sName}', () => { " . $this->xHandler->__toString() . '; })'; + } } diff --git a/src/start.php b/src/start.php index 15d04a4..fc9bcc0 100644 --- a/src/start.php +++ b/src/start.php @@ -68,7 +68,7 @@ function pm(): ParameterFactory */ function jq(string $sPath = '', $xContext = null): Selector { - return Ajax::getInstance()->di()->getJQueryPlugin()->command(false)->selector($sPath, $xContext); + return new Selector($sPath, $xContext); } // Register the Jaxon request and response plugins