Passed
Push — master ( b63c5d...f2c7af )
by Jean-Christophe
02:28
created

JsUtilsAjaxTrait::ajaxOnClick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 4
dl 0
loc 2
rs 10
1
<?php
2
namespace Ajax\common\traits;
3
4
use Ajax\service\AjaxTransition;
5
use Ajax\service\JString;
6
use Ajax\service\Javascript;
7
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
18
	protected $ajaxLoader = "<div class=\"ui active centered inline text loader\">Loading</div>";
19
20
	abstract public function getUrl($url);
21
22
	abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn = false);
23
24
	abstract public function interval($jsCode, $time, $globalName = null, $immediatly = true);
25
26
	protected function _ajax($method, $url, $responseElement = '', $parameters = []) {
27
		if (isset($this->params['ajax'])) {
28
			extract($this->params['ajax']);
29
		}
30
		extract($parameters);
31
32
		$jsCallback = isset($jsCallback) ? $jsCallback : '';
33
		$retour = $this->_getAjaxUrl($url, $attr);
34
		$originalSelector = $responseElement;
35
		$responseElement = $this->_getResponseElement($responseElement);
36
		$retour .= "var self=this;\n";
37
		$before = isset($before) ? $before : "";
38
		$retour .= $before;
39
		if ($hasLoader === true && JString::isNotNull($responseElement)) {
40
			$this->addLoading($retour, $responseElement, $ajaxLoader);
41
		} elseif ($hasLoader === 'response') {
42
			$this->addResponseLoading($retour, $responseElement, $ajaxLoader);
43
		} elseif ($hasLoader === 'internal-x') {
44
			$this->addLoading($retour, '$(this).closest(".item, .step")', $ajaxLoader);
45
		} elseif ($hasLoader === 'internal') {
46
			$retour .= "\n$(this).addClass('loading');";
47
		}
48
		$ajaxParameters = [
49
			"url" => "url",
50
			"method" => "'" . \strtoupper($method) . "'"
51
		];
52
53
		$ajaxParameters["async"] = ($async ? "true" : "false");
54
55
		if (isset($params)) {
56
			$ajaxParameters["data"] = self::_correctParams($params, $parameters);
57
		}
58
		if (isset($headers)) {
59
			$ajaxParameters["headers"] = $headers;
60
		}
61
		if ($csrf) {
62
			$csrf = (is_string($csrf)) ? $csrf : 'csrf-token';
63
			$parameters["beforeSend"] = "jqXHR.setRequestHeader('{$csrf}', $('meta[name=\"{$csrf}\"]').attr('content'));";
64
		}
65
		if (isset($partial)) {
66
			$ajaxParameters["xhr"] = "xhrProvider";
67
			$retour .= "var xhr = $.ajaxSettings.xhr();function xhrProvider() {return xhr;};xhr.onreadystatechange = function (e) { if (3==e.target.readyState){let response=e.target.responseText;" . $partial . ";}; };";
68
		}
69
		$this->createAjaxParameters($ajaxParameters, $parameters);
70
		$retour .= "$.ajax({" . $this->implodeAjaxParameters($ajaxParameters) . "}).done(function( data, textStatus, jqXHR ) {\n";
71
		$retour .= $this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader, ($historize ? $originalSelector : null)) . "});\n";
72
73
		$retour = $this->_addJsCondition($jsCondition, $retour);
74
		if ($immediatly)
75
			$this->jquery_code_for_compile[] = $retour;
76
		return $retour;
77
	}
78
79
	protected function createAjaxParameters(&$original, $parameters) {
80
		$validParameters = [
81
			"contentType" => "%value%",
82
			"dataType" => "'%value%'",
83
			"beforeSend" => "function(jqXHR,settings){%value%}",
84
			"complete" => "function(jqXHR){%value%}",
85
			"processData" => "%value%"
86
		];
87
		foreach ($validParameters as $param => $mask) {
88
			if (isset($parameters[$param])) {
89
				$original[$param] = \str_replace("%value%", $parameters[$param], $mask);
90
			}
91
		}
92
	}
93
94
	protected function implodeAjaxParameters($ajaxParameters) {
95
		$s = '';
96
		foreach ($ajaxParameters as $k => $v) {
97
			if ($s !== '') {
98
				$s .= ',';
99
			}
100
			if (is_array($v)) {
101
				$s .= "'{$k}':{" . self::implodeAjaxParameters($v) . "}";
0 ignored issues
show
Bug Best Practice introduced by
The method Ajax\common\traits\JsUti...implodeAjaxParameters() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
				$s .= "'{$k}':{" . self::/** @scrutinizer ignore-call */ implodeAjaxParameters($v) . "}";
Loading history...
102
			} else {
103
				$s .= "'{$k}':{$v}";
104
			}
105
		}
106
		return $s;
107
	}
108
109
	protected function _addJsCondition($jsCondition, $jsSource) {
110
		if (isset($jsCondition)) {
111
			return "if(" . $jsCondition . "){\n" . $jsSource . "\n}";
112
		}
113
		return $jsSource;
114
	}
115
116
	protected function _getAjaxUrl($url, $attr) {
117
		$url = $this->_correctAjaxUrl($url);
118
		$retour = "url='" . $url . "';";
119
		$slash = "/";
120
		if (JString::endswith($url, "/") === true) {
121
			$slash = "";
122
		}
123
124
		if (JString::isNotNull($attr)) {
125
			if ($attr === "value") {
126
				$retour .= "url=url+'" . $slash . "'+$(this).val();\n";
127
			} elseif ($attr === "html") {
128
				$retour .= "url=url+'" . $slash . "'+$(this).html();\n";
129
			} elseif (\substr($attr, 0, 3) === "js:") {
130
				$retour .= "url=url+'" . $slash . "'+" . \substr($attr, 3) . ";\n";
131
			} elseif ($attr !== null && $attr !== "") {
132
				$retour .= "let elmUrl=$(this).attr('" . $attr . "')||'';";
133
				$retour .= "url=(!/^((http|https|ftp):\/\/)/.test(elmUrl))?url+'" . $slash . "'+elmUrl:elmUrl;\n";
134
			}
135
		}
136
		return $retour;
137
	}
138
139
	protected function onPopstate() {
140
		return "window.onpopstate = function(e){if(e.state){var target=e.state.jqueryDone;$(e.state.selector)[target](e.state.html);}};";
141
	}
142
143
	protected function autoActiveLinks($previousURL = "window.location.href") {
144
		$result = "\nfunction getHref(url) { return \$('a').filter(function(){return \$(this).prop('href') == url; });}";
145
		$result .= "\nvar myurl={$previousURL};if(window._previousURL) getHref(window._previousURL).removeClass('active');getHref(myurl).addClass('active');window._previousURL=myurl;";
146
		return $result;
147
	}
148
149
	protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader = false, $history = null) {
150
		$retour = "";
151
		$call = null;
152
		if (JString::isNotNull($responseElement)) {
153
			if (isset($ajaxTransition)) {
154
				$call = $this->setAjaxDataCall($ajaxTransition);
155
			} elseif (isset($this->ajaxTransition)) {
156
				$call = $this->ajaxTransition;
157
			}
158
			if (\is_callable($call))
159
				$retour = "\t" . $call($responseElement, $jqueryDone) . ";\n";
160
			else
161
				$retour = "\t{$responseElement}.{$jqueryDone}( data );\n";
162
		}
163
		if (isset($history)) {
164
			if ($this->params["autoActiveLinks"]) {
165
				$retour .= $this->autoActiveLinks("url");
166
			}
167
			$retour .= "\nwindow.history.pushState({'html':data,'selector':" . Javascript::prep_value($history) . ",'jqueryDone':'{$jqueryDone}'},'', url);";
168
		}
169
		if ($hasLoader === 'internal') {
170
			$retour .= "\n$(self).removeClass('loading');";
171
		} elseif ($hasLoader === 'internal-x') {
172
			$retour .= "\n$(self).children('.ajax-loader').remove();";
173
		}
174
		$retour .= "\t" . $jsCallback . "\n";
175
		return $retour;
176
	}
177
178
	protected function _getResponseElement($responseElement) {
179
		if (JString::isNotNull($responseElement)) {
180
			$responseElement = Javascript::prep_jquery_selector($responseElement);
181
		}
182
		return $responseElement;
183
	}
184
185
	protected function _correctAjaxUrl($url) {
186
		if ($url !== "/" && JString::endsWith($url, "/") === true)
187
			$url = substr($url, 0, strlen($url) - 1);
188
		if (strncmp($url, 'http://', 7) != 0 && strncmp($url, 'https://', 8) != 0) {
189
			$url = $this->getUrl($url);
190
		}
191
		return $url;
192
	}
193
194
	public static function _correctParams($params, $ajaxParameters = []) {
195
		if (JString::isNull($params)) {
196
			return "";
197
		}
198
		if (\preg_match("@^\{.*?\}$@", $params)) {
199
			if (! isset($ajaxParameters['contentType']) || ! JString::contains($ajaxParameters['contentType'], 'json')) {
200
				return '$.param(' . $params . ')';
201
			} else {
202
				return 'JSON.stringify(' . $params . ')';
203
			}
204
		}
205
		return $params;
206
	}
207
208
	public static function _implodeParams($parameters) {
209
		$allParameters = [];
210
		foreach ($parameters as $params) {
211
			if (isset($params))
212
				$allParameters[] = self::_correctParams($params);
213
		}
214
		return \implode("+'&'+", $allParameters);
215
	}
216
217
	protected function addLoading(&$retour, $responseElement, $ajaxLoader = null) {
218
		if (! isset($ajaxLoader)) {
219
			$ajaxLoader = $this->ajaxLoader;
220
		}
221
		$loading_notifier = '<div class="ajax-loader ui active inverted dimmer">' . $ajaxLoader . '</div>';
222
		$retour .= "\t\t{$responseElement}.append('{$loading_notifier}');\n";
223
	}
224
225
	protected function addResponseLoading(&$retour, $responseElement, $ajaxLoader = null) {
226
		if (! isset($ajaxLoader)) {
227
			$ajaxLoader = $this->ajaxLoader;
228
		}
229
		$loading_notifier = '<div class="ajax-loader">' . $ajaxLoader . '</div>';
230
		$retour .= "{$responseElement}.empty();\n";
231
		$retour .= "\t\t{$responseElement}.prepend('{$loading_notifier}');\n";
232
	}
233
234
	protected function setAjaxDataCall($params) {
235
		$result = null;
236
		if (! \is_callable($params)) {
237
			$result = function ($responseElement, $jqueryDone = 'html') use ($params) {
238
				return AjaxTransition::{$params}($responseElement, $jqueryDone);
239
			};
240
		}
241
		return $result;
242
	}
243
244
	protected function setDefaultParameters(&$parameters, $default) {
245
		foreach ($default as $k => $v) {
246
			if (! isset($parameters[$k]))
247
				$parameters[$k] = $v;
248
		}
249
	}
250
251
	public function setAjaxLoader($loader) {
252
		$this->ajaxLoader = $loader;
253
	}
254
255
	/**
256
	 * Performs an ajax GET request
257
	 *
258
	 * @param string $url
259
	 *        	The url of the request
260
	 * @param string $responseElement
261
	 *        	selector of the HTML element displaying the answer
262
	 */
263
	private function _get($url, $responseElement = '', $parameters = []) {
264
		return $this->_ajax('get', $url, $responseElement, $parameters);
265
	}
266
267
	/**
268
	 * Performs an ajax GET request
269
	 *
270
	 * @param string $url
271
	 *        	The url of the request
272
	 * @param string $responseElement
273
	 *        	selector of the HTML element displaying the answer
274
	 * @param array $parameters
275
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
276
	 */
277
	public function get($url, $responseElement = '', $parameters = []) {
278
		$parameters['immediatly'] = true;
279
		return $this->_get($url, $responseElement, $parameters);
280
	}
281
282
	/**
283
	 * Performs an ajax request
284
	 *
285
	 * @param string $method
286
	 *        	The http method (get, post, delete, put, head)
287
	 * @param string $url
288
	 *        	The url of the request
289
	 * @param string $responseElement
290
	 *        	selector of the HTML element displaying the answer
291
	 * @param array $parameters
292
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
293
	 */
294
	public function ajax($method, $url, $responseElement = '', $parameters = []) {
295
		$parameters['immediatly'] = true;
296
		return $this->_ajax($method, $url, $responseElement, $parameters);
297
	}
298
299
	/**
300
	 * Executes an ajax query at regular intervals
301
	 *
302
	 * @param string $method
303
	 *        	The http method (post, get...)
304
	 * @param string $url
305
	 *        	The url of the request
306
	 * @param int $interval
307
	 *        	The interval in milliseconds
308
	 * @param string $globalName
309
	 *        	The interval name, for clear it
310
	 * @param string $responseElement
311
	 * @param array $parameters
312
	 *        	The ajax parameters, default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
313
	 * @param
314
	 *        	$immediatly
315
	 * @return string
316
	 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $immediatly at position 0 could not be parsed: Unknown type name '$immediatly' at position 0 in $immediatly.
Loading history...
317
	public function ajaxInterval($method, $url, $interval, $globalName = null, $responseElement = '', $parameters = [], $immediatly = true) {
318
		return $this->interval($this->ajaxDeferred($method, $url, $responseElement, $parameters), $interval, $globalName, $immediatly);
319
	}
320
321
	/**
322
	 * Performs a deferred ajax request
323
	 *
324
	 * @param string $method
325
	 *        	The http method (get, post, delete, put, head)
326
	 * @param string $url
327
	 *        	The url of the request
328
	 * @param string $responseElement
329
	 *        	selector of the HTML element displaying the answer
330
	 * @param array $parameters
331
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
332
	 */
333
	public function ajaxDeferred($method, $url, $responseElement = '', $parameters = []) {
334
		$parameters['immediatly'] = false;
335
		return $this->_ajax($method, $url, $responseElement, $parameters);
336
	}
337
338
	/**
339
	 * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name
340
	 *
341
	 * @param string $url
342
	 *        	the request url
343
	 * @param string $method
344
	 *        	Method used
345
	 * @param array $parameters
346
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"before"=>null)
347
	 */
348
	private function _json($url, $method = "get", $parameters = []) {
349
		$parameters = \array_merge($parameters, [
350
			"hasLoader" => false
351
		]);
352
		$jsCallback = isset($parameters['jsCallback']) ? $parameters['jsCallback'] : "";
353
		$context = isset($parameters['context']) ? $parameters['context'] : "document";
354
		$retour = "\tdata=($.isPlainObject(data))?data:JSON.parse(data);\t" . $jsCallback . ";" . "\n\tfor(var key in data){" . "if($('#'+key," . $context . ").length){ if($('#'+key," . $context . ").is('[value]')) { $('#'+key," . $context . ").val(data[key]);} else { $('#'+key," . $context . ").html(data[key]); }}};\n";
355
		$retour .= "\t$(document).trigger('jsonReady',[data]);\n";
356
		$parameters["jsCallback"] = $retour;
357
		return $this->_ajax($method, $url, null, $parameters);
358
	}
359
360
	/**
361
	 * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name
362
	 *
363
	 * @param string $url
364
	 *        	the request url
365
	 * @param string $method
366
	 *        	Method used
367
	 * @param array $parameters
368
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"before"=>null)
369
	 */
370
	public function json($url, $method = "get", $parameters = []) {
371
		return $this->_json($url, $method, $parameters);
372
	}
373
374
	/**
375
	 * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element
376
	 *
377
	 * @param string $element
378
	 * @param string $event
379
	 * @param string $url
380
	 *        	the request address
381
	 * @param string $method
382
	 *        	default get
383
	 * @param array $parameters
384
	 *        	default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true,"before"=>null,"listenerOn"=>false)
385
	 */
386
	public function jsonOn($event, $element, $url, $method = 'get', $parameters = array()) {
387
		$this->setDefaultParameters($parameters, [
388
			'preventDefault' => true,
389
			'stopPropagation' => true,
390
			'immediatly' => true,
391
			'listenerOn' => false
392
		]);
393
		return $this->_add_event($element, $this->jsonDeferred($url, $method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"], $parameters["immediatly"], $parameters['listenerOn']);
394
	}
395
396
	/**
397
	 * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name
398
	 *
399
	 * @param string $url
400
	 *        	the request url
401
	 * @param string $method
402
	 *        	Method used
403
	 * @param array $parameters
404
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"before"=>null)
405
	 */
406
	public function jsonDeferred($url, $method = 'get', $parameters = []) {
407
		$parameters['immediatly'] = false;
408
		return $this->_json($url, $method, $parameters);
409
	}
410
411
	/**
412
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name
413
	 *
414
	 * @param string $maskSelector
415
	 * @param string $url
416
	 *        	the request url
417
	 * @param string $method
418
	 *        	Method used, default : get
419
	 * @param array $parameters
420
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json","before"=>null)
421
	 */
422
	private function _jsonArray($maskSelector, $url, $method = 'get', $parameters = []) {
423
		$parameters = \array_merge($parameters, [
424
			"hasLoader" => false
425
		]);
426
		$rowClass = isset($parameters['rowClass']) ? $parameters['rowClass'] : "_json";
427
		$jsCallback = isset($parameters['jsCallback']) ? $parameters['jsCallback'] : "";
428
		$context = isset($parameters['context']) ? $parameters['context'] : null;
429
		if ($context === null) {
430
			$parent = "$('" . $maskSelector . "').parent()";
431
			$newElm = "$('#'+newId)";
432
		} else {
433
			$parent = $context;
434
			$newElm = $context . ".find('#'+newId)";
435
		}
436
		$appendTo = "\t\tnewElm.appendTo(" . $parent . ");\n";
437
		$retour = $parent . ".find('.{$rowClass}').remove();";
438
		$retour .= "\tdata=($.isPlainObject(data)||$.isArray(data))?data:JSON.parse(data);\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();
439
		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";
440
		$retour .= $appendTo;
441
		$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";
442
		$retour .= "\t$(document).trigger('jsonReady',[data]);\n";
443
		$retour .= "\t" . $jsCallback;
444
		$parameters["jsCallback"] = $retour;
445
		return $this->_ajax($method, $url, null, $parameters);
446
	}
447
448
	/**
449
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name
450
	 *
451
	 * @param string $maskSelector
452
	 * @param string $url
453
	 *        	the request url
454
	 * @param string $method
455
	 *        	Method used, default : get
456
	 * @param array $parameters
457
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json","before"=>null)
458
	 */
459
	public function jsonArray($maskSelector, $url, $method = 'get', $parameters = []) {
460
		return $this->_jsonArray($maskSelector, $url, $method, $parameters);
461
	}
462
463
	/**
464
	 * 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
465
	 *
466
	 * @param string $maskSelector
467
	 * @param string $url
468
	 *        	the request url
469
	 * @param string $method
470
	 *        	Method used, default : get
471
	 * @param array $parameters
472
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"rowClass"=>"_json","before"=>null)
473
	 */
474
	public function jsonArrayDeferred($maskSelector, $url, $method = 'get', $parameters = []) {
475
		$parameters['immediatly'] = false;
476
		return $this->jsonArray($maskSelector, $url, $method, $parameters);
477
	}
478
479
	/**
480
	 * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element
481
	 *
482
	 * @param string $element
483
	 * @param string $event
484
	 * @param string $url
485
	 *        	the request url
486
	 * @param string $method
487
	 *        	Method used, default : get
488
	 * @param array $parameters
489
	 *        	default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true,"before"=>null,"listenerOn"=>false)
490
	 */
491
	public function jsonArrayOn($event, $element, $maskSelector, $url, $method = 'get', $parameters = array()) {
492
		$this->setDefaultParameters($parameters, [
493
			'preventDefault' => true,
494
			'stopPropagation' => true,
495
			'immediatly' => true,
496
			'listenerOn' => false
497
		]);
498
		return $this->_add_event($element, $this->jsonArrayDeferred($maskSelector, $url, $method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"], $parameters["immediatly"], $parameters['listenerOn']);
499
	}
500
501
	/**
502
	 * Prepares a Get ajax request
503
	 * for using on an event
504
	 *
505
	 * @param string $url
506
	 *        	The url of the request
507
	 * @param string $responseElement
508
	 *        	selector of the HTML element displaying the answer
509
	 * @param array $parameters
510
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
511
	 */
512
	public function getDeferred($url, $responseElement = "", $parameters = []) {
513
		$parameters['immediatly'] = false;
514
		return $this->_get($url, $responseElement, $parameters);
515
	}
516
517
	/**
518
	 * Performs a get to $url on the event $event on $element
519
	 * and display it in $responseElement
520
	 *
521
	 * @param string $event
522
	 *        	the event
523
	 * @param string $element
524
	 *        	the element on which event is observed
525
	 * @param string $url
526
	 *        	The url of the request
527
	 * @param string $responseElement
528
	 *        	The selector of the HTML element displaying the answer
529
	 * @param array $parameters
530
	 *        	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,"before"=>null,"listenerOn"=>false)
531
	 */
532
	public function getOn($event, $element, $url, $responseElement = "", $parameters = array()) {
533
		$parameters['method'] = 'get';
534
		return $this->ajaxOn($event, $element, $url, $responseElement, $parameters);
535
	}
536
537
	/**
538
	 * Performs an ajax request to $url on the event $event on $element
539
	 * and display it in $responseElement
540
	 *
541
	 * @param string $event
542
	 *        	the event observed
543
	 * @param string $element
544
	 *        	the element on which event is observed
545
	 * @param string $url
546
	 *        	The url of the request
547
	 * @param string $responseElement
548
	 *        	The selector of the HTML element displaying the answer
549
	 * @param array $parameters
550
	 *        	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,"before"=>null,"listenerOn"=>false)
551
	 */
552
	public function ajaxOn($event, $element, $url, $responseElement = '', $parameters = array()) {
553
		$this->setDefaultParameters($parameters, [
554
			'preventDefault' => true,
555
			'stopPropagation' => true,
556
			'immediatly' => true,
557
			'method' => 'get',
558
			'listenerOn' => false
559
		]);
560
		return $this->_add_event($element, $this->ajaxDeferred($parameters['method'], $url, $responseElement, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"], $parameters["immediatly"], $parameters['listenerOn']);
561
	}
562
563
	/**
564
	 * Performs a get to $url on the click event on $element
565
	 * and display it in $responseElement
566
	 *
567
	 * @param string $element
568
	 *        	the element on which event is observed
569
	 * @param string $url
570
	 *        	The url of the request
571
	 * @param string $responseElement
572
	 *        	The selector of the HTML element displaying the answer
573
	 * @param array $parameters
574
	 *        	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,"before"=>null,"listenerOn"=>false)
575
	 */
576
	public function ajaxOnClick($element, $url, $responseElement = '', $parameters = array()) {
577
		return $this->ajaxOn('click', $element, $url, $responseElement, $parameters);
578
	}
579
580
	/**
581
	 * Performs a get to $url on the click event on $element
582
	 * and display it in $responseElement
583
	 *
584
	 * @param string $element
585
	 *        	the element on which click is observed
586
	 * @param string $url
587
	 *        	The url of the request
588
	 * @param string $responseElement
589
	 *        	The selector of the HTML element displaying the answer
590
	 * @param array $parameters
591
	 *        	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,"before"=>null,"listenerOn"=>false)
592
	 */
593
	public function getOnClick($element, $url, $responseElement = '', $parameters = array()) {
594
		return $this->getOn('click', $element, $url, $responseElement, $parameters);
595
	}
596
597
	/**
598
	 * Uses an hyperlink to make an ajax get request
599
	 *
600
	 * @param string $element
601
	 *        	an hyperlink selector
602
	 * @param string $responseElement
603
	 *        	the target of the ajax request (data-target attribute of the element is used if responseElement is omited)
604
	 * @param array $parameters
605
	 *        	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,"before"=>null,"listenerOn"=>false)
606
	 * @return $this
607
	 */
608
	public function getHref($element, $responseElement = "", $parameters = array()) {
609
		$parameters['attr'] = 'href';
610
		if (JString::isNull($responseElement)) {
611
			$responseElement = '%$(self).attr("data-target")%';
612
		} else {
613
			$responseElement = '%$(self).attr("data-target") || "' . $responseElement . '"%';
614
		}
615
		if (! isset($parameters['historize'])) {
616
			$parameters['historize'] = true;
617
		}
618
		return $this->getOnClick($element, "", $responseElement, $parameters);
619
	}
620
621
	/**
622
	 * Uses an hyperlink to make an ajax get request
623
	 *
624
	 * @param string $element
625
	 *        	an hyperlink selector
626
	 * @param string $responseElement
627
	 *        	the target of the ajax request (data-target attribute of the element is used if responseElement is omited)
628
	 * @param array $parameters
629
	 *        	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,"before"=>null,"listenerOn"=>false)
630
	 * @return $this
631
	 */
632
	public function postHref($element, $responseElement = "", $parameters = array()) {
633
		$parameters['attr'] = 'href';
634
		if (JString::isNull($responseElement)) {
635
			$responseElement = '%$(this).attr("data-target")%';
636
		} else {
637
			$responseElement = '%$(self).attr("data-target") || "' . $responseElement . '"%';
638
		}
639
		if (! isset($parameters['historize'])) {
640
			$parameters['historize'] = true;
641
		}
642
		return $this->postOnClick($element, '', '{}', $responseElement, $parameters);
643
	}
644
645
	/**
646
	 * Uses a form action to make an ajax post request
647
	 *
648
	 * @param string $element
649
	 *        	a form submit selector
650
	 * @param string $formId
651
	 *        	the form identifier
652
	 * @param string $responseElement
653
	 *        	the target of the ajax request (data-target attribute of the element is used if responseElement is omited)
654
	 * @param array $parameters
655
	 *        	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,"before"=>null,"listenerOn"=>false)
656
	 * @return $this
657
	 */
658
	public function postFormAction($element, $formId = "", $responseElement = "", $parameters = array()) {
659
		$parameters['attr'] = 'action';
660
		if (JString::isNull($responseElement)) {
661
			$responseElement = '%$(self).closest("form").attr("data-target")%';
662
		} else {
663
			$responseElement = '%$(self).closest("form").attr("data-target") || "' . $responseElement . '"%';
664
		}
665
		if (JString::isNull($formId)) {
666
			$formId = '%$(self).closest("form").attr("id")%';
667
		} else {
668
			$formId = '%$(self).closest("form").attr("id") || "' . $formId . '"%';
669
		}
670
		if (! isset($parameters['historize'])) {
671
			$parameters['historize'] = true;
672
		}
673
		return $this->postFormOnClick($element, '', $formId, $responseElement, $parameters);
674
	}
675
676
	private function _post($url, $params = '{}', $responseElement = '', $parameters = []) {
677
		$parameters['params'] = $params;
678
		return $this->_ajax('POST', $url, $responseElement, $parameters);
679
	}
680
681
	/**
682
	 * Makes an ajax post
683
	 *
684
	 * @param string $url
685
	 *        	the request url
686
	 * @param string $responseElement
687
	 *        	selector of the HTML element displaying the answer
688
	 * @param string $params
689
	 *        	JSON parameters
690
	 * @param array $parameters
691
	 *        	default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
692
	 */
693
	public function post($url, $params = "{}", $responseElement = "", $parameters = []) {
694
		$parameters['immediatly'] = true;
695
		return $this->_post($url, $params, $responseElement, $parameters);
696
	}
697
698
	/**
699
	 * Prepares a delayed ajax POST
700
	 * to use on an event
701
	 *
702
	 * @param string $url
703
	 *        	the request url
704
	 * @param string $params
705
	 *        	JSON parameters
706
	 * @param string $responseElement
707
	 *        	selector of the HTML element displaying the answer
708
	 * @param array $parameters
709
	 *        	default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
710
	 */
711
	public function postDeferred($url, $params = "{}", $responseElement = "", $parameters = []) {
712
		$parameters['immediatly'] = false;
713
		return $this->_post($url, $params, $responseElement, $parameters);
714
	}
715
716
	/**
717
	 * Performs a post to $url on the event $event fired on $element and pass the parameters $params
718
	 * Display the result in $responseElement
719
	 *
720
	 * @param string $event
721
	 * @param string $element
722
	 * @param string $url
723
	 *        	The url of the request
724
	 * @param string $params
725
	 *        	The parameters to send
726
	 * @param string $responseElement
727
	 *        	selector of the HTML element displaying the answer
728
	 * @param array $parameters
729
	 *        	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,"before"=>null,"listenerOn"=>false)
730
	 */
731
	public function postOn($event, $element, $url, $params = "{}", $responseElement = "", $parameters = array()) {
732
		$parameters['method'] = 'post';
733
		$parameters['params'] = $params;
734
		return $this->ajaxOn($event, $element, $url, $responseElement, $parameters);
735
	}
736
737
	/**
738
	 * Performs a post to $url on the click event fired on $element and pass the parameters $params
739
	 * Display the result in $responseElement
740
	 *
741
	 * @param string $element
742
	 * @param string $url
743
	 *        	The url of the request
744
	 * @param string $params
745
	 *        	The parameters to send
746
	 * @param string $responseElement
747
	 *        	selector of the HTML element displaying the answer
748
	 * @param array $parameters
749
	 *        	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,"before"=>null,"before"=>null,"listenerOn"=>false)
750
	 */
751
	public function postOnClick($element, $url, $params = '{}', $responseElement = '', $parameters = array()) {
752
		return $this->postOn('click', $element, $url, $params, $responseElement, $parameters);
753
	}
754
755
	private function _postForm($url, $form, $responseElement, $parameters = []) {
756
		if (isset($this->params['ajax'])) {
757
			extract($this->params['ajax']);
758
		}
759
		$params = '{}';
760
		$validation = false;
761
		\extract($parameters);
762
		$async = ($async) ? 'true' : 'false';
763
		$jsCallback = isset($jsCallback) ? $jsCallback : "";
764
		$retour = $this->_getAjaxUrl($url, $attr);
765
		$retour .= "\n$('#" . $form . "').trigger('ajaxSubmit');";
766
		if (! isset($contentType) || $contentType != 'false') {
767
			$retour .= "\nvar params=$('#" . $form . "').serialize();\n";
768
			if (isset($params)) {
769
				$retour .= "params+='&'+" . self::_correctParams($params) . ";\n";
770
			}
771
		} else {
772
			$retour .= "\nvar params=new FormData($('#" . $form . "')[0]);\n";
773
		}
774
		$responseElement = $this->_getResponseElement($responseElement);
775
		$retour .= "var self=this;\n";
776
		$before = isset($before) ? $before : "";
777
		$retour .= $before;
778
		if ($hasLoader === true) {
779
			$this->addLoading($retour, $responseElement, $ajaxLoader);
780
		} elseif ($hasLoader === 'response') {
781
			$this->addResponseLoading($retour, $responseElement, $ajaxLoader);
782
		} elseif ($hasLoader === 'internal') {
783
			$retour .= "\n$(this).addClass('loading');";
784
		}
785
		$ajaxParameters = [
786
			"url" => "url",
787
			"method" => "'POST'",
788
			"data" => "params",
789
			"async" => $async
790
		];
791
		if (isset($headers)) {
792
			$ajaxParameters["headers"] = $headers;
793
		}
794
		if (isset($partial)) {
795
			$ajaxParameters["xhr"] = "xhrProvider";
796
			$retour .= "var xhr = $.ajaxSettings.xhr();function xhrProvider() {return xhr;};xhr.onreadystatechange = function (e) { if (3==e.target.readyState){let response=e.target.responseText;" . $partial . ";}; };";
797
		}
798
		$this->createAjaxParameters($ajaxParameters, $parameters);
799
		$retour .= "$.ajax({" . $this->implodeAjaxParameters($ajaxParameters) . "}).done(function( data ) {\n";
800
		$retour .= $this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader) . "});\n";
801
802
		if ($validation) {
0 ignored issues
show
introduced by
The condition $validation is always false.
Loading history...
803
			$retour = "$('#" . $form . "').validate({submitHandler: function(form) {
804
			" . $retour . "
805
			}});\n";
806
			$retour .= "$('#" . $form . "').submit();\n";
807
		}
808
		$retour = $this->_addJsCondition($jsCondition, $retour);
809
		if ($immediatly)
810
			$this->jquery_code_for_compile[] = $retour;
811
		return $retour;
812
	}
813
814
	/**
815
	 * Performs a post form with ajax
816
	 *
817
	 * @param string $url
818
	 *        	The url of the request
819
	 * @param string $form
820
	 *        	The form HTML id
821
	 * @param string $responseElement
822
	 *        	selector of the HTML element displaying the answer
823
	 * @param array $parameters
824
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
825
	 */
826
	public function postForm($url, $form, $responseElement, $parameters = []) {
827
		$parameters['immediatly'] = true;
828
		return $this->_postForm($url, $form, $responseElement, $parameters);
829
	}
830
831
	/**
832
	 * Performs a delayed post form with ajax
833
	 * For use on an event
834
	 *
835
	 * @param string $url
836
	 *        	The url of the request
837
	 * @param string $form
838
	 *        	The form HTML id
839
	 * @param string $responseElement
840
	 *        	selector of the HTML element displaying the answer
841
	 * @param array $parameters
842
	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null)
843
	 */
844
	public function postFormDeferred($url, $form, $responseElement, $parameters = []) {
845
		$parameters['immediatly'] = false;
846
		return $this->_postForm($url, $form, $responseElement, $parameters);
847
	}
848
849
	/**
850
	 * Performs a post form with ajax in response to an event $event on $element
851
	 * display the result in $responseElement
852
	 *
853
	 * @param string $event
854
	 * @param string $element
855
	 * @param string $url
856
	 * @param string $form
857
	 * @param string $responseElement
858
	 *        	selector of the HTML element displaying the answer
859
	 * @param array $parameters
860
	 *        	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,"before"=>null,"listenerOn"=>false)
861
	 */
862
	public function postFormOn($event, $element, $url, $form, $responseElement = "", $parameters = array()) {
863
		$this->setDefaultParameters($parameters, [
864
			'preventDefault' => true,
865
			'stopPropagation' => true,
866
			'immediatly' => true,
867
			'listenerOn' => false
868
		]);
869
		return $this->_add_event($element, $this->postFormDeferred($url, $form, $responseElement, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"], $parameters["immediatly"], $parameters['listenerOn']);
870
	}
871
872
	/**
873
	 * Performs a post form with ajax in response to the click event on $element
874
	 * display the result in $responseElement
875
	 *
876
	 * @param string $element
877
	 * @param string $url
878
	 * @param string $form
879
	 * @param string $responseElement
880
	 *        	selector of the HTML element displaying the answer
881
	 * @param array $parameters
882
	 *        	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,"before"=>null,"listenerOn"=>false)
883
	 */
884
	public function postFormOnClick($element, $url, $form, $responseElement = "", $parameters = array()) {
885
		return $this->postFormOn("click", $element, $url, $form, $responseElement, $parameters);
886
	}
887
888
	public function addCsrf($name = 'csrf-token') {
889
		return "
890
		$.ajaxSetup({
891
			beforeSend: function(xhr, settings) {
892
				let csrfSafeMethod=function(method) { return (/^(GET|HEAD|OPTIONS)$/.test(method));};
893
				if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
894
					xhr.setRequestHeader('{$name}', $('meta[name=\"{$name}\"]').attr('content'));
895
				}
896
			}
897
		});";
898
	}
899
}
900