1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jaxon\Script\Factory; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* CallFactory.php |
7
|
|
|
* |
8
|
|
|
* Creates calls to js functions and selectors. |
9
|
|
|
* |
10
|
|
|
* @package jaxon-core |
11
|
|
|
* @author Thierry Feuzeu <[email protected]> |
12
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
13
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
14
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use Jaxon\Plugin\Response\Dialog\DialogCommand; |
18
|
|
|
use Jaxon\Di\ClassContainer; |
19
|
|
|
use Jaxon\Exception\SetupException; |
20
|
|
|
use Jaxon\Script\JqCall; |
21
|
|
|
use Jaxon\Script\JsCall; |
22
|
|
|
use Jaxon\Script\JxnCall; |
23
|
|
|
use Closure; |
24
|
|
|
|
25
|
|
|
use function trim; |
26
|
|
|
|
27
|
|
|
class CallFactory |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The constructor. |
31
|
|
|
* |
32
|
|
|
* @param ClassContainer $cls |
33
|
|
|
* @param DialogCommand $xDialog |
34
|
|
|
*/ |
35
|
|
|
public function __construct(private ClassContainer $cls, private DialogCommand $xDialog) |
36
|
|
|
{} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get a factory for a js function call. |
40
|
|
|
* |
41
|
|
|
* @param string $sClassName |
42
|
|
|
* |
43
|
|
|
* @return JxnCall|null |
44
|
|
|
* @throws SetupException |
45
|
|
|
*/ |
46
|
|
|
public function rq(string $sClassName = ''): ?JxnCall |
47
|
|
|
{ |
48
|
|
|
return $this->cls->getRequestFactory($sClassName); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get a factory for a js function call. |
53
|
|
|
* |
54
|
|
|
* @param string $sObject |
55
|
|
|
* @param Closure|null $xExprCb |
56
|
|
|
* |
57
|
|
|
* @return JsCall|null |
58
|
|
|
*/ |
59
|
|
|
public function js(string $sObject = '', ?Closure $xExprCb = null): ?JsCall |
60
|
|
|
{ |
61
|
|
|
/* |
62
|
|
|
* The provided closure will be called each time a js expression is created with this factory, |
63
|
|
|
* with the expression as the only parameter. |
64
|
|
|
* It is currently used to attach the expression to a Jaxon response. |
65
|
|
|
*/ |
66
|
|
|
return new JsCall($this->xDialog, $xExprCb, trim($sObject, " \t")); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get a factory for a JQuery selector. |
71
|
|
|
* |
72
|
|
|
* @param string $sPath The jQuery selector path |
73
|
|
|
* @param mixed $xContext A context associated to the selector |
74
|
|
|
* @param Closure|null $xExprCb |
75
|
|
|
* |
76
|
|
|
* @return JqCall |
77
|
|
|
*/ |
78
|
|
|
public function jq(string $sPath = '', $xContext = null, ?Closure $xExprCb = null): JqCall |
79
|
|
|
{ |
80
|
|
|
/* |
81
|
|
|
* The provided closure will be called each time a js expression is created with this factory, |
82
|
|
|
* with the expression as the only parameter. |
83
|
|
|
* It is currently used to attach the expression to a Jaxon response. |
84
|
|
|
*/ |
85
|
|
|
return new JqCall($this->xDialog, $xExprCb, trim($sPath, " \t"), $xContext); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|