1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Selector.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 Selector object must be converted to the corresponding jQuery code. |
9
|
|
|
* Therefore, the Selector class implements the JsonSerializable interface. |
10
|
|
|
* |
11
|
|
|
* When used as a parameter of a Jaxon call, the Selector must be converted to Jaxon request parameter. |
12
|
|
|
* Therefore, the Selector class also implements the Jaxon\Request\Js\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\Request\Js; |
22
|
|
|
|
23
|
|
|
use function implode; |
24
|
|
|
use function trim; |
25
|
|
|
|
26
|
|
|
class Selector implements ParameterInterface |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* The jQuery selector path |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $sPath; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The jQuery selector context |
37
|
|
|
* |
38
|
|
|
* @var mixed |
39
|
|
|
*/ |
40
|
|
|
protected $xContext; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The actions to be applied on the selected element |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $aCalls = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Convert the selector value to integer |
51
|
|
|
* |
52
|
|
|
* @var bool |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
protected $bToInt = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The constructor. |
58
|
|
|
* |
59
|
|
|
* @param string $sPath The jQuery selector path |
60
|
|
|
* @param mixed $xContext A context associated to the selector |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function __construct(string $sPath, $xContext) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->sPath = trim($sPath, " \t"); |
|
|
|
|
65
|
|
|
$this->xContext = $xContext; |
66
|
|
|
} |
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
|
|
|
|
71
|
|
|
public function getType(): string |
72
|
|
|
{ |
73
|
|
|
return 'selector'; |
74
|
|
|
} |
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add a call to a jQuery method on the selected elements |
78
|
|
|
* |
79
|
|
|
* @param string $sMethod |
|
|
|
|
80
|
|
|
* @param array $aArguments |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @return Selector |
83
|
|
|
*/ |
84
|
|
|
public function __call(string $sMethod, array $aArguments) |
85
|
|
|
{ |
86
|
|
|
// Append the action into the array |
87
|
|
|
$this->aCalls[] = new Selector\Method($sMethod, $aArguments); |
88
|
|
|
// Return $this so the calls can be chained |
89
|
|
|
return $this; |
90
|
|
|
} |
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the value of an attribute on the first selected element |
94
|
|
|
* |
95
|
|
|
* @param string $sAttribute |
|
|
|
|
96
|
|
|
* |
97
|
|
|
* @return Selector |
98
|
|
|
*/ |
99
|
|
|
public function __get(string $sAttribute) |
100
|
|
|
{ |
101
|
|
|
// Append the action into the array |
102
|
|
|
$this->aCalls[] = new Selector\AttrGet($sAttribute); |
103
|
|
|
// Return $this so the calls can be chained |
104
|
|
|
return $this; |
105
|
|
|
} |
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the value of an attribute on the first selected element |
109
|
|
|
* |
110
|
|
|
* @param string $sAttribute |
|
|
|
|
111
|
|
|
* @param mixed $xValue |
|
|
|
|
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function __set(string $sAttribute, $xValue) |
116
|
|
|
{ |
117
|
|
|
// Append the action into the array |
118
|
|
|
$this->aCalls[] = new Selector\AttrSet($sAttribute, $xValue); |
119
|
|
|
// No other call is allowed after a set |
120
|
|
|
// return $this; |
121
|
|
|
} |
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set an event handler on the first selected element |
125
|
|
|
* |
126
|
|
|
* @param string $sName |
|
|
|
|
127
|
|
|
* @param Call $xHandler |
|
|
|
|
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function on(string $sName, Call $xHandler) |
132
|
|
|
{ |
133
|
|
|
$this->aCalls[] = new Selector\Event($sName, $xHandler); |
134
|
|
|
} |
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set an "click" event handler on the first selected element |
138
|
|
|
* |
139
|
|
|
* @param Call $xHandler |
|
|
|
|
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function click(Call $xHandler) |
144
|
|
|
{ |
145
|
|
|
$this->on('click', $xHandler); |
146
|
|
|
} |
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return Selector |
150
|
|
|
*/ |
151
|
|
|
public function toInt(): Selector |
152
|
|
|
{ |
153
|
|
|
$this->bToInt = true; |
154
|
|
|
return $this; |
155
|
|
|
} |
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the selector js. |
159
|
|
|
* |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
|
|
private function getPathAsStr() |
163
|
|
|
{ |
164
|
|
|
$jQuery = 'jaxon.jq'; // The JQuery selector |
165
|
|
|
if(!$this->sPath) |
166
|
|
|
{ |
167
|
|
|
// If an empty selector is given, use the event target instead |
168
|
|
|
return "$jQuery(e.currentTarget)"; |
169
|
|
|
} |
170
|
|
|
if(!$this->xContext) |
171
|
|
|
{ |
172
|
|
|
return "$jQuery('" . $this->sPath . "')"; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$sContext = is_a($this->xContext, self::class) ? |
|
|
|
|
176
|
|
|
$this->xContext->getScript() : |
|
|
|
|
177
|
|
|
"$jQuery('" . trim("{$this->xContext}") . "')"; |
178
|
|
|
return "$jQuery('{$this->sPath}', $sContext)"; |
179
|
|
|
} |
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Generate the jQuery call. |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function __toString(): string |
187
|
|
|
{ |
188
|
|
|
$sScript = $this->getPathAsStr() . implode('', $this->aCalls); |
189
|
|
|
return $this->bToInt ? "parseInt($sScript)" : $sScript; |
190
|
|
|
} |
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
|
|
private function getPathAsArray() |
196
|
|
|
{ |
197
|
|
|
$sName = $this->sPath ?? 'this'; |
198
|
|
|
$aCall = ['_type' => 'select', '_name' => $sName]; |
199
|
|
|
if(($this->xContext)) |
200
|
|
|
{ |
201
|
|
|
$aCall['context'] = $this->xContext; |
202
|
|
|
} |
203
|
|
|
return $aCall; |
204
|
|
|
} |
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function toArray(): array |
210
|
|
|
{ |
211
|
|
|
$aCalls = [$this->getPathAsArray()]; |
212
|
|
|
foreach($this->aCalls as $xCall) |
213
|
|
|
{ |
214
|
|
|
$aCalls[] = $xCall->jsonSerialize(); |
215
|
|
|
} |
216
|
|
|
if($this->bToInt) |
217
|
|
|
{ |
218
|
|
|
$aCalls[] = [ |
219
|
|
|
'_type' => 'func', |
220
|
|
|
'_name' => 'toInt', |
221
|
|
|
'args' => [[ '_type' => '_', '_name' => 'this' ]], |
222
|
|
|
]; |
223
|
|
|
} |
224
|
|
|
return ['_type' => 'expr', 'calls' => $aCalls]; |
225
|
|
|
} |
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Generate the jQuery call, when converting the response into json. |
229
|
|
|
* This is a method of the JsonSerializable interface. |
230
|
|
|
* |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
public function jsonSerialize(): array |
234
|
|
|
{ |
235
|
|
|
return $this->toArray(); |
236
|
|
|
} |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|