|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* DomCommands.php - Provides DOM (HTML) related commands for the Response |
|
5
|
|
|
* |
|
6
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
7
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
8
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Jaxon\Response\Features; |
|
12
|
|
|
|
|
13
|
|
|
trait DomCommands |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $sName The command name |
|
|
|
|
|
|
19
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
20
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
21
|
|
|
* @param boolean $bRemoveEmpty If true, remove empty attributes |
|
|
|
|
|
|
22
|
|
|
* |
|
23
|
|
|
* @return Response |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract protected function _addCommand($sName, array $aAttributes, $mData, $bRemoveEmpty = false); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Add a command to assign the specified value to the given element's attribute |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $sTarget The id of the html element on the browser |
|
|
|
|
|
|
31
|
|
|
* @param string $sAttribute The attribute to be assigned |
|
|
|
|
|
|
32
|
|
|
* @param string $sData The value to be assigned to the attribute |
|
|
|
|
|
|
33
|
|
|
* |
|
34
|
|
|
* @return Response |
|
35
|
|
|
*/ |
|
36
|
|
|
public function assign($sTarget, $sAttribute, $sData) |
|
37
|
|
|
{ |
|
38
|
|
|
$aAttributes = [ |
|
39
|
|
|
'id' => $sTarget, |
|
40
|
|
|
'prop' => $sAttribute |
|
41
|
|
|
]; |
|
42
|
|
|
return $this->_addCommand('as', $aAttributes, $sData); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Add a command to assign the specified HTML content to the given element |
|
47
|
|
|
* |
|
48
|
|
|
* This is a shortcut for assign() on the innerHTML attribute. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $sTarget The id of the html element on the browser |
|
|
|
|
|
|
51
|
|
|
* @param string $sData The value to be assigned to the attribute |
|
|
|
|
|
|
52
|
|
|
* |
|
53
|
|
|
* @return Response |
|
54
|
|
|
*/ |
|
55
|
|
|
public function html($sTarget, $sData) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->assign($sTarget, 'innerHTML', $sData); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Add a command to append the specified data to the given element's attribute |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $sTarget The id of the element to be updated |
|
|
|
|
|
|
64
|
|
|
* @param string $sAttribute The name of the attribute to be appended to |
|
|
|
|
|
|
65
|
|
|
* @param string $sData The data to be appended to the attribute |
|
|
|
|
|
|
66
|
|
|
* |
|
67
|
|
|
* @return Response |
|
68
|
|
|
*/ |
|
69
|
|
|
public function append($sTarget, $sAttribute, $sData) |
|
70
|
|
|
{ |
|
71
|
|
|
$aAttributes = [ |
|
72
|
|
|
'id' => $sTarget, |
|
73
|
|
|
'prop' => $sAttribute |
|
74
|
|
|
]; |
|
75
|
|
|
return $this->_addCommand('ap', $aAttributes, $sData); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Add a command to prepend the specified data to the given element's attribute |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $sTarget The id of the element to be updated |
|
|
|
|
|
|
82
|
|
|
* @param string $sAttribute The name of the attribute to be prepended to |
|
|
|
|
|
|
83
|
|
|
* @param string $sData The value to be prepended to the attribute |
|
|
|
|
|
|
84
|
|
|
* |
|
85
|
|
|
* @return Response |
|
86
|
|
|
*/ |
|
87
|
|
|
public function prepend($sTarget, $sAttribute, $sData) |
|
88
|
|
|
{ |
|
89
|
|
|
$aAttributes = [ |
|
90
|
|
|
'id' => $sTarget, |
|
91
|
|
|
'prop' => $sAttribute |
|
92
|
|
|
]; |
|
93
|
|
|
return $this->_addCommand('pp', $aAttributes, $sData); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Add a command to replace a specified value with another value within the given element's attribute |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $sTarget The id of the element to update |
|
|
|
|
|
|
100
|
|
|
* @param string $sAttribute The attribute to be updated |
|
|
|
|
|
|
101
|
|
|
* @param string $sSearch The needle to search for |
|
|
|
|
|
|
102
|
|
|
* @param string $sData The data to use in place of the needle |
|
|
|
|
|
|
103
|
|
|
* |
|
104
|
|
|
* @return Response |
|
105
|
|
|
*/ |
|
106
|
|
|
public function replace($sTarget, $sAttribute, $sSearch, $sData) |
|
107
|
|
|
{ |
|
108
|
|
|
$aAttributes = [ |
|
109
|
|
|
'id' => $sTarget, |
|
110
|
|
|
'prop' => $sAttribute |
|
111
|
|
|
]; |
|
112
|
|
|
$aData = [ |
|
|
|
|
|
|
113
|
|
|
's' => $sSearch, |
|
114
|
|
|
'r' => $sData |
|
115
|
|
|
]; |
|
116
|
|
|
return $this->_addCommand('rp', $aAttributes, $aData); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add a command to clear the specified attribute of the given element |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $sTarget The id of the element to be updated. |
|
|
|
|
|
|
123
|
|
|
* @param string $sAttribute The attribute to be cleared |
|
|
|
|
|
|
124
|
|
|
* |
|
125
|
|
|
* @return Response |
|
126
|
|
|
*/ |
|
127
|
|
|
public function clear($sTarget, $sAttribute) |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->assign($sTarget, $sAttribute, ''); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Add a command to assign a value to a member of a javascript object (or element) |
|
134
|
|
|
* that is specified by the context member of the request |
|
135
|
|
|
* |
|
136
|
|
|
* The object is referenced using the 'this' keyword in the sAttribute parameter. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $sAttribute The attribute to be updated |
|
|
|
|
|
|
139
|
|
|
* @param string $sData The value to assign |
|
|
|
|
|
|
140
|
|
|
* |
|
141
|
|
|
* @return Response |
|
142
|
|
|
*/ |
|
143
|
|
|
public function contextAssign($sAttribute, $sData) |
|
144
|
|
|
{ |
|
145
|
|
|
$aAttributes = ['prop' => $sAttribute]; |
|
146
|
|
|
return $this->_addCommand('c:as', $aAttributes, $sData); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Add a command to append a value onto the specified member of the javascript |
|
151
|
|
|
* context object (or element) specified by the context member of the request |
|
152
|
|
|
* |
|
153
|
|
|
* The object is referenced using the 'this' keyword in the sAttribute parameter. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $sAttribute The attribute to be appended to |
|
|
|
|
|
|
156
|
|
|
* @param string $sData The value to append |
|
|
|
|
|
|
157
|
|
|
* |
|
158
|
|
|
* @return Response |
|
159
|
|
|
*/ |
|
160
|
|
|
public function contextAppend($sAttribute, $sData) |
|
161
|
|
|
{ |
|
162
|
|
|
$aAttributes = ['prop' => $sAttribute]; |
|
163
|
|
|
return $this->_addCommand('c:ap', $aAttributes, $sData); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Add a command to prepend the speicified data to the given member of the current |
|
168
|
|
|
* javascript object specified by context in the current request |
|
169
|
|
|
* |
|
170
|
|
|
* The object is access via the 'this' keyword in the sAttribute parameter. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $sAttribute The attribute to be updated |
|
|
|
|
|
|
173
|
|
|
* @param string $sData The value to be prepended |
|
|
|
|
|
|
174
|
|
|
* |
|
175
|
|
|
* @return Response |
|
176
|
|
|
*/ |
|
177
|
|
|
public function contextPrepend($sAttribute, $sData) |
|
178
|
|
|
{ |
|
179
|
|
|
$aAttributes = ['prop' => $sAttribute]; |
|
180
|
|
|
return $this->_addCommand('c:pp', $aAttributes, $sData); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Add a command to to clear the value of the attribute specified in the sAttribute parameter |
|
185
|
|
|
* |
|
186
|
|
|
* The member is access via the 'this' keyword and can be used to update a javascript |
|
187
|
|
|
* object specified by context in the request parameters. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $sAttribute The attribute to be cleared |
|
|
|
|
|
|
190
|
|
|
* |
|
191
|
|
|
* @return Response |
|
192
|
|
|
*/ |
|
193
|
|
|
public function contextClear($sAttribute) |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->contextAssign($sAttribute, ''); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Add a command to remove an element from the document |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $sTarget The id of the element to be removed |
|
|
|
|
|
|
202
|
|
|
* |
|
203
|
|
|
* @return Response |
|
204
|
|
|
*/ |
|
205
|
|
|
public function remove($sTarget) |
|
206
|
|
|
{ |
|
207
|
|
|
$aAttributes = ['id' => $sTarget]; |
|
208
|
|
|
return $this->_addCommand('rm', $aAttributes, ''); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Add a command to create a new element on the browser |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $sParent The id of the parent element |
|
|
|
|
|
|
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 Response |
|
219
|
|
|
*/ |
|
220
|
|
|
public function create($sParent, $sTag, $sId) |
|
221
|
|
|
{ |
|
222
|
|
|
$aAttributes = [ |
|
223
|
|
|
'id' => $sParent, |
|
224
|
|
|
'prop' => $sId |
|
225
|
|
|
]; |
|
226
|
|
|
return $this->_addCommand('ce', $aAttributes, $sTag); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Add a command to insert a new element just prior to the specified element |
|
231
|
|
|
* |
|
232
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
|
|
|
|
|
|
233
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
|
|
|
|
|
234
|
|
|
* @param string $sId The id to assign to the new element |
|
|
|
|
|
|
235
|
|
|
* |
|
236
|
|
|
* @return Response |
|
237
|
|
|
*/ |
|
238
|
|
|
public function insert($sBefore, $sTag, $sId) |
|
239
|
|
|
{ |
|
240
|
|
|
$aAttributes = [ |
|
241
|
|
|
'id' => $sBefore, |
|
242
|
|
|
'prop' => $sId |
|
243
|
|
|
]; |
|
244
|
|
|
return $this->_addCommand('ie', $aAttributes, $sTag); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Add a command to insert a new element after the specified |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $sAfter The id of the element used as a reference point for the insertion |
|
|
|
|
|
|
251
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
|
|
|
|
|
252
|
|
|
* @param string $sId The id to assign to the new element |
|
|
|
|
|
|
253
|
|
|
* |
|
254
|
|
|
* @return Response |
|
255
|
|
|
*/ |
|
256
|
|
|
public function insertAfter($sAfter, $sTag, $sId) |
|
257
|
|
|
{ |
|
258
|
|
|
$aAttributes = [ |
|
259
|
|
|
'id' => $sAfter, |
|
260
|
|
|
'prop' => $sId |
|
261
|
|
|
]; |
|
262
|
|
|
return $this->_addCommand('ia', $aAttributes, $sTag); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Add a command to create an input element on the browser |
|
267
|
|
|
* |
|
268
|
|
|
* @param string $sParent The id of the parent element |
|
|
|
|
|
|
269
|
|
|
* @param string $sType The type of the new input element |
|
|
|
|
|
|
270
|
|
|
* @param string $sName The name of the new input element |
|
|
|
|
|
|
271
|
|
|
* @param string $sId The id of the new element |
|
|
|
|
|
|
272
|
|
|
* |
|
273
|
|
|
* @return Response |
|
274
|
|
|
*/ |
|
275
|
|
|
public function createInput($sParent, $sType, $sName, $sId) |
|
276
|
|
|
{ |
|
277
|
|
|
$aAttributes = [ |
|
278
|
|
|
'id' => $sParent, |
|
279
|
|
|
'prop' => $sId, |
|
280
|
|
|
'type' => $sType |
|
281
|
|
|
]; |
|
282
|
|
|
return $this->_addCommand('ci', $aAttributes, $sName); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Add a command to insert a new input element preceding the specified element |
|
287
|
|
|
* |
|
288
|
|
|
* @param string $sBefore The id of the element to be used as the reference point for the insertion |
|
|
|
|
|
|
289
|
|
|
* @param string $sType The type of the new input element |
|
|
|
|
|
|
290
|
|
|
* @param string $sName The name of the new input element |
|
|
|
|
|
|
291
|
|
|
* @param string $sId The id of the new element |
|
|
|
|
|
|
292
|
|
|
* |
|
293
|
|
|
* @return Response |
|
294
|
|
|
*/ |
|
295
|
|
|
public function insertInput($sBefore, $sType, $sName, $sId) |
|
296
|
|
|
{ |
|
297
|
|
|
$aAttributes = [ |
|
298
|
|
|
'id' => $sBefore, |
|
299
|
|
|
'prop' => $sId, |
|
300
|
|
|
'type' => $sType |
|
301
|
|
|
]; |
|
302
|
|
|
return $this->_addCommand('ii', $aAttributes, $sName); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Add a command to insert a new input element after the specified element |
|
307
|
|
|
* |
|
308
|
|
|
* @param string $sAfter The id of the element to be used as the reference point for the insertion |
|
|
|
|
|
|
309
|
|
|
* @param string $sType The type of the new input element |
|
|
|
|
|
|
310
|
|
|
* @param string $sName The name of the new input element |
|
|
|
|
|
|
311
|
|
|
* @param string $sId The id of the new element |
|
|
|
|
|
|
312
|
|
|
* |
|
313
|
|
|
* @return Response |
|
314
|
|
|
*/ |
|
315
|
|
|
public function insertInputAfter($sAfter, $sType, $sName, $sId) |
|
316
|
|
|
{ |
|
317
|
|
|
$aAttributes = [ |
|
318
|
|
|
'id' => $sAfter, |
|
319
|
|
|
'prop' => $sId, |
|
320
|
|
|
'type' => $sType |
|
321
|
|
|
]; |
|
322
|
|
|
return $this->_addCommand('iia', $aAttributes, $sName); |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|