Completed
Push — master ( d27f99...f4e97d )
by Jean-Christophe
03:35
created

BaseHtmlEventsTrait::addEvent()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 4
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
		if(isset($this->_events["_create"])){
63
			$create=$this->_events["_create"];
64
			if(\is_array($create)){
65
				$create=\implode("", $create);
66
			}
67
			if($create!=="")
68
				$js->exec($create,true);
69
				unset($this->_events["_create"]);
70
		}
71
		if (isset($this->_bsComponent)) {
72
			foreach ( $this->_events as $event => $jsCode ) {
73
				$code=$jsCode;
74
				if (is_array($jsCode)) {
75
					$code="";
76
					foreach ( $jsCode as $jsC ) {
77
						if ($jsC instanceof AjaxCall) {
78
							$code.="\n" . $jsC->compile($js);
79
						} else {
80
							$code.="\n" . $jsC;
81
						}
82
					}
83
				} elseif ($jsCode instanceof AjaxCall) {
84
					$code=$jsCode->compile($js);
85
				}
86
				$this->_bsComponent->addEvent($event, $code);
87
			}
88
			$this->_events=array ();
89
		}
90
	}
91
92
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
93
		$params=array ("url" => $url,"responseElement" => $responseElement );
94
		$params=array_merge($params, $parameters);
95
		$this->_addEvent($event, new AjaxCall($operation, $params));
96
		return $this;
97
	}
98
99
	public function getOn($event, $url, $responseElement="", $parameters=array()) {
100
		return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters);
101
	}
102
103
	public function getOnClick($url, $responseElement="", $parameters=array()) {
104
		return $this->getOn("click", $url, $responseElement, $parameters);
105
	}
106
107
	public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) {
108
		$parameters["params"]=$params;
109
		return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters);
110
	}
111
112
	public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) {
113
		return $this->postOn("click", $url, $params, $responseElement, $parameters);
114
	}
115
116
	public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) {
117
		$parameters["form"]=$form;
118
		return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters);
119
	}
120
121
	public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) {
122
		return $this->postFormOn("click", $url, $form, $responseElement, $parameters);
123
	}
124
125
	public function jsDoJquery($jqueryCall, $param="") {
126
		return "$('#" . $this->identifier . "')." . $jqueryCall . "(" . Javascript::prep_value($param) . ");";
127
	}
128
129
	public function executeOnRun($jsCode) {
130
		return $this->_addEvent("execute", $jsCode);
131
	}
132
133
	public function jsHtml($content="") {
134
		return $this->jsDoJquery("html", $content);
135
	}
136
137
	public function jsShow() {
138
		return $this->jsDoJquery("show");
139
	}
140
141
	public function jsHide() {
142
		return $this->jsDoJquery("hide");
143
	}
144
}
145