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