1
|
|
|
<?php |
2
|
|
|
namespace Ajax\common\html\traits; |
3
|
|
|
use Ajax\JsUtils; |
4
|
|
|
use Ajax\service\AjaxCall; |
5
|
|
|
use Ajax\common\components\SimpleExtComponent; |
6
|
|
|
use Ajax\service\Javascript; |
7
|
|
|
use Ajax\common\html\BaseHtml; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author jc |
11
|
|
|
* @property SimpleExtComponent $_bsComponent |
12
|
|
|
* @property string identifier |
13
|
|
|
*/ |
14
|
|
|
trait BaseHtmlEventsTrait{ |
15
|
|
|
|
16
|
|
|
protected $_events=array (); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $event |
20
|
|
|
* @param string|AjaxCall $jsCode |
21
|
|
|
* @param boolean $stopPropagation |
22
|
|
|
* @param boolean $preventDefault |
23
|
|
|
* @return \Ajax\common\html\BaseHtml |
24
|
|
|
*/ |
25
|
|
|
public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
26
|
|
|
if ($stopPropagation === true) { |
27
|
|
|
$jsCode=Javascript::$stopPropagation . $jsCode; |
28
|
|
|
} |
29
|
|
|
if ($preventDefault === true) { |
30
|
|
|
$jsCode=Javascript::$preventDefault . $jsCode; |
31
|
|
|
} |
32
|
|
|
return $this->_addEvent($event, $jsCode); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $event |
37
|
|
|
* @param string|AjaxCall $jsCode |
38
|
|
|
* @return BaseHtml |
39
|
|
|
*/ |
40
|
|
|
public function _addEvent($event, $jsCode) { |
41
|
|
|
if (array_key_exists($event, $this->_events)) { |
42
|
|
|
if (\is_array($this->_events[$event])) { |
43
|
|
|
$this->_events[$event][]=$jsCode; |
44
|
|
|
} else { |
45
|
|
|
$this->_events[$event]=array ($this->_events[$event],$jsCode ); |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
$this->_events[$event]=$jsCode; |
49
|
|
|
} |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $event |
55
|
|
|
* @param string $jsCode |
56
|
|
|
* @param boolean $stopPropagation |
57
|
|
|
* @param boolean $preventDefault |
58
|
|
|
* @return \Ajax\common\html\BaseHtml |
59
|
|
|
*/ |
60
|
|
|
public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
61
|
|
|
return $this->addEvent($event, $jsCode, $stopPropagation, $preventDefault); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function onClick($jsCode, $stopPropagation=false, $preventDefault=true) { |
65
|
|
|
return $this->on("click", $jsCode, $stopPropagation, $preventDefault); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function setClick($jsCode) { |
69
|
|
|
return $this->onClick($jsCode); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function onCreate($jsCode){ |
73
|
|
|
if(isset($this->_events["_create"])){ |
74
|
|
|
$this->_events["_create"][]=$jsCode; |
75
|
|
|
}else{ |
76
|
|
|
$this->_events["_create"]=[$jsCode]; |
77
|
|
|
} |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function addEventsOnRun(JsUtils $js=NULL) { |
82
|
|
|
$this->_eventsOnCreate($js); |
83
|
|
|
if (isset($this->_bsComponent)) { |
84
|
|
|
foreach ( $this->_events as $event => $jsCode ) { |
85
|
|
|
$code=$jsCode; |
86
|
|
|
if (\is_array($jsCode)) { |
87
|
|
|
$code=""; |
88
|
|
|
foreach ( $jsCode as $jsC ) { |
89
|
|
|
if ($jsC instanceof AjaxCall) { |
90
|
|
|
$code.="\n" . $jsC->compile($js); |
91
|
|
|
} else { |
92
|
|
|
$code.="\n" . $jsC; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} elseif ($jsCode instanceof AjaxCall) { |
96
|
|
|
$code=$jsCode->compile($js); |
97
|
|
|
} |
98
|
|
|
$this->_bsComponent->addEvent($event, $code); |
99
|
|
|
} |
100
|
|
|
$this->_events=array (); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function _eventsOnCreate(JsUtils $js=NULL){ |
105
|
|
|
if(isset($this->_events["_create"])){ |
106
|
|
|
$create=$this->_events["_create"]; |
107
|
|
|
if(\is_array($create)){ |
108
|
|
|
$create=\implode("", $create); |
109
|
|
|
} |
110
|
|
|
if(isset($js) && $create!=="") |
111
|
|
|
$js->exec($create,true); |
112
|
|
|
unset($this->_events["_create"]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $operation |
118
|
|
|
* @param string $event |
119
|
|
|
* @param string $url |
120
|
|
|
* @param string $responseElement |
121
|
|
|
* @param array $parameters |
122
|
|
|
* @return BaseHtml |
123
|
|
|
*/ |
124
|
|
|
public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) { |
125
|
|
|
$params=array ("url" => $url,"responseElement" => $responseElement ); |
126
|
|
|
$params=array_merge($params, $parameters); |
127
|
|
|
$this->_addEvent($event, new AjaxCall($operation, $params)); |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getOn($event, $url, $responseElement="", $parameters=array()) { |
132
|
|
|
return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getOnClick($url, $responseElement="", $parameters=array()) { |
136
|
|
|
return $this->getOn("click", $url, $responseElement, $parameters); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) { |
140
|
|
|
$parameters["params"]=$params; |
141
|
|
|
return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) { |
145
|
|
|
return $this->postOn("click", $url, $params, $responseElement, $parameters); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) { |
149
|
|
|
$parameters["form"]=$form; |
150
|
|
|
return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) { |
154
|
|
|
return $this->postFormOn("click", $url, $form, $responseElement, $parameters); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function jsDoJquery($jqueryCall, $param="") { |
158
|
|
|
return "$('#" . $this->identifier . "')." . $jqueryCall . "(" . Javascript::prep_value($param) . ");"; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function executeOnRun($jsCode) { |
162
|
|
|
return $this->_addEvent("execute", $jsCode); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function jsHtml($content="") { |
166
|
|
|
return $this->jsDoJquery("html", $content); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function jsShow() { |
170
|
|
|
return $this->jsDoJquery("show"); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function jsHide() { |
174
|
|
|
return $this->jsDoJquery("hide"); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function jsToggle($value) { |
178
|
|
|
return $this->jsDoJquery("hide",$value); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|