|
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\Response\ResponseInterface; |
|
16
|
|
|
|
|
17
|
|
|
use function func_get_args; |
|
18
|
|
|
use function array_shift; |
|
19
|
|
|
|
|
20
|
|
|
trait ScriptTrait |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
26
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
27
|
|
|
* |
|
28
|
|
|
* @return ResponseInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract public function addCommand(array $aAttributes, $mData): ResponseInterface; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $sName The command name |
|
|
|
|
|
|
36
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
37
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
38
|
|
|
* @param bool $bRemoveEmpty If true, remove empty attributes |
|
|
|
|
|
|
39
|
|
|
* |
|
40
|
|
|
* @return ResponseInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract protected function _addCommand(string $sName, array $aAttributes, |
|
43
|
|
|
$mData, bool $bRemoveEmpty = false): ResponseInterface; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Response command that prompts user with [ok] [cancel] style message box |
|
47
|
|
|
* |
|
48
|
|
|
* If the user clicks cancel, the specified number of response commands |
|
49
|
|
|
* following this one, will be skipped. |
|
50
|
|
|
* |
|
51
|
|
|
* @param integer $nCommandCount The number of commands to skip upon cancel |
|
|
|
|
|
|
52
|
|
|
* @param string $sMessage The message to display to the user |
|
|
|
|
|
|
53
|
|
|
* |
|
54
|
|
|
* @return ResponseInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
public function confirmCommands(int $nCommandCount, string $sMessage): ResponseInterface |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$aAttributes = ['count' => $nCommandCount]; |
|
59
|
|
|
return $this->_addCommand('cc', $aAttributes, $sMessage); |
|
60
|
|
|
} |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Add a command to call the specified javascript function with the given (optional) parameters |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $sFunc The name of the function to call |
|
|
|
|
|
|
66
|
|
|
* |
|
67
|
|
|
* @return ResponseInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
public function call(string $sFunc): ResponseInterface |
|
70
|
|
|
{ |
|
71
|
|
|
$aArgs = func_get_args(); |
|
72
|
|
|
array_shift($aArgs); |
|
73
|
|
|
$aAttributes = ['cmd' => 'jc', 'func' => $sFunc]; |
|
74
|
|
|
return $this->addCommand($aAttributes, $aArgs); |
|
75
|
|
|
} |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add a command to display an alert message to the user |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $sMessage The message to be displayed |
|
|
|
|
|
|
81
|
|
|
* |
|
82
|
|
|
* @return ResponseInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
public function alert(string $sMessage): ResponseInterface |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->_addCommand('al', [], $sMessage); |
|
87
|
|
|
} |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Add a command to display a debug message to the user |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $sMessage The message to be displayed |
|
|
|
|
|
|
93
|
|
|
* |
|
94
|
|
|
* @return ResponseInterface |
|
95
|
|
|
*/ |
|
96
|
|
|
public function debug(string $sMessage): ResponseInterface |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->_addCommand('dbg', [], $sMessage); |
|
99
|
|
|
} |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Add a command to ask the browser to navigate to the specified URL |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $sURL The relative or fully qualified URL |
|
|
|
|
|
|
105
|
|
|
* @param integer $nDelay Number of seconds to delay before the redirect occurs |
|
|
|
|
|
|
106
|
|
|
* |
|
107
|
|
|
* @return ResponseInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
public function redirect(string $sURL, int $nDelay = 0): ResponseInterface |
|
110
|
|
|
{ |
|
111
|
|
|
$sURL = $this->xPluginManager->getParameterReader()->parseUrl($sURL); |
|
112
|
|
|
return $this->_addCommand('rd', ['delay' => $nDelay], $sURL); |
|
113
|
|
|
} |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Add a command to make Jaxon to pause execution of the response commands, |
|
117
|
|
|
* returning control to the browser so it can perform other commands asynchronously. |
|
118
|
|
|
* |
|
119
|
|
|
* After the specified delay, Jaxon will continue execution of the response commands. |
|
120
|
|
|
* |
|
121
|
|
|
* @param integer $tenths The number of 1/10ths of a second to sleep |
|
|
|
|
|
|
122
|
|
|
* |
|
123
|
|
|
* @return ResponseInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
public function sleep(int $tenths): ResponseInterface |
|
126
|
|
|
{ |
|
127
|
|
|
$aAttributes = ['cmd' =>'s', 'prop' => $tenths]; |
|
|
|
|
|
|
128
|
|
|
return $this->addCommand($aAttributes, ''); |
|
129
|
|
|
} |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|