| Total Complexity | 90 |
| Total Lines | 543 |
| 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 |
||
| 13 | trait JsUtilsAjaxTrait { |
||
| 14 | |||
| 15 | protected $ajaxTransition; |
||
| 16 | protected $ajaxLoader='<span></span><span></span><span></span><span></span><span></span>'; |
||
| 17 | |||
| 18 | abstract public function getUrl($url); |
||
| 19 | abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$immediatly=true); |
||
| 20 | |||
| 21 | protected function _ajax($method,$url,$responseElement="",$parameters=[]) { |
||
| 22 | $jsCallback=null; |
||
| 23 | $attr="id"; |
||
| 24 | $hasLoader=true; |
||
| 25 | $immediatly=false; |
||
| 26 | $jqueryDone="html"; |
||
| 27 | $ajaxTransition=null; |
||
| 28 | $async=true; |
||
| 29 | $params=null; |
||
| 30 | $headers=null; |
||
| 31 | $jsCondition=null; |
||
| 32 | extract($parameters); |
||
| 33 | |||
| 34 | $jsCallback=isset($jsCallback) ? $jsCallback : ""; |
||
| 35 | $retour=$this->_getAjaxUrl($url, $attr); |
||
| 36 | $responseElement=$this->_getResponseElement($responseElement); |
||
| 37 | $retour.="var self=this;\n"; |
||
| 38 | if($hasLoader===true && JString::isNotNull($responseElement)){ |
||
| 39 | $this->addLoading($retour, $responseElement); |
||
| 40 | } |
||
| 41 | $ajaxParameters=["url"=>"url","method"=>"'".\strtoupper($method)."'"]; |
||
| 42 | if(!$async){ |
||
|
|
|||
| 43 | $ajaxParameters["async"]="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)."});\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 _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback){ |
||
| 103 | $retour="";$call=null; |
||
| 104 | if (JString::isNotNull($responseElement)) { |
||
| 105 | if(isset($ajaxTransition)){ |
||
| 106 | $call=$this->setAjaxDataCall($ajaxTransition); |
||
| 107 | }elseif(isset($this->ajaxTransition)){ |
||
| 108 | $call=$this->ajaxTransition; |
||
| 109 | } |
||
| 110 | if(\is_callable($call)) |
||
| 111 | $retour="\t".$call($responseElement,$jqueryDone).";\n"; |
||
| 112 | else |
||
| 113 | $retour="\t{$responseElement}.{$jqueryDone}( data );\n"; |
||
| 114 | } |
||
| 115 | $retour.="\t".$jsCallback."\n"; |
||
| 116 | return $retour; |
||
| 117 | } |
||
| 118 | |||
| 119 | protected function _getResponseElement($responseElement){ |
||
| 120 | if (JString::isNotNull($responseElement)) { |
||
| 121 | $responseElement=Javascript::prep_value($responseElement); |
||
| 122 | $responseElement=Javascript::prep_jquery_selector($responseElement); |
||
| 123 | } |
||
| 124 | return $responseElement; |
||
| 125 | } |
||
| 126 | |||
| 127 | protected function _correctAjaxUrl($url) { |
||
| 128 | if ($url!=="/" && JString::endsWith($url, "/")===true) |
||
| 129 | $url=substr($url, 0, strlen($url)-1); |
||
| 130 | if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) { |
||
| 131 | $url=$this->getUrl($url); |
||
| 132 | } |
||
| 133 | return $url; |
||
| 134 | } |
||
| 135 | |||
| 136 | public static function _correctParams($params){ |
||
| 137 | if(JString::isNull($params)){ |
||
| 138 | return ""; |
||
| 139 | } |
||
| 140 | if(\preg_match("@^\{.*?\}$@", $params)){ |
||
| 141 | return '$.param('.$params.')'; |
||
| 142 | } |
||
| 143 | return $params; |
||
| 144 | } |
||
| 145 | |||
| 146 | public static function _implodeParams($parameters){ |
||
| 147 | $allParameters=[]; |
||
| 148 | foreach ($parameters as $params){ |
||
| 149 | if(isset($params)) |
||
| 150 | $allParameters[]=self::_correctParams($params); |
||
| 151 | } |
||
| 152 | return \implode("+'&'+", $allParameters); |
||
| 153 | } |
||
| 154 | |||
| 155 | protected function addLoading(&$retour, $responseElement) { |
||
| 156 | $loading_notifier='<div class="ajax-loader">'; |
||
| 157 | if ($this->ajaxLoader==='') { |
||
| 158 | $loading_notifier.="Loading..."; |
||
| 159 | } else { |
||
| 160 | $loading_notifier.=$this->ajaxLoader; |
||
| 161 | } |
||
| 162 | $loading_notifier.='</div>'; |
||
| 163 | $retour.="{$responseElement}.empty();\n"; |
||
| 164 | $retour.="\t\t{$responseElement}.prepend('{$loading_notifier}');\n"; |
||
| 165 | } |
||
| 166 | |||
| 167 | protected function setAjaxDataCall($params){ |
||
| 168 | $result=null; |
||
| 169 | if(!\is_callable($params)){ |
||
| 170 | $result=function ($responseElement,$jqueryDone="html") use($params){ |
||
| 171 | return AjaxTransition::{$params}($responseElement,$jqueryDone); |
||
| 172 | }; |
||
| 173 | } |
||
| 174 | return $result; |
||
| 175 | } |
||
| 176 | |||
| 177 | protected function setDefaultParameters(&$parameters,$default){ |
||
| 178 | foreach ($default as $k=>$v){ |
||
| 179 | if(!isset($parameters[$k])) |
||
| 180 | $parameters[$k]=$v; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | public function setAjaxLoader($loader) { |
||
| 185 | $this->ajaxLoader=$loader; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Performs an ajax GET request |
||
| 190 | * @param string $url The url of the request |
||
| 191 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 192 | */ |
||
| 193 | private function _get($url, $responseElement="",$parameters=[]) { |
||
| 194 | return $this->_ajax("get", $url,$responseElement,$parameters); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Performs an ajax GET request |
||
| 199 | * @param string $url The url of the request |
||
| 200 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 201 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 202 | */ |
||
| 203 | public function get($url, $responseElement="",$parameters=[]) { |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Performs an ajax request |
||
| 210 | * @param string $method The http method (get, post, delete, put, head) |
||
| 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,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 214 | */ |
||
| 215 | public function ajax($method,$url, $responseElement="", $parameters=[]) { |
||
| 216 | $parameters["immediatly"]=true; |
||
| 217 | return $this->_ajax($method,$url,$responseElement,$parameters); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Performs a deferred 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,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 226 | */ |
||
| 227 | public function ajaxDeferred($method,$url, $responseElement="", $parameters=[]) { |
||
| 228 | $parameters["immediatly"]=false; |
||
| 229 | return $this->_ajax($method,$url,$responseElement,$parameters); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
| 234 | * @param string $url the request url |
||
| 235 | * @param string $method Method used |
||
| 236 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
||
| 237 | */ |
||
| 238 | private function _json($url, $method="get",$parameters=[]) { |
||
| 239 | $parameters=\array_merge($parameters,["hasLoader"=>false]); |
||
| 240 | $jsCallback=isset($parameters['jsCallback']) ? $parameters['jsCallback'] : ""; |
||
| 241 | $context=isset($parameters['context']) ? $parameters['context'] : "document"; |
||
| 242 | $retour="\tdata=($.isPlainObject(data))?data:JSON.parse(data);\t".$jsCallback.";\n\tfor(var key in data){" |
||
| 243 | ."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n"; |
||
| 244 | $retour.="\t$(document).trigger('jsonReady',[data]);\n"; |
||
| 245 | $parameters["jsCallback"]=$retour; |
||
| 246 | return $this->_ajax($method, $url,null,$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 | public function json($url, $method="get", $parameters=[]) { |
||
| 256 | return $this->_json($url,$method,$parameters); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
| 261 | * @param string $element |
||
| 262 | * @param string $event |
||
| 263 | * @param string $url the request address |
||
| 264 | * @param string $method default get |
||
| 265 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
| 266 | */ |
||
| 267 | public function jsonOn($event,$element, $url,$method="get",$parameters=array()) { |
||
| 268 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
||
| 269 | return $this->_add_event($element, $this->jsonDeferred($url,$method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name |
||
| 274 | * @param string $url the request url |
||
| 275 | * @param string $method Method used |
||
| 276 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
||
| 277 | */ |
||
| 278 | public function jsonDeferred($url, $method="get", $parameters=[]) { |
||
| 279 | $parameters["immediatly"]=false; |
||
| 280 | return $this->_json($url,$method,$parameters); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
| 285 | * @param string $maskSelector |
||
| 286 | * @param string $url the request url |
||
| 287 | * @param string $method Method used, default : get |
||
| 288 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json") |
||
| 289 | */ |
||
| 290 | private function _jsonArray($maskSelector, $url, $method="get", $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 | public function jsonArray($maskSelector, $url, $method="get", $parameters=[]) { |
||
| 321 | return $this->_jsonArray($maskSelector, $url,$method,$parameters); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * 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 |
||
| 326 | * @param string $maskSelector |
||
| 327 | * @param string $url the request url |
||
| 328 | * @param string $method Method used, default : get |
||
| 329 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"rowClass"=>"_json") |
||
| 330 | */ |
||
| 331 | public function jsonArrayDeferred($maskSelector, $url, $method="get", $parameters) { |
||
| 332 | $parameters["immediatly"]=false; |
||
| 333 | return $this->jsonArray($maskSelector, $url, $method, $parameters); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element |
||
| 338 | * @param string $element |
||
| 339 | * @param string $event |
||
| 340 | * @param string $url the request url |
||
| 341 | * @param string $method Method used, default : get |
||
| 342 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true) |
||
| 343 | */ |
||
| 344 | public function jsonArrayOn($event,$element,$maskSelector, $url,$method="get",$parameters=array()) { |
||
| 345 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
||
| 346 | return $this->_add_event($element, $this->jsonArrayDeferred($maskSelector,$url,$method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Prepares a Get ajax request |
||
| 351 | * To use on an event |
||
| 352 | * @param string $url The url of the request |
||
| 353 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 354 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 355 | */ |
||
| 356 | public function getDeferred($url, $responseElement="", $parameters=[]) { |
||
| 357 | $parameters["immediatly"]=false; |
||
| 358 | return $this->_get($url, $responseElement,$parameters); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Performs a get to $url on the event $event on $element |
||
| 363 | * and display it in $responseElement |
||
| 364 | * @param string $event |
||
| 365 | * @param string $element |
||
| 366 | * @param string $url The url of the request |
||
| 367 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
| 368 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null) |
||
| 369 | */ |
||
| 370 | public function getOn($event, $element, $url, $responseElement="", $parameters=array()) { |
||
| 371 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
||
| 372 | return $this->_add_event($element, $this->getDeferred($url,$responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Performs an ajax request to $url on the event $event on $element |
||
| 377 | * and display it in $responseElement |
||
| 378 | * @param string $event |
||
| 379 | * @param string $element |
||
| 380 | * @param string $url The url of the request |
||
| 381 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
| 382 | * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null) |
||
| 383 | */ |
||
| 384 | public function ajaxOn($event, $element, $url, $responseElement="", $parameters=array()) { |
||
| 385 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true,"method"=>"get"]); |
||
| 386 | return $this->_add_event($element, $this->ajaxDeferred($parameters["method"],$url,$responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Performs a get to $url on the click event on $element |
||
| 391 | * and display it in $responseElement |
||
| 392 | * @param string $element |
||
| 393 | * @param string $url The url of the request |
||
| 394 | * @param string $responseElement The selector of the HTML element displaying the answer |
||
| 395 | * @param array $parameters default : array("method"=>"get","preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null) |
||
| 396 | */ |
||
| 397 | public function ajaxOnClick($element, $url, $responseElement="", $parameters=array()) { |
||
| 398 | return $this->ajaxOn("click", $element, $url, $responseElement, $parameters); |
||
| 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 |
||
| 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("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null) |
||
| 408 | */ |
||
| 409 | public function getOnClick($element, $url, $responseElement="", $parameters=array()) { |
||
| 410 | return $this->getOn("click", $element, $url, $responseElement, $parameters); |
||
| 411 | } |
||
| 412 | |||
| 413 | private function _post($url, $params="{}",$responseElement="", $parameters=[]) { |
||
| 414 | $parameters["params"]=$params; |
||
| 415 | return $this->_ajax("POST", $url,$responseElement,$parameters); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Makes an ajax post |
||
| 420 | * @param string $url the request url |
||
| 421 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 422 | * @param string $params JSON parameters |
||
| 423 | * @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 424 | */ |
||
| 425 | public function post($url, $params="{}",$responseElement="", $parameters=[]) { |
||
| 426 | return $this->_post($url, $params,$responseElement, $parameters); |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Prepares a delayed ajax POST |
||
| 431 | * to use on an event |
||
| 432 | * @param string $url the request url |
||
| 433 | * @param string $params JSON parameters |
||
| 434 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 435 | * @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 436 | */ |
||
| 437 | public function postDeferred($url, $params="{}",$responseElement="", $parameters=[]) { |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Performs a post to $url on the event $event fired on $element and pass the parameters $params |
||
| 444 | * Display the result in $responseElement |
||
| 445 | * @param string $event |
||
| 446 | * @param string $element |
||
| 447 | * @param string $url The url of the request |
||
| 448 | * @param string $params The parameters to send |
||
| 449 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 450 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 451 | */ |
||
| 452 | public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 453 | $this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
||
| 454 | return $this->_add_event($element, $this->postDeferred($url, $params, $responseElement, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Performs a post to $url on the click event fired on $element and pass the parameters $params |
||
| 459 | * Display the result in $responseElement |
||
| 460 | * @param string $element |
||
| 461 | * @param string $url The url of the request |
||
| 462 | * @param string $params The parameters to send |
||
| 463 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 464 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 465 | */ |
||
| 466 | public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) { |
||
| 467 | return $this->postOn("click", $element, $url, $params, $responseElement, $parameters); |
||
| 468 | } |
||
| 469 | |||
| 470 | private function _postForm($url, $form, $responseElement, $parameters=[]) { |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Performs a post form with ajax |
||
| 507 | * @param string $url The url of the request |
||
| 508 | * @param string $form The form HTML id |
||
| 509 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 510 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 511 | */ |
||
| 512 | public function postForm($url, $form, $responseElement, $parameters=[]) { |
||
| 513 | $parameters["immediatly"]=true; |
||
| 514 | return $this->_postForm($url, $form, $responseElement, $parameters); |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Performs a delayed post form with ajax |
||
| 519 | * For use on an event |
||
| 520 | * @param string $url The url of the request |
||
| 521 | * @param string $form The form HTML id |
||
| 522 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 523 | * @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null) |
||
| 524 | */ |
||
| 525 | public function postFormDeferred($url, $form, $responseElement, $parameters=[]) { |
||
| 526 | $parameters["immediatly"]=false; |
||
| 527 | return $this->_postForm($url, $form, $responseElement, $parameters); |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Performs a post form with ajax in response to an event $event on $element |
||
| 532 | * display the result in $responseElement |
||
| 533 | * @param string $event |
||
| 534 | * @param string $element |
||
| 535 | * @param string $url |
||
| 536 | * @param string $form |
||
| 537 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 538 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null) |
||
| 539 | */ |
||
| 540 | public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) { |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Performs a post form with ajax in response to the click event on $element |
||
| 547 | * display the result in $responseElement |
||
| 548 | * @param string $element |
||
| 549 | * @param string $url |
||
| 550 | * @param string $form |
||
| 551 | * @param string $responseElement selector of the HTML element displaying the answer |
||
| 552 | * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null) |
||
| 553 | */ |
||
| 554 | public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) { |
||
| 556 | } |
||
| 557 | } |
||
| 558 |