1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* DomSelector.php - A jQuery selector |
5
|
|
|
* |
6
|
|
|
* This class is used to create client side requests to the Jaxon functions and callable objects. |
7
|
|
|
* |
8
|
|
|
* When inserted into a Jaxon response, a DomSelector object must be converted to the corresponding jQuery code. |
9
|
|
|
* Therefore, the DomSelector class implements the JsonSerializable interface. |
10
|
|
|
* |
11
|
|
|
* When used as a parameter of a Jaxon call, the DomSelector must be converted to Jaxon request parameter. |
12
|
|
|
* Therefore, the DomSelector class also implements the Jaxon\Request\Call\ParameterInterface interface. |
13
|
|
|
* |
14
|
|
|
* @package jaxon-jquery |
|
|
|
|
15
|
|
|
* @author Thierry Feuzeu <[email protected]> |
16
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
18
|
|
|
* @link https://github.com/jaxon-php/jaxon-jquery |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace Jaxon\Plugin\Response\JQuery; |
22
|
|
|
|
23
|
|
|
use Jaxon\Plugin\Response\JQuery\Call\AttrGet; |
24
|
|
|
use Jaxon\Plugin\Response\JQuery\Call\AttrSet; |
25
|
|
|
use Jaxon\Plugin\Response\JQuery\Call\Method; |
26
|
|
|
use Jaxon\Request\Call\JsCall; |
27
|
|
|
use Jaxon\Request\Call\ParameterInterface; |
28
|
|
|
|
29
|
|
|
use JsonSerializable; |
30
|
|
|
|
31
|
|
|
use function count; |
32
|
|
|
use function implode; |
33
|
|
|
use function is_a; |
34
|
|
|
use function trim; |
35
|
|
|
|
36
|
|
|
class DomSelector implements JsonSerializable, ParameterInterface |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* The jQuery selector path |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $sPath; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The actions to be applied on the selected element |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $aCalls; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Convert the selector value to integer |
54
|
|
|
* |
55
|
|
|
* @var bool |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
protected $bToInt = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* True if this selector is a callback |
61
|
|
|
* |
62
|
|
|
* @var bool|null |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
protected $bIsCallback = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The constructor. |
68
|
|
|
* |
69
|
|
|
* @param string $jQueryNs The jQuery symbol |
|
|
|
|
70
|
|
|
* @param string $sPath The jQuery selector path |
71
|
|
|
* @param mixed $xContext A context associated to the selector |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
public function __construct(string $jQueryNs, string $sPath, $xContext) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$sPath = trim($sPath, " \t"); |
|
|
|
|
76
|
|
|
$this->aCalls = []; |
77
|
|
|
$this->sPath = $this->getPath($jQueryNs, $sPath, $xContext); |
78
|
|
|
} |
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the selector js. |
82
|
|
|
* |
83
|
|
|
* @param string $jQueryNs The jQuery symbol |
|
|
|
|
84
|
|
|
* @param string $sPath The jQuery selector path |
85
|
|
|
* @param mixed $xContext A context associated to the selector |
|
|
|
|
86
|
|
|
*/ |
|
|
|
|
87
|
|
|
private function getPath(string $jQueryNs, string $sPath, $xContext) |
88
|
|
|
{ |
89
|
|
|
if(!$sPath) |
90
|
|
|
{ |
91
|
|
|
// If an empty selector is given, use the event target instead |
92
|
|
|
return "$jQueryNs(e.currentTarget)"; |
93
|
|
|
} |
94
|
|
|
if(!$xContext) |
95
|
|
|
{ |
96
|
|
|
return "$jQueryNs('" . $sPath . "')"; |
97
|
|
|
} |
98
|
|
|
$sContext = is_a($xContext, self::class) ? |
|
|
|
|
99
|
|
|
$xContext->getScript() : "$jQueryNs('" . trim("$xContext") . "')"; |
100
|
|
|
return "$jQueryNs('$sPath', $sContext)"; |
101
|
|
|
} |
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add a call to a jQuery method on the selected elements |
105
|
|
|
* |
106
|
|
|
* @param string $sMethod |
|
|
|
|
107
|
|
|
* @param array $aArguments |
|
|
|
|
108
|
|
|
* |
109
|
|
|
* @return DomSelector |
110
|
|
|
*/ |
111
|
|
|
public function __call(string $sMethod, array $aArguments) |
112
|
|
|
{ |
113
|
|
|
if(count($aArguments) === 1) |
114
|
|
|
{ |
115
|
|
|
// If the only parameter is a selector, and the first call |
116
|
|
|
// on that selector is a method, then the selector is a callback. |
117
|
|
|
$xArgument = $aArguments[0]; |
118
|
|
|
if(is_a($xArgument, self::class) && $xArgument->bIsCallback === null && |
119
|
|
|
count($xArgument->aCalls) > 0 && is_a($xArgument->aCalls[0], JsCall::class)) |
120
|
|
|
{ |
121
|
|
|
$xArgument->bIsCallback = true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
// Push the action into the array |
125
|
|
|
$this->aCalls[] = new Method($sMethod, $aArguments); |
126
|
|
|
// Return $this so the calls can be chained |
127
|
|
|
return $this; |
128
|
|
|
} |
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the value of an attribute on the first selected element |
132
|
|
|
* |
133
|
|
|
* @param string $sAttribute |
|
|
|
|
134
|
|
|
* |
135
|
|
|
* @return DomSelector |
136
|
|
|
*/ |
137
|
|
|
public function __get(string $sAttribute) |
138
|
|
|
{ |
139
|
|
|
// Push the action into the array |
140
|
|
|
$this->aCalls[] = new AttrGet($sAttribute); |
141
|
|
|
// Return $this so the calls can be chained |
142
|
|
|
return $this; |
143
|
|
|
} |
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set the value of an attribute on the first selected element |
147
|
|
|
* |
148
|
|
|
* @param string $sAttribute |
|
|
|
|
149
|
|
|
* @param $xValue |
|
|
|
|
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function __set(string $sAttribute, $xValue) |
154
|
|
|
{ |
155
|
|
|
// Push the action into the array |
156
|
|
|
$this->aCalls[] = new AttrSet($sAttribute, $xValue); |
157
|
|
|
// No other call is allowed after a set |
158
|
|
|
// return $this; |
159
|
|
|
} |
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Explicitely declare the selector as a callback. |
163
|
|
|
* |
164
|
|
|
* @param bool $bIsCallback |
|
|
|
|
165
|
|
|
* |
166
|
|
|
* @return DomSelector |
167
|
|
|
*/ |
168
|
|
|
public function cb(bool $bIsCallback = true): DomSelector |
169
|
|
|
{ |
170
|
|
|
$this->bIsCallback = $bIsCallback; |
171
|
|
|
return $this; |
172
|
|
|
} |
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return DomSelector |
176
|
|
|
*/ |
177
|
|
|
public function toInt(): DomSelector |
178
|
|
|
{ |
179
|
|
|
$this->bToInt = true; |
180
|
|
|
return $this; |
181
|
|
|
} |
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Generate the jQuery call. |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public function getScript(): string |
189
|
|
|
{ |
190
|
|
|
$sScript = $this->sPath; |
191
|
|
|
if(count($this->aCalls) > 0) |
192
|
|
|
{ |
193
|
|
|
$sScript .= '.' . implode('.', $this->aCalls); |
194
|
|
|
} |
195
|
|
|
return $this->bIsCallback ? '(e) => {' . $sScript . '}' : |
|
|
|
|
196
|
|
|
($this->bToInt ? "parseInt($sScript)" : $sScript); |
197
|
|
|
} |
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Magic function to generate the jQuery call. |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
public function __toString() |
205
|
|
|
{ |
206
|
|
|
return $this->getScript(); |
207
|
|
|
} |
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Generate the jQuery call, when converting the response into json. |
211
|
|
|
* |
212
|
|
|
* This is a method of the JsonSerializable interface. |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
public function jsonSerialize(): string |
217
|
|
|
{ |
218
|
|
|
return $this->getScript(); |
219
|
|
|
} |
|
|
|
|
220
|
|
|
} |
221
|
|
|
|