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