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