Total Complexity | 113 |
Total Lines | 807 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 0 | Features | 1 |
Complex classes like JsUtilsAjaxTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JsUtilsAjaxTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait JsUtilsAjaxTrait { |
||
15 | |||
16 | protected $ajaxTransition; |
||
17 | |||
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); |
||
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 === "internal") { |
||
42 | $retour .= "\n$(this).addClass('loading');"; |
||
43 | } |
||
44 | $ajaxParameters = [ |
||
45 | "url" => "url", |
||
46 | "method" => "'" . \strtoupper($method) . "'" |
||
47 | ]; |
||
48 | |||
49 | $ajaxParameters["async"] = ($async ? "true" : "false"); |
||
50 | |||
51 | if (isset($params)) { |
||
52 | $ajaxParameters["data"] = self::_correctParams($params, $parameters); |
||
53 | } |
||
54 | if (isset($headers)) { |
||
55 | $ajaxParameters["headers"] = $headers; |
||
56 | } |
||
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); |
||
61 | if ($immediatly) |
||
62 | $this->jquery_code_for_compile[] = $retour; |
||
63 | return $retour; |
||
64 | } |
||
65 | |||
66 | protected function createAjaxParameters(&$original, $parameters) { |
||
67 | $validParameters = [ |
||
68 | "contentType" => "%value%", |
||
69 | "dataType" => "'%value%'", |
||
70 | "beforeSend" => "function(jqXHR,settings){%value%}", |
||
71 | "complete" => "function(jqXHR){%value%}", |
||
72 | "processData"=>"%value%" |
||
73 | ]; |
||
74 | foreach ($validParameters as $param => $mask) { |
||
75 | if (isset($parameters[$param])) { |
||
76 | $original[$param] = \str_replace("%value%", $parameters[$param], $mask); |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | |||
81 | protected function implodeAjaxParameters($ajaxParameters) { |
||
82 | $s = ''; |
||
83 | foreach ($ajaxParameters as $k => $v) { |
||
84 | if ($s !== '') { |
||
85 | $s .= ','; |
||
86 | } |
||
87 | if (is_array($v)) { |
||
88 | $s .= "'{$k}':{" . self::implodeAjaxParameters($v) . "}"; |
||
|
|||
89 | } else { |
||
90 | $s .= "'{$k}':{$v}"; |
||
91 | } |
||
92 | } |
||
93 | return $s; |
||
94 | } |
||
95 | |||
96 | protected function _addJsCondition($jsCondition, $jsSource) { |
||
97 | if (isset($jsCondition)) { |
||
98 | return "if(" . $jsCondition . "){\n" . $jsSource . "\n}"; |
||
99 | } |
||
100 | return $jsSource; |
||
101 | } |
||
102 | |||
103 | protected function _getAjaxUrl($url, $attr) { |
||
104 | $url = $this->_correctAjaxUrl($url); |
||
105 | $retour = "url='" . $url . "';"; |
||
106 | $slash = "/"; |
||
107 | if (JString::endswith($url, "/") === true) { |
||
108 | $slash = ""; |
||
109 | } |
||
110 | if (JString::isNotNull($attr)) { |
||
111 | if ($attr === "value") { |
||
112 | $retour .= "url=url+'" . $slash . "'+$(this).val();\n"; |
||
113 | } elseif ($attr === "html") { |
||
114 | $retour .= "url=url+'" . $slash . "'+$(this).html();\n"; |
||
115 | } elseif (\substr($attr, 0, 3) === "js:") { |
||
116 | $retour .= "url=url+'" . $slash . "'+" . \substr($attr, 3) . ";\n"; |
||
117 | } elseif ($attr !== null && $attr !== "") |
||
118 | $retour .= "url=url+'" . $slash . "'+($(this).attr('" . $attr . "')||'');\n"; |
||
119 | } |
||
120 | return $retour; |
||
121 | } |
||
122 | |||
123 | protected function onPopstate() { |
||
124 | return "window.onpopstate = function(e){if(e.state){var target=e.state.jqueryDone;$(e.state.selector)[target](e.state.html);}};"; |
||
125 | } |
||
126 | |||
127 | protected function autoActiveLinks($previousURL = "window.location.href") { |
||
128 | $result = "\nfunction getHref(url) { return \$('a').filter(function(){return \$(this).prop('href') == url; });}"; |
||
129 | $result .= "\nvar myurl={$previousURL};if(window._previousURL) getHref(window._previousURL).removeClass('active');getHref(myurl).addClass('active');window._previousURL=myurl;"; |
||
130 | return $result; |
||
131 | } |
||
132 | |||
133 | protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition, $jsCallback, $hasLoader = false, $history = null) { |
||
134 | $retour = ""; |
||
135 | $call = null; |
||
136 | if (JString::isNotNull($responseElement)) { |
||
137 | if (isset($ajaxTransition)) { |
||
138 | $call = $this->setAjaxDataCall($ajaxTransition); |
||
139 | } elseif (isset($this->ajaxTransition)) { |
||
140 | $call = $this->ajaxTransition; |
||
141 | } |
||
142 | if (\is_callable($call)) |
||
143 | $retour = "\t" . $call($responseElement, $jqueryDone) . ";\n"; |
||
144 | else |
||
145 | $retour = "\t{$responseElement}.{$jqueryDone}( data );\n"; |
||
146 | } |
||
147 | if (isset($history)) { |
||
148 | if ($this->params["autoActiveLinks"]) { |
||
149 | $retour .= $this->autoActiveLinks("url"); |
||
150 | } |
||
151 | $retour .= "\nwindow.history.pushState({'html':data,'selector':" . Javascript::prep_value($history) . ",'jqueryDone':'{$jqueryDone}'},'', url);"; |
||
152 | } |
||
153 | if ($hasLoader === "internal") { |
||
154 | $retour .= "\n$(self).removeClass('loading');"; |
||
155 | } |
||
156 | $retour .= "\t" . $jsCallback . "\n"; |
||
157 | return $retour; |
||
158 | } |
||
159 | |||
160 | protected function _getResponseElement($responseElement) { |
||
161 | if (JString::isNotNull($responseElement)) { |
||
162 | $responseElement = Javascript::prep_jquery_selector($responseElement); |
||
163 | } |
||
164 | return $responseElement; |
||
165 | } |
||
166 | |||
167 | protected function _correctAjaxUrl($url) { |
||
168 | if ($url !== "/" && JString::endsWith($url, "/") === true) |
||
169 | $url = substr($url, 0, strlen($url) - 1); |
||
170 | if (strncmp($url, 'http://', 7) != 0 && strncmp($url, 'https://', 8) != 0) { |
||
171 | $url = $this->getUrl($url); |
||
172 | } |
||
173 | return $url; |
||
174 | } |
||
175 | |||
176 | public static function _correctParams($params, $ajaxParameters = []) { |
||
177 | if (JString::isNull($params)) { |
||
178 | return ""; |
||
179 | } |
||
180 | if (\preg_match("@^\{.*?\}$@", $params)) { |
||
181 | if (! isset($ajaxParameters['contentType']) || ! JString::contains($ajaxParameters['contentType'], 'json')) { |
||
182 | return '$.param(' . $params . ')'; |
||
183 | } else { |
||
184 | return 'JSON.stringify(' . $params . ')'; |
||
185 | } |
||
186 | } |
||
187 | return $params; |
||
188 | } |
||
189 | |||
190 | public static function _implodeParams($parameters) { |
||
191 | $allParameters = []; |
||
192 | foreach ($parameters as $params) { |
||
193 | if (isset($params)) |
||
194 | $allParameters[] = self::_correctParams($params); |
||
195 | } |
||
196 | return \implode("+'&'+", $allParameters); |
||
197 | } |
||
198 | |||
199 | protected function addLoading(&$retour, $responseElement, $ajaxLoader = null) { |
||
200 | if (! isset($ajaxLoader)) { |
||
201 | $ajaxLoader = $this->ajaxLoader; |
||
202 | } |
||
203 | $loading_notifier = '<div class="ajax-loader">' . $ajaxLoader . '</div>'; |
||
204 | $retour .= "{$responseElement}.empty();\n"; |
||
205 | $retour .= "\t\t{$responseElement}.prepend('{$loading_notifier}');\n"; |
||
206 | } |
||
207 | |||
208 | protected function setAjaxDataCall($params) { |
||
209 | $result = null; |
||
210 | if (! \is_callable($params)) { |
||
211 | $result = function ($responseElement, $jqueryDone = "html") use ($params) { |
||
212 | return AjaxTransition::{$params}($responseElement, $jqueryDone); |
||
213 | }; |
||
214 | } |
||
215 | return $result; |
||
216 | } |
||
217 | |||
218 | protected function setDefaultParameters(&$parameters, $default) { |
||
219 | foreach ($default as $k => $v) { |
||
220 | if (! isset($parameters[$k])) |
||
221 | $parameters[$k] = $v; |
||
222 | } |
||
223 | } |
||
224 | |||
225 | public function setAjaxLoader($loader) { |
||
226 | $this->ajaxLoader = $loader; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Performs an ajax GET request |
||
231 | * |
||
232 | * @param string $url |
||
233 | * The url of the request |
||
234 | * @param string $responseElement |
||
235 | * selector of the HTML element displaying the answer |
||
236 | */ |
||
237 | private function _get($url, $responseElement = "", $parameters = []) { |
||
238 | return $this->_ajax("get", $url, $responseElement, $parameters); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Performs an ajax GET request |
||
243 | * |
||
244 | * @param string $url |
||
245 | * The url of the request |
||
246 | * @param string $responseElement |
||
247 | * selector of the HTML element displaying the answer |
||
248 | * @param array $parameters |
||
249 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
250 | */ |
||
251 | public function get($url, $responseElement = "", $parameters = []) { |
||
252 | $parameters["immediatly"] = true; |
||
253 | return $this->_get($url, $responseElement, $parameters); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Performs an ajax request |
||
258 | * |
||
259 | * @param string $method |
||
260 | * The http method (get, post, delete, put, head) |
||
261 | * @param string $url |
||
262 | * The url of the request |
||
263 | * @param string $responseElement |
||
264 | * selector of the HTML element displaying the answer |
||
265 | * @param array $parameters |
||
266 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
267 | */ |
||
268 | public function ajax($method, $url, $responseElement = "", $parameters = []) { |
||
269 | $parameters["immediatly"] = true; |
||
270 | return $this->_ajax($method, $url, $responseElement, $parameters); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Executes an ajax query at regular intervals |
||
275 | * |
||
276 | * @param string $method |
||
277 | * The http method (post, get...) |
||
278 | * @param string $url |
||
279 | * The url of the request |
||
280 | * @param int $interval |
||
281 | * The interval in milliseconds |
||
282 | * @param string $globalName |
||
283 | * The interval name, for clear it |
||
284 | * @param string $responseElement |
||
285 | * @param array $parameters |
||
286 | * The ajax parameters, default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
287 | * @return string |
||
288 | */ |
||
289 | public function ajaxInterval($method, $url, $interval, $globalName = null, $responseElement = "", $parameters = []) { |
||
290 | return $this->interval($this->ajaxDeferred($method, $url, $responseElement, $parameters), $interval, $globalName); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Performs a deferred ajax request |
||
295 | * |
||
296 | * @param string $method |
||
297 | * The http method (get, post, delete, put, head) |
||
298 | * @param string $url |
||
299 | * The url of the request |
||
300 | * @param string $responseElement |
||
301 | * selector of the HTML element displaying the answer |
||
302 | * @param array $parameters |
||
303 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
304 | */ |
||
305 | public function ajaxDeferred($method, $url, $responseElement = "", $parameters = []) { |
||
306 | $parameters["immediatly"] = false; |
||
307 | return $this->_ajax($method, $url, $responseElement, $parameters); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
312 | * |
||
313 | * @param string $url |
||
314 | * the request url |
||
315 | * @param string $method |
||
316 | * Method used |
||
317 | * @param array $parameters |
||
318 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
||
319 | */ |
||
320 | private function _json($url, $method = "get", $parameters = []) { |
||
321 | $parameters = \array_merge($parameters, [ |
||
322 | "hasLoader" => false |
||
323 | ]); |
||
324 | $jsCallback = isset($parameters['jsCallback']) ? $parameters['jsCallback'] : ""; |
||
325 | $context = isset($parameters['context']) ? $parameters['context'] : "document"; |
||
326 | $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"; |
||
327 | $retour .= "\t$(document).trigger('jsonReady',[data]);\n"; |
||
328 | $parameters["jsCallback"] = $retour; |
||
329 | return $this->_ajax($method, $url, null, $parameters); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
334 | * |
||
335 | * @param string $url |
||
336 | * the request url |
||
337 | * @param string $method |
||
338 | * Method used |
||
339 | * @param array $parameters |
||
340 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
||
341 | */ |
||
342 | public function json($url, $method = "get", $parameters = []) { |
||
343 | return $this->_json($url, $method, $parameters); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
348 | * |
||
349 | * @param string $element |
||
350 | * @param string $event |
||
351 | * @param string $url |
||
352 | * the request address |
||
353 | * @param string $method |
||
354 | * default get |
||
355 | * @param array $parameters |
||
356 | * default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
||
357 | */ |
||
358 | public function jsonOn($event, $element, $url, $method = "get", $parameters = array()) { |
||
359 | $this->setDefaultParameters($parameters, [ |
||
360 | "preventDefault" => true, |
||
361 | "stopPropagation" => true, |
||
362 | "immediatly" => true |
||
363 | ]); |
||
364 | return $this->_add_event($element, $this->jsonDeferred($url, $method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"], $parameters["immediatly"]); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name |
||
369 | * |
||
370 | * @param string $url |
||
371 | * the request url |
||
372 | * @param string $method |
||
373 | * Method used |
||
374 | * @param array $parameters |
||
375 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
||
376 | */ |
||
377 | public function jsonDeferred($url, $method = "get", $parameters = []) { |
||
378 | $parameters["immediatly"] = false; |
||
379 | return $this->_json($url, $method, $parameters); |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
384 | * |
||
385 | * @param string $maskSelector |
||
386 | * @param string $url |
||
387 | * the request url |
||
388 | * @param string $method |
||
389 | * Method used, default : get |
||
390 | * @param array $parameters |
||
391 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json") |
||
392 | */ |
||
393 | private function _jsonArray($maskSelector, $url, $method = "get", $parameters = []) { |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
421 | * |
||
422 | * @param string $maskSelector |
||
423 | * @param string $url |
||
424 | * the request url |
||
425 | * @param string $method |
||
426 | * Method used, default : get |
||
427 | * @param array $parameters |
||
428 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json") |
||
429 | */ |
||
430 | public function jsonArray($maskSelector, $url, $method = "get", $parameters = []) { |
||
431 | return $this->_jsonArray($maskSelector, $url, $method, $parameters); |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * 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 |
||
436 | * |
||
437 | * @param string $maskSelector |
||
438 | * @param string $url |
||
439 | * the request url |
||
440 | * @param string $method |
||
441 | * Method used, default : get |
||
442 | * @param array $parameters |
||
443 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"rowClass"=>"_json") |
||
444 | */ |
||
445 | public function jsonArrayDeferred($maskSelector, $url, $method = "get", $parameters) { |
||
446 | $parameters["immediatly"] = false; |
||
447 | return $this->jsonArray($maskSelector, $url, $method, $parameters); |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element |
||
452 | * |
||
453 | * @param string $element |
||
454 | * @param string $event |
||
455 | * @param string $url |
||
456 | * the request url |
||
457 | * @param string $method |
||
458 | * Method used, default : get |
||
459 | * @param array $parameters |
||
460 | * default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true) |
||
461 | */ |
||
462 | public function jsonArrayOn($event, $element, $maskSelector, $url, $method = "get", $parameters = array()) { |
||
463 | $this->setDefaultParameters($parameters, [ |
||
464 | "preventDefault" => true, |
||
465 | "stopPropagation" => true, |
||
466 | "immediatly" => true |
||
467 | ]); |
||
468 | return $this->_add_event($element, $this->jsonArrayDeferred($maskSelector, $url, $method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"], $parameters["immediatly"]); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Prepares a Get ajax request |
||
473 | * for using on an event |
||
474 | * |
||
475 | * @param string $url |
||
476 | * The url of the request |
||
477 | * @param string $responseElement |
||
478 | * selector of the HTML element displaying the answer |
||
479 | * @param array $parameters |
||
480 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
481 | */ |
||
482 | public function getDeferred($url, $responseElement = "", $parameters = []) { |
||
483 | $parameters["immediatly"] = false; |
||
484 | return $this->_get($url, $responseElement, $parameters); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Performs a get to $url on the event $event on $element |
||
489 | * and display it in $responseElement |
||
490 | * |
||
491 | * @param string $event |
||
492 | * the event |
||
493 | * @param string $element |
||
494 | * the element on which event is observed |
||
495 | * @param string $url |
||
496 | * The url of the request |
||
497 | * @param string $responseElement |
||
498 | * The selector of the HTML element displaying the answer |
||
499 | * @param array $parameters |
||
500 | * 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) |
||
501 | */ |
||
502 | public function getOn($event, $element, $url, $responseElement = "", $parameters = array()) { |
||
503 | $this->setDefaultParameters($parameters, [ |
||
504 | "preventDefault" => true, |
||
505 | "stopPropagation" => true, |
||
506 | "immediatly" => true |
||
507 | ]); |
||
508 | return $this->_add_event($element, $this->getDeferred($url, $responseElement, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"], $parameters["immediatly"]); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Performs an ajax request to $url on the event $event on $element |
||
513 | * and display it in $responseElement |
||
514 | * |
||
515 | * @param string $event |
||
516 | * the event observed |
||
517 | * @param string $element |
||
518 | * the element on which event is observed |
||
519 | * @param string $url |
||
520 | * The url of the request |
||
521 | * @param string $responseElement |
||
522 | * The selector of the HTML element displaying the answer |
||
523 | * @param array $parameters |
||
524 | * 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) |
||
525 | */ |
||
526 | public function ajaxOn($event, $element, $url, $responseElement = "", $parameters = array()) { |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * Performs a get to $url on the click event on $element |
||
538 | * and display it in $responseElement |
||
539 | * |
||
540 | * @param string $element |
||
541 | * the element on which event is observed |
||
542 | * @param string $url |
||
543 | * The url of the request |
||
544 | * @param string $responseElement |
||
545 | * The selector of the HTML element displaying the answer |
||
546 | * @param array $parameters |
||
547 | * 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) |
||
548 | */ |
||
549 | public function ajaxOnClick($element, $url, $responseElement = "", $parameters = array()) { |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * Performs a get to $url on the click event on $element |
||
555 | * and display it in $responseElement |
||
556 | * |
||
557 | * @param string $element |
||
558 | * the element on which click is observed |
||
559 | * @param string $url |
||
560 | * The url of the request |
||
561 | * @param string $responseElement |
||
562 | * The selector of the HTML element displaying the answer |
||
563 | * @param array $parameters |
||
564 | * 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) |
||
565 | */ |
||
566 | public function getOnClick($element, $url, $responseElement = "", $parameters = array()) { |
||
567 | return $this->getOn("click", $element, $url, $responseElement, $parameters); |
||
568 | } |
||
569 | |||
570 | /** |
||
571 | * Uses an hyperlink to make an ajax get request |
||
572 | * |
||
573 | * @param string $element |
||
574 | * an hyperlink selector |
||
575 | * @param string $responseElement |
||
576 | * the target of the ajax request (data-target attribute of the element is used if responseElement is omited) |
||
577 | * @param array $parameters |
||
578 | * 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) |
||
579 | * @return $this |
||
580 | */ |
||
581 | public function getHref($element, $responseElement = "", $parameters = array()) { |
||
582 | $parameters["attr"] = "href"; |
||
583 | if (JString::isNull($responseElement)) { |
||
584 | $responseElement = '%$(self).attr("data-target")%'; |
||
585 | } else { |
||
586 | $responseElement = '%$(self).attr("data-target") || "' . $responseElement . '"%'; |
||
587 | } |
||
588 | if (! isset($parameters["historize"])) { |
||
589 | $parameters["historize"] = true; |
||
590 | } |
||
591 | return $this->getOnClick($element, "", $responseElement, $parameters); |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * Uses an hyperlink to make an ajax get request |
||
596 | * |
||
597 | * @param string $element |
||
598 | * an hyperlink selector |
||
599 | * @param string $responseElement |
||
600 | * the target of the ajax request (data-target attribute of the element is used if responseElement is omited) |
||
601 | * @param array $parameters |
||
602 | * 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) |
||
603 | * @return $this |
||
604 | */ |
||
605 | public function postHref($element, $responseElement = "", $parameters = array()) { |
||
606 | $parameters["attr"] = "href"; |
||
607 | if (JString::isNull($responseElement)) { |
||
608 | $responseElement = '%$(this).attr("data-target")%'; |
||
609 | } else { |
||
610 | $responseElement = '%$(self).attr("data-target") || "' . $responseElement . '"%'; |
||
611 | } |
||
612 | if (! isset($parameters["historize"])) { |
||
613 | $parameters["historize"] = true; |
||
614 | } |
||
615 | return $this->postOnClick($element, "", "{}", $responseElement, $parameters); |
||
616 | } |
||
617 | |||
618 | private function _post($url, $params = "{}", $responseElement = "", $parameters = []) { |
||
619 | $parameters["params"] = $params; |
||
620 | return $this->_ajax("POST", $url, $responseElement, $parameters); |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Makes an ajax post |
||
625 | * |
||
626 | * @param string $url |
||
627 | * the request url |
||
628 | * @param string $responseElement |
||
629 | * selector of the HTML element displaying the answer |
||
630 | * @param string $params |
||
631 | * JSON parameters |
||
632 | * @param array $parameters |
||
633 | * default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
634 | */ |
||
635 | public function post($url, $params = "{}", $responseElement = "", $parameters = []) { |
||
636 | return $this->_post($url, $params, $responseElement, $parameters); |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * Prepares a delayed ajax POST |
||
641 | * to use on an event |
||
642 | * |
||
643 | * @param string $url |
||
644 | * the request url |
||
645 | * @param string $params |
||
646 | * JSON parameters |
||
647 | * @param string $responseElement |
||
648 | * selector of the HTML element displaying the answer |
||
649 | * @param array $parameters |
||
650 | * default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
651 | */ |
||
652 | public function postDeferred($url, $params = "{}", $responseElement = "", $parameters = []) { |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * Performs a post to $url on the event $event fired on $element and pass the parameters $params |
||
659 | * Display the result in $responseElement |
||
660 | * |
||
661 | * @param string $event |
||
662 | * @param string $element |
||
663 | * @param string $url |
||
664 | * The url of the request |
||
665 | * @param string $params |
||
666 | * The parameters to send |
||
667 | * @param string $responseElement |
||
668 | * selector of the HTML element displaying the answer |
||
669 | * @param array $parameters |
||
670 | * 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) |
||
671 | */ |
||
672 | public function postOn($event, $element, $url, $params = "{}", $responseElement = "", $parameters = array()) { |
||
673 | $this->setDefaultParameters($parameters, [ |
||
674 | "preventDefault" => true, |
||
675 | "stopPropagation" => true, |
||
676 | "immediatly" => true |
||
677 | ]); |
||
678 | return $this->_add_event($element, $this->postDeferred($url, $params, $responseElement, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"], $parameters["immediatly"]); |
||
679 | } |
||
680 | |||
681 | /** |
||
682 | * Performs a post to $url on the click event fired on $element and pass the parameters $params |
||
683 | * Display the result in $responseElement |
||
684 | * |
||
685 | * @param string $element |
||
686 | * @param string $url |
||
687 | * The url of the request |
||
688 | * @param string $params |
||
689 | * The parameters to send |
||
690 | * @param string $responseElement |
||
691 | * selector of the HTML element displaying the answer |
||
692 | * @param array $parameters |
||
693 | * 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) |
||
694 | */ |
||
695 | public function postOnClick($element, $url, $params = "{}", $responseElement = "", $parameters = array()) { |
||
696 | return $this->postOn("click", $element, $url, $params, $responseElement, $parameters); |
||
697 | } |
||
698 | |||
699 | private function _postForm($url, $form, $responseElement, $parameters = []) { |
||
748 | } |
||
749 | |||
750 | /** |
||
751 | * Performs a post form with ajax |
||
752 | * |
||
753 | * @param string $url |
||
754 | * The url of the request |
||
755 | * @param string $form |
||
756 | * The form HTML id |
||
757 | * @param string $responseElement |
||
758 | * selector of the HTML element displaying the answer |
||
759 | * @param array $parameters |
||
760 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
761 | */ |
||
762 | public function postForm($url, $form, $responseElement, $parameters = []) { |
||
763 | $parameters["immediatly"] = true; |
||
764 | return $this->_postForm($url, $form, $responseElement, $parameters); |
||
765 | } |
||
766 | |||
767 | /** |
||
768 | * Performs a delayed post form with ajax |
||
769 | * For use on an event |
||
770 | * |
||
771 | * @param string $url |
||
772 | * The url of the request |
||
773 | * @param string $form |
||
774 | * The form HTML id |
||
775 | * @param string $responseElement |
||
776 | * selector of the HTML element displaying the answer |
||
777 | * @param array $parameters |
||
778 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
||
779 | */ |
||
780 | public function postFormDeferred($url, $form, $responseElement, $parameters = []) { |
||
781 | $parameters["immediatly"] = false; |
||
782 | return $this->_postForm($url, $form, $responseElement, $parameters); |
||
783 | } |
||
784 | |||
785 | /** |
||
786 | * Performs a post form with ajax in response to an event $event on $element |
||
787 | * display the result in $responseElement |
||
788 | * |
||
789 | * @param string $event |
||
790 | * @param string $element |
||
791 | * @param string $url |
||
792 | * @param string $form |
||
793 | * @param string $responseElement |
||
794 | * selector of the HTML element displaying the answer |
||
795 | * @param array $parameters |
||
796 | * 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) |
||
797 | */ |
||
798 | public function postFormOn($event, $element, $url, $form, $responseElement = "", $parameters = array()) { |
||
805 | } |
||
806 | |||
807 | /** |
||
808 | * Performs a post form with ajax in response to the click event on $element |
||
809 | * display the result in $responseElement |
||
810 | * |
||
811 | * @param string $element |
||
812 | * @param string $url |
||
813 | * @param string $form |
||
814 | * @param string $responseElement |
||
815 | * selector of the HTML element displaying the answer |
||
816 | * @param array $parameters |
||
817 | * 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) |
||
818 | */ |
||
819 | public function postFormOnClick($element, $url, $form, $responseElement = "", $parameters = array()) { |
||
821 | } |
||
822 | } |
||
823 |