1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Response.php |
5
|
|
|
* |
6
|
|
|
* This class collects commands to be sent back to the browser in response to a jaxon request. |
7
|
|
|
* Commands are encoded and packaged in json format. |
8
|
|
|
* |
9
|
|
|
* @package jaxon-core |
10
|
|
|
* @author Jared White |
11
|
|
|
* @author J. Max Wilson |
12
|
|
|
* @author Joseph Woolley |
13
|
|
|
* @author Steffen Konerow |
14
|
|
|
* @author Thierry Feuzeu <[email protected]> |
15
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
16
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
17
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
19
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Jaxon\Response; |
23
|
|
|
|
24
|
|
|
use Jaxon\Script\JsExpr; |
25
|
|
|
|
26
|
|
|
class Response extends AjaxResponse |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Add a command to assign the specified value to the given element's attribute |
30
|
|
|
* |
31
|
|
|
* @param string $sTarget The id of the html element on the browser |
32
|
|
|
* @param string $sAttribute The attribute to be assigned |
33
|
|
|
* @param string $sValue The value to be assigned to the attribute |
34
|
|
|
* |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
|
|
public function assign(string $sTarget, string $sAttribute, string $sValue): self |
38
|
|
|
{ |
39
|
|
|
$this->xManager->addCommand('node.assign', [ |
40
|
|
|
'id' => $this->str($sTarget), |
41
|
|
|
'attr' => $this->str($sAttribute), |
42
|
|
|
'value' => $this->str($sValue), |
43
|
|
|
]); |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add a command to assign the specified HTML content to the given element |
49
|
|
|
* |
50
|
|
|
* This is a shortcut for assign() on the innerHTML attribute. |
51
|
|
|
* |
52
|
|
|
* @param string $sTarget The id of the html element on the browser |
53
|
|
|
* @param string $sValue The value to be assigned to the attribute |
54
|
|
|
* |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
|
|
public function html(string $sTarget, string $sValue): self |
58
|
|
|
{ |
59
|
|
|
return $this->assign($sTarget, 'innerHTML', $sValue); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a command to append the specified data to the given element's attribute |
64
|
|
|
* |
65
|
|
|
* @param string $sTarget The id of the element to be updated |
66
|
|
|
* @param string $sAttribute The name of the attribute to be appended to |
67
|
|
|
* @param string $sValue The data to be appended to the attribute |
68
|
|
|
* |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
|
|
public function append(string $sTarget, string $sAttribute, string $sValue): self |
72
|
|
|
{ |
73
|
|
|
$this->xManager->addCommand('node.append', [ |
74
|
|
|
'id' => $this->str($sTarget), |
75
|
|
|
'attr' => $this->str($sAttribute), |
76
|
|
|
'value' => $this->str($sValue), |
77
|
|
|
]); |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add a command to prepend the specified data to the given element's attribute |
83
|
|
|
* |
84
|
|
|
* @param string $sTarget The id of the element to be updated |
85
|
|
|
* @param string $sAttribute The name of the attribute to be prepended to |
86
|
|
|
* @param string $sValue The value to be prepended to the attribute |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
|
|
public function prepend(string $sTarget, string $sAttribute, string $sValue): self |
91
|
|
|
{ |
92
|
|
|
$this->xManager->addCommand('node.prepend', [ |
93
|
|
|
'id' => $this->str($sTarget), |
94
|
|
|
'attr' => $this->str($sAttribute), |
95
|
|
|
'value' => $this->str($sValue), |
96
|
|
|
]); |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add a command to replace a specified value with another value within the given element's attribute |
102
|
|
|
* |
103
|
|
|
* @param string $sTarget The id of the element to update |
104
|
|
|
* @param string $sAttribute The attribute to be updated |
105
|
|
|
* @param string $sSearch The needle to search for |
106
|
|
|
* @param string $sReplace The data to use in place of the needle |
107
|
|
|
* |
108
|
|
|
* @return self |
109
|
|
|
*/ |
110
|
|
|
public function replace(string $sTarget, string $sAttribute, |
111
|
|
|
string $sSearch, string $sReplace): self |
112
|
|
|
{ |
113
|
|
|
$this->xManager->addCommand('node.replace', [ |
114
|
|
|
'id' => $this->str($sTarget), |
115
|
|
|
'attr' => $this->str($sAttribute), |
116
|
|
|
'search' => $this->str($sSearch), |
117
|
|
|
'replace' => $this->str($sReplace), |
118
|
|
|
]); |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add a command to clear the specified attribute of the given element |
124
|
|
|
* |
125
|
|
|
* @param string $sTarget The id of the element to be updated. |
126
|
|
|
* @param string $sAttribute The attribute to be cleared |
127
|
|
|
* |
128
|
|
|
* @return self |
129
|
|
|
*/ |
130
|
|
|
public function clear(string $sTarget, string $sAttribute = 'innerHTML'): self |
131
|
|
|
{ |
132
|
|
|
$this->xManager->addCommand('node.clear', [ |
133
|
|
|
'id' => $this->str($sTarget), |
134
|
|
|
'attr' => $this->str($sAttribute), |
135
|
|
|
]); |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Add a command to remove an element from the document |
141
|
|
|
* |
142
|
|
|
* @param string $sTarget The id of the element to be removed |
143
|
|
|
* |
144
|
|
|
* @return self |
145
|
|
|
*/ |
146
|
|
|
public function remove(string $sTarget): self |
147
|
|
|
{ |
148
|
|
|
$this->xManager->addCommand('node.remove', [ |
149
|
|
|
'id' => $this->str($sTarget), |
150
|
|
|
]); |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Add a command to create a new element on the browser |
156
|
|
|
* |
157
|
|
|
* @param string $sParent The id of the parent element |
158
|
|
|
* @param string $sTag The tag name to be used for the new element |
159
|
|
|
* @param string $sId The id to assign to the new element |
160
|
|
|
* |
161
|
|
|
* @return self |
162
|
|
|
*/ |
163
|
|
|
public function create(string $sParent, string $sTag, string $sId): self |
164
|
|
|
{ |
165
|
|
|
$this->xManager->addCommand('node.create', [ |
166
|
|
|
'id' => $this->str($sParent), |
167
|
|
|
'tag' => [ |
168
|
|
|
'name' => $this->str($sTag), |
169
|
|
|
'id' => $this->str($sId), |
170
|
|
|
], |
171
|
|
|
]); |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Add a command to insert a new element just prior to the specified element |
177
|
|
|
* |
178
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
179
|
|
|
* @param string $sTag The tag name to be used for the new element |
180
|
|
|
* @param string $sId The id to assign to the new element |
181
|
|
|
* |
182
|
|
|
* @return self |
183
|
|
|
*/ |
184
|
|
|
public function insertBefore(string $sBefore, string $sTag, string $sId): self |
185
|
|
|
{ |
186
|
|
|
$this->xManager->addCommand('node.insert.before', [ |
187
|
|
|
'id' => $this->str($sBefore), |
188
|
|
|
'tag' => [ |
189
|
|
|
'name' => $this->str($sTag), |
190
|
|
|
'id' => $this->str($sId), |
191
|
|
|
], |
192
|
|
|
]); |
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Add a command to insert a new element just prior to the specified element |
198
|
|
|
* This is an alias for insertBefore. |
199
|
|
|
* |
200
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
201
|
|
|
* @param string $sTag The tag name to be used for the new element |
202
|
|
|
* @param string $sId The id to assign to the new element |
203
|
|
|
* |
204
|
|
|
* @return self |
205
|
|
|
*/ |
206
|
|
|
public function insert(string $sBefore, string $sTag, string $sId): self |
207
|
|
|
{ |
208
|
|
|
return $this->insertBefore($sBefore, $sTag, $sId); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Add a command to insert a new element after the specified |
213
|
|
|
* |
214
|
|
|
* @param string $sAfter The id of the element used as a reference point for the insertion |
215
|
|
|
* @param string $sTag The tag name to be used for the new element |
216
|
|
|
* @param string $sId The id to assign to the new element |
217
|
|
|
* |
218
|
|
|
* @return self |
219
|
|
|
*/ |
220
|
|
|
public function insertAfter(string $sAfter, string $sTag, string $sId): self |
221
|
|
|
{ |
222
|
|
|
$this->xManager->addCommand('node.insert.after', [ |
223
|
|
|
'id' => $this->str($sAfter), |
224
|
|
|
'tag' => [ |
225
|
|
|
'name' => $this->str($sTag), |
226
|
|
|
'id' => $this->str($sId), |
227
|
|
|
], |
228
|
|
|
]); |
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Add a command to set an event handler on the specified element |
234
|
|
|
* This handler can take custom parameters, and is is executed in a specific context. |
235
|
|
|
* |
236
|
|
|
* @param string $sTarget The id of the element |
237
|
|
|
* @param string $sEvent The name of the event |
238
|
|
|
* @param JsExpr $xCall The event handler |
239
|
|
|
* |
240
|
|
|
* @return self |
241
|
|
|
*/ |
242
|
|
|
public function setEventHandler(string $sTarget, string $sEvent, JsExpr $xCall): self |
243
|
|
|
{ |
244
|
|
|
$this->xManager->addCommand('handler.event.set', [ |
245
|
|
|
'id' => $this->str($sTarget), |
246
|
|
|
'event' => $this->str($sEvent), |
247
|
|
|
'func' => $xCall, |
248
|
|
|
]); |
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Add a command to set a click handler on the browser |
254
|
|
|
* |
255
|
|
|
* @param string $sTarget The id of the element |
256
|
|
|
* @param JsExpr $xCall The event handler |
257
|
|
|
* |
258
|
|
|
* @return self |
259
|
|
|
*/ |
260
|
|
|
public function onClick(string $sTarget, JsExpr $xCall): self |
261
|
|
|
{ |
262
|
|
|
return $this->setEventHandler($sTarget, 'onclick', $xCall); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Add a command to add an event handler on the specified element |
267
|
|
|
* This handler can take custom parameters, and is is executed in a specific context. |
268
|
|
|
* |
269
|
|
|
* @param string $sTarget The id of the element |
270
|
|
|
* @param string $sEvent The name of the event |
271
|
|
|
* @param JsExpr $xCall The event handler |
272
|
|
|
* |
273
|
|
|
* @return self |
274
|
|
|
*/ |
275
|
|
|
public function addEventHandler(string $sTarget, string $sEvent, JsExpr $xCall): self |
276
|
|
|
{ |
277
|
|
|
$this->xManager->addCommand('handler.event.add', [ |
278
|
|
|
'id' => $this->str($sTarget), |
279
|
|
|
'event' => $this->str($sEvent), |
280
|
|
|
'func' => $xCall, |
281
|
|
|
]); |
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Add a command to install an event handler on the specified element |
287
|
|
|
* |
288
|
|
|
* You can add more than one event handler to an element's event using this method. |
289
|
|
|
* |
290
|
|
|
* @param string $sTarget The id of the element |
291
|
|
|
* @param string $sEvent The name of the event |
292
|
|
|
* @param string $sHandler The name of the javascript function to call when the event is fired |
293
|
|
|
* |
294
|
|
|
* @return self |
295
|
|
|
*/ |
296
|
|
|
public function addHandler(string $sTarget, string $sEvent, string $sHandler): self |
297
|
|
|
{ |
298
|
|
|
$this->xManager->addCommand('handler.add', [ |
299
|
|
|
'id' => $this->str($sTarget), |
300
|
|
|
'event' => $this->str($sEvent), |
301
|
|
|
'func' => $this->str($sHandler), |
302
|
|
|
]); |
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Add a command to remove an event handler from an element |
308
|
|
|
* |
309
|
|
|
* @param string $sTarget The id of the element |
310
|
|
|
* @param string $sEvent The name of the event |
311
|
|
|
* @param string $sHandler The name of the javascript function called when the event is fired |
312
|
|
|
* |
313
|
|
|
* @return self |
314
|
|
|
*/ |
315
|
|
|
public function removeHandler(string $sTarget, string $sEvent, string $sHandler): self |
316
|
|
|
{ |
317
|
|
|
$this->xManager->addCommand('handler.remove', [ |
318
|
|
|
'id' => $this->str($sTarget), |
319
|
|
|
'event' => $this->str($sEvent), |
320
|
|
|
'func' => $this->str($sHandler), |
321
|
|
|
]); |
322
|
|
|
return $this; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|