Passed
Push — master ( ef609f...421c61 )
by Jean-Christophe
02:48
created

BaseHtmlEventsTrait::postOn()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 5
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
 * @property BaseHtml _self
14
 */
15
trait BaseHtmlEventsTrait{
16
17
	protected $_events=array ();
18
19
	/**
20
	 * @param string $event
21
	 * @param string|AjaxCall $jsCode
22
	 * @param boolean $stopPropagation
23
	 * @param boolean $preventDefault
24
	 * @return BaseHtml
25
	 */
26
	public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
27
		if ($stopPropagation === true) {
28
			$jsCode=Javascript::$stopPropagation . $jsCode;
1 ignored issue
show
Bug introduced by
Are you sure $jsCode of type string|Ajax\service\AjaxCall can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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