|
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\Contracts\Parameter 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\Response\Plugin\JQuery; |
|
22
|
|
|
|
|
23
|
|
|
use Jaxon\Request\Call\Contracts\Parameter; |
|
24
|
|
|
use Jaxon\Response\Plugin\JQuery\Call\AttrGet; |
|
25
|
|
|
use Jaxon\Response\Plugin\JQuery\Call\AttrSet; |
|
26
|
|
|
use Jaxon\Response\Plugin\JQuery\Call\Method; |
|
27
|
|
|
|
|
28
|
|
|
use JsonSerializable; |
|
29
|
|
|
|
|
30
|
|
|
use function count; |
|
31
|
|
|
use function implode; |
|
32
|
|
|
use function trim; |
|
33
|
|
|
|
|
34
|
|
|
class DomSelector implements JsonSerializable, Parameter |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* The jQuery selector path |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $sPath; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The actions to be applied on the selected element |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $aCalls; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $jQueryNs The jQuery symbol |
|
|
|
|
|
|
54
|
|
|
* @param string $sPath The jQuery selector path |
|
55
|
|
|
* @param string $sContext A context associated to the selector |
|
|
|
|
|
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(string $jQueryNs, string $sPath, string $sContext) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$sPath = trim($sPath, " \t"); |
|
|
|
|
|
|
60
|
|
|
$sContext = trim($sContext, " \t"); |
|
|
|
|
|
|
61
|
|
|
$this->aCalls = []; |
|
62
|
|
|
|
|
63
|
|
|
if(!$sPath) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->sPath = "$jQueryNs(this)"; // If an empty selector is given, use javascript "this" instead |
|
66
|
|
|
} |
|
67
|
|
|
elseif(($sContext)) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->sPath = "$jQueryNs('" . $sPath . "', $jQueryNs('" . $sContext . "'))"; |
|
70
|
|
|
} |
|
71
|
|
|
else |
|
72
|
|
|
{ |
|
73
|
|
|
$this->sPath = "$jQueryNs('" . $sPath . "')"; |
|
74
|
|
|
} |
|
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 DomSelector |
|
84
|
|
|
*/ |
|
85
|
|
|
public function __call(string $sMethod, array $aArguments) |
|
86
|
|
|
{ |
|
87
|
|
|
// Push the action into the array |
|
88
|
|
|
$this->aCalls[] = new Method($sMethod, $aArguments); |
|
89
|
|
|
// Return $this so the calls can be chained |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the value of an attribute on the first selected element |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $sAttribute |
|
|
|
|
|
|
97
|
|
|
* |
|
98
|
|
|
* @return DomSelector |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __get(string $sAttribute) |
|
101
|
|
|
{ |
|
102
|
|
|
// Push the action into the array |
|
103
|
|
|
$this->aCalls[] = new AttrGet($sAttribute); |
|
104
|
|
|
// Return $this so the calls can be chained |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Set the value of an attribute on the first selected element |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $sAttribute |
|
|
|
|
|
|
112
|
|
|
* @param $xValue |
|
|
|
|
|
|
113
|
|
|
* |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
public function __set(string $sAttribute, $xValue) |
|
117
|
|
|
{ |
|
118
|
|
|
// Push the action into the array |
|
119
|
|
|
$this->aCalls[] = new AttrSet($sAttribute, $xValue); |
|
120
|
|
|
// No other call is allowed after a set |
|
121
|
|
|
// return $this; |
|
122
|
|
|
} |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Generate the jQuery call. |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getScript(): string |
|
130
|
|
|
{ |
|
131
|
|
|
if(count($this->aCalls) === 0) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->sPath; |
|
134
|
|
|
} |
|
135
|
|
|
return $this->sPath . '.' . implode('.', $this->aCalls); |
|
136
|
|
|
} |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Magic function to generate the jQuery call. |
|
140
|
|
|
* |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function __toString() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->getScript(); |
|
146
|
|
|
} |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Generate the jQuery call, when converting the response into json. |
|
150
|
|
|
* |
|
151
|
|
|
* This is a method of the JsonSerializable interface. |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function jsonSerialize(): string |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->getScript(); |
|
158
|
|
|
} |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|