Total Complexity | 42 |
Total Lines | 243 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BaseHtmlEventsTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseHtmlEventsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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) { |
||
33 | } |
||
34 | |||
35 | public function trigger($event,$params="[]"){ |
||
36 | $this->executeOnRun('$("#'.$this->identifier.'").trigger("'.$event.'",'.$params.');'); |
||
37 | } |
||
38 | |||
39 | public function jsTrigger($event,$params="[this]"){ |
||
40 | return $this->jsDoJquery("trigger",["'".$event."'",$params]); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param string $event |
||
45 | * @param string|AjaxCall $jsCode |
||
46 | * @return BaseHtml |
||
47 | */ |
||
48 | public function _addEvent($event, $jsCode) { |
||
49 | if (array_key_exists($event, $this->_events)) { |
||
50 | if (\is_array($this->_events[$event])) { |
||
51 | if(array_search($jsCode, $this->_events[$event])===false){ |
||
52 | $this->_events[$event][]=$jsCode; |
||
53 | } |
||
54 | } else { |
||
55 | $this->_events[$event]=array ($this->_events[$event],$jsCode ); |
||
56 | } |
||
57 | } else { |
||
58 | $this->_events[$event]=$jsCode; |
||
59 | } |
||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $event |
||
65 | * @param string $jsCode |
||
66 | * @param boolean $stopPropagation |
||
67 | * @param boolean $preventDefault |
||
68 | * @return BaseHtml |
||
69 | */ |
||
70 | public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
||
71 | return $this->_self->addEvent($event, $jsCode, $stopPropagation, $preventDefault); |
||
72 | } |
||
73 | |||
74 | public function onClick($jsCode, $stopPropagation=false, $preventDefault=true) { |
||
75 | return $this->on("click", $jsCode, $stopPropagation, $preventDefault); |
||
76 | } |
||
77 | |||
78 | public function setClick($jsCode) { |
||
79 | return $this->onClick($jsCode); |
||
80 | } |
||
81 | |||
82 | public function onCreate($jsCode){ |
||
83 | if(isset($this->_events["_create"])){ |
||
84 | $this->_events["_create"][]=$jsCode; |
||
85 | }else{ |
||
86 | $this->_events["_create"]=[$jsCode]; |
||
87 | } |
||
88 | return $this; |
||
89 | } |
||
90 | |||
91 | public function addEventsOnRun(JsUtils $js=NULL) { |
||
92 | $this->_eventsOnCreate($js); |
||
93 | if (isset($this->_bsComponent)) { |
||
94 | foreach ( $this->_events as $event => $jsCode ) { |
||
95 | $code=$jsCode; |
||
96 | if (\is_array($jsCode)) { |
||
97 | $code=""; |
||
98 | foreach ( $jsCode as $jsC ) { |
||
99 | if ($jsC instanceof AjaxCall) { |
||
100 | $code.="\n" . $jsC->compile($js); |
||
101 | } else { |
||
102 | $code.="\n" . $jsC; |
||
103 | } |
||
104 | } |
||
105 | } elseif ($jsCode instanceof AjaxCall) { |
||
106 | $code=$jsCode->compile($js); |
||
107 | } |
||
108 | $this->_bsComponent->addEvent($event, $code); |
||
109 | } |
||
110 | $this->_events=array (); |
||
111 | return $this->_bsComponent->getScript(); |
||
112 | } |
||
113 | return ""; |
||
114 | } |
||
115 | |||
116 | protected function _eventsOnCreate(JsUtils $js=NULL){ |
||
117 | if(isset($this->_events["_create"])){ |
||
118 | $create=$this->_events["_create"]; |
||
119 | if(\is_array($create)){ |
||
120 | $create=\implode("", $create); |
||
121 | } |
||
122 | if(isset($js) && $create!=="") |
||
123 | $js->exec($create,true); |
||
124 | unset($this->_events["_create"]); |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param string $operation http method get, post, postForm or json |
||
130 | * @param string $event the event that triggers the request |
||
131 | * @param string $url The url of the request |
||
132 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
133 | * @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) |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) { |
||
137 | $params=array ("url" => $url,"responseElement" => $responseElement ); |
||
138 | $params=array_merge($params, $parameters); |
||
139 | $this->_addEvent($event, new AjaxCall($operation, $params)); |
||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Performs a get to $url on the event $event on $element |
||
145 | * and display it in $responseElement |
||
146 | * @param string $event the event that triggers the get request |
||
147 | * @param string $url The url of the request |
||
148 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
149 | * @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) |
||
150 | * @return $this |
||
151 | **/ |
||
152 | public function getOn($event, $url, $responseElement="", $parameters=array()) { |
||
153 | return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Performs a get to $url on the click event on $element |
||
158 | * and display it in $responseElement |
||
159 | * @param string $url The url of the request |
||
160 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
161 | * @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) |
||
162 | * @return $this |
||
163 | **/ |
||
164 | public function getOnClick($url, $responseElement="", $parameters=array()) { |
||
165 | return $this->getOn("click", $url, $responseElement, $parameters); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Performs a post to $url on the event $event on $element |
||
170 | * and display it in $responseElement |
||
171 | * @param string $event the event that triggers the post request |
||
172 | * @param string $url The url of the request |
||
173 | * @param string $params the request parameters in JSON format |
||
174 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
175 | * @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) |
||
176 | * @return $this |
||
177 | **/ |
||
178 | public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
179 | $allParameters=[]; |
||
180 | if(isset($parameters["params"])){ |
||
181 | $allParameters[]=JsUtils::_correctParams($parameters["params"]); |
||
182 | } |
||
183 | if(isset($params)){ |
||
184 | $allParameters[]=JsUtils::_correctParams($params); |
||
185 | } |
||
186 | $parameters["params"]=\implode("+'&'+", $allParameters); |
||
187 | return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Performs a post to $url on the click event on $element |
||
192 | * and display it in $responseElement |
||
193 | * @param string $url The url of the request |
||
194 | * @param string $params the request parameters in JSON format |
||
195 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
196 | * @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) |
||
197 | * @return $this |
||
198 | **/ |
||
199 | public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) { |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Performs a post form with ajax |
||
205 | * @param string $event the event that triggers the post request |
||
206 | * @param string $url The url of the request |
||
207 | * @param string $form The form HTML id |
||
208 | * @param string $responseElement selector of the HTML element displaying the answer |
||
209 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) { |
||
213 | $parameters["form"]=$form; |
||
214 | return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Performs a post form with ajax on click |
||
219 | * @param string $url The url of the request |
||
220 | * @param string $form The form HTML id |
||
221 | * @param string $responseElement selector of the HTML element displaying the answer |
||
222 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) { |
||
226 | return $this->postFormOn("click", $url, $form, $responseElement, $parameters); |
||
227 | } |
||
228 | |||
229 | public function jsDoJquery($jqueryCall, $param="") { |
||
230 | return "$('#" . $this->identifier . "')." . $jqueryCall . "(" . Javascript::prep_value($param) . ");"; |
||
231 | } |
||
232 | |||
233 | public function executeOnRun($jsCode) { |
||
235 | } |
||
236 | |||
237 | public function jsHtml($content="") { |
||
238 | return $this->jsDoJquery("html", $content); |
||
239 | } |
||
240 | |||
241 | public function jsShow() { |
||
242 | return $this->jsDoJquery("show"); |
||
243 | } |
||
244 | |||
245 | public function jsHide() { |
||
247 | } |
||
248 | |||
249 | public function jsToggle($value) { |
||
250 | return $this->jsDoJquery("toggle",$value); |
||
251 | } |
||
252 | /** |
||
253 | * @return array |
||
254 | */ |
||
255 | public function getEvents() { |
||
256 | return $this->_events; |
||
257 | } |
||
258 | |||
259 | } |
||
260 |