|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* DomTrait.php |
|
5
|
|
|
* |
|
6
|
|
|
* Provides DOM (HTML) related commands for the Response |
|
7
|
|
|
* |
|
8
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
namespace Jaxon\Response\Traits; |
|
14
|
|
|
|
|
15
|
|
|
use Jaxon\Response\ResponseInterface; |
|
16
|
|
|
|
|
17
|
|
|
trait DomTrait |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $sName The command name |
|
|
|
|
|
|
23
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
24
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
25
|
|
|
* @param bool $bRemoveEmpty If true, remove empty attributes |
|
|
|
|
|
|
26
|
|
|
* |
|
27
|
|
|
* @return ResponseInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract protected function _addCommand(string $sName, array $aAttributes, |
|
|
|
|
|
|
30
|
|
|
$mData, bool $bRemoveEmpty = false): ResponseInterface; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Add a command to assign the specified value to the given element's attribute |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $sTarget The id of the html element on the browser |
|
36
|
|
|
* @param string $sAttribute The attribute to be assigned |
|
|
|
|
|
|
37
|
|
|
* @param string $sData The value to be assigned to the attribute |
|
|
|
|
|
|
38
|
|
|
* |
|
39
|
|
|
* @return ResponseInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
public function assign(string $sTarget, string $sAttribute, string $sData): ResponseInterface |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$aAttributes = ['id' => $sTarget, 'prop' => $sAttribute]; |
|
44
|
|
|
return $this->_addCommand('as', $aAttributes, $sData); |
|
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 $sData The value to be assigned to the attribute |
|
|
|
|
|
|
54
|
|
|
* |
|
55
|
|
|
* @return ResponseInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function html(string $sTarget, string $sData): ResponseInterface |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->assign($sTarget, 'innerHTML', $sData); |
|
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 $sData The data to be appended to the attribute |
|
|
|
|
|
|
68
|
|
|
* |
|
69
|
|
|
* @return ResponseInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
public function append(string $sTarget, string $sAttribute, string $sData): ResponseInterface |
|
72
|
|
|
{ |
|
73
|
|
|
$aAttributes = ['id' => $sTarget, 'prop' => $sAttribute]; |
|
74
|
|
|
return $this->_addCommand('ap', $aAttributes, $sData); |
|
75
|
|
|
} |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add a command to prepend 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 prepended to |
|
|
|
|
|
|
82
|
|
|
* @param string $sData The value to be prepended to the attribute |
|
|
|
|
|
|
83
|
|
|
* |
|
84
|
|
|
* @return ResponseInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public function prepend(string $sTarget, string $sAttribute, string $sData): ResponseInterface |
|
87
|
|
|
{ |
|
88
|
|
|
$aAttributes = ['id' => $sTarget, 'prop' => $sAttribute]; |
|
89
|
|
|
return $this->_addCommand('pp', $aAttributes, $sData); |
|
90
|
|
|
} |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Add a command to replace a specified value with another value within the given element's attribute |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $sTarget The id of the element to update |
|
96
|
|
|
* @param string $sAttribute The attribute to be updated |
|
|
|
|
|
|
97
|
|
|
* @param string $sSearch The needle to search for |
|
98
|
|
|
* @param string $sData The data to use in place of the needle |
|
|
|
|
|
|
99
|
|
|
* |
|
100
|
|
|
* @return ResponseInterface |
|
101
|
|
|
*/ |
|
102
|
|
|
public function replace(string $sTarget, string $sAttribute, string $sSearch, string $sData): ResponseInterface |
|
103
|
|
|
{ |
|
104
|
|
|
$aAttributes = ['id' => $sTarget, 'prop' => $sAttribute]; |
|
105
|
|
|
$aData = ['s' => $sSearch, 'r' => $sData]; |
|
|
|
|
|
|
106
|
|
|
return $this->_addCommand('rp', $aAttributes, $aData); |
|
107
|
|
|
} |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Add a command to clear the specified attribute of the given element |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $sTarget The id of the element to be updated. |
|
113
|
|
|
* @param string $sAttribute The attribute to be cleared |
|
|
|
|
|
|
114
|
|
|
* |
|
115
|
|
|
* @return ResponseInterface |
|
116
|
|
|
*/ |
|
117
|
|
|
public function clear(string $sTarget, string $sAttribute = 'innerHTML'): ResponseInterface |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->assign($sTarget, $sAttribute, ''); |
|
120
|
|
|
} |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Add a command to remove an element from the document |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $sTarget The id of the element to be removed |
|
|
|
|
|
|
126
|
|
|
* |
|
127
|
|
|
* @return ResponseInterface |
|
128
|
|
|
*/ |
|
129
|
|
|
public function remove(string $sTarget): ResponseInterface |
|
130
|
|
|
{ |
|
131
|
|
|
$aAttributes = ['id' => $sTarget]; |
|
132
|
|
|
return $this->_addCommand('rm', $aAttributes, ''); |
|
133
|
|
|
} |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Add a command to create a new element on the browser |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $sParent The id of the parent element |
|
|
|
|
|
|
139
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
140
|
|
|
* @param string $sId The id to assign to the new element |
|
|
|
|
|
|
141
|
|
|
* |
|
142
|
|
|
* @return ResponseInterface |
|
143
|
|
|
*/ |
|
144
|
|
|
public function create(string $sParent, string $sTag, string $sId): ResponseInterface |
|
145
|
|
|
{ |
|
146
|
|
|
$aAttributes = ['id' => $sParent, 'prop' => $sId]; |
|
147
|
|
|
return $this->_addCommand('ce', $aAttributes, $sTag); |
|
148
|
|
|
} |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Add a command to insert a new element just prior to the specified element |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
|
|
|
|
|
|
154
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
155
|
|
|
* @param string $sId The id to assign to the new element |
|
|
|
|
|
|
156
|
|
|
* |
|
157
|
|
|
* @return ResponseInterface |
|
158
|
|
|
*/ |
|
159
|
|
|
public function insertBefore(string $sBefore, string $sTag, string $sId): ResponseInterface |
|
160
|
|
|
{ |
|
161
|
|
|
$aAttributes = ['id' => $sBefore, 'prop' => $sId]; |
|
162
|
|
|
return $this->_addCommand('ie', $aAttributes, $sTag); |
|
163
|
|
|
} |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Add a command to insert a new element just prior to the specified element |
|
167
|
|
|
* This is an alias for insertBefore. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $sBefore The id of the element used as a reference point for the insertion |
|
|
|
|
|
|
170
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
171
|
|
|
* @param string $sId The id to assign to the new element |
|
|
|
|
|
|
172
|
|
|
* |
|
173
|
|
|
* @return ResponseInterface |
|
174
|
|
|
*/ |
|
175
|
|
|
public function insert(string $sBefore, string $sTag, string $sId): ResponseInterface |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->insertBefore($sBefore, $sTag, $sId); |
|
178
|
|
|
} |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Add a command to insert a new element after the specified |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $sAfter The id of the element used as a reference point for the insertion |
|
|
|
|
|
|
184
|
|
|
* @param string $sTag The tag name to be used for the new element |
|
|
|
|
|
|
185
|
|
|
* @param string $sId The id to assign to the new element |
|
186
|
|
|
* |
|
187
|
|
|
* @return ResponseInterface |
|
188
|
|
|
*/ |
|
189
|
|
|
public function insertAfter(string $sAfter, string $sTag, string $sId): ResponseInterface |
|
190
|
|
|
{ |
|
191
|
|
|
$aAttributes = ['id' => $sAfter, 'prop' => $sId]; |
|
192
|
|
|
return $this->_addCommand('ia', $aAttributes, $sTag); |
|
193
|
|
|
} |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|