Skip to content

Commit

Permalink
Convert JQuery selector and plugin to string.
Browse files Browse the repository at this point in the history
  • Loading branch information
feuzeu committed May 15, 2024
1 parent 87a13ba commit e527efd
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 61 deletions.
3 changes: 1 addition & 2 deletions src/Di/Traits/PluginTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 1 addition & 39 deletions src/Plugin/Response/JQuery/JQueryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
20 changes: 15 additions & 5 deletions src/Request/Js/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down
23 changes: 12 additions & 11 deletions src/Request/Js/Selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace Jaxon\Request\Js;

use function implode;
use function is_a;
use function trim;

class Selector implements ParameterInterface
Expand Down Expand Up @@ -70,7 +71,7 @@ public function __construct(string $sPath, $xContext)
*/
public function getType(): string
{
return 'selector';
return 'expr';
}

/**
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand All @@ -126,23 +124,25 @@ 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;
}

/**
* Set an "click" event handler on the first selected element
*
* @param Call $xHandler
*
* @return void
* @return Selector
*/
public function click(Call $xHandler)
public function click(Call $xHandler): Selector
{
$this->on('click', $xHandler);
return $this;
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -221,7 +222,7 @@ public function toArray(): array
'args' => [[ '_type' => '_', '_name' => 'this' ]],
];
}
return ['_type' => 'expr', 'calls' => $aCalls];
return ['_type' => $this->getType(), 'calls' => $aCalls];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Request/Js/Selector/AttrGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Request/Js/Selector/AttrSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/Request/Js/Selector/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() . '; })';
}
}
2 changes: 1 addition & 1 deletion src/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e527efd

Please sign in to comment.