Passed
Push — master ( ccea9a...7a8d95 )
by Jean-Christophe
01:50
created

JsUtilsAjaxTrait::setAjaxLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\common\traits;
4
5
use Ajax\service\AjaxTransition;
6
use Ajax\service\Javascript;
7
use Ajax\service\JString;
8
9
/**
10
 * @author jc
11
 * @property array $jquery_code_for_compile
12
 * @property array $params
13
 */
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){
67
		$s = ''; foreach ($ajaxParameters as $k=>$v) { if ($s !== '') { $s .= ','; } $s .= "'{$k}':{$v}"; }
68
		return $s;
69
	}
70
71
	protected function _addJsCondition($jsCondition,$jsSource){
72
		if(isset($jsCondition)){
73
			return "if(".$jsCondition."){\n".$jsSource."\n}";
74
		}
75
		return $jsSource;
76
	}
77
78
79
	protected function _getAjaxUrl($url,$attr){
80
		$url=$this->_correctAjaxUrl($url);
81
		$retour="url='".$url."';";
82
		$slash="/";
83
		if(JString::endswith($url, "/")===true){
84
			$slash="";
85
		}
86
		if(JString::isNotNull($attr)){
87
			if ($attr==="value"){
88
				$retour.="url=url+'".$slash."'+$(this).val();\n";
89
			}elseif ($attr==="html"){
90
				$retour.="url=url+'".$slash."'+$(this).html();\n";
91
			}elseif(\substr($attr, 0,3)==="js:"){
92
				$retour.="url=url+'".$slash."'+".\substr($attr, 3).";\n";
93
			}elseif($attr!==null && $attr!=="")
94
				$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
95
		}
96
		return $retour;
97
	}
98
	
99
	protected function onPopstate(){
100
		return "window.onpopstate = function(e){if(e.state){var target=e.state.jqueryDone;$(e.state.selector)[target](e.state.html);}};";
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=[]) {
216
		$parameters["immediatly"]=true;
217
		return $this->_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=[]) {
303
		$parameters=\array_merge($parameters,["hasLoader"=>false]);
304
		$rowClass=isset($parameters['rowClass']) ? $parameters['rowClass'] : "_json";
305
		$jsCallback=isset($parameters['jsCallback']) ? $parameters['jsCallback'] : "";
306
		$context=isset($parameters['context']) ? $parameters['context'] : null;
307
		if($context===null){
308
			$parent="$('".$maskSelector."').parent()";
309
			$newElm = "$('#'+newId)";
310
		}else{
311
			$parent=$context;
312
			$newElm = $context.".find('#'+newId)";
313
		}
314
		$appendTo="\t\tnewElm.appendTo(".$parent.");\n";
315
		$retour=$parent.".find('.{$rowClass}').remove();";
316
		$retour.="\tdata=($.isPlainObject(data))?data:JSON.parse(data);\t".$jsCallback.";\n$.each(data, function(index, value) {\n"."\tvar created=false;var maskElm=$('".$maskSelector."').first();maskElm.hide();"."\tvar newId=(maskElm.attr('id') || 'mask')+'-'+index;"."\tvar newElm=".$newElm.";\n"."\tif(!newElm.length){\n"."\t\tnewElm=maskElm.clone();
317
		newElm.attr('id',newId);\n;newElm.addClass('{$rowClass}').removeClass('_jsonArrayModel');\nnewElm.find('[id]').each(function(){ var newId=$(this).attr('id')+'-'+index;$(this).attr('id',newId).removeClass('_jsonArrayChecked');});\n";
318
		$retour.= $appendTo;
319
		$retour.="\t}\n"."\tfor(var key in value){\n"."\t\t\tvar html = $('<div />').append($(newElm).clone()).html();\n"."\t\t\tif(html.indexOf('__'+key+'__')>-1){\n"."\t\t\t\tcontent=$(html.split('__'+key+'__').join(value[key]));\n"."\t\t\t\t$(newElm).replaceWith(content);newElm=content;\n"."\t\t\t}\n"."\t\tvar sel='[data-id=\"'+key+'\"]';if($(sel,newElm).length){\n"."\t\t\tvar selElm=$(sel,newElm);\n"."\t\t\t if(selElm.is('[value]')) { selElm.attr('value',value[key]);selElm.val(value[key]);} else { selElm.html(value[key]); }\n"."\t\t}\n"."}\n"."\t$(newElm).show(true);"."\n"."\t$(newElm).removeClass('hide');"."});\n";
320
		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
321
		$parameters["jsCallback"]=$retour;
322
		return $this->_ajax($method, $url,null,$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=[]) {
486
		$parameters["immediatly"]=false;
487
		return $this->_post($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=[]) {
519
		if(isset($this->params["ajax"])){
520
			extract($this->params["ajax"]);
521
		}
522
		$params="{}";$validation=false;
523
		\extract($parameters);
524
		$async=($async)?"true":"false";
525
		$jsCallback=isset($jsCallback) ? $jsCallback : "";
526
		$retour=$this->_getAjaxUrl($url, $attr);
527
		$retour.="\n$('#".$form."').trigger('ajaxSubmit');";
528
		$retour.="\nvar params=$('#".$form."').serialize();\n";
529
		if(isset($params)){
530
			$retour.="params+='&'+".self::_correctParams($params).";\n";
531
		}
532
		$responseElement=$this->_getResponseElement($responseElement);
533
		$retour.="var self=this;\n";
534
		if($hasLoader===true){
535
			$this->addLoading($retour, $responseElement,$ajaxLoader);
536
		}elseif($hasLoader==="internal"){
537
			$retour.="\n$(this).addClass('loading');";
538
		}
539
		$ajaxParameters=["url"=>"url","method"=>"'POST'","data"=>"params","async"=>$async];
540
		if(isset($headers)){
541
			$ajaxParameters["headers"]=$headers;
542
		}
543
		$this->createAjaxParameters($ajaxParameters, $parameters);
544
		$retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data ) {\n";
545
		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback,$hasLoader)."});\n";
546
547
		if ($validation) {
548
			$retour="$('#".$form."').validate({submitHandler: function(form) {
549
			".$retour."
550
			}});\n";
551
			$retour.="$('#".$form."').submit();\n";
552
		}
553
		$retour=$this->_addJsCondition($jsCondition, $retour);
554
		if ($immediatly)
555
			$this->jquery_code_for_compile[]=$retour;
556
		return $retour;
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()) {
595
		$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]);
596
		return $this->_add_event($element, $this->postFormDeferred($url, $form, $responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]);
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()) {
609
		return $this->postFormOn("click", $element, $url, $form, $responseElement, $parameters);
610
	}
611
}
612