Total Complexity | 108 |
Total Lines | 615 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like JsUtilsAjaxTrait 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 JsUtilsAjaxTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait JsUtilsAjaxTrait { |
||
15 | |||
16 | protected $ajaxTransition; |
||
17 | protected $ajaxLoader="<div class=\"ui active centered inline text loader\">Loading</div>"; |
||
18 | |||
19 | abstract public function getUrl($url); |
||
20 | abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true); |
||
21 | abstract public function interval($jsCode,$time,$globalName=null,$immediatly=true); |
||
22 | |||
23 | protected function _ajax($method,$url,$responseElement="",$parameters=[]) { |
||
24 | if(isset($this->params["ajax"])){ |
||
25 | extract($this->params["ajax"]); |
||
26 | } |
||
27 | extract($parameters); |
||
28 | |||
29 | $jsCallback=isset($jsCallback) ? $jsCallback : ""; |
||
30 | $retour=$this->_getAjaxUrl($url, $attr); |
||
31 | $originalSelector=$responseElement; |
||
32 | $responseElement=$this->_getResponseElement($responseElement); |
||
33 | $retour.="var self=this;\n"; |
||
34 | $before=isset($before)?$before:""; |
||
35 | $retour.=$before; |
||
36 | if($hasLoader===true && JString::isNotNull($responseElement)){ |
||
37 | $this->addLoading($retour, $responseElement,$ajaxLoader); |
||
38 | }elseif($hasLoader==="internal"){ |
||
39 | $retour.="\n$(this).addClass('loading');"; |
||
40 | } |
||
41 | $ajaxParameters=["url"=>"url","method"=>"'".\strtoupper($method)."'"]; |
||
42 | |||
43 | $ajaxParameters["async"]=($async?"true":"false"); |
||
44 | |||
45 | if(isset($params)){ |
||
46 | $ajaxParameters["data"]=self::_correctParams($params); |
||
47 | } |
||
48 | if(isset($headers)){ |
||
49 | $ajaxParameters["headers"]=$headers; |
||
50 | } |
||
51 | $this->createAjaxParameters($ajaxParameters, $parameters); |
||
52 | $retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data, textStatus, jqXHR ) {\n"; |
||
53 | $retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback,$hasLoader,($historize?$originalSelector:null))."});\n"; |
||
54 | $retour=$this->_addJsCondition($jsCondition,$retour); |
||
55 | if ($immediatly) |
||
56 | $this->jquery_code_for_compile[]=$retour; |
||
57 | return $retour; |
||
58 | } |
||
59 | |||
60 | protected function createAjaxParameters(&$original,$parameters){ |
||
61 | $validParameters=["dataType"=>"'%value%'","beforeSend"=>"function(jqXHR,settings){%value%}","complete"=>"function(jqXHR){%value%}"]; |
||
62 | foreach ($validParameters as $param=>$mask){ |
||
63 | if(isset($parameters[$param])){ |
||
64 | $original[$param]=\str_replace("%value%", $parameters[$param], $mask); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | protected function implodeAjaxParameters($ajaxParameters){ |
||
72 | } |
||
73 | |||
74 | protected function _addJsCondition($jsCondition,$jsSource){ |
||
75 | if(isset($jsCondition)){ |
||
76 | return "if(".$jsCondition."){\n".$jsSource."\n}"; |
||
77 | } |
||
78 | return $jsSource; |
||
79 | } |
||
80 | |||
81 | |||
82 | protected function _getAjaxUrl($url,$attr){ |
||
83 | $url=$this->_correctAjaxUrl($url); |
||
84 | $retour="url='".$url."';"; |
||
85 | $slash="/"; |
||
86 | if(JString::endswith($url, "/")===true){ |
||
87 | $slash=""; |
||
88 | } |
||
89 | if(JString::isNotNull($attr)){ |
||
90 | if ($attr==="value"){ |
||
91 | $retour.="url=url+'".$slash."'+$(this).val();\n"; |
||
92 | }elseif ($attr==="html"){ |
||
93 | $retour.="url=url+'".$slash."'+$(this).html();\n"; |
||
94 | }elseif(\substr($attr, 0,3)==="js:"){ |
||
95 | $retour.="url=url+'".$slash."'+".\substr($attr, 3).";\n"; |
||
96 | }elseif($attr!==null && $attr!=="") |
||
97 | $retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n"; |
||
98 | } |
||
99 | return $retour; |
||
100 | } |
||
101 | |||
102 | protected function onPopstate(){ |
||
103 | return "window.onpopstate = function(e){if(e.state){var target=e.state.jqueryDone;$(e.state.selector)[target](e.state.html);}};"; |
||
104 | } |
||
105 | |||
106 | protected function autoActiveLinks($previousURL="window.location.href"){ |
||
107 | $result= "\nfunction getHref(url) { return \$('a').filter(function(){return \$(this).prop('href') == url; });}"; |
||
108 | $result.="\nvar myurl={$previousURL};if(window._previousURL) getHref(window._previousURL).removeClass('active');getHref(myurl).addClass('active');window._previousURL=myurl;"; |
||
109 | return $result; |
||
110 | } |
||
111 | |||
112 | protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback,$hasLoader=false,$history=null){ |
||
113 | $retour="";$call=null; |
||
114 | if (JString::isNotNull($responseElement)) { |
||
115 | if(isset($ajaxTransition)){ |
||
116 | $call=$this->setAjaxDataCall($ajaxTransition); |
||
117 | }elseif(isset($this->ajaxTransition)){ |
||
118 | $call=$this->ajaxTransition; |
||
119 | } |
||
120 | if(\is_callable($call)) |
||
121 | $retour="\t".$call($responseElement,$jqueryDone).";\n"; |
||
122 | else |
||
123 | $retour="\t{$responseElement}.{$jqueryDone}( data );\n"; |
||
124 | } |
||
125 | if(isset($history)){ |
||
126 | if($this->params["autoActiveLinks"]){ |
||
127 | $retour.=$this->autoActiveLinks("url"); |
||
128 | } |
||
129 | $retour.="\nwindow.history.pushState({'html':data,'selector':".Javascript::prep_value($history).",'jqueryDone':'{$jqueryDone}'},'', url);"; |
||
130 | } |
||
131 | if($hasLoader==="internal"){ |
||
132 | $retour.="\n$(self).removeClass('loading');"; |
||
133 | } |
||
134 | $retour.="\t".$jsCallback."\n"; |
||
135 | return $retour; |
||
136 | } |
||
137 | |||
138 | protected function _getResponseElement($responseElement){ |
||
139 | if (JString::isNotNull($responseElement)) { |
||
140 | $responseElement=Javascript::prep_jquery_selector($responseElement); |
||
141 | } |
||
142 | return $responseElement; |
||
143 | } |
||
144 | |||
145 | protected function _correctAjaxUrl($url) { |
||
146 | if ($url!=="/" && JString::endsWith($url, "/")===true) |
||
147 | $url=substr($url, 0, strlen($url)-1); |
||
148 | if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) { |
||
149 | $url=$this->getUrl($url); |
||
150 | } |
||
151 | return $url; |
||
152 | } |
||
153 | |||
154 | public static function _correctParams($params){ |
||
155 | if(JString::isNull($params)){ |
||
156 | return ""; |
||
157 | } |
||
158 | if(\preg_match("@^\{.*?\}$@", $params)){ |
||
159 | return '$.param('.$params.')'; |
||
160 | } |
||
161 | return $params; |
||
162 | } |
||
163 | |||
164 | public static function _implodeParams($parameters){ |
||
165 | $allParameters=[]; |
||
166 | foreach ($parameters as $params){ |
||
167 | if(isset($params)) |
||
168 | $allParameters[]=self::_correctParams($params); |
||
169 | } |
||
170 | return \implode("+'&'+", $allParameters); |
||
171 | } |
||
172 | |||
173 | protected function addLoading(&$retour, $responseElement,$ajaxLoader=null) { |
||
174 | if(!isset($ajaxLoader)){ |
||
175 | $ajaxLoader=$this->ajaxLoader; |
||
176 | } |
||
177 | $loading_notifier='<div class="ajax-loader">'.$ajaxLoader.'</div>'; |
||
178 | $retour.="{$responseElement}.empty();\n"; |
||
179 | $retour.="\t\t{$responseElement}.prepend('{$loading_notifier}');\n"; |
||
180 | } |
||
181 | |||
182 | protected function setAjaxDataCall($params){ |
||
183 | $result=null; |
||
184 | if(!\is_callable($params)){ |
||
185 | $result=function ($responseElement,$jqueryDone="html") use($params){ |
||
186 | return AjaxTransition::{$params}($responseElement,$jqueryDone); |
||
187 | }; |
||
188 | } |
||
189 | return $result; |
||
190 | } |
||
191 | |||
192 | protected function setDefaultParameters(&$parameters,$default){ |
||
193 | foreach ($default as $k=>$v){ |
||
194 | if(!isset($parameters[$k])) |
||
195 | $parameters[$k]=$v; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | public function setAjaxLoader($loader) { |
||
200 | $this->ajaxLoader=$loader; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Performs an ajax GET request |
||
205 | * @param string $url The url of the request |
||
206 | * @param string $responseElement selector of the HTML element displaying the answer |
||
207 | */ |
||
208 | private function _get($url, $responseElement="",$parameters=[]) { |
||
209 | return $this->_ajax("get", $url,$responseElement,$parameters); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Performs an ajax GET request |
||
214 | * @param string $url The url of the request |
||
215 | * @param string $responseElement selector of the HTML element displaying the answer |
||
216 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
217 | */ |
||
218 | public function get($url, $responseElement="",$parameters=[]) { |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Performs an ajax request |
||
225 | * @param string $method The http method (get, post, delete, put, head) |
||
226 | * @param string $url The url of the request |
||
227 | * @param string $responseElement selector of the HTML element displaying the answer |
||
228 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
229 | */ |
||
230 | public function ajax($method,$url, $responseElement="", $parameters=[]) { |
||
231 | $parameters["immediatly"]=true; |
||
232 | return $this->_ajax($method,$url,$responseElement,$parameters); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Executes an ajax query at regular intervals |
||
237 | * @param string $method The http method (post, get...) |
||
238 | * @param string $url The url of the request |
||
239 | * @param int $interval The interval in milliseconds |
||
240 | * @param string $globalName The interval name, for clear it |
||
241 | * @param string $responseElement |
||
242 | * @param array $parameters The ajax parameters, default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
243 | * @return string |
||
244 | */ |
||
245 | public function ajaxInterval($method,$url, $interval,$globalName=null,$responseElement="", $parameters=[]){ |
||
246 | return $this->interval($this->ajaxDeferred($method, $url,$responseElement,$parameters), $interval,$globalName); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Performs a deferred ajax request |
||
251 | * @param string $method The http method (get, post, delete, put, head) |
||
252 | * @param string $url The url of the request |
||
253 | * @param string $responseElement selector of the HTML element displaying the answer |
||
254 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
255 | */ |
||
256 | public function ajaxDeferred($method,$url, $responseElement="", $parameters=[]) { |
||
257 | $parameters["immediatly"]=false; |
||
258 | return $this->_ajax($method,$url,$responseElement,$parameters); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
263 | * @param string $url the request url |
||
264 | * @param string $method Method used |
||
265 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
||
266 | */ |
||
267 | private function _json($url, $method="get",$parameters=[]) { |
||
268 | $parameters=\array_merge($parameters,["hasLoader"=>false]); |
||
269 | $jsCallback=isset($parameters['jsCallback']) ? $parameters['jsCallback'] : ""; |
||
270 | $context=isset($parameters['context']) ? $parameters['context'] : "document"; |
||
271 | $retour="\tdata=($.isPlainObject(data))?data:JSON.parse(data);\t".$jsCallback.";" |
||
272 | ."\n\tfor(var key in data){" |
||
273 | ."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n"; |
||
274 | $retour.="\t$(document).trigger('jsonReady',[data]);\n"; |
||
275 | $parameters["jsCallback"]=$retour; |
||
276 | return $this->_ajax($method, $url,null,$parameters); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
281 | * @param string $url the request url |
||
282 | * @param string $method Method used |
||
283 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
||
284 | */ |
||
285 | public function json($url, $method="get", $parameters=[]) { |
||
286 | return $this->_json($url,$method,$parameters); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
291 | * @param string $element |
||
292 | * @param string $event |
||
293 | * @param string $url the request address |
||
294 | * @param string $method default get |
||
295 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
296 | */ |
||
297 | public function jsonOn($event,$element, $url,$method="get",$parameters=array()) { |
||
298 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
||
299 | return $this->_add_event($element, $this->jsonDeferred($url,$method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name |
||
304 | * @param string $url the request url |
||
305 | * @param string $method Method used |
||
306 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
||
307 | */ |
||
308 | public function jsonDeferred($url, $method="get", $parameters=[]) { |
||
309 | $parameters["immediatly"]=false; |
||
310 | return $this->_json($url,$method,$parameters); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
315 | * @param string $maskSelector |
||
316 | * @param string $url the request url |
||
317 | * @param string $method Method used, default : get |
||
318 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json") |
||
319 | */ |
||
320 | private function _jsonArray($maskSelector, $url, $method="get", $parameters=[]) { |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
346 | * @param string $maskSelector |
||
347 | * @param string $url the request url |
||
348 | * @param string $method Method used, default : get |
||
349 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json") |
||
350 | */ |
||
351 | public function jsonArray($maskSelector, $url, $method="get", $parameters=[]) { |
||
352 | return $this->_jsonArray($maskSelector, $url,$method,$parameters); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Peforms an ajax request delayed and receives a JSON array data types by copying and assigning them to the DOM elements with the same name |
||
357 | * @param string $maskSelector |
||
358 | * @param string $url the request url |
||
359 | * @param string $method Method used, default : get |
||
360 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"rowClass"=>"_json") |
||
361 | */ |
||
362 | public function jsonArrayDeferred($maskSelector, $url, $method="get", $parameters) { |
||
363 | $parameters["immediatly"]=false; |
||
364 | return $this->jsonArray($maskSelector, $url, $method, $parameters); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element |
||
369 | * @param string $element |
||
370 | * @param string $event |
||
371 | * @param string $url the request url |
||
372 | * @param string $method Method used, default : get |
||
373 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true) |
||
374 | */ |
||
375 | public function jsonArrayOn($event,$element,$maskSelector, $url,$method="get",$parameters=array()) { |
||
376 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
||
377 | return $this->_add_event($element, $this->jsonArrayDeferred($maskSelector,$url,$method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Prepares a Get ajax request |
||
382 | * for using on an event |
||
383 | * @param string $url The url of the request |
||
384 | * @param string $responseElement selector of the HTML element displaying the answer |
||
385 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
386 | */ |
||
387 | public function getDeferred($url, $responseElement="", $parameters=[]) { |
||
388 | $parameters["immediatly"]=false; |
||
389 | return $this->_get($url, $responseElement,$parameters); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Performs a get to $url on the event $event on $element |
||
394 | * and display it in $responseElement |
||
395 | * @param string $event the event |
||
396 | * @param string $element the element on which event is observed |
||
397 | * @param string $url The url of the request |
||
398 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
399 | * @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) |
||
400 | */ |
||
401 | public function getOn($event, $element, $url, $responseElement="", $parameters=array()) { |
||
402 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
||
403 | return $this->_add_event($element, $this->getDeferred($url,$responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Performs an ajax request to $url on the event $event on $element |
||
408 | * and display it in $responseElement |
||
409 | * @param string $event the event observed |
||
410 | * @param string $element the element on which event is observed |
||
411 | * @param string $url The url of the request |
||
412 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
413 | * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
414 | */ |
||
415 | public function ajaxOn($event, $element, $url, $responseElement="", $parameters=array()) { |
||
416 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true,"method"=>"get"]); |
||
417 | return $this->_add_event($element, $this->ajaxDeferred($parameters["method"],$url,$responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Performs a get to $url on the click event on $element |
||
422 | * and display it in $responseElement |
||
423 | * @param string $element the element on which event is observed |
||
424 | * @param string $url The url of the request |
||
425 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
426 | * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
427 | */ |
||
428 | public function ajaxOnClick($element, $url, $responseElement="", $parameters=array()) { |
||
429 | return $this->ajaxOn("click", $element, $url, $responseElement, $parameters); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Performs a get to $url on the click event on $element |
||
434 | * and display it in $responseElement |
||
435 | * @param string $element the element on which click is observed |
||
436 | * @param string $url The url of the request |
||
437 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
438 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
439 | */ |
||
440 | public function getOnClick($element, $url, $responseElement="", $parameters=array()) { |
||
441 | return $this->getOn("click", $element, $url, $responseElement, $parameters); |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Uses an hyperlink to make an ajax get request |
||
446 | * @param string $element an hyperlink selector |
||
447 | * @param string $responseElement the target of the ajax request (data-target attribute of the element is used if responseElement is omited) |
||
448 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"href","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>true) |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function getHref($element,$responseElement="",$parameters=array()){ |
||
452 | $parameters["attr"]="href"; |
||
453 | if(JString::isNull($responseElement)){ |
||
454 | $responseElement='%$(self).attr("data-target")%'; |
||
455 | } |
||
456 | if(!isset($parameters["historize"])){ |
||
457 | $parameters["historize"]=true; |
||
458 | } |
||
459 | return $this->getOnClick($element, "",$responseElement,$parameters); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Uses an hyperlink to make an ajax get request |
||
464 | * @param string $element an hyperlink selector |
||
465 | * @param string $responseElement the target of the ajax request (data-target attribute of the element is used if responseElement is omited) |
||
466 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"href","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
467 | * @return $this |
||
468 | */ |
||
469 | public function postHref($element,$responseElement="",$parameters=array()){ |
||
470 | $parameters["attr"]="href"; |
||
471 | if(JString::isNull($responseElement)){ |
||
472 | $responseElement='%$(this).attr("data-target")%'; |
||
473 | } |
||
474 | if(!isset($parameters["historize"])){ |
||
475 | $parameters["historize"]=true; |
||
476 | } |
||
477 | return $this->postOnClick($element, "","{}",$responseElement,$parameters); |
||
478 | } |
||
479 | |||
480 | private function _post($url, $params="{}",$responseElement="", $parameters=[]) { |
||
481 | $parameters["params"]=$params; |
||
482 | return $this->_ajax("POST", $url,$responseElement,$parameters); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Makes an ajax post |
||
487 | * @param string $url the request url |
||
488 | * @param string $responseElement selector of the HTML element displaying the answer |
||
489 | * @param string $params JSON parameters |
||
490 | * @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
491 | */ |
||
492 | public function post($url, $params="{}",$responseElement="", $parameters=[]) { |
||
493 | return $this->_post($url, $params,$responseElement, $parameters); |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Prepares a delayed ajax POST |
||
498 | * to use on an event |
||
499 | * @param string $url the request url |
||
500 | * @param string $params JSON parameters |
||
501 | * @param string $responseElement selector of the HTML element displaying the answer |
||
502 | * @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
503 | */ |
||
504 | public function postDeferred($url, $params="{}",$responseElement="", $parameters=[]) { |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Performs a post to $url on the event $event fired on $element and pass the parameters $params |
||
511 | * Display the result in $responseElement |
||
512 | * @param string $event |
||
513 | * @param string $element |
||
514 | * @param string $url The url of the request |
||
515 | * @param string $params The parameters to send |
||
516 | * @param string $responseElement selector of the HTML element displaying the answer |
||
517 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
518 | */ |
||
519 | public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
520 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
||
521 | return $this->_add_event($element, $this->postDeferred($url, $params, $responseElement, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * Performs a post to $url on the click event fired on $element and pass the parameters $params |
||
526 | * Display the result in $responseElement |
||
527 | * @param string $element |
||
528 | * @param string $url The url of the request |
||
529 | * @param string $params The parameters to send |
||
530 | * @param string $responseElement selector of the HTML element displaying the answer |
||
531 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
532 | */ |
||
533 | public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
534 | return $this->postOn("click", $element, $url, $params, $responseElement, $parameters); |
||
535 | } |
||
536 | |||
537 | private function _postForm($url, $form, $responseElement, $parameters=[]) { |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * Performs a post form with ajax |
||
580 | * @param string $url The url of the request |
||
581 | * @param string $form The form HTML id |
||
582 | * @param string $responseElement selector of the HTML element displaying the answer |
||
583 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
584 | */ |
||
585 | public function postForm($url, $form, $responseElement, $parameters=[]) { |
||
586 | $parameters["immediatly"]=true; |
||
587 | return $this->_postForm($url, $form, $responseElement, $parameters); |
||
588 | } |
||
589 | |||
590 | /** |
||
591 | * Performs a delayed post form with ajax |
||
592 | * For use on an event |
||
593 | * @param string $url The url of the request |
||
594 | * @param string $form The form HTML id |
||
595 | * @param string $responseElement selector of the HTML element displaying the answer |
||
596 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
597 | */ |
||
598 | public function postFormDeferred($url, $form, $responseElement, $parameters=[]) { |
||
599 | $parameters["immediatly"]=false; |
||
600 | return $this->_postForm($url, $form, $responseElement, $parameters); |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * Performs a post form with ajax in response to an event $event on $element |
||
605 | * display the result in $responseElement |
||
606 | * @param string $event |
||
607 | * @param string $element |
||
608 | * @param string $url |
||
609 | * @param string $form |
||
610 | * @param string $responseElement selector of the HTML element displaying the answer |
||
611 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false) |
||
612 | */ |
||
613 | public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) { |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * Performs a post form with ajax in response to the click event on $element |
||
620 | * display the result in $responseElement |
||
621 | * @param string $element |
||
622 | * @param string $url |
||
623 | * @param string $form |
||
624 | * @param string $responseElement selector of the HTML element displaying the answer |
||
625 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false) |
||
626 | */ |
||
627 | public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) { |
||
629 | } |
||
630 | } |
||
631 |