|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* EventTrait.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
|
|
|
trait EventTrait |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $sName The command name |
|
|
|
|
|
|
22
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
23
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
24
|
|
|
* @param bool $bRemoveEmpty If true, remove empty attributes |
|
|
|
|
|
|
25
|
|
|
* |
|
26
|
|
|
* @return ResponseInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract protected function _addCommand(string $sName, array $aAttributes, |
|
|
|
|
|
|
29
|
|
|
$mData, bool $bRemoveEmpty = false): ResponseInterface; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Add a command to install an event handler on the specified element |
|
33
|
|
|
* |
|
34
|
|
|
* You can add more than one event handler to an element's event using this method. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
|
|
37
|
|
|
* @param string $sEvent The name of the event |
|
|
|
|
|
|
38
|
|
|
* @param string $sHandler The name of the javascript function to call when the event is fired |
|
|
|
|
|
|
39
|
|
|
* |
|
40
|
|
|
* @return ResponseInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
public function addHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$aAttributes = ['id' => $sTarget, 'prop' => $sEvent]; |
|
45
|
|
|
return $this->_addCommand('ah', $aAttributes, $sHandler); |
|
46
|
|
|
} |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add a command to remove an event handler from an element |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
|
|
52
|
|
|
* @param string $sEvent The name of the event |
|
|
|
|
|
|
53
|
|
|
* @param string $sHandler The name of the javascript function called when the event is fired |
|
|
|
|
|
|
54
|
|
|
* |
|
55
|
|
|
* @return ResponseInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function removeHandler(string $sTarget, string $sEvent, string $sHandler): ResponseInterface |
|
58
|
|
|
{ |
|
59
|
|
|
$aAttributes = ['id' => $sTarget, 'prop' => $sEvent]; |
|
60
|
|
|
return $this->_addCommand('rh', $aAttributes, $sHandler); |
|
61
|
|
|
} |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Add a command to add an event handler on the specified element |
|
65
|
|
|
* This handler can take custom parameters, and is is executed in a specific context. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
|
|
68
|
|
|
* @param string $sEvent The name of the event |
|
|
|
|
|
|
69
|
|
|
* @param string $sHandler The name of the javascript function to call when the event is fired |
|
|
|
|
|
|
70
|
|
|
* @param array $aParams The parameters to pass to the event handler |
|
|
|
|
|
|
71
|
|
|
* |
|
72
|
|
|
* @return ResponseInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
public function addEventHandler(string $sTarget, string $sEvent, |
|
75
|
|
|
string $sHandler, array $aParams = []): ResponseInterface |
|
76
|
|
|
{ |
|
77
|
|
|
$aAttributes = ['id' => $sTarget, 'prop' => $sEvent, 'func' => $sHandler]; |
|
78
|
|
|
return $this->_addCommand('ae', $aAttributes, $aParams); |
|
79
|
|
|
} |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Add a command to set an event handler on the specified element |
|
83
|
|
|
* This handler can take custom parameters, and is is executed in a specific context. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $sTarget The id of the element |
|
|
|
|
|
|
86
|
|
|
* @param string $sEvent The name of the event |
|
|
|
|
|
|
87
|
|
|
* @param string $sHandler The name of the javascript function to call when the event is fired |
|
|
|
|
|
|
88
|
|
|
* @param array $aParams The parameters to pass to the event handler |
|
|
|
|
|
|
89
|
|
|
* |
|
90
|
|
|
* @return ResponseInterface |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setEventHandler(string $sTarget, string $sEvent, |
|
93
|
|
|
string $sHandler, array $aParams = []): ResponseInterface |
|
94
|
|
|
{ |
|
95
|
|
|
$aAttributes = ['id' => $sTarget, 'prop' => $sEvent, 'func' => $sHandler]; |
|
96
|
|
|
return $this->_addCommand('se', $aAttributes, $aParams); |
|
97
|
|
|
} |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Add a command to set a click handler on the browser |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $sTarget The id of the element that contains the event |
|
|
|
|
|
|
103
|
|
|
* @param string $sHandler The name of the javascript function to call when the event is fired |
|
|
|
|
|
|
104
|
|
|
* @param array $aParams The parameters to pass to the event handler |
|
|
|
|
|
|
105
|
|
|
* |
|
106
|
|
|
* @return ResponseInterface |
|
107
|
|
|
*/ |
|
108
|
|
|
public function onClick(string $sTarget, string $sHandler, array $aParams = []): ResponseInterface |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->setEventHandler($sTarget, 'onclick', $sHandler, $aParams); |
|
111
|
|
|
} |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|