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