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