|
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
|
|
|
use Jaxon\Script\Call\JxnCall; |
|
26
|
|
|
|
|
27
|
|
|
class Response extends AjaxResponse |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Add a command to assign the specified value to the given element's attribute |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $sTarget The id of the html element on the browser |
|
33
|
|
|
* @param string $sAttribute The attribute to be assigned |
|
34
|
|
|
* @param string $sValue The value to be assigned to the attribute |
|
35
|
|
|
* |
|
36
|
|
|
* @return self |
|
37
|
|
|
*/ |
|
38
|
|
|
public function assign(string $sTarget, string $sAttribute, string $sValue): self |
|
39
|
|
|
{ |
|
40
|
|
|
$this->xManager->addCommand('node.assign', [ |
|
41
|
|
|
'id' => $this->str($sTarget), |
|
42
|
|
|
'attr' => $this->str($sAttribute), |
|
43
|
|
|
'value' => $this->str($sValue), |
|
44
|
|
|
]); |
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add a command to assign the specified HTML content to the given element |
|
50
|
|
|
* |
|
51
|
|
|
* This is a shortcut for assign() on the innerHTML attribute. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $sTarget The id of the html element on the browser |
|
54
|
|
|
* @param string $sValue The value to be assigned to the attribute |
|
55
|
|
|
* |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
|
|
public function html(string $sTarget, string $sValue): self |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->assign($sTarget, 'innerHTML', $sValue); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Add a command to assign the specified value to the given CSS attribute |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $sTarget The id of the html element on the browser |
|
67
|
|
|
* @param string $sCssAttribute The CSS attribute to be assigned |
|
68
|
|
|
* @param string $sValue The value to be assigned to the attribute |
|
69
|
|
|
* |
|
70
|
|
|
* @return self |
|
71
|
|
|
*/ |
|
72
|
|
|
public function style(string $sTarget, string $sCssAttribute, string $sValue): self |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->assign($sTarget, "style.$sCssAttribute", $sValue); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add a command to append the specified data to the given element's attribute |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $sTarget The id of the element to be updated |
|
81
|
|
|
* @param string $sAttribute The name of the attribute to be appended to |
|
82
|
|
|
* @param string $sValue The data to be appended to the attribute |
|
83
|
|
|
* |
|
84
|
|
|
* @return self |
|
85
|
|
|
*/ |
|
86
|
|
|
public function append(string $sTarget, string $sAttribute, string $sValue): self |
|
87
|
|
|
{ |
|
88
|
|
|
$this->xManager->addCommand('node.append', [ |
|
89
|
|
|
'id' => $this->str($sTarget), |
|
90
|
|
|
'attr' => $this->str($sAttribute), |
|
91
|
|
|
'value' => $this->str($sValue), |
|
92
|
|
|
]); |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Add a command to prepend the specified data to the given element's attribute |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $sTarget The id of the element to be updated |
|
100
|
|
|
* @param string $sAttribute The name of the attribute to be prepended to |
|
101
|
|
|
* @param string $sValue The value to be prepended to the attribute |
|
102
|
|
|
* |
|
103
|
|
|
* @return self |
|
104
|
|
|
*/ |
|
105
|
|
|
public function prepend(string $sTarget, string $sAttribute, string $sValue): self |
|
106
|
|
|
{ |
|
107
|
|
|
$this->xManager->addCommand('node.prepend', [ |
|
108
|
|
|
'id' => $this->str($sTarget), |
|
109
|
|
|
'attr' => $this->str($sAttribute), |
|
110
|
|
|
'value' => $this->str($sValue), |
|
111
|
|
|
]); |
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Add a command to replace a specified value with another value within the given element's attribute |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $sTarget The id of the element to update |
|
119
|
|
|
* @param string $sAttribute The attribute to be updated |
|
120
|
|
|
* @param string $sSearch The needle to search for |
|
121
|
|
|
* @param string $sReplace The data to use in place of the needle |
|
122
|
|
|
* |
|
123
|
|
|
* @return self |
|
124
|
|
|
*/ |
|
125
|
|
|
public function replace(string $sTarget, string $sAttribute, |
|
126
|
|
|
string $sSearch, string $sReplace): self |
|
127
|
|
|
{ |
|
128
|
|
|
$this->xManager->addCommand('node.replace', [ |
|
129
|
|
|
'id' => $this->str($sTarget), |
|
130
|
|
|
'attr' => $this->str($sAttribute), |
|
131
|
|
|
'search' => $this->str($sSearch), |
|
132
|
|
|
'replace' => $this->str($sReplace), |
|
133
|
|
|
]); |
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Add a command to clear the specified attribute of the given element |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $sTarget The id of the element to be updated. |
|
141
|
|
|
* @param string $sAttribute The attribute to be cleared |
|
142
|
|
|
* |
|
143
|
|
|
* @return self |
|
144
|
|
|
*/ |
|
145
|
|
|
public function clear(string $sTarget, string $sAttribute = 'innerHTML'): self |
|
146
|
|
|
{ |
|
147
|
|
|
$this->xManager->addCommand('node.clear', [ |
|
148
|
|
|
'id' => $this->str($sTarget), |
|
149
|
|
|
'attr' => $this->str($sAttribute), |
|
150
|
|
|
]); |
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Add a command to remove an element from the document |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $sTarget The id of the element to be removed |
|
158
|
|
|
* |
|
159
|
|
|
* @return self |
|
160
|
|
|
*/ |
|
161
|
|
|
public function remove(string $sTarget): self |
|
162
|
|
|
{ |
|
163
|
|
|
$this->xManager->addCommand('node.remove', [ |
|
164
|
|
|
'id' => $this->str($sTarget), |
|
165
|
|
|
]); |
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Add a command to bind an element to a component |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $sTarget The id of the element |
|
173
|
|
|
* @param JxnCall $xCall A call to the component |
|
174
|
|
|
* @param string $sItem The component item |
|
175
|
|
|
* |
|
176
|
|
|
* @return self |
|
177
|
|
|
*/ |
|
178
|
|
|
public function bind(string $sTarget, JxnCall $xCall, string $sItem = ''): self |
|
179
|
|
|
{ |
|
180
|
|
|
$this->xManager->addCommand('node.bind', [ |
|
181
|
|
|
'id' => $this->str($sTarget), |
|
182
|
|
|
'component' => !$sItem ? [ |
|
183
|
|
|
'name' => $xCall->_class(), |
|
184
|
|
|
] : [ |
|
185
|
|
|
'name' => $xCall->_class(), |
|
186
|
|
|
'item' => $this->str($sItem), |
|
187
|
|
|
], |
|
188
|
|
|
]); |
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Add a command to create a new element on the browser |
|
194
|
|
|
* @deprecated DOM element creation functions are deprecated |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $sParent The id of the parent element |
|
197
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
198
|
|
|
* @param string $sId The id to assign to the new element |
|
199
|
|
|
* |
|
200
|
|
|
* @return self |
|
201
|
|
|
*/ |
|
202
|
|
|
public function create(string $sParent, string $sTag, string $sId): self |
|
203
|
|
|
{ |
|
204
|
|
|
$this->xManager->addCommand('node.create', [ |
|
205
|
|
|
'id' => $this->str($sParent), |
|
206
|
|
|
'tag' => [ |
|
207
|
|
|
'name' => $this->str($sTag), |
|
208
|
|
|
'id' => $this->str($sId), |
|
209
|
|
|
], |
|
210
|
|
|
]); |
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Add a command to insert a new element just prior to the specified element |
|
216
|
|
|
* @deprecated DOM element creation functions are deprecated |
|
217
|
|
|
* |
|
218
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
|
219
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
220
|
|
|
* @param string $sId The id to assign to the new element |
|
221
|
|
|
* |
|
222
|
|
|
* @return self |
|
223
|
|
|
*/ |
|
224
|
|
|
public function insertBefore(string $sBefore, string $sTag, string $sId): self |
|
225
|
|
|
{ |
|
226
|
|
|
$this->xManager->addCommand('node.insert.before', [ |
|
227
|
|
|
'id' => $this->str($sBefore), |
|
228
|
|
|
'tag' => [ |
|
229
|
|
|
'name' => $this->str($sTag), |
|
230
|
|
|
'id' => $this->str($sId), |
|
231
|
|
|
], |
|
232
|
|
|
]); |
|
233
|
|
|
return $this; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Add a command to insert a new element just prior to the specified element |
|
238
|
|
|
* This is an alias for insertBefore. |
|
239
|
|
|
* @deprecated DOM element creation functions are deprecated |
|
240
|
|
|
* |
|
241
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
|
242
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
243
|
|
|
* @param string $sId The id to assign to the new element |
|
244
|
|
|
* |
|
245
|
|
|
* @return self |
|
246
|
|
|
*/ |
|
247
|
|
|
public function insert(string $sBefore, string $sTag, string $sId): self |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->insertBefore($sBefore, $sTag, $sId); |
|
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Add a command to insert a new element after the specified |
|
254
|
|
|
* @deprecated DOM element creation functions are deprecated |
|
255
|
|
|
* |
|
256
|
|
|
* @param string $sAfter The id of the element used as a reference point for the insertion |
|
257
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
258
|
|
|
* @param string $sId The id to assign to the new element |
|
259
|
|
|
* |
|
260
|
|
|
* @return self |
|
261
|
|
|
*/ |
|
262
|
|
|
public function insertAfter(string $sAfter, string $sTag, string $sId): self |
|
263
|
|
|
{ |
|
264
|
|
|
$this->xManager->addCommand('node.insert.after', [ |
|
265
|
|
|
'id' => $this->str($sAfter), |
|
266
|
|
|
'tag' => [ |
|
267
|
|
|
'name' => $this->str($sTag), |
|
268
|
|
|
'id' => $this->str($sId), |
|
269
|
|
|
], |
|
270
|
|
|
]); |
|
271
|
|
|
return $this; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Add a command to set an event handler on the specified element |
|
276
|
|
|
* This handler can take custom parameters, and is is executed in a specific context. |
|
277
|
|
|
* @deprecated Event handler functions are deprecated |
|
278
|
|
|
* |
|
279
|
|
|
* @param string $sTarget The id of the element |
|
280
|
|
|
* @param string $sEvent The name of the event |
|
281
|
|
|
* @param JsExpr $xCall The event handler |
|
282
|
|
|
* |
|
283
|
|
|
* @return self |
|
284
|
|
|
*/ |
|
285
|
|
|
public function setEventHandler(string $sTarget, string $sEvent, JsExpr $xCall): self |
|
286
|
|
|
{ |
|
287
|
|
|
$this->xManager->addCommand('handler.event.set', [ |
|
288
|
|
|
'id' => $this->str($sTarget), |
|
289
|
|
|
'event' => $this->str($sEvent), |
|
290
|
|
|
'func' => $xCall, |
|
291
|
|
|
]); |
|
292
|
|
|
return $this; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Add a command to set a click handler on the browser |
|
297
|
|
|
* @deprecated Event handler functions are deprecated |
|
298
|
|
|
* |
|
299
|
|
|
* @param string $sTarget The id of the element |
|
300
|
|
|
* @param JsExpr $xCall The event handler |
|
301
|
|
|
* |
|
302
|
|
|
* @return self |
|
303
|
|
|
*/ |
|
304
|
|
|
public function onClick(string $sTarget, JsExpr $xCall): self |
|
305
|
|
|
{ |
|
306
|
|
|
return $this->setEventHandler($sTarget, 'onclick', $xCall); |
|
|
|
|
|
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Add a command to add an event handler on the specified element |
|
311
|
|
|
* This handler can take custom parameters, and is is executed in a specific context. |
|
312
|
|
|
* @deprecated Event handler functions are deprecated |
|
313
|
|
|
* |
|
314
|
|
|
* @param string $sTarget The id of the element |
|
315
|
|
|
* @param string $sEvent The name of the event |
|
316
|
|
|
* @param JsExpr $xCall The event handler |
|
317
|
|
|
* |
|
318
|
|
|
* @return self |
|
319
|
|
|
*/ |
|
320
|
|
|
public function addEventHandler(string $sTarget, string $sEvent, JsExpr $xCall): self |
|
321
|
|
|
{ |
|
322
|
|
|
$this->xManager->addCommand('handler.event.add', [ |
|
323
|
|
|
'id' => $this->str($sTarget), |
|
324
|
|
|
'event' => $this->str($sEvent), |
|
325
|
|
|
'func' => $xCall, |
|
326
|
|
|
]); |
|
327
|
|
|
return $this; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Add a command to install an event handler on the specified element |
|
332
|
|
|
* You can add more than one event handler to an element's event using this method. |
|
333
|
|
|
* @deprecated Event handler functions are deprecated |
|
334
|
|
|
* |
|
335
|
|
|
* @param string $sTarget The id of the element |
|
336
|
|
|
* @param string $sEvent The name of the event |
|
337
|
|
|
* @param string $sHandler The name of the javascript function to call when the event is fired |
|
338
|
|
|
* |
|
339
|
|
|
* @return self |
|
340
|
|
|
*/ |
|
341
|
|
|
public function addHandler(string $sTarget, string $sEvent, string $sHandler): self |
|
342
|
|
|
{ |
|
343
|
|
|
$this->xManager->addCommand('handler.add', [ |
|
344
|
|
|
'id' => $this->str($sTarget), |
|
345
|
|
|
'event' => $this->str($sEvent), |
|
346
|
|
|
'func' => $this->str($sHandler), |
|
347
|
|
|
]); |
|
348
|
|
|
return $this; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* Add a command to remove an event handler from an element |
|
353
|
|
|
* @deprecated Event handler functions are deprecated |
|
354
|
|
|
* |
|
355
|
|
|
* @param string $sTarget The id of the element |
|
356
|
|
|
* @param string $sEvent The name of the event |
|
357
|
|
|
* @param string $sHandler The name of the javascript function called when the event is fired |
|
358
|
|
|
* |
|
359
|
|
|
* @return self |
|
360
|
|
|
*/ |
|
361
|
|
|
public function removeHandler(string $sTarget, string $sEvent, string $sHandler): self |
|
362
|
|
|
{ |
|
363
|
|
|
$this->xManager->addCommand('handler.remove', [ |
|
364
|
|
|
'id' => $this->str($sTarget), |
|
365
|
|
|
'event' => $this->str($sEvent), |
|
366
|
|
|
'func' => $this->str($sHandler), |
|
367
|
|
|
]); |
|
368
|
|
|
return $this; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.