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