1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ScriptTrait.php |
5
|
|
|
* |
6
|
|
|
* Provides javascript 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\App\Dialog\DialogManager; |
16
|
|
|
use Jaxon\Request\Call\JsCall; |
17
|
|
|
use Jaxon\Response\ResponseInterface; |
18
|
|
|
use Closure; |
19
|
|
|
use JsonSerializable; |
20
|
|
|
|
21
|
|
|
use function Jaxon\jaxon; |
22
|
|
|
use function func_get_args; |
23
|
|
|
use function array_shift; |
24
|
|
|
|
25
|
|
|
trait ScriptTrait |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
29
|
|
|
* |
30
|
|
|
* @param string $sName The command name |
|
|
|
|
31
|
|
|
* @param array|JsonSerializable $aOptions The command options |
|
|
|
|
32
|
|
|
* |
33
|
|
|
* @return ResponseInterface |
34
|
|
|
*/ |
35
|
|
|
abstract public function addCommand(string $sName, array|JsonSerializable $aOptions): ResponseInterface; |
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Convert to string |
39
|
|
|
* |
40
|
|
|
* @param mixed $xData |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
abstract protected function str($xData): string; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return DialogManager |
48
|
|
|
*/ |
49
|
|
|
abstract protected function dialog(): DialogManager; |
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add a command to call the specified javascript function with the given (optional) parameters |
53
|
|
|
* |
54
|
|
|
* @param string $sFunc The name of the function to call |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @return ResponseInterface |
57
|
|
|
*/ |
58
|
|
|
public function call(string $sFunc): ResponseInterface |
59
|
|
|
{ |
60
|
|
|
$aArgs = func_get_args(); |
61
|
|
|
array_shift($aArgs); |
62
|
|
|
return $this->addCommand('script.call', ['func' => $this->str($sFunc),'args' => $aArgs]); |
63
|
|
|
} |
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Response command that prompts user with [ok] [cancel] style message box |
67
|
|
|
* |
68
|
|
|
* The provided closure will be called with a response object as unique parameter. |
69
|
|
|
* If the user clicks cancel, the response commands defined in the closure will be skipped. |
70
|
|
|
* |
71
|
|
|
* @param Closure $fCalls A closure that defines the commands that can be skipped |
72
|
|
|
* @param string $sQuestion The question to ask to the user |
|
|
|
|
73
|
|
|
* @param array $aArgs The arguments for the placeholders in the question |
|
|
|
|
74
|
|
|
* |
75
|
|
|
* @return ResponseInterface |
76
|
|
|
*/ |
77
|
|
|
public function confirm(Closure $fCalls, string $sQuestion, array $aArgs = []): ResponseInterface |
78
|
|
|
{ |
79
|
|
|
$xResponse = jaxon()->newResponse(); |
80
|
|
|
$fCalls($xResponse); |
81
|
|
|
if(($nCommandCount = $xResponse->getCommandCount()) > 0) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->addCommand('script.confirm', [ |
84
|
|
|
'count' => $nCommandCount, |
85
|
|
|
'question' => $this->dialog()->confirm($this->str($sQuestion), $aArgs), |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
// Append the provided commands |
89
|
|
|
$this->appendResponse($xResponse); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
return $this; |
|
|
|
|
92
|
|
|
} |
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add a command to display an alert message to the user |
96
|
|
|
* |
97
|
|
|
* @param string $sMessage The message to be displayed |
|
|
|
|
98
|
|
|
* @param array $aArgs The arguments for the placeholders in the message |
|
|
|
|
99
|
|
|
* |
100
|
|
|
* @return ResponseInterface |
101
|
|
|
*/ |
102
|
|
|
public function alert(string $sMessage, array $aArgs = []): ResponseInterface |
103
|
|
|
{ |
104
|
|
|
$this->addCommand('dialog.message', $this->dialog()->info($this->str($sMessage), $aArgs)); |
105
|
|
|
return $this; |
|
|
|
|
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Add a command to display a debug message to the user |
110
|
|
|
* |
111
|
|
|
* @param string $sMessage The message to be displayed |
|
|
|
|
112
|
|
|
* |
113
|
|
|
* @return ResponseInterface |
114
|
|
|
*/ |
115
|
|
|
public function debug(string $sMessage): ResponseInterface |
116
|
|
|
{ |
117
|
|
|
return $this->addCommand('script.debug', ['message' => $this->str($sMessage)]); |
118
|
|
|
} |
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Add a command to ask the browser to navigate to the specified URL |
122
|
|
|
* |
123
|
|
|
* @param string $sURL The relative or fully qualified URL |
|
|
|
|
124
|
|
|
* @param integer $nDelay Number of seconds to delay before the redirect occurs |
|
|
|
|
125
|
|
|
* |
126
|
|
|
* @return ResponseInterface |
127
|
|
|
*/ |
128
|
|
|
public function redirect(string $sURL, int $nDelay = 0): ResponseInterface |
129
|
|
|
{ |
130
|
|
|
return $this->addCommand('script.redirect', [ |
131
|
|
|
'delay' => $nDelay, |
132
|
|
|
'url' => $this->xPluginManager->getParameterReader()->parseUrl($sURL), |
133
|
|
|
]); |
134
|
|
|
} |
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Add a command to make Jaxon to pause execution of the response commands, |
138
|
|
|
* returning control to the browser so it can perform other commands asynchronously. |
139
|
|
|
* |
140
|
|
|
* After the specified delay, Jaxon will continue execution of the response commands. |
141
|
|
|
* |
142
|
|
|
* @param integer $tenths The number of 1/10ths of a second to sleep |
|
|
|
|
143
|
|
|
* |
144
|
|
|
* @return ResponseInterface |
145
|
|
|
*/ |
146
|
|
|
public function sleep(int $tenths): ResponseInterface |
147
|
|
|
{ |
148
|
|
|
return $this->addCommand('script.sleep', ['duration' => $tenths]); |
149
|
|
|
} |
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Add a command to set an event handler on the specified element |
153
|
|
|
* This handler can take custom parameters, and is is executed in a specific context. |
154
|
|
|
* |
155
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
156
|
|
|
* @param string $sEvent The name of the event |
|
|
|
|
157
|
|
|
* @param JsCall $xCall The event handler |
|
|
|
|
158
|
|
|
* |
159
|
|
|
* @return ResponseInterface |
160
|
|
|
*/ |
161
|
|
|
public function setEventHandler(string $sTarget, string $sEvent, JsCall $xCall): ResponseInterface |
162
|
|
|
{ |
163
|
|
|
return $this->addCommand('handler.event.set', [ |
164
|
|
|
'id' => $this->str($sTarget), |
165
|
|
|
'event' => $this->str($sEvent), |
166
|
|
|
'func' => $xCall, |
167
|
|
|
]); |
168
|
|
|
} |
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Add a command to set a click handler on the browser |
172
|
|
|
* |
173
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
174
|
|
|
* @param JsCall $xCall The event handler |
|
|
|
|
175
|
|
|
* |
176
|
|
|
* @return ResponseInterface |
177
|
|
|
*/ |
178
|
|
|
public function onClick(string $sTarget, JsCall $xCall): ResponseInterface |
179
|
|
|
{ |
180
|
|
|
return $this->setEventHandler($sTarget, 'onclick', $xCall); |
181
|
|
|
} |
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Add a command to add an event handler on the specified element |
185
|
|
|
* This handler can take custom parameters, and is is executed in a specific context. |
186
|
|
|
* |
187
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
188
|
|
|
* @param string $sEvent The name of the event |
|
|
|
|
189
|
|
|
* @param JsCall $xCall The event handler |
|
|
|
|
190
|
|
|
* |
191
|
|
|
* @return ResponseInterface |
192
|
|
|
*/ |
193
|
|
|
public function addEventHandler(string $sTarget, string $sEvent, JsCall $xCall): ResponseInterface |
194
|
|
|
{ |
195
|
|
|
return $this->addCommand('handler.event.add', [ |
196
|
|
|
'id' => $this->str($sTarget), |
197
|
|
|
'event' => $this->str($sEvent), |
198
|
|
|
'func' => $xCall, |
199
|
|
|
]); |
200
|
|
|
} |
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Add a command to install an event handler on the specified element |
204
|
|
|
* |
205
|
|
|
* You can add more than one event handler to an element's event using this method. |
206
|
|
|
* |
207
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
208
|
|
|
* @param string $sEvent The name of the event |
|
|
|
|
209
|
|
|
* @param string $sHandler The name of the javascript function to call when the event is fired |
|
|
|
|
210
|
|
|
* |
211
|
|
|
* @return ResponseInterface |
212
|
|
|
*/ |
213
|
|
|
public function addHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface |
214
|
|
|
{ |
215
|
|
|
return $this->addCommand('handler.add', [ |
216
|
|
|
'id' => $this->str($sTarget), |
217
|
|
|
'event' => $this->str($sEvent), |
218
|
|
|
'func' => $this->str($sHandler), |
219
|
|
|
]); |
220
|
|
|
} |
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Add a command to remove an event handler from an element |
224
|
|
|
* |
225
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
226
|
|
|
* @param string $sEvent The name of the event |
|
|
|
|
227
|
|
|
* @param string $sHandler The name of the javascript function called when the event is fired |
|
|
|
|
228
|
|
|
* |
229
|
|
|
* @return ResponseInterface |
230
|
|
|
*/ |
231
|
|
|
public function removeHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface |
232
|
|
|
{ |
233
|
|
|
return $this->addCommand('handler.remove', [ |
234
|
|
|
'id' => $this->str($sTarget), |
235
|
|
|
'event' => $this->str($sEvent), |
236
|
|
|
'func' => $this->str($sHandler), |
237
|
|
|
]); |
238
|
|
|
} |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|