Passed
Push — master ( 42337f...468e47 )
by Jean-Christophe
02:24
created

BaseHtmlEventsTrait::getOnClick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 2
rs 10
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\service\Javascript;
6
use Ajax\common\html\BaseHtml;
7
8
/**
9
 * @author jc
10
 * @property \Ajax\common\components\SimpleExtComponent $_bsComponent
11
 * @property string identifier
12
 * @property BaseHtml _self
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 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
				if(array_search($jsCode, $this->_events[$event])===false){
44
					$this->_events[$event][]=$jsCode;
45
				}
46
			} else {
47
				$this->_events[$event]=array ($this->_events[$event],$jsCode );
48
			}
49
		} else {
50
			$this->_events[$event]=$jsCode;
51
		}
52
		return $this;
53
	}
54
55
	/**
56
	 * @param string $event
57
	 * @param string $jsCode
58
	 * @param boolean $stopPropagation
59
	 * @param boolean $preventDefault
60
	 * @return BaseHtml
61
	 */
62
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
63
		return $this->_self->addEvent($event, $jsCode, $stopPropagation, $preventDefault);
64
	}
65
66
	public function onClick($jsCode, $stopPropagation=false, $preventDefault=true) {
67
		return $this->on("click", $jsCode, $stopPropagation, $preventDefault);
68
	}
69
70
	public function setClick($jsCode) {
71
		return $this->onClick($jsCode);
72
	}
73
74
	public function onCreate($jsCode){
75
		if(isset($this->_events["_create"])){
76
			$this->_events["_create"][]=$jsCode;
77
		}else{
78
			$this->_events["_create"]=[$jsCode];
79
		}
80
		return $this;
81
	}
82
83
	public function addEventsOnRun(JsUtils $js=NULL) {
84
		$this->_eventsOnCreate($js);
85
		if (isset($this->_bsComponent)) {
86
			foreach ( $this->_events as $event => $jsCode ) {
87
				$code=$jsCode;
88
				if (\is_array($jsCode)) {
89
					$code="";
90
					foreach ( $jsCode as $jsC ) {
91
						if ($jsC instanceof AjaxCall) {
92
							$code.="\n" . $jsC->compile($js);
93
						} else {
94
							$code.="\n" . $jsC;
95
						}
96
					}
97
				} elseif ($jsCode instanceof AjaxCall) {
98
					$code=$jsCode->compile($js);
99
				}
100
				$this->_bsComponent->addEvent($event, $code);
101
			}
102
			$this->_events=array ();
103
			return $this->_bsComponent->getScript();
104
		}
105
		return "";
106
	}
107
108
	protected function _eventsOnCreate(JsUtils $js=NULL){
109
		if(isset($this->_events["_create"])){
110
			$create=$this->_events["_create"];
111
			if(\is_array($create)){
112
				$create=\implode("", $create);
113
			}
114
			if(isset($js) && $create!=="")
115
				$js->exec($create,true);
116
			unset($this->_events["_create"]);
117
		}
118
	}
119
120
	/**
121
	 * @param string $operation http method get, post, postForm or json
122
	 * @param string $event the event that triggers the request
123
	 * @param string $url The url of the request
124
	 * @param string $responseElement The selector of the HTML element displaying the answer
125
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
126
	 * @return $this
127
	 */
128
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
129
		$params=array ("url" => $url,"responseElement" => $responseElement );
130
		$params=array_merge($params, $parameters);
131
		$this->_addEvent($event, new AjaxCall($operation, $params));
132
		return $this;
133
	}
134
135
	/**
136
	 * Performs a get to $url on the event $event on $element
137
	 * and display it in $responseElement
138
	 * @param string $event the event that triggers the get request
139
	 * @param string $url The url of the request
140
	 * @param string $responseElement The selector of the HTML element displaying the answer
141
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
142
	 * @return $this
143
	 **/
144
	public function getOn($event, $url, $responseElement="", $parameters=array()) {
145
		return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters);
146
	}
147
148
	/**
149
	 * Performs a get to $url on the click event on $element
150
	 * and display it in $responseElement
151
	 * @param string $url The url of the request
152
	 * @param string $responseElement The selector of the HTML element displaying the answer
153
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
154
	 * @return $this
155
	 **/
156
	public function getOnClick($url, $responseElement="", $parameters=array()) {
157
		return $this->getOn("click", $url, $responseElement, $parameters);
158
	}
159
160
	/**
161
	 * Performs a post to $url on the event $event on $element
162
	 * and display it in $responseElement
163
	 * @param string $event the event that triggers the post request
164
	 * @param string $url The url of the request
165
	 * @param string $params the request parameters in JSON format
166
	 * @param string $responseElement The selector of the HTML element displaying the answer
167
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
168
	 * @return $this
169
	 **/
170
	public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) {
171
		$allParameters=[];
172
		if(isset($parameters["params"])){
173
			$allParameters[]=JsUtils::_correctParams($parameters["params"]);
174
		}
175
		if(isset($params)){
176
			$allParameters[]=JsUtils::_correctParams($params);
177
		}
178
		$parameters["params"]=\implode("+'&'+", $allParameters);
179
		return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters);
180
	}
181
	
182
	/**
183
	 * Performs a post to $url on the click event on $element
184
	 * and display it in $responseElement
185
	 * @param string $url The url of the request
186
	 * @param string $params the request parameters in JSON format
187
	 * @param string $responseElement The selector of the HTML element displaying the answer
188
	 * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false)
189
	 * @return $this
190
	 **/
191
	public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) {
192
		return $this->postOn("click", $url, $params, $responseElement, $parameters);
193
	}
194
195
	/**
196
	 * Performs a post form with ajax
197
	 * @param string $event the event that triggers the post request
198
	 * @param string $url The url of the request
199
	 * @param string $form The form HTML id
200
	 * @param string $responseElement selector of the HTML element displaying the answer
201
	 * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
202
	 * @return $this
203
	 */
204
	public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) {
205
		$parameters["form"]=$form;
206
		return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters);
207
	}
208
209
	/**
210
	 * Performs a post form with ajax on click
211
	 * @param string $url The url of the request
212
	 * @param string $form The form HTML id
213
	 * @param string $responseElement selector of the HTML element displaying the answer
214
	 * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
215
	 * @return $this
216
	 */
217
	public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) {
218
		return $this->postFormOn("click", $url, $form, $responseElement, $parameters);
219
	}
220
221
	public function jsDoJquery($jqueryCall, $param="") {
222
		return "$('#" . $this->identifier . "')." . $jqueryCall . "(" . Javascript::prep_value($param) . ");";
223
	}
224
225
	public function executeOnRun($jsCode) {
226
		return $this->_addEvent("execute", $jsCode);
227
	}
228
229
	public function jsHtml($content="") {
230
		return $this->jsDoJquery("html", $content);
231
	}
232
233
	public function jsShow() {
234
		return $this->jsDoJquery("show");
235
	}
236
237
	public function jsHide() {
238
		return $this->jsDoJquery("hide");
239
	}
240
241
	public function jsToggle($value) {
242
		return $this->jsDoJquery("toggle",$value);
243
	}
244
	/**
245
	 * @return multitype:
0 ignored issues
show
Bug introduced by
The type Ajax\common\html\traits\multitype was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
246
	 */
247
	public function getEvents() {
248
		return $this->_events;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_events returns the type array which is incompatible with the documented return type Ajax\common\html\traits\multitype.
Loading history...
249
	}
250
251
}
252