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