Passed
Push — master ( 6f52f8...fa067a )
by Jean-Christophe
01:55
created
Ajax/common/traits/JsUtilsAjaxTrait.php 1 patch
Spacing   +177 added lines, -177 removed lines patch added patch discarded remove patch
@@ -15,56 +15,56 @@  discard block
 block discarded – undo
15 15
 
16 16
 	protected $ajaxTransition;
17 17
 
18
-	protected $ajaxLoader = "<div class=\"ui active centered inline text loader\">Loading</div>";
18
+	protected $ajaxLoader="<div class=\"ui active centered inline text loader\">Loading</div>";
19 19
 
20 20
 	abstract public function getUrl($url);
21 21
 
22
-	abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true);
22
+	abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false, $immediatly=true);
23 23
 
24
-	abstract public function interval($jsCode, $time, $globalName = null, $immediatly = true);
24
+	abstract public function interval($jsCode, $time, $globalName=null, $immediatly=true);
25 25
 
26
-	protected function _ajax($method, $url, $responseElement = "", $parameters = []) {
26
+	protected function _ajax($method, $url, $responseElement="", $parameters=[]) {
27 27
 		if (isset($this->params["ajax"])) {
28 28
 			extract($this->params["ajax"]);
29 29
 		}
30 30
 		extract($parameters);
31 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)) {
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 40
 			$this->addLoading($retour, $responseElement, $ajaxLoader);
41
-		} elseif ($hasLoader === "internal") {
42
-			$retour .= "\n$(this).addClass('loading');";
41
+		} elseif ($hasLoader==="internal") {
42
+			$retour.="\n$(this).addClass('loading');";
43 43
 		}
44
-		$ajaxParameters = [
44
+		$ajaxParameters=[
45 45
 			"url" => "url",
46
-			"method" => "'" . \strtoupper($method) . "'"
46
+			"method" => "'".\strtoupper($method)."'"
47 47
 		];
48 48
 
49
-		$ajaxParameters["async"] = ($async ? "true" : "false");
49
+		$ajaxParameters["async"]=($async ? "true" : "false");
50 50
 
51 51
 		if (isset($params)) {
52
-			$ajaxParameters["data"] = self::_correctParams($params, $parameters);
52
+			$ajaxParameters["data"]=self::_correctParams($params, $parameters);
53 53
 		}
54 54
 		if (isset($headers)) {
55
-			$ajaxParameters["headers"] = $headers;
55
+			$ajaxParameters["headers"]=$headers;
56 56
 		}
57 57
 		$this->createAjaxParameters($ajaxParameters, $parameters);
58
-		$retour .= "$.ajax({" . $this->implodeAjaxParameters($ajaxParameters) . "}).done(function( data, textStatus, jqXHR ) {\n";
59
-		$retour .= $this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader, ($historize ? $originalSelector : null)) . "});\n";
60
-		$retour = $this->_addJsCondition($jsCondition, $retour);
58
+		$retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data, textStatus, jqXHR ) {\n";
59
+		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader, ($historize ? $originalSelector : null))."});\n";
60
+		$retour=$this->_addJsCondition($jsCondition, $retour);
61 61
 		if ($immediatly)
62
-			$this->jquery_code_for_compile[] = $retour;
62
+			$this->jquery_code_for_compile[]=$retour;
63 63
 		return $retour;
64 64
 	}
65 65
 
66 66
 	protected function createAjaxParameters(&$original, $parameters) {
67
-		$validParameters = [
67
+		$validParameters=[
68 68
 			"contentType" => "%value%",
69 69
 			"dataType" => "'%value%'",
70 70
 			"beforeSend" => "function(jqXHR,settings){%value%}",
@@ -72,21 +72,21 @@  discard block
 block discarded – undo
72 72
 		];
73 73
 		foreach ($validParameters as $param => $mask) {
74 74
 			if (isset($parameters[$param])) {
75
-				$original[$param] = \str_replace("%value%", $parameters[$param], $mask);
75
+				$original[$param]=\str_replace("%value%", $parameters[$param], $mask);
76 76
 			}
77 77
 		}
78 78
 	}
79 79
 
80 80
 	protected function implodeAjaxParameters($ajaxParameters) {
81
-		$s = '';
81
+		$s='';
82 82
 		foreach ($ajaxParameters as $k => $v) {
83
-			if ($s !== '') {
84
-				$s .= ',';
83
+			if ($s!=='') {
84
+				$s.=',';
85 85
 			}
86 86
 			if (is_array($v)) {
87
-				$s .= "'{$k}':{" . self::implodeAjaxParameters($v) . "}";
87
+				$s.="'{$k}':{".self::implodeAjaxParameters($v)."}";
88 88
 			} else {
89
-				$s .= "'{$k}':{$v}";
89
+				$s.="'{$k}':{$v}";
90 90
 			}
91 91
 		}
92 92
 		return $s;
@@ -94,27 +94,27 @@  discard block
 block discarded – undo
94 94
 
95 95
 	protected function _addJsCondition($jsCondition, $jsSource) {
96 96
 		if (isset($jsCondition)) {
97
-			return "if(" . $jsCondition . "){\n" . $jsSource . "\n}";
97
+			return "if(".$jsCondition."){\n".$jsSource."\n}";
98 98
 		}
99 99
 		return $jsSource;
100 100
 	}
101 101
 
102 102
 	protected function _getAjaxUrl($url, $attr) {
103
-		$url = $this->_correctAjaxUrl($url);
104
-		$retour = "url='" . $url . "';";
105
-		$slash = "/";
106
-		if (JString::endswith($url, "/") === true) {
107
-			$slash = "";
103
+		$url=$this->_correctAjaxUrl($url);
104
+		$retour="url='".$url."';";
105
+		$slash="/";
106
+		if (JString::endswith($url, "/")===true) {
107
+			$slash="";
108 108
 		}
109 109
 		if (JString::isNotNull($attr)) {
110
-			if ($attr === "value") {
111
-				$retour .= "url=url+'" . $slash . "'+$(this).val();\n";
112
-			} elseif ($attr === "html") {
113
-				$retour .= "url=url+'" . $slash . "'+$(this).html();\n";
114
-			} elseif (\substr($attr, 0, 3) === "js:") {
115
-				$retour .= "url=url+'" . $slash . "'+" . \substr($attr, 3) . ";\n";
116
-			} elseif ($attr !== null && $attr !== "")
117
-				$retour .= "url=url+'" . $slash . "'+($(this).attr('" . $attr . "')||'');\n";
110
+			if ($attr==="value") {
111
+				$retour.="url=url+'".$slash."'+$(this).val();\n";
112
+			} elseif ($attr==="html") {
113
+				$retour.="url=url+'".$slash."'+$(this).html();\n";
114
+			} elseif (\substr($attr, 0, 3)==="js:") {
115
+				$retour.="url=url+'".$slash."'+".\substr($attr, 3).";\n";
116
+			} elseif ($attr!==null && $attr!=="")
117
+				$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n";
118 118
 		}
119 119
 		return $retour;
120 120
 	}
@@ -123,91 +123,91 @@  discard block
 block discarded – undo
123 123
 		return "window.onpopstate = function(e){if(e.state){var target=e.state.jqueryDone;$(e.state.selector)[target](e.state.html);}};";
124 124
 	}
125 125
 
126
-	protected function autoActiveLinks($previousURL = "window.location.href") {
127
-		$result = "\nfunction getHref(url) { return \$('a').filter(function(){return \$(this).prop('href') == url; });}";
128
-		$result .= "\nvar myurl={$previousURL};if(window._previousURL) getHref(window._previousURL).removeClass('active');getHref(myurl).addClass('active');window._previousURL=myurl;";
126
+	protected function autoActiveLinks($previousURL="window.location.href") {
127
+		$result="\nfunction getHref(url) { return \$('a').filter(function(){return \$(this).prop('href') == url; });}";
128
+		$result.="\nvar myurl={$previousURL};if(window._previousURL) getHref(window._previousURL).removeClass('active');getHref(myurl).addClass('active');window._previousURL=myurl;";
129 129
 		return $result;
130 130
 	}
131 131
 
132
-	protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader = false, $history = null) {
133
-		$retour = "";
134
-		$call = null;
132
+	protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader=false, $history=null) {
133
+		$retour="";
134
+		$call=null;
135 135
 		if (JString::isNotNull($responseElement)) {
136 136
 			if (isset($ajaxTransition)) {
137
-				$call = $this->setAjaxDataCall($ajaxTransition);
137
+				$call=$this->setAjaxDataCall($ajaxTransition);
138 138
 			} elseif (isset($this->ajaxTransition)) {
139
-				$call = $this->ajaxTransition;
139
+				$call=$this->ajaxTransition;
140 140
 			}
141 141
 			if (\is_callable($call))
142
-				$retour = "\t" . $call($responseElement, $jqueryDone) . ";\n";
142
+				$retour="\t".$call($responseElement, $jqueryDone).";\n";
143 143
 			else
144
-				$retour = "\t{$responseElement}.{$jqueryDone}( data );\n";
144
+				$retour="\t{$responseElement}.{$jqueryDone}( data );\n";
145 145
 		}
146 146
 		if (isset($history)) {
147 147
 			if ($this->params["autoActiveLinks"]) {
148
-				$retour .= $this->autoActiveLinks("url");
148
+				$retour.=$this->autoActiveLinks("url");
149 149
 			}
150
-			$retour .= "\nwindow.history.pushState({'html':data,'selector':" . Javascript::prep_value($history) . ",'jqueryDone':'{$jqueryDone}'},'', url);";
150
+			$retour.="\nwindow.history.pushState({'html':data,'selector':".Javascript::prep_value($history).",'jqueryDone':'{$jqueryDone}'},'', url);";
151 151
 		}
152
-		if ($hasLoader === "internal") {
153
-			$retour .= "\n$(self).removeClass('loading');";
152
+		if ($hasLoader==="internal") {
153
+			$retour.="\n$(self).removeClass('loading');";
154 154
 		}
155
-		$retour .= "\t" . $jsCallback . "\n";
155
+		$retour.="\t".$jsCallback."\n";
156 156
 		return $retour;
157 157
 	}
158 158
 
159 159
 	protected function _getResponseElement($responseElement) {
160 160
 		if (JString::isNotNull($responseElement)) {
161
-			$responseElement = Javascript::prep_jquery_selector($responseElement);
161
+			$responseElement=Javascript::prep_jquery_selector($responseElement);
162 162
 		}
163 163
 		return $responseElement;
164 164
 	}
165 165
 
166 166
 	protected function _correctAjaxUrl($url) {
167
-		if ($url !== "/" && JString::endsWith($url, "/") === true)
168
-			$url = substr($url, 0, strlen($url) - 1);
169
-		if (strncmp($url, 'http://', 7) != 0 && strncmp($url, 'https://', 8) != 0) {
170
-			$url = $this->getUrl($url);
167
+		if ($url!=="/" && JString::endsWith($url, "/")===true)
168
+			$url=substr($url, 0, strlen($url)-1);
169
+		if (strncmp($url, 'http://', 7)!=0 && strncmp($url, 'https://', 8)!=0) {
170
+			$url=$this->getUrl($url);
171 171
 		}
172 172
 		return $url;
173 173
 	}
174 174
 
175
-	public static function _correctParams($params, $ajaxParameters = []) {
175
+	public static function _correctParams($params, $ajaxParameters=[]) {
176 176
 		if (JString::isNull($params)) {
177 177
 			return "";
178 178
 		}
179 179
 		if (\preg_match("@^\{.*?\}$@", $params)) {
180
-			if (! isset($ajaxParameters['contentType']) || ! JString::contains($ajaxParameters['contentType'], 'json')) {
181
-				return '$.param(' . $params . ')';
180
+			if (!isset($ajaxParameters['contentType']) || !JString::contains($ajaxParameters['contentType'], 'json')) {
181
+				return '$.param('.$params.')';
182 182
 			} else {
183
-				return 'JSON.stringify(' . $params . ')';
183
+				return 'JSON.stringify('.$params.')';
184 184
 			}
185 185
 		}
186 186
 		return $params;
187 187
 	}
188 188
 
189 189
 	public static function _implodeParams($parameters) {
190
-		$allParameters = [];
190
+		$allParameters=[];
191 191
 		foreach ($parameters as $params) {
192 192
 			if (isset($params))
193
-				$allParameters[] = self::_correctParams($params);
193
+				$allParameters[]=self::_correctParams($params);
194 194
 		}
195 195
 		return \implode("+'&'+", $allParameters);
196 196
 	}
197 197
 
198
-	protected function addLoading(&$retour, $responseElement, $ajaxLoader = null) {
199
-		if (! isset($ajaxLoader)) {
200
-			$ajaxLoader = $this->ajaxLoader;
198
+	protected function addLoading(&$retour, $responseElement, $ajaxLoader=null) {
199
+		if (!isset($ajaxLoader)) {
200
+			$ajaxLoader=$this->ajaxLoader;
201 201
 		}
202
-		$loading_notifier = '<div class="ajax-loader">' . $ajaxLoader . '</div>';
203
-		$retour .= "{$responseElement}.empty();\n";
204
-		$retour .= "\t\t{$responseElement}.prepend('{$loading_notifier}');\n";
202
+		$loading_notifier='<div class="ajax-loader">'.$ajaxLoader.'</div>';
203
+		$retour.="{$responseElement}.empty();\n";
204
+		$retour.="\t\t{$responseElement}.prepend('{$loading_notifier}');\n";
205 205
 	}
206 206
 
207 207
 	protected function setAjaxDataCall($params) {
208
-		$result = null;
209
-		if (! \is_callable($params)) {
210
-			$result = function ($responseElement, $jqueryDone = "html") use ($params) {
208
+		$result=null;
209
+		if (!\is_callable($params)) {
210
+			$result=function($responseElement, $jqueryDone="html") use ($params) {
211 211
 				return AjaxTransition::{$params}($responseElement, $jqueryDone);
212 212
 			};
213 213
 		}
@@ -216,13 +216,13 @@  discard block
 block discarded – undo
216 216
 
217 217
 	protected function setDefaultParameters(&$parameters, $default) {
218 218
 		foreach ($default as $k => $v) {
219
-			if (! isset($parameters[$k]))
220
-				$parameters[$k] = $v;
219
+			if (!isset($parameters[$k]))
220
+				$parameters[$k]=$v;
221 221
 		}
222 222
 	}
223 223
 
224 224
 	public function setAjaxLoader($loader) {
225
-		$this->ajaxLoader = $loader;
225
+		$this->ajaxLoader=$loader;
226 226
 	}
227 227
 
228 228
 	/**
@@ -233,7 +233,7 @@  discard block
 block discarded – undo
233 233
 	 * @param string $responseElement
234 234
 	 *        	selector of the HTML element displaying the answer
235 235
 	 */
236
-	private function _get($url, $responseElement = "", $parameters = []) {
236
+	private function _get($url, $responseElement="", $parameters=[]) {
237 237
 		return $this->_ajax("get", $url, $responseElement, $parameters);
238 238
 	}
239 239
 
@@ -247,8 +247,8 @@  discard block
 block discarded – undo
247 247
 	 * @param array $parameters
248 248
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
249 249
 	 */
250
-	public function get($url, $responseElement = "", $parameters = []) {
251
-		$parameters["immediatly"] = true;
250
+	public function get($url, $responseElement="", $parameters=[]) {
251
+		$parameters["immediatly"]=true;
252 252
 		return $this->_get($url, $responseElement, $parameters);
253 253
 	}
254 254
 
@@ -264,8 +264,8 @@  discard block
 block discarded – undo
264 264
 	 * @param array $parameters
265 265
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
266 266
 	 */
267
-	public function ajax($method, $url, $responseElement = "", $parameters = []) {
268
-		$parameters["immediatly"] = true;
267
+	public function ajax($method, $url, $responseElement="", $parameters=[]) {
268
+		$parameters["immediatly"]=true;
269 269
 		return $this->_ajax($method, $url, $responseElement, $parameters);
270 270
 	}
271 271
 
@@ -285,7 +285,7 @@  discard block
 block discarded – undo
285 285
 	 *        	The ajax parameters, default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
286 286
 	 * @return string
287 287
 	 */
288
-	public function ajaxInterval($method, $url, $interval, $globalName = null, $responseElement = "", $parameters = []) {
288
+	public function ajaxInterval($method, $url, $interval, $globalName=null, $responseElement="", $parameters=[]) {
289 289
 		return $this->interval($this->ajaxDeferred($method, $url, $responseElement, $parameters), $interval, $globalName);
290 290
 	}
291 291
 
@@ -301,8 +301,8 @@  discard block
 block discarded – undo
301 301
 	 * @param array $parameters
302 302
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
303 303
 	 */
304
-	public function ajaxDeferred($method, $url, $responseElement = "", $parameters = []) {
305
-		$parameters["immediatly"] = false;
304
+	public function ajaxDeferred($method, $url, $responseElement="", $parameters=[]) {
305
+		$parameters["immediatly"]=false;
306 306
 		return $this->_ajax($method, $url, $responseElement, $parameters);
307 307
 	}
308 308
 
@@ -316,15 +316,15 @@  discard block
 block discarded – undo
316 316
 	 * @param array $parameters
317 317
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false)
318 318
 	 */
319
-	private function _json($url, $method = "get", $parameters = []) {
320
-		$parameters = \array_merge($parameters, [
319
+	private function _json($url, $method="get", $parameters=[]) {
320
+		$parameters=\array_merge($parameters, [
321 321
 			"hasLoader" => false
322 322
 		]);
323
-		$jsCallback = isset($parameters['jsCallback']) ? $parameters['jsCallback'] : "";
324
-		$context = isset($parameters['context']) ? $parameters['context'] : "document";
325
-		$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";
326
-		$retour .= "\t$(document).trigger('jsonReady',[data]);\n";
327
-		$parameters["jsCallback"] = $retour;
323
+		$jsCallback=isset($parameters['jsCallback']) ? $parameters['jsCallback'] : "";
324
+		$context=isset($parameters['context']) ? $parameters['context'] : "document";
325
+		$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";
326
+		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
327
+		$parameters["jsCallback"]=$retour;
328 328
 		return $this->_ajax($method, $url, null, $parameters);
329 329
 	}
330 330
 
@@ -338,7 +338,7 @@  discard block
 block discarded – undo
338 338
 	 * @param array $parameters
339 339
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false)
340 340
 	 */
341
-	public function json($url, $method = "get", $parameters = []) {
341
+	public function json($url, $method="get", $parameters=[]) {
342 342
 		return $this->_json($url, $method, $parameters);
343 343
 	}
344 344
 
@@ -354,7 +354,7 @@  discard block
 block discarded – undo
354 354
 	 * @param array $parameters
355 355
 	 *        	default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true)
356 356
 	 */
357
-	public function jsonOn($event, $element, $url, $method = "get", $parameters = array()) {
357
+	public function jsonOn($event, $element, $url, $method="get", $parameters=array()) {
358 358
 		$this->setDefaultParameters($parameters, [
359 359
 			"preventDefault" => true,
360 360
 			"stopPropagation" => true,
@@ -373,8 +373,8 @@  discard block
 block discarded – undo
373 373
 	 * @param array $parameters
374 374
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false)
375 375
 	 */
376
-	public function jsonDeferred($url, $method = "get", $parameters = []) {
377
-		$parameters["immediatly"] = false;
376
+	public function jsonDeferred($url, $method="get", $parameters=[]) {
377
+		$parameters["immediatly"]=false;
378 378
 		return $this->_json($url, $method, $parameters);
379 379
 	}
380 380
 
@@ -389,29 +389,29 @@  discard block
 block discarded – undo
389 389
 	 * @param array $parameters
390 390
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json")
391 391
 	 */
392
-	private function _jsonArray($maskSelector, $url, $method = "get", $parameters = []) {
393
-		$parameters = \array_merge($parameters, [
392
+	private function _jsonArray($maskSelector, $url, $method="get", $parameters=[]) {
393
+		$parameters=\array_merge($parameters, [
394 394
 			"hasLoader" => false
395 395
 		]);
396
-		$rowClass = isset($parameters['rowClass']) ? $parameters['rowClass'] : "_json";
397
-		$jsCallback = isset($parameters['jsCallback']) ? $parameters['jsCallback'] : "";
398
-		$context = isset($parameters['context']) ? $parameters['context'] : null;
399
-		if ($context === null) {
400
-			$parent = "$('" . $maskSelector . "').parent()";
401
-			$newElm = "$('#'+newId)";
396
+		$rowClass=isset($parameters['rowClass']) ? $parameters['rowClass'] : "_json";
397
+		$jsCallback=isset($parameters['jsCallback']) ? $parameters['jsCallback'] : "";
398
+		$context=isset($parameters['context']) ? $parameters['context'] : null;
399
+		if ($context===null) {
400
+			$parent="$('".$maskSelector."').parent()";
401
+			$newElm="$('#'+newId)";
402 402
 		} else {
403
-			$parent = $context;
404
-			$newElm = $context . ".find('#'+newId)";
403
+			$parent=$context;
404
+			$newElm=$context.".find('#'+newId)";
405 405
 		}
406
-		$appendTo = "\t\tnewElm.appendTo(" . $parent . ");\n";
407
-		$retour = $parent . ".find('.{$rowClass}').remove();";
408
-		$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();
406
+		$appendTo="\t\tnewElm.appendTo(".$parent.");\n";
407
+		$retour=$parent.".find('.{$rowClass}').remove();";
408
+		$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();
409 409
 		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";
410
-		$retour .= $appendTo;
411
-		$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";
412
-		$retour .= "\t$(document).trigger('jsonReady',[data]);\n";
413
-		$retour .= "\t" . $jsCallback;
414
-		$parameters["jsCallback"] = $retour;
410
+		$retour.=$appendTo;
411
+		$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";
412
+		$retour.="\t$(document).trigger('jsonReady',[data]);\n";
413
+		$retour.="\t".$jsCallback;
414
+		$parameters["jsCallback"]=$retour;
415 415
 		return $this->_ajax($method, $url, null, $parameters);
416 416
 	}
417 417
 
@@ -426,7 +426,7 @@  discard block
 block discarded – undo
426 426
 	 * @param array $parameters
427 427
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json")
428 428
 	 */
429
-	public function jsonArray($maskSelector, $url, $method = "get", $parameters = []) {
429
+	public function jsonArray($maskSelector, $url, $method="get", $parameters=[]) {
430 430
 		return $this->_jsonArray($maskSelector, $url, $method, $parameters);
431 431
 	}
432 432
 
@@ -441,8 +441,8 @@  discard block
 block discarded – undo
441 441
 	 * @param array $parameters
442 442
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"rowClass"=>"_json")
443 443
 	 */
444
-	public function jsonArrayDeferred($maskSelector, $url, $method = "get", $parameters) {
445
-		$parameters["immediatly"] = false;
444
+	public function jsonArrayDeferred($maskSelector, $url, $method="get", $parameters) {
445
+		$parameters["immediatly"]=false;
446 446
 		return $this->jsonArray($maskSelector, $url, $method, $parameters);
447 447
 	}
448 448
 
@@ -458,7 +458,7 @@  discard block
 block discarded – undo
458 458
 	 * @param array $parameters
459 459
 	 *        	default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true)
460 460
 	 */
461
-	public function jsonArrayOn($event, $element, $maskSelector, $url, $method = "get", $parameters = array()) {
461
+	public function jsonArrayOn($event, $element, $maskSelector, $url, $method="get", $parameters=array()) {
462 462
 		$this->setDefaultParameters($parameters, [
463 463
 			"preventDefault" => true,
464 464
 			"stopPropagation" => true,
@@ -478,8 +478,8 @@  discard block
 block discarded – undo
478 478
 	 * @param array $parameters
479 479
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
480 480
 	 */
481
-	public function getDeferred($url, $responseElement = "", $parameters = []) {
482
-		$parameters["immediatly"] = false;
481
+	public function getDeferred($url, $responseElement="", $parameters=[]) {
482
+		$parameters["immediatly"]=false;
483 483
 		return $this->_get($url, $responseElement, $parameters);
484 484
 	}
485 485
 
@@ -498,7 +498,7 @@  discard block
 block discarded – undo
498 498
 	 * @param array $parameters
499 499
 	 *        	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)
500 500
 	 */
501
-	public function getOn($event, $element, $url, $responseElement = "", $parameters = array()) {
501
+	public function getOn($event, $element, $url, $responseElement="", $parameters=array()) {
502 502
 		$this->setDefaultParameters($parameters, [
503 503
 			"preventDefault" => true,
504 504
 			"stopPropagation" => true,
@@ -522,7 +522,7 @@  discard block
 block discarded – undo
522 522
 	 * @param array $parameters
523 523
 	 *        	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)
524 524
 	 */
525
-	public function ajaxOn($event, $element, $url, $responseElement = "", $parameters = array()) {
525
+	public function ajaxOn($event, $element, $url, $responseElement="", $parameters=array()) {
526 526
 		$this->setDefaultParameters($parameters, [
527 527
 			"preventDefault" => true,
528 528
 			"stopPropagation" => true,
@@ -545,7 +545,7 @@  discard block
 block discarded – undo
545 545
 	 * @param array $parameters
546 546
 	 *        	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)
547 547
 	 */
548
-	public function ajaxOnClick($element, $url, $responseElement = "", $parameters = array()) {
548
+	public function ajaxOnClick($element, $url, $responseElement="", $parameters=array()) {
549 549
 		return $this->ajaxOn("click", $element, $url, $responseElement, $parameters);
550 550
 	}
551 551
 
@@ -562,7 +562,7 @@  discard block
 block discarded – undo
562 562
 	 * @param array $parameters
563 563
 	 *        	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)
564 564
 	 */
565
-	public function getOnClick($element, $url, $responseElement = "", $parameters = array()) {
565
+	public function getOnClick($element, $url, $responseElement="", $parameters=array()) {
566 566
 		return $this->getOn("click", $element, $url, $responseElement, $parameters);
567 567
 	}
568 568
 
@@ -577,15 +577,15 @@  discard block
 block discarded – undo
577 577
 	 *        	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)
578 578
 	 * @return $this
579 579
 	 */
580
-	public function getHref($element, $responseElement = "", $parameters = array()) {
581
-		$parameters["attr"] = "href";
580
+	public function getHref($element, $responseElement="", $parameters=array()) {
581
+		$parameters["attr"]="href";
582 582
 		if (JString::isNull($responseElement)) {
583
-			$responseElement = '%$(self).attr("data-target")%';
583
+			$responseElement='%$(self).attr("data-target")%';
584 584
 		} else {
585
-			$responseElement = '%$(self).attr("data-target") || "' . $responseElement . '"%';
585
+			$responseElement='%$(self).attr("data-target") || "'.$responseElement.'"%';
586 586
 		}
587
-		if (! isset($parameters["historize"])) {
588
-			$parameters["historize"] = true;
587
+		if (!isset($parameters["historize"])) {
588
+			$parameters["historize"]=true;
589 589
 		}
590 590
 		return $this->getOnClick($element, "", $responseElement, $parameters);
591 591
 	}
@@ -601,21 +601,21 @@  discard block
 block discarded – undo
601 601
 	 *        	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)
602 602
 	 * @return $this
603 603
 	 */
604
-	public function postHref($element, $responseElement = "", $parameters = array()) {
605
-		$parameters["attr"] = "href";
604
+	public function postHref($element, $responseElement="", $parameters=array()) {
605
+		$parameters["attr"]="href";
606 606
 		if (JString::isNull($responseElement)) {
607
-			$responseElement = '%$(this).attr("data-target")%';
607
+			$responseElement='%$(this).attr("data-target")%';
608 608
 		} else {
609
-			$responseElement = '%$(self).attr("data-target") || "' . $responseElement . '"%';
609
+			$responseElement='%$(self).attr("data-target") || "'.$responseElement.'"%';
610 610
 		}
611
-		if (! isset($parameters["historize"])) {
612
-			$parameters["historize"] = true;
611
+		if (!isset($parameters["historize"])) {
612
+			$parameters["historize"]=true;
613 613
 		}
614 614
 		return $this->postOnClick($element, "", "{}", $responseElement, $parameters);
615 615
 	}
616 616
 
617
-	private function _post($url, $params = "{}", $responseElement = "", $parameters = []) {
618
-		$parameters["params"] = $params;
617
+	private function _post($url, $params="{}", $responseElement="", $parameters=[]) {
618
+		$parameters["params"]=$params;
619 619
 		return $this->_ajax("POST", $url, $responseElement, $parameters);
620 620
 	}
621 621
 
@@ -631,7 +631,7 @@  discard block
 block discarded – undo
631 631
 	 * @param array $parameters
632 632
 	 *        	default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
633 633
 	 */
634
-	public function post($url, $params = "{}", $responseElement = "", $parameters = []) {
634
+	public function post($url, $params="{}", $responseElement="", $parameters=[]) {
635 635
 		return $this->_post($url, $params, $responseElement, $parameters);
636 636
 	}
637 637
 
@@ -648,8 +648,8 @@  discard block
 block discarded – undo
648 648
 	 * @param array $parameters
649 649
 	 *        	default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
650 650
 	 */
651
-	public function postDeferred($url, $params = "{}", $responseElement = "", $parameters = []) {
652
-		$parameters["immediatly"] = false;
651
+	public function postDeferred($url, $params="{}", $responseElement="", $parameters=[]) {
652
+		$parameters["immediatly"]=false;
653 653
 		return $this->_post($url, $params, $responseElement, $parameters);
654 654
 	}
655 655
 
@@ -668,7 +668,7 @@  discard block
 block discarded – undo
668 668
 	 * @param array $parameters
669 669
 	 *        	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)
670 670
 	 */
671
-	public function postOn($event, $element, $url, $params = "{}", $responseElement = "", $parameters = array()) {
671
+	public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) {
672 672
 		$this->setDefaultParameters($parameters, [
673 673
 			"preventDefault" => true,
674 674
 			"stopPropagation" => true,
@@ -691,54 +691,54 @@  discard block
 block discarded – undo
691 691
 	 * @param array $parameters
692 692
 	 *        	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)
693 693
 	 */
694
-	public function postOnClick($element, $url, $params = "{}", $responseElement = "", $parameters = array()) {
694
+	public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) {
695 695
 		return $this->postOn("click", $element, $url, $params, $responseElement, $parameters);
696 696
 	}
697 697
 
698
-	private function _postForm($url, $form, $responseElement, $parameters = []) {
698
+	private function _postForm($url, $form, $responseElement, $parameters=[]) {
699 699
 		if (isset($this->params["ajax"])) {
700 700
 			extract($this->params["ajax"]);
701 701
 		}
702
-		$params = "{}";
703
-		$validation = false;
702
+		$params="{}";
703
+		$validation=false;
704 704
 		\extract($parameters);
705
-		$async = ($async) ? "true" : "false";
706
-		$jsCallback = isset($jsCallback) ? $jsCallback : "";
707
-		$retour = $this->_getAjaxUrl($url, $attr);
708
-		$retour .= "\n$('#" . $form . "').trigger('ajaxSubmit');";
709
-		$retour .= "\nvar params=$('#" . $form . "').serialize();\n";
705
+		$async=($async) ? "true" : "false";
706
+		$jsCallback=isset($jsCallback) ? $jsCallback : "";
707
+		$retour=$this->_getAjaxUrl($url, $attr);
708
+		$retour.="\n$('#".$form."').trigger('ajaxSubmit');";
709
+		$retour.="\nvar params=$('#".$form."').serialize();\n";
710 710
 		if (isset($params)) {
711
-			$retour .= "params+='&'+" . self::_correctParams($params) . ";\n";
711
+			$retour.="params+='&'+".self::_correctParams($params).";\n";
712 712
 		}
713
-		$responseElement = $this->_getResponseElement($responseElement);
714
-		$retour .= "var self=this;\n";
715
-		if ($hasLoader === true) {
713
+		$responseElement=$this->_getResponseElement($responseElement);
714
+		$retour.="var self=this;\n";
715
+		if ($hasLoader===true) {
716 716
 			$this->addLoading($retour, $responseElement, $ajaxLoader);
717
-		} elseif ($hasLoader === "internal") {
718
-			$retour .= "\n$(this).addClass('loading');";
717
+		} elseif ($hasLoader==="internal") {
718
+			$retour.="\n$(this).addClass('loading');";
719 719
 		}
720
-		$ajaxParameters = [
720
+		$ajaxParameters=[
721 721
 			"url" => "url",
722 722
 			"method" => "'POST'",
723 723
 			"data" => "params",
724 724
 			"async" => $async
725 725
 		];
726 726
 		if (isset($headers)) {
727
-			$ajaxParameters["headers"] = $headers;
727
+			$ajaxParameters["headers"]=$headers;
728 728
 		}
729 729
 		$this->createAjaxParameters($ajaxParameters, $parameters);
730
-		$retour .= "$.ajax({" . $this->implodeAjaxParameters($ajaxParameters) . "}).done(function( data ) {\n";
731
-		$retour .= $this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader) . "});\n";
730
+		$retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data ) {\n";
731
+		$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader)."});\n";
732 732
 
733 733
 		if ($validation) {
734
-			$retour = "$('#" . $form . "').validate({submitHandler: function(form) {
735
-			" . $retour . "
734
+			$retour="$('#".$form."').validate({submitHandler: function(form) {
735
+			" . $retour."
736 736
 			}});\n";
737
-			$retour .= "$('#" . $form . "').submit();\n";
737
+			$retour.="$('#".$form."').submit();\n";
738 738
 		}
739
-		$retour = $this->_addJsCondition($jsCondition, $retour);
739
+		$retour=$this->_addJsCondition($jsCondition, $retour);
740 740
 		if ($immediatly)
741
-			$this->jquery_code_for_compile[] = $retour;
741
+			$this->jquery_code_for_compile[]=$retour;
742 742
 		return $retour;
743 743
 	}
744 744
 
@@ -754,8 +754,8 @@  discard block
 block discarded – undo
754 754
 	 * @param array $parameters
755 755
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
756 756
 	 */
757
-	public function postForm($url, $form, $responseElement, $parameters = []) {
758
-		$parameters["immediatly"] = true;
757
+	public function postForm($url, $form, $responseElement, $parameters=[]) {
758
+		$parameters["immediatly"]=true;
759 759
 		return $this->_postForm($url, $form, $responseElement, $parameters);
760 760
 	}
761 761
 
@@ -772,8 +772,8 @@  discard block
 block discarded – undo
772 772
 	 * @param array $parameters
773 773
 	 *        	default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false)
774 774
 	 */
775
-	public function postFormDeferred($url, $form, $responseElement, $parameters = []) {
776
-		$parameters["immediatly"] = false;
775
+	public function postFormDeferred($url, $form, $responseElement, $parameters=[]) {
776
+		$parameters["immediatly"]=false;
777 777
 		return $this->_postForm($url, $form, $responseElement, $parameters);
778 778
 	}
779 779
 
@@ -790,7 +790,7 @@  discard block
 block discarded – undo
790 790
 	 * @param array $parameters
791 791
 	 *        	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)
792 792
 	 */
793
-	public function postFormOn($event, $element, $url, $form, $responseElement = "", $parameters = array()) {
793
+	public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) {
794 794
 		$this->setDefaultParameters($parameters, [
795 795
 			"preventDefault" => true,
796 796
 			"stopPropagation" => true,
@@ -811,7 +811,7 @@  discard block
 block discarded – undo
811 811
 	 * @param array $parameters
812 812
 	 *        	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)
813 813
 	 */
814
-	public function postFormOnClick($element, $url, $form, $responseElement = "", $parameters = array()) {
814
+	public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) {
815 815
 		return $this->postFormOn("click", $element, $url, $form, $responseElement, $parameters);
816 816
 	}
817 817
 }
Please login to merge, or discard this patch.