|
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 function array_merge; |
|
30
|
|
|
use function count; |
|
31
|
|
|
use function is_a; |
|
32
|
|
|
use function trim; |
|
33
|
|
|
|
|
34
|
|
|
class DomSelector implements ParameterInterface |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* The jQuery selector path |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $sPath; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The jQuery selector path |
|
45
|
|
|
* |
|
46
|
|
|
* @var mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $xContext; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The actions to be applied on the selected element |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $aCalls; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Convert the selector value to integer |
|
59
|
|
|
* |
|
60
|
|
|
* @var bool |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
protected $bToInt = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* True if this selector is a callback |
|
66
|
|
|
* |
|
67
|
|
|
* @var bool|null |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
protected $bIsCallback = null; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The constructor. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $jQueryNs The jQuery symbol |
|
|
|
|
|
|
75
|
|
|
* @param string $sPath The jQuery selector path |
|
|
|
|
|
|
76
|
|
|
* @param mixed $xContext A context associated to the selector |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
public function __construct(string $sPath, $xContext) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$this->sPath = trim($sPath, " \t"); |
|
|
|
|
|
|
81
|
|
|
$this->xContext = $xContext; |
|
82
|
|
|
$this->aCalls = []; |
|
|
|
|
|
|
83
|
|
|
} |
|
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritDoc |
|
86
|
|
|
*/ |
|
|
|
|
|
|
87
|
|
|
public function getType(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return 'select'; |
|
90
|
|
|
} |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Add a call to a jQuery method on the selected elements |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $sMethod |
|
|
|
|
|
|
96
|
|
|
* @param array $aArguments |
|
|
|
|
|
|
97
|
|
|
* |
|
98
|
|
|
* @return DomSelector |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __call(string $sMethod, array $aArguments) |
|
101
|
|
|
{ |
|
102
|
|
|
if(count($aArguments) === 1) |
|
103
|
|
|
{ |
|
104
|
|
|
// If the only parameter is a selector, and the first call |
|
105
|
|
|
// on that selector is a method, then the selector is a callback. |
|
106
|
|
|
$xArgument = $aArguments[0]; |
|
107
|
|
|
if(is_a($xArgument, self::class) && $xArgument->bIsCallback === null && |
|
108
|
|
|
count($xArgument->aCalls) > 0 && is_a($xArgument->aCalls[0], JsCall::class)) |
|
109
|
|
|
{ |
|
110
|
|
|
$xArgument->bIsCallback = true; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
// Push the action into the array |
|
114
|
|
|
$this->aCalls[] = new Method($sMethod, $aArguments); |
|
115
|
|
|
// Return $this so the calls can be chained |
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the value of an attribute on the first selected element |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $sAttribute |
|
|
|
|
|
|
123
|
|
|
* |
|
124
|
|
|
* @return DomSelector |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __get(string $sAttribute) |
|
127
|
|
|
{ |
|
128
|
|
|
// Push the action into the array |
|
129
|
|
|
$this->aCalls[] = new AttrGet($sAttribute); |
|
130
|
|
|
// Return $this so the calls can be chained |
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set the value of an attribute on the first selected element |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $sAttribute |
|
|
|
|
|
|
138
|
|
|
* @param $xValue |
|
|
|
|
|
|
139
|
|
|
* |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
|
public function __set(string $sAttribute, $xValue) |
|
143
|
|
|
{ |
|
144
|
|
|
// Push the action into the array |
|
145
|
|
|
$this->aCalls[] = new AttrSet($sAttribute, $xValue); |
|
146
|
|
|
// No other call is allowed after a set |
|
147
|
|
|
// return $this; |
|
148
|
|
|
} |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Explicitely declare the selector as a callback. |
|
152
|
|
|
* |
|
153
|
|
|
* @param bool $bIsCallback |
|
|
|
|
|
|
154
|
|
|
* |
|
155
|
|
|
* @return DomSelector |
|
156
|
|
|
*/ |
|
157
|
|
|
public function cb(bool $bIsCallback = true): DomSelector |
|
158
|
|
|
{ |
|
159
|
|
|
$this->bIsCallback = $bIsCallback; |
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return DomSelector |
|
165
|
|
|
*/ |
|
166
|
|
|
public function toInt(): DomSelector |
|
167
|
|
|
{ |
|
168
|
|
|
$this->bToInt = true; |
|
169
|
|
|
return $this; |
|
170
|
|
|
} |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
|
|
private function selectCalls() |
|
176
|
|
|
{ |
|
177
|
|
|
if(!$this->sPath) |
|
178
|
|
|
{ |
|
179
|
|
|
// If an empty selector is given, use the event target instead |
|
180
|
|
|
return [['_type' => 'select', '_name' => 'this']]; |
|
181
|
|
|
} |
|
182
|
|
|
if(!$this->xContext) |
|
183
|
|
|
{ |
|
184
|
|
|
return [['_type' => 'select', '_name' => $this->sPath]]; |
|
185
|
|
|
} |
|
186
|
|
|
// Todo: chain the 2 selectors. |
|
187
|
|
|
return [ |
|
188
|
|
|
// ['_type' => 'select', '_name' => $this->xContext], |
|
189
|
|
|
['_type' => 'select', '_name' => $this->sPath], |
|
190
|
|
|
]; |
|
191
|
|
|
} |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param Method|AttrSet|AttrGet $xParam |
|
|
|
|
|
|
195
|
|
|
*/ |
|
|
|
|
|
|
196
|
|
|
private function makeCallsArray($xParam): array |
|
197
|
|
|
{ |
|
198
|
|
|
if(!is_a($xParam, Method::class)) |
|
199
|
|
|
{ |
|
200
|
|
|
// Return an array of array. |
|
201
|
|
|
return [$xParam->jsonSerialize()]; |
|
202
|
|
|
} |
|
203
|
|
|
// The param is serialized to an array of arrays. |
|
204
|
|
|
$aCalls = $xParam->jsonSerialize(); |
|
205
|
|
|
// Set the correct type on the first call. |
|
206
|
|
|
$aCalls[0]['_type'] = $this->bIsCallback ? 'event' : 'method'; |
|
207
|
|
|
return $aCalls; |
|
208
|
|
|
} |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return array |
|
212
|
|
|
*/ |
|
213
|
|
|
public function toArray(): array |
|
214
|
|
|
{ |
|
215
|
|
|
$aCalls = $this->selectCalls(); |
|
216
|
|
|
foreach($this->aCalls as $xCall) |
|
217
|
|
|
{ |
|
218
|
|
|
$aCalls = array_merge($aCalls, $this->makeCallsArray($xCall)); |
|
219
|
|
|
} |
|
220
|
|
|
if($this->bToInt) |
|
221
|
|
|
{ |
|
222
|
|
|
$aCalls[] = [ |
|
223
|
|
|
'_type' => 'func', |
|
224
|
|
|
'_name' => 'jaxon.utils.string.toInt', |
|
225
|
|
|
'params' => [ |
|
226
|
|
|
[ '_type' => '_', '_name' => 'this' ], |
|
227
|
|
|
], |
|
228
|
|
|
]; |
|
229
|
|
|
} |
|
230
|
|
|
return $aCalls; |
|
231
|
|
|
} |
|
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Generate the jQuery call, when converting the response into json. |
|
235
|
|
|
* |
|
236
|
|
|
* This is a method of the JsonSerializable interface. |
|
237
|
|
|
* |
|
238
|
|
|
* @return array |
|
239
|
|
|
*/ |
|
240
|
|
|
public function jsonSerialize(): array |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->toArray(); |
|
243
|
|
|
} |
|
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
|