Total Complexity | 123 |
Total Lines | 820 |
Duplicated Lines | 0 % |
Changes | 23 | ||
Bugs | 0 | Features | 4 |
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 |
||
15 | trait JsUtilsAjaxTrait { |
||
16 | protected $ajaxTransition; |
||
17 | protected $ajaxLoader = "<div class=\"ui active centered inline text loader\">Loading</div>"; |
||
18 | abstract public function getUrl($url); |
||
19 | abstract public function _add_event($element, $js, $event, $preventDefault = false, $stopPropagation = false, $immediatly = true, $listenerOn=false); |
||
20 | abstract public function interval($jsCode, $time, $globalName = null, $immediatly = true); |
||
21 | |||
22 | protected function _ajax($method, $url, $responseElement = '', $parameters = [ ]) { |
||
23 | if (isset ( $this->params ['ajax'] )) { |
||
24 | extract ( $this->params ['ajax'] ); |
||
25 | } |
||
26 | extract ( $parameters ); |
||
27 | |||
28 | $jsCallback = isset ( $jsCallback ) ? $jsCallback : ''; |
||
29 | $retour = $this->_getAjaxUrl ( $url, $attr ); |
||
30 | $originalSelector = $responseElement; |
||
31 | $responseElement = $this->_getResponseElement ( $responseElement ); |
||
32 | $retour .= "var self=this;\n"; |
||
33 | $before = isset ( $before ) ? $before : ""; |
||
34 | $retour .= $before; |
||
35 | if ($hasLoader === true && JString::isNotNull ( $responseElement )) { |
||
36 | $this->addLoading ( $retour, $responseElement, $ajaxLoader ); |
||
37 | } elseif ($hasLoader === 'response') { |
||
38 | $this->addResponseLoading ( $retour, $responseElement, $ajaxLoader ); |
||
39 | } elseif ($hasLoader === 'internal') { |
||
40 | $retour .= "\n$(this).addClass('loading');"; |
||
41 | } |
||
42 | $ajaxParameters = [ |
||
43 | "url" => "url", |
||
44 | "method" => "'" . \strtoupper ( $method ) . "'" |
||
45 | ]; |
||
46 | |||
47 | $ajaxParameters ["async"] = ($async ? "true" : "false"); |
||
48 | |||
49 | if (isset ( $params )) { |
||
50 | $ajaxParameters ["data"] = self::_correctParams ( $params, $parameters ); |
||
51 | } |
||
52 | if (isset ( $headers )) { |
||
53 | $ajaxParameters ["headers"] = $headers; |
||
54 | } |
||
55 | if ($csrf) { |
||
56 | $csrf=(is_string($csrf))?$csrf:'csrf-token'; |
||
57 | $parameters ["beforeSend"] = "jqXHR.setRequestHeader('{$csrf}', $('meta[name=\"{$csrf}\"]').attr('content'));"; |
||
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 | protected function createAjaxParameters(&$original, $parameters) { |
||
73 | $validParameters = [ |
||
74 | "contentType" => "%value%", |
||
75 | "dataType" => "'%value%'", |
||
76 | "beforeSend" => "function(jqXHR,settings){%value%}", |
||
77 | "complete" => "function(jqXHR){%value%}", |
||
78 | "processData" => "%value%" |
||
79 | ]; |
||
80 | foreach ( $validParameters as $param => $mask ) { |
||
81 | if (isset ( $parameters [$param] )) { |
||
82 | $original [$param] = \str_replace ( "%value%", $parameters [$param], $mask ); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | protected function implodeAjaxParameters($ajaxParameters) { |
||
87 | $s = ''; |
||
88 | foreach ( $ajaxParameters as $k => $v ) { |
||
89 | if ($s !== '') { |
||
90 | $s .= ','; |
||
91 | } |
||
92 | if (is_array ( $v )) { |
||
93 | $s .= "'{$k}':{" . self::implodeAjaxParameters ( $v ) . "}"; |
||
|
|||
94 | } else { |
||
95 | $s .= "'{$k}':{$v}"; |
||
96 | } |
||
97 | } |
||
98 | return $s; |
||
99 | } |
||
100 | protected function _addJsCondition($jsCondition, $jsSource) { |
||
101 | if (isset ( $jsCondition )) { |
||
102 | return "if(" . $jsCondition . "){\n" . $jsSource . "\n}"; |
||
103 | } |
||
104 | return $jsSource; |
||
105 | } |
||
106 | protected function _getAjaxUrl($url, $attr) { |
||
107 | $url = $this->_correctAjaxUrl ( $url ); |
||
108 | $retour = "url='" . $url . "';"; |
||
109 | $slash = "/"; |
||
110 | if (JString::endswith ( $url, "/" ) === true) { |
||
111 | $slash = ""; |
||
112 | } |
||
113 | if (JString::isNotNull ( $attr )) { |
||
114 | if ($attr === "value") { |
||
115 | $retour .= "url=url+'" . $slash . "'+$(this).val();\n"; |
||
116 | } elseif ($attr === "html") { |
||
117 | $retour .= "url=url+'" . $slash . "'+$(this).html();\n"; |
||
118 | } elseif (\substr ( $attr, 0, 3 ) === "js:") { |
||
119 | $retour .= "url=url+'" . $slash . "'+" . \substr ( $attr, 3 ) . ";\n"; |
||
120 | } elseif ($attr !== null && $attr !== "") |
||
121 | $retour .= "url=url+'" . $slash . "'+($(this).attr('" . $attr . "')||'');\n"; |
||
122 | } |
||
123 | return $retour; |
||
124 | } |
||
125 | protected function onPopstate() { |
||
126 | return "window.onpopstate = function(e){if(e.state){var target=e.state.jqueryDone;$(e.state.selector)[target](e.state.html);}};"; |
||
127 | } |
||
128 | protected function autoActiveLinks($previousURL = "window.location.href") { |
||
129 | $result = "\nfunction getHref(url) { return \$('a').filter(function(){return \$(this).prop('href') == url; });}"; |
||
130 | $result .= "\nvar myurl={$previousURL};if(window._previousURL) getHref(window._previousURL).removeClass('active');getHref(myurl).addClass('active');window._previousURL=myurl;"; |
||
131 | return $result; |
||
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 | protected function _getResponseElement($responseElement) { |
||
160 | if (JString::isNotNull ( $responseElement )) { |
||
161 | $responseElement = Javascript::prep_jquery_selector ( $responseElement ); |
||
162 | } |
||
163 | return $responseElement; |
||
164 | } |
||
165 | protected function _correctAjaxUrl($url) { |
||
166 | if ($url !== "/" && JString::endsWith ( $url, "/" ) === true) |
||
167 | $url = substr ( $url, 0, strlen ( $url ) - 1 ); |
||
168 | if (strncmp ( $url, 'http://', 7 ) != 0 && strncmp ( $url, 'https://', 8 ) != 0) { |
||
169 | $url = $this->getUrl ( $url ); |
||
170 | } |
||
171 | return $url; |
||
172 | } |
||
173 | public static function _correctParams($params, $ajaxParameters = [ ]) { |
||
174 | if (JString::isNull ( $params )) { |
||
175 | return ""; |
||
176 | } |
||
177 | if (\preg_match ( "@^\{.*?\}$@", $params )) { |
||
178 | if (! isset ( $ajaxParameters ['contentType'] ) || ! JString::contains ( $ajaxParameters ['contentType'], 'json' )) { |
||
179 | return '$.param(' . $params . ')'; |
||
180 | } else { |
||
181 | return 'JSON.stringify(' . $params . ')'; |
||
182 | } |
||
183 | } |
||
184 | return $params; |
||
185 | } |
||
186 | public static function _implodeParams($parameters) { |
||
187 | $allParameters = [ ]; |
||
188 | foreach ( $parameters as $params ) { |
||
189 | if (isset ( $params )) |
||
190 | $allParameters [] = self::_correctParams ( $params ); |
||
191 | } |
||
192 | return \implode ( "+'&'+", $allParameters ); |
||
193 | } |
||
194 | protected function addLoading(&$retour, $responseElement, $ajaxLoader = null) { |
||
195 | if (! isset ( $ajaxLoader )) { |
||
196 | $ajaxLoader = $this->ajaxLoader; |
||
197 | } |
||
198 | $loading_notifier = '<div class="ajax-loader ui active inverted dimmer">' . $ajaxLoader . '</div>'; |
||
199 | $retour .= "\t\t{$responseElement}.append('{$loading_notifier}');\n"; |
||
200 | } |
||
201 | protected function addResponseLoading(&$retour, $responseElement, $ajaxLoader = null) { |
||
202 | if (! isset ( $ajaxLoader )) { |
||
203 | $ajaxLoader = $this->ajaxLoader; |
||
204 | } |
||
205 | $loading_notifier = '<div class="ajax-loader">' . $ajaxLoader . '</div>'; |
||
206 | $retour .= "{$responseElement}.empty();\n"; |
||
207 | $retour .= "\t\t{$responseElement}.prepend('{$loading_notifier}');\n"; |
||
208 | } |
||
209 | protected function setAjaxDataCall($params) { |
||
210 | $result = null; |
||
211 | if (! \is_callable ( $params )) { |
||
212 | $result = function ($responseElement, $jqueryDone = 'html') use ($params) { |
||
213 | return AjaxTransition::{$params} ( $responseElement, $jqueryDone ); |
||
214 | }; |
||
215 | } |
||
216 | return $result; |
||
217 | } |
||
218 | protected function setDefaultParameters(&$parameters, $default) { |
||
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,"before"=>null) |
||
249 | */ |
||
250 | public function get($url, $responseElement = '', $parameters = [ ]) { |
||
251 | $parameters ['immediatly'] = true; |
||
252 | return $this->_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,"before"=>null) |
||
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,"before"=>null) |
||
286 | * @param |
||
287 | * $immediatly |
||
288 | * @return string |
||
289 | */ |
||
290 | public function ajaxInterval($method, $url, $interval, $globalName = null, $responseElement = '', $parameters = [ ], $immediatly = true) { |
||
291 | return $this->interval ( $this->ajaxDeferred ( $method, $url, $responseElement, $parameters ), $interval, $globalName, $immediatly ); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Performs a deferred ajax request |
||
296 | * |
||
297 | * @param string $method |
||
298 | * The http method (get, post, delete, put, head) |
||
299 | * @param string $url |
||
300 | * The url of the request |
||
301 | * @param string $responseElement |
||
302 | * selector of the HTML element displaying the answer |
||
303 | * @param array $parameters |
||
304 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null) |
||
305 | */ |
||
306 | public function ajaxDeferred($method, $url, $responseElement = '', $parameters = [ ]) { |
||
307 | $parameters ['immediatly'] = false; |
||
308 | return $this->_ajax ( $method, $url, $responseElement, $parameters ); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
313 | * |
||
314 | * @param string $url |
||
315 | * the request url |
||
316 | * @param string $method |
||
317 | * Method used |
||
318 | * @param array $parameters |
||
319 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"before"=>null) |
||
320 | */ |
||
321 | private function _json($url, $method = "get", $parameters = [ ]) { |
||
322 | $parameters = \array_merge ( $parameters, [ |
||
323 | "hasLoader" => false |
||
324 | ] ); |
||
325 | $jsCallback = isset ( $parameters ['jsCallback'] ) ? $parameters ['jsCallback'] : ""; |
||
326 | $context = isset ( $parameters ['context'] ) ? $parameters ['context'] : "document"; |
||
327 | $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"; |
||
328 | $retour .= "\t$(document).trigger('jsonReady',[data]);\n"; |
||
329 | $parameters ["jsCallback"] = $retour; |
||
330 | return $this->_ajax ( $method, $url, null, $parameters ); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
||
335 | * |
||
336 | * @param string $url |
||
337 | * the request url |
||
338 | * @param string $method |
||
339 | * Method used |
||
340 | * @param array $parameters |
||
341 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"before"=>null) |
||
342 | */ |
||
343 | public function json($url, $method = "get", $parameters = [ ]) { |
||
344 | return $this->_json ( $url, $method, $parameters ); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
||
349 | * |
||
350 | * @param string $element |
||
351 | * @param string $event |
||
352 | * @param string $url |
||
353 | * the request address |
||
354 | * @param string $method |
||
355 | * default get |
||
356 | * @param array $parameters |
||
357 | * default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true,"before"=>null,"listenerOn"=>false) |
||
358 | */ |
||
359 | public function jsonOn($event, $element, $url, $method = 'get', $parameters = array ()) { |
||
360 | $this->setDefaultParameters ( $parameters, [ |
||
361 | 'preventDefault' => true, |
||
362 | 'stopPropagation' => true, |
||
363 | 'immediatly' => true, |
||
364 | 'listenerOn'=>false |
||
365 | ] ); |
||
366 | return $this->_add_event ( $element, $this->jsonDeferred ( $url, $method, $parameters ), $event, $parameters ["preventDefault"], $parameters ["stopPropagation"], $parameters ["immediatly"] ,$parameters['listenerOn']); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name |
||
371 | * |
||
372 | * @param string $url |
||
373 | * the request url |
||
374 | * @param string $method |
||
375 | * Method used |
||
376 | * @param array $parameters |
||
377 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"before"=>null) |
||
378 | */ |
||
379 | public function jsonDeferred($url, $method = 'get', $parameters = [ ]) { |
||
380 | $parameters ['immediatly'] = false; |
||
381 | return $this->_json ( $url, $method, $parameters ); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
386 | * |
||
387 | * @param string $maskSelector |
||
388 | * @param string $url |
||
389 | * the request url |
||
390 | * @param string $method |
||
391 | * Method used, default : get |
||
392 | * @param array $parameters |
||
393 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json","before"=>null) |
||
394 | */ |
||
395 | private function _jsonArray($maskSelector, $url, $method = 'get', $parameters = [ ]) { |
||
396 | $parameters = \array_merge ( $parameters, [ |
||
397 | "hasLoader" => false |
||
398 | ] ); |
||
399 | $rowClass = isset ( $parameters ['rowClass'] ) ? $parameters ['rowClass'] : "_json"; |
||
400 | $jsCallback = isset ( $parameters ['jsCallback'] ) ? $parameters ['jsCallback'] : ""; |
||
401 | $context = isset ( $parameters ['context'] ) ? $parameters ['context'] : null; |
||
402 | if ($context === null) { |
||
403 | $parent = "$('" . $maskSelector . "').parent()"; |
||
404 | $newElm = "$('#'+newId)"; |
||
405 | } else { |
||
406 | $parent = $context; |
||
407 | $newElm = $context . ".find('#'+newId)"; |
||
408 | } |
||
409 | $appendTo = "\t\tnewElm.appendTo(" . $parent . ");\n"; |
||
410 | $retour = $parent . ".find('.{$rowClass}').remove();"; |
||
411 | $retour .= "\tdata=($.isPlainObject(data)||$.isArray(data))?data:JSON.parse(data);\n$.each(data, function(index, value) {\n" . "\tvar created=false;var maskElm=$('" . $maskSelector . "').first();maskElm.hide();" . "\tvar newId=(maskElm.attr('id') || 'mask')+'-'+index;" . "\tvar newElm=" . $newElm . ";\n" . "\tif(!newElm.length){\n" . "\t\tnewElm=maskElm.clone(); |
||
412 | newElm.attr('id',newId);\n;newElm.addClass('{$rowClass}').removeClass('_jsonArrayModel');\nnewElm.find('[id]').each(function(){ var newId=$(this).attr('id')+'-'+index;$(this).attr('id',newId).removeClass('_jsonArrayChecked');});\n"; |
||
413 | $retour .= $appendTo; |
||
414 | $retour .= "\t}\n" . "\tfor(var key in value){\n" . "\t\t\tvar html = $('<div />').append($(newElm).clone()).html();\n" . "\t\t\tif(html.indexOf('__'+key+'__')>-1){\n" . "\t\t\t\tcontent=$(html.split('__'+key+'__').join(value[key]));\n" . "\t\t\t\t$(newElm).replaceWith(content);newElm=content;\n" . "\t\t\t}\n" . "\t\tvar sel='[data-id=\"'+key+'\"]';if($(sel,newElm).length){\n" . "\t\t\tvar selElm=$(sel,newElm);\n" . "\t\t\t if(selElm.is('[value]')) { selElm.attr('value',value[key]);selElm.val(value[key]);} else { selElm.html(value[key]); }\n" . "\t\t}\n" . "}\n" . "\t$(newElm).show(true);" . "\n" . "\t$(newElm).removeClass('hide');" . "});\n"; |
||
415 | $retour .= "\t$(document).trigger('jsonReady',[data]);\n"; |
||
416 | $retour .= "\t" . $jsCallback; |
||
417 | $parameters ["jsCallback"] = $retour; |
||
418 | return $this->_ajax ( $method, $url, null, $parameters ); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
||
423 | * |
||
424 | * @param string $maskSelector |
||
425 | * @param string $url |
||
426 | * the request url |
||
427 | * @param string $method |
||
428 | * Method used, default : get |
||
429 | * @param array $parameters |
||
430 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json","before"=>null) |
||
431 | */ |
||
432 | public function jsonArray($maskSelector, $url, $method = 'get', $parameters = [ ]) { |
||
433 | return $this->_jsonArray ( $maskSelector, $url, $method, $parameters ); |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * 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 |
||
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,"rowClass"=>"_json","before"=>null) |
||
446 | */ |
||
447 | public function jsonArrayDeferred($maskSelector, $url, $method = 'get', $parameters = [ ]) { |
||
448 | $parameters ['immediatly'] = false; |
||
449 | return $this->jsonArray ( $maskSelector, $url, $method, $parameters ); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element |
||
454 | * |
||
455 | * @param string $element |
||
456 | * @param string $event |
||
457 | * @param string $url |
||
458 | * the request url |
||
459 | * @param string $method |
||
460 | * Method used, default : get |
||
461 | * @param array $parameters |
||
462 | * default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true,"before"=>null,"listenerOn"=>false) |
||
463 | */ |
||
464 | public function jsonArrayOn($event, $element, $maskSelector, $url, $method = 'get', $parameters = array ()) { |
||
465 | $this->setDefaultParameters ( $parameters, [ |
||
466 | 'preventDefault' => true, |
||
467 | 'stopPropagation' => true, |
||
468 | 'immediatly' => true, |
||
469 | 'listenerOn'=>false |
||
470 | ] ); |
||
471 | return $this->_add_event ( $element, $this->jsonArrayDeferred ( $maskSelector, $url, $method, $parameters ), $event, $parameters ["preventDefault"], $parameters ["stopPropagation"], $parameters ["immediatly"] ,$parameters['listenerOn']); |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Prepares a Get ajax request |
||
476 | * for using on an event |
||
477 | * |
||
478 | * @param string $url |
||
479 | * The url of the request |
||
480 | * @param string $responseElement |
||
481 | * selector of the HTML element displaying the answer |
||
482 | * @param array $parameters |
||
483 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null) |
||
484 | */ |
||
485 | public function getDeferred($url, $responseElement = "", $parameters = [ ]) { |
||
486 | $parameters ['immediatly'] = false; |
||
487 | return $this->_get ( $url, $responseElement, $parameters ); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Performs a get to $url on the event $event on $element |
||
492 | * and display it in $responseElement |
||
493 | * |
||
494 | * @param string $event |
||
495 | * the event |
||
496 | * @param string $element |
||
497 | * the element on which event is observed |
||
498 | * @param string $url |
||
499 | * The url of the request |
||
500 | * @param string $responseElement |
||
501 | * The selector of the HTML element displaying the answer |
||
502 | * @param array $parameters |
||
503 | * default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false,"before"=>null,"listenerOn"=>false) |
||
504 | */ |
||
505 | public function getOn($event, $element, $url, $responseElement = "", $parameters = array ()) { |
||
506 | $parameters['method']='get'; |
||
507 | return $this->ajaxOn($event, $element, $url,$responseElement,$parameters); |
||
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,"before"=>null,"listenerOn"=>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 | 'listenerOn'=>false |
||
532 | ] ); |
||
533 | return $this->_add_event ( $element, $this->ajaxDeferred ( $parameters ['method'], $url, $responseElement, $parameters ), $event, $parameters ["preventDefault"], $parameters ["stopPropagation"], $parameters ["immediatly"],$parameters['listenerOn'] ); |
||
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,"before"=>null,"listenerOn"=>false) |
||
548 | */ |
||
549 | public function ajaxOnClick($element, $url, $responseElement = '', $parameters = array ()) { |
||
550 | return $this->ajaxOn ( 'click', $element, $url, $responseElement, $parameters ); |
||
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,"before"=>null,"listenerOn"=>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,"before"=>null) |
||
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,"before"=>null) |
||
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 | private function _post($url, $params = '{}', $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,"before"=>null) |
||
633 | */ |
||
634 | public function post($url, $params = "{}", $responseElement = "", $parameters = [ ]) { |
||
635 | $parameters ['immediatly'] = true; |
||
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,"before"=>null) |
||
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,"before"=>null,"listenerOn"=>false) |
||
671 | */ |
||
672 | public function postOn($event, $element, $url, $params = "{}", $responseElement = "", $parameters = array ()) { |
||
673 | $parameters['method']='post'; |
||
674 | $parameters ['params'] = $params; |
||
675 | return $this->ajaxOn($event, $element, $url,$responseElement,$parameters); |
||
676 | } |
||
677 | |||
678 | /** |
||
679 | * Performs a post to $url on the click event fired on $element and pass the parameters $params |
||
680 | * Display the result in $responseElement |
||
681 | * |
||
682 | * @param string $element |
||
683 | * @param string $url |
||
684 | * The url of the request |
||
685 | * @param string $params |
||
686 | * The parameters to send |
||
687 | * @param string $responseElement |
||
688 | * selector of the HTML element displaying the answer |
||
689 | * @param array $parameters |
||
690 | * default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null,"before"=>null,"listenerOn"=>false) |
||
691 | */ |
||
692 | public function postOnClick($element, $url, $params = '{}', $responseElement = '', $parameters = array ()) { |
||
693 | return $this->postOn ( 'click', $element, $url, $params, $responseElement, $parameters ); |
||
694 | } |
||
695 | private function _postForm($url, $form, $responseElement, $parameters = [ ]) { |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * Performs a post form with ajax |
||
756 | * |
||
757 | * @param string $url |
||
758 | * The url of the request |
||
759 | * @param string $form |
||
760 | * The form HTML id |
||
761 | * @param string $responseElement |
||
762 | * selector of the HTML element displaying the answer |
||
763 | * @param array $parameters |
||
764 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null) |
||
765 | */ |
||
766 | public function postForm($url, $form, $responseElement, $parameters = [ ]) { |
||
767 | $parameters ['immediatly'] = true; |
||
768 | return $this->_postForm ( $url, $form, $responseElement, $parameters ); |
||
769 | } |
||
770 | |||
771 | /** |
||
772 | * Performs a delayed post form with ajax |
||
773 | * For use on an event |
||
774 | * |
||
775 | * @param string $url |
||
776 | * The url of the request |
||
777 | * @param string $form |
||
778 | * The form HTML id |
||
779 | * @param string $responseElement |
||
780 | * selector of the HTML element displaying the answer |
||
781 | * @param array $parameters |
||
782 | * default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false,"before"=>null) |
||
783 | */ |
||
784 | public function postFormDeferred($url, $form, $responseElement, $parameters = [ ]) { |
||
785 | $parameters ['immediatly'] = false; |
||
786 | return $this->_postForm ( $url, $form, $responseElement, $parameters ); |
||
787 | } |
||
788 | |||
789 | /** |
||
790 | * Performs a post form with ajax in response to an event $event on $element |
||
791 | * display the result in $responseElement |
||
792 | * |
||
793 | * @param string $event |
||
794 | * @param string $element |
||
795 | * @param string $url |
||
796 | * @param string $form |
||
797 | * @param string $responseElement |
||
798 | * selector of the HTML element displaying the answer |
||
799 | * @param array $parameters |
||
800 | * default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>null,"headers"=>null,"historize"=>false,"before"=>null,"listenerOn"=>false) |
||
801 | */ |
||
802 | public function postFormOn($event, $element, $url, $form, $responseElement = "", $parameters = array ()) { |
||
810 | } |
||
811 | |||
812 | /** |
||
813 | * Performs a post form with ajax in response to the click event on $element |
||
814 | * display the result in $responseElement |
||
815 | * |
||
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,"listenerOn"=>false) |
||
823 | */ |
||
824 | public function postFormOnClick($element, $url, $form, $responseElement = "", $parameters = array ()) { |
||
825 | return $this->postFormOn ( "click", $element, $url, $form, $responseElement, $parameters ); |
||
826 | } |
||
827 | |||
828 | public function addCsrf($name='csrf-token'){ |
||
835 | } |
||
836 | } |
||
837 | });"; |
||
838 | } |
||
839 | } |
||
840 |