|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ScriptPlugin.php |
|
5
|
|
|
* |
|
6
|
|
|
* Adds Javascript selector and function call commands to the Jaxon response. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
9
|
|
|
* @copyright 2024 Thierry Feuzeu |
|
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
|
11
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Jaxon\Plugin\Response\Script; |
|
15
|
|
|
|
|
16
|
|
|
use Jaxon\Plugin\AbstractResponsePlugin; |
|
17
|
|
|
use Jaxon\Response\AjaxResponse; |
|
18
|
|
|
use Jaxon\Response\ComponentResponse; |
|
19
|
|
|
use Jaxon\Script\Factory\CallFactory; |
|
20
|
|
|
use Jaxon\Script\JqCall; |
|
21
|
|
|
use Jaxon\Script\JsExpr; |
|
22
|
|
|
use Jaxon\Script\JsCall; |
|
23
|
|
|
use Closure; |
|
24
|
|
|
|
|
25
|
|
|
use function is_a; |
|
26
|
|
|
|
|
27
|
|
|
class ScriptPlugin extends AbstractResponsePlugin |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @const The plugin name |
|
31
|
|
|
*/ |
|
32
|
|
|
const NAME = 'script'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The class constructor |
|
36
|
|
|
* |
|
37
|
|
|
* @param CallFactory $xFactory |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(private CallFactory $xFactory) |
|
40
|
|
|
{} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param JsExpr $xJsExpr |
|
44
|
|
|
* @param AjaxResponse $xResponse |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
private function _addCommand(JsExpr $xJsExpr, AjaxResponse $xResponse) |
|
49
|
|
|
{ |
|
50
|
|
|
// Add the newly created expression to the response |
|
51
|
|
|
$xResponse |
|
52
|
|
|
->addCommand('script.exec.expr', [ |
|
53
|
|
|
'expr' => $xJsExpr, |
|
54
|
|
|
'context' => [ |
|
55
|
|
|
'component' => is_a($xResponse, ComponentResponse::class), |
|
56
|
|
|
], |
|
57
|
|
|
]) |
|
58
|
|
|
->setOption('plugin', $this->getName()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return Closure |
|
63
|
|
|
*/ |
|
64
|
|
|
private function getCallback(): Closure |
|
65
|
|
|
{ |
|
66
|
|
|
// The closure needs to capture the response object the script plugin is called with. |
|
67
|
|
|
$xResponse = $this->response(); |
|
68
|
|
|
return function(JsExpr $xJsExpr) use($xResponse) { |
|
69
|
|
|
$this->_addCommand($xJsExpr, $xResponse); |
|
70
|
|
|
}; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritDoc |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getName(): string |
|
77
|
|
|
{ |
|
78
|
|
|
return self::NAME; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @inheritDoc |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getHash(): string |
|
85
|
|
|
{ |
|
86
|
|
|
// Use the version number as hash |
|
87
|
|
|
return '5.0.0'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Create a JQuery selector expression, and link it to the current response. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $sPath The jQuery selector path |
|
94
|
|
|
* @param mixed $xContext A context associated to the selector |
|
95
|
|
|
* |
|
96
|
|
|
* @return JqCall |
|
97
|
|
|
*/ |
|
98
|
|
|
public function jq(string $sPath = '', $xContext = null): JqCall |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->xFactory->jq($sPath, $xContext, $this->getCallback()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Create a js expression, and link it to the current response. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $sObject |
|
107
|
|
|
* |
|
108
|
|
|
* @return JsCall |
|
109
|
|
|
*/ |
|
110
|
|
|
public function js(string $sObject = ''): JsCall |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->xFactory->js($sObject, $this->getCallback()); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|