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 js call parameter. |
12
|
|
|
* Therefore, the Selector class also implements the Jaxon\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\Js; |
22
|
|
|
|
23
|
|
|
use function implode; |
24
|
|
|
use function is_a; |
25
|
|
|
use function trim; |
26
|
|
|
|
27
|
|
|
class Selector implements ParameterInterface |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The jQuery selector path |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $sPath; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The jQuery selector context |
38
|
|
|
* |
39
|
|
|
* @var mixed |
40
|
|
|
*/ |
41
|
|
|
protected $xContext; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The actions to be applied on the selected element |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $aCalls = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Convert the selector value to integer |
52
|
|
|
* |
53
|
|
|
* @var bool |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
protected $bToInt = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The constructor. |
59
|
|
|
* |
60
|
|
|
* @param string $sPath The jQuery selector path |
61
|
|
|
* @param mixed $xContext A context associated to the selector |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function __construct(string $sPath, $xContext) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->sPath = trim($sPath, " \t"); |
|
|
|
|
66
|
|
|
$this->xContext = $xContext; |
67
|
|
|
} |
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
|
|
|
|
72
|
|
|
public function getType(): string |
73
|
|
|
{ |
74
|
|
|
return 'expr'; |
75
|
|
|
} |
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add a call to a jQuery method on the selected elements |
79
|
|
|
* |
80
|
|
|
* @param string $sMethod |
|
|
|
|
81
|
|
|
* @param array $aArguments |
|
|
|
|
82
|
|
|
* |
83
|
|
|
* @return Selector |
84
|
|
|
*/ |
85
|
|
|
public function __call(string $sMethod, array $aArguments) |
86
|
|
|
{ |
87
|
|
|
// Append the action into the array |
88
|
|
|
$this->aCalls[] = new Selector\Method($sMethod, $aArguments); |
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; |
104
|
|
|
} |
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the value of an attribute on the first selected element |
108
|
|
|
* |
109
|
|
|
* @param string $sAttribute |
|
|
|
|
110
|
|
|
* @param mixed $xValue |
|
|
|
|
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function __set(string $sAttribute, $xValue) |
115
|
|
|
{ |
116
|
|
|
// Append the action into the array |
117
|
|
|
$this->aCalls[] = new Selector\AttrSet($sAttribute, $xValue); |
118
|
|
|
return $this; |
|
|
|
|
119
|
|
|
} |
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set an event handler on the first selected element |
123
|
|
|
* |
124
|
|
|
* @param string $sName |
|
|
|
|
125
|
|
|
* @param Call $xHandler |
|
|
|
|
126
|
|
|
* |
127
|
|
|
* @return Selector |
128
|
|
|
*/ |
129
|
|
|
public function on(string $sName, Call $xHandler): Selector |
130
|
|
|
{ |
131
|
|
|
$this->aCalls[] = new Selector\Event($sName, $xHandler); |
132
|
|
|
return $this; |
133
|
|
|
} |
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set an "click" event handler on the first selected element |
137
|
|
|
* |
138
|
|
|
* @param Call $xHandler |
|
|
|
|
139
|
|
|
* |
140
|
|
|
* @return Selector |
141
|
|
|
*/ |
142
|
|
|
public function click(Call $xHandler): Selector |
143
|
|
|
{ |
144
|
|
|
$this->on('click', $xHandler); |
145
|
|
|
return $this; |
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'] = is_a($this->xContext, self::class) ? |
|
|
|
|
202
|
|
|
$this->xContext->jsonSerialize() :$this->xContext; |
|
|
|
|
203
|
|
|
} |
204
|
|
|
return $aCall; |
205
|
|
|
} |
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Generate the jQuery call, when converting the response into json. |
209
|
|
|
* |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
public function jsonSerialize(): array |
213
|
|
|
{ |
214
|
|
|
$aCalls = [$this->getPathAsArray()]; |
215
|
|
|
foreach($this->aCalls as $xCall) |
216
|
|
|
{ |
217
|
|
|
$aCalls[] = $xCall->jsonSerialize(); |
218
|
|
|
} |
219
|
|
|
if($this->bToInt) |
220
|
|
|
{ |
221
|
|
|
$aCalls[] = [ |
222
|
|
|
'_type' => 'method', |
223
|
|
|
'_name' => 'toInt', |
224
|
|
|
'args' => [], |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
return ['_type' => $this->getType(), 'calls' => $aCalls]; |
228
|
|
|
} |
|
|
|
|
229
|
|
|
} |
230
|
|
|
|