|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ajax\common\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ajax\service\AjaxTransition; |
|
6
|
|
|
use Ajax\service\Javascript; |
|
7
|
|
|
use Ajax\service\JString; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author jc |
|
11
|
|
|
* @property array $jquery_code_for_compile |
|
12
|
|
|
* @property array $params |
|
13
|
|
|
*/ |
|
14
|
|
|
trait JsUtilsAjaxTrait { |
|
15
|
|
|
|
|
16
|
|
|
protected $ajaxTransition; |
|
17
|
|
|
protected $ajaxLoader="<div class=\"ui active centered inline text loader\">Loading</div>"; |
|
18
|
|
|
|
|
19
|
|
|
abstract public function getUrl($url); |
|
20
|
|
|
abstract public function _add_event($element, $js, $event, $preventDefault=false, $stopPropagation=false,$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
|
|
|
if($hasLoader===true && JString::isNotNull($responseElement)){ |
|
34
|
|
|
$this->addLoading($retour, $responseElement,$ajaxLoader); |
|
35
|
|
|
}elseif($hasLoader==="internal"){ |
|
36
|
|
|
$retour.="\n$(this).addClass('loading');"; |
|
37
|
|
|
} |
|
38
|
|
|
$ajaxParameters=["url"=>"url","method"=>"'".\strtoupper($method)."'"]; |
|
39
|
|
|
|
|
40
|
|
|
$ajaxParameters["async"]=($async?"true":"false"); |
|
41
|
|
|
|
|
42
|
|
|
if(isset($params)){ |
|
43
|
|
|
$ajaxParameters["data"]=self::_correctParams($params); |
|
44
|
|
|
} |
|
45
|
|
|
if(isset($headers)){ |
|
46
|
|
|
$ajaxParameters["headers"]=$headers; |
|
47
|
|
|
} |
|
48
|
|
|
$this->createAjaxParameters($ajaxParameters, $parameters); |
|
49
|
|
|
$retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data, textStatus, jqXHR ) {\n"; |
|
50
|
|
|
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback,$hasLoader,($historize?$originalSelector:null))."});\n"; |
|
51
|
|
|
$retour=$this->_addJsCondition($jsCondition,$retour); |
|
52
|
|
|
if ($immediatly) |
|
53
|
|
|
$this->jquery_code_for_compile[]=$retour; |
|
54
|
|
|
return $retour; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function createAjaxParameters(&$original,$parameters){ |
|
58
|
|
|
$validParameters=["dataType"=>"'%value%'","beforeSend"=>"function(jqXHR,settings){%value%}","complete"=>"function(jqXHR){%value%}"]; |
|
59
|
|
|
foreach ($validParameters as $param=>$mask){ |
|
60
|
|
|
if(isset($parameters[$param])){ |
|
61
|
|
|
$original[$param]=\str_replace("%value%", $parameters[$param], $mask); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function implodeAjaxParameters($ajaxParameters){ |
|
67
|
|
|
$s = ''; foreach ($ajaxParameters as $k=>$v) { if ($s !== '') { $s .= ','; } $s .= "'{$k}':{$v}"; } |
|
68
|
|
|
return $s; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function _addJsCondition($jsCondition,$jsSource){ |
|
72
|
|
|
if(isset($jsCondition)){ |
|
73
|
|
|
return "if(".$jsCondition."){\n".$jsSource."\n}"; |
|
74
|
|
|
} |
|
75
|
|
|
return $jsSource; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
protected function _getAjaxUrl($url,$attr){ |
|
80
|
|
|
$url=$this->_correctAjaxUrl($url); |
|
81
|
|
|
$retour="url='".$url."';"; |
|
82
|
|
|
$slash="/"; |
|
83
|
|
|
if(JString::endswith($url, "/")===true){ |
|
84
|
|
|
$slash=""; |
|
85
|
|
|
} |
|
86
|
|
|
if(JString::isNotNull($attr)){ |
|
87
|
|
|
if ($attr==="value"){ |
|
88
|
|
|
$retour.="url=url+'".$slash."'+$(this).val();\n"; |
|
89
|
|
|
}elseif ($attr==="html"){ |
|
90
|
|
|
$retour.="url=url+'".$slash."'+$(this).html();\n"; |
|
91
|
|
|
}elseif(\substr($attr, 0,3)==="js:"){ |
|
92
|
|
|
$retour.="url=url+'".$slash."'+".\substr($attr, 3).";\n"; |
|
93
|
|
|
}elseif($attr!==null && $attr!=="") |
|
94
|
|
|
$retour.="url=url+'".$slash."'+($(this).attr('".$attr."')||'');\n"; |
|
95
|
|
|
} |
|
96
|
|
|
return $retour; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function onPopstate(){ |
|
100
|
|
|
return "window.onpopstate = function(e){if(e.state){var target=e.state.jqueryDone;$(e.state.selector)[target](e.state.html);}};"; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function _getOnAjaxDone($responseElement,$jqueryDone,$ajaxTransition,$jsCallback,$hasLoader=false,$history=null){ |
|
104
|
|
|
$retour="";$call=null; |
|
105
|
|
|
if (JString::isNotNull($responseElement)) { |
|
106
|
|
|
if(isset($ajaxTransition)){ |
|
107
|
|
|
$call=$this->setAjaxDataCall($ajaxTransition); |
|
108
|
|
|
}elseif(isset($this->ajaxTransition)){ |
|
109
|
|
|
$call=$this->ajaxTransition; |
|
110
|
|
|
} |
|
111
|
|
|
if(\is_callable($call)) |
|
112
|
|
|
$retour="\t".$call($responseElement,$jqueryDone).";\n"; |
|
113
|
|
|
else |
|
114
|
|
|
$retour="\t{$responseElement}.{$jqueryDone}( data );\n"; |
|
115
|
|
|
} |
|
116
|
|
|
if(isset($history)){ |
|
117
|
|
|
$retour.="\nwindow.history.pushState({'html':data,'selector':".Javascript::prep_value($history).",'jqueryDone':'{$jqueryDone}'},'', url);"; |
|
118
|
|
|
} |
|
119
|
|
|
if($hasLoader==="internal"){ |
|
120
|
|
|
$retour.="\n$(self).removeClass('loading');"; |
|
121
|
|
|
} |
|
122
|
|
|
$retour.="\t".$jsCallback."\n"; |
|
123
|
|
|
return $retour; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function _getResponseElement($responseElement){ |
|
127
|
|
|
if (JString::isNotNull($responseElement)) { |
|
128
|
|
|
$responseElement=Javascript::prep_value($responseElement); |
|
129
|
|
|
$responseElement=Javascript::prep_jquery_selector($responseElement); |
|
130
|
|
|
} |
|
131
|
|
|
return $responseElement; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
protected function _correctAjaxUrl($url) { |
|
135
|
|
|
if ($url!=="/" && JString::endsWith($url, "/")===true) |
|
136
|
|
|
$url=substr($url, 0, strlen($url)-1); |
|
137
|
|
|
if (strncmp($url, 'http://', 7)!=0&&strncmp($url, 'https://', 8)!=0) { |
|
138
|
|
|
$url=$this->getUrl($url); |
|
139
|
|
|
} |
|
140
|
|
|
return $url; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public static function _correctParams($params){ |
|
144
|
|
|
if(JString::isNull($params)){ |
|
145
|
|
|
return ""; |
|
146
|
|
|
} |
|
147
|
|
|
if(\preg_match("@^\{.*?\}$@", $params)){ |
|
148
|
|
|
return '$.param('.$params.')'; |
|
149
|
|
|
} |
|
150
|
|
|
return $params; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public static function _implodeParams($parameters){ |
|
154
|
|
|
$allParameters=[]; |
|
155
|
|
|
foreach ($parameters as $params){ |
|
156
|
|
|
if(isset($params)) |
|
157
|
|
|
$allParameters[]=self::_correctParams($params); |
|
158
|
|
|
} |
|
159
|
|
|
return \implode("+'&'+", $allParameters); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
protected function addLoading(&$retour, $responseElement,$ajaxLoader=null) { |
|
163
|
|
|
if(!isset($ajaxLoader)){ |
|
164
|
|
|
$ajaxLoader=$this->ajaxLoader; |
|
165
|
|
|
} |
|
166
|
|
|
$loading_notifier='<div class="ajax-loader">'.$ajaxLoader.'</div>'; |
|
167
|
|
|
$retour.="{$responseElement}.empty();\n"; |
|
168
|
|
|
$retour.="\t\t{$responseElement}.prepend('{$loading_notifier}');\n"; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
protected function setAjaxDataCall($params){ |
|
172
|
|
|
$result=null; |
|
173
|
|
|
if(!\is_callable($params)){ |
|
174
|
|
|
$result=function ($responseElement,$jqueryDone="html") use($params){ |
|
175
|
|
|
return AjaxTransition::{$params}($responseElement,$jqueryDone); |
|
176
|
|
|
}; |
|
177
|
|
|
} |
|
178
|
|
|
return $result; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
protected function setDefaultParameters(&$parameters,$default){ |
|
182
|
|
|
foreach ($default as $k=>$v){ |
|
183
|
|
|
if(!isset($parameters[$k])) |
|
184
|
|
|
$parameters[$k]=$v; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function setAjaxLoader($loader) { |
|
189
|
|
|
$this->ajaxLoader=$loader; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Performs an ajax GET request |
|
194
|
|
|
* @param string $url The url of the request |
|
195
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
196
|
|
|
*/ |
|
197
|
|
|
private function _get($url, $responseElement="",$parameters=[]) { |
|
198
|
|
|
return $this->_ajax("get", $url,$responseElement,$parameters); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Performs an ajax GET request |
|
203
|
|
|
* @param string $url The url of the request |
|
204
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
205
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
206
|
|
|
*/ |
|
207
|
|
|
public function get($url, $responseElement="",$parameters=[]) { |
|
208
|
|
|
$parameters["immediatly"]=true; |
|
209
|
|
|
return $this->_get($url,$responseElement,$parameters); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Performs an ajax request |
|
214
|
|
|
* @param string $method The http method (get, post, delete, put, head) |
|
215
|
|
|
* @param string $url The url of the request |
|
216
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
217
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
218
|
|
|
*/ |
|
219
|
|
|
public function ajax($method,$url, $responseElement="", $parameters=[]) { |
|
220
|
|
|
$parameters["immediatly"]=true; |
|
221
|
|
|
return $this->_ajax($method,$url,$responseElement,$parameters); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Performs a deferred ajax request |
|
226
|
|
|
* @param string $method The http method (get, post, delete, put, head) |
|
227
|
|
|
* @param string $url The url of the request |
|
228
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
229
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
230
|
|
|
*/ |
|
231
|
|
|
public function ajaxDeferred($method,$url, $responseElement="", $parameters=[]) { |
|
232
|
|
|
$parameters["immediatly"]=false; |
|
233
|
|
|
return $this->_ajax($method,$url,$responseElement,$parameters); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
|
238
|
|
|
* @param string $url the request url |
|
239
|
|
|
* @param string $method Method used |
|
240
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
|
241
|
|
|
*/ |
|
242
|
|
|
private function _json($url, $method="get",$parameters=[]) { |
|
243
|
|
|
$parameters=\array_merge($parameters,["hasLoader"=>false]); |
|
244
|
|
|
$jsCallback=isset($parameters['jsCallback']) ? $parameters['jsCallback'] : ""; |
|
245
|
|
|
$context=isset($parameters['context']) ? $parameters['context'] : "document"; |
|
246
|
|
|
$retour="\tdata=($.isPlainObject(data))?data:JSON.parse(data);\t".$jsCallback.";\n\tfor(var key in data){" |
|
247
|
|
|
."if($('#'+key,".$context.").length){ if($('#'+key,".$context.").is('[value]')) { $('#'+key,".$context.").val(data[key]);} else { $('#'+key,".$context.").html(data[key]); }}};\n"; |
|
248
|
|
|
$retour.="\t$(document).trigger('jsonReady',[data]);\n"; |
|
249
|
|
|
$parameters["jsCallback"]=$retour; |
|
250
|
|
|
return $this->_ajax($method, $url,null,$parameters); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Performs an ajax request and receives the JSON data types by assigning DOM elements with the same name |
|
255
|
|
|
* @param string $url the request url |
|
256
|
|
|
* @param string $method Method used |
|
257
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
|
258
|
|
|
*/ |
|
259
|
|
|
public function json($url, $method="get", $parameters=[]) { |
|
260
|
|
|
return $this->_json($url,$method,$parameters); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Makes an ajax request and receives the JSON data types by assigning DOM elements with the same name when $event fired on $element |
|
265
|
|
|
* @param string $element |
|
266
|
|
|
* @param string $event |
|
267
|
|
|
* @param string $url the request address |
|
268
|
|
|
* @param string $method default get |
|
269
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","immediatly"=>true) |
|
270
|
|
|
*/ |
|
271
|
|
|
public function jsonOn($event,$element, $url,$method="get",$parameters=array()) { |
|
272
|
|
|
$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
|
273
|
|
|
return $this->_add_event($element, $this->jsonDeferred($url,$method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Prepares an ajax request delayed and receives the JSON data types by assigning DOM elements with the same name |
|
278
|
|
|
* @param string $url the request url |
|
279
|
|
|
* @param string $method Method used |
|
280
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>"document","jsCondition"=>NULL,"headers"=>null,"immediatly"=>false) |
|
281
|
|
|
*/ |
|
282
|
|
|
public function jsonDeferred($url, $method="get", $parameters=[]) { |
|
283
|
|
|
$parameters["immediatly"]=false; |
|
284
|
|
|
return $this->_json($url,$method,$parameters); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
|
289
|
|
|
* @param string $maskSelector |
|
290
|
|
|
* @param string $url the request url |
|
291
|
|
|
* @param string $method Method used, default : get |
|
292
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json") |
|
293
|
|
|
*/ |
|
294
|
|
|
private function _jsonArray($maskSelector, $url, $method="get", $parameters=[]) { |
|
295
|
|
|
$parameters=\array_merge($parameters,["hasLoader"=>false]); |
|
296
|
|
|
$rowClass=isset($parameters['rowClass']) ? $parameters['rowClass'] : "_json"; |
|
297
|
|
|
$jsCallback=isset($parameters['jsCallback']) ? $parameters['jsCallback'] : ""; |
|
298
|
|
|
$context=isset($parameters['context']) ? $parameters['context'] : null; |
|
299
|
|
|
if($context===null){ |
|
300
|
|
|
$parent="$('".$maskSelector."').parent()"; |
|
301
|
|
|
$newElm = "$('#'+newId)"; |
|
302
|
|
|
}else{ |
|
303
|
|
|
$parent=$context; |
|
304
|
|
|
$newElm = $context.".find('#'+newId)"; |
|
305
|
|
|
} |
|
306
|
|
|
$appendTo="\t\tnewElm.appendTo(".$parent.");\n"; |
|
307
|
|
|
$retour=$parent.".find('.{$rowClass}').remove();"; |
|
308
|
|
|
$retour.="\tdata=($.isPlainObject(data))?data:JSON.parse(data);\t".$jsCallback.";\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(); |
|
309
|
|
|
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"; |
|
310
|
|
|
$retour.= $appendTo; |
|
311
|
|
|
$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"; |
|
312
|
|
|
$retour.="\t$(document).trigger('jsonReady',[data]);\n"; |
|
313
|
|
|
$parameters["jsCallback"]=$retour; |
|
314
|
|
|
return $this->_ajax($method, $url,null,$parameters); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name |
|
319
|
|
|
* @param string $maskSelector |
|
320
|
|
|
* @param string $url the request url |
|
321
|
|
|
* @param string $method Method used, default : get |
|
322
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"immediatly"=>false,"rowClass"=>"_json") |
|
323
|
|
|
*/ |
|
324
|
|
|
public function jsonArray($maskSelector, $url, $method="get", $parameters=[]) { |
|
325
|
|
|
return $this->_jsonArray($maskSelector, $url,$method,$parameters); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* 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 |
|
330
|
|
|
* @param string $maskSelector |
|
331
|
|
|
* @param string $url the request url |
|
332
|
|
|
* @param string $method Method used, default : get |
|
333
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","context"=>null,"jsCondition"=>NULL,"headers"=>null,"rowClass"=>"_json") |
|
334
|
|
|
*/ |
|
335
|
|
|
public function jsonArrayDeferred($maskSelector, $url, $method="get", $parameters) { |
|
336
|
|
|
$parameters["immediatly"]=false; |
|
337
|
|
|
return $this->jsonArray($maskSelector, $url, $method, $parameters); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Performs an ajax request and receives the JSON array data types by assigning DOM elements with the same name when $event fired on $element |
|
342
|
|
|
* @param string $element |
|
343
|
|
|
* @param string $event |
|
344
|
|
|
* @param string $url the request url |
|
345
|
|
|
* @param string $method Method used, default : get |
|
346
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","params"=>"{}","method"=>"get","rowClass"=>"_json","immediatly"=>true) |
|
347
|
|
|
*/ |
|
348
|
|
|
public function jsonArrayOn($event,$element,$maskSelector, $url,$method="get",$parameters=array()) { |
|
349
|
|
|
$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
|
350
|
|
|
return $this->_add_event($element, $this->jsonArrayDeferred($maskSelector,$url,$method, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Prepares a Get ajax request |
|
355
|
|
|
* for using on an event |
|
356
|
|
|
* @param string $url The url of the request |
|
357
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
358
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
359
|
|
|
*/ |
|
360
|
|
|
public function getDeferred($url, $responseElement="", $parameters=[]) { |
|
361
|
|
|
$parameters["immediatly"]=false; |
|
362
|
|
|
return $this->_get($url, $responseElement,$parameters); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Performs a get to $url on the event $event on $element |
|
367
|
|
|
* and display it in $responseElement |
|
368
|
|
|
* @param string $event the event |
|
369
|
|
|
* @param string $element the element on which event is observed |
|
370
|
|
|
* @param string $url The url of the request |
|
371
|
|
|
* @param string $responseElement The selector of the HTML element displaying the answer |
|
372
|
|
|
* @param array $parameters 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) |
|
373
|
|
|
*/ |
|
374
|
|
|
public function getOn($event, $element, $url, $responseElement="", $parameters=array()) { |
|
375
|
|
|
$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
|
376
|
|
|
return $this->_add_event($element, $this->getDeferred($url,$responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Performs an ajax request to $url on the event $event on $element |
|
381
|
|
|
* and display it in $responseElement |
|
382
|
|
|
* @param string $event the event observed |
|
383
|
|
|
* @param string $element the element on which event is observed |
|
384
|
|
|
* @param string $url The url of the request |
|
385
|
|
|
* @param string $responseElement The selector of the HTML element displaying the answer |
|
386
|
|
|
* @param array $parameters 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) |
|
387
|
|
|
*/ |
|
388
|
|
|
public function ajaxOn($event, $element, $url, $responseElement="", $parameters=array()) { |
|
389
|
|
|
$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true,"method"=>"get"]); |
|
390
|
|
|
return $this->_add_event($element, $this->ajaxDeferred($parameters["method"],$url,$responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* Performs a get to $url on the click event on $element |
|
395
|
|
|
* and display it in $responseElement |
|
396
|
|
|
* @param string $element the element on which event is observed |
|
397
|
|
|
* @param string $url The url of the request |
|
398
|
|
|
* @param string $responseElement The selector of the HTML element displaying the answer |
|
399
|
|
|
* @param array $parameters 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) |
|
400
|
|
|
*/ |
|
401
|
|
|
public function ajaxOnClick($element, $url, $responseElement="", $parameters=array()) { |
|
402
|
|
|
return $this->ajaxOn("click", $element, $url, $responseElement, $parameters); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* Performs a get to $url on the click event on $element |
|
407
|
|
|
* and display it in $responseElement |
|
408
|
|
|
* @param string $element the element on which click is observed |
|
409
|
|
|
* @param string $url The url of the request |
|
410
|
|
|
* @param string $responseElement The selector of the HTML element displaying the answer |
|
411
|
|
|
* @param array $parameters 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) |
|
412
|
|
|
*/ |
|
413
|
|
|
public function getOnClick($element, $url, $responseElement="", $parameters=array()) { |
|
414
|
|
|
return $this->getOn("click", $element, $url, $responseElement, $parameters); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* Uses an hyperlink to make an ajax get request |
|
419
|
|
|
* @param string $element an hyperlink selector |
|
420
|
|
|
* @param string $responseElement the target of the ajax request (data-target attribute of the element is used if responseElement is omited) |
|
421
|
|
|
* @param array $parameters 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) |
|
422
|
|
|
* @return $this |
|
423
|
|
|
*/ |
|
424
|
|
|
public function getHref($element,$responseElement="",$parameters=array()){ |
|
425
|
|
|
$parameters["attr"]="href"; |
|
426
|
|
|
if(JString::isNull($responseElement)){ |
|
427
|
|
|
$responseElement='$($(this).attr("data-target"))'; |
|
428
|
|
|
} |
|
429
|
|
|
if(!isset($parameters["historize"])){ |
|
430
|
|
|
$parameters["historize"]=true; |
|
431
|
|
|
} |
|
432
|
|
|
return $this->getOnClick($element, "",$responseElement,$parameters); |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* Uses an hyperlink to make an ajax get request |
|
437
|
|
|
* @param string $element an hyperlink selector |
|
438
|
|
|
* @param string $responseElement the target of the ajax request (data-target attribute of the element is used if responseElement is omited) |
|
439
|
|
|
* @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"params"=>"{}","jsCallback"=>NULL,"attr"=>"href","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
440
|
|
|
* @return $this |
|
441
|
|
|
*/ |
|
442
|
|
|
public function postHref($element,$responseElement="",$parameters=array()){ |
|
443
|
|
|
$parameters["attr"]="href"; |
|
444
|
|
|
if(JString::isNull($responseElement)){ |
|
445
|
|
|
$responseElement='$($(this).attr("data-target"))'; |
|
446
|
|
|
} |
|
447
|
|
|
if(!isset($parameters["historize"])){ |
|
448
|
|
|
$parameters["historize"]=true; |
|
449
|
|
|
} |
|
450
|
|
|
return $this->postOnClick($element, "","{}",$responseElement,$parameters); |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
private function _post($url, $params="{}",$responseElement="", $parameters=[]) { |
|
454
|
|
|
$parameters["params"]=$params; |
|
455
|
|
|
return $this->_ajax("POST", $url,$responseElement,$parameters); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* Makes an ajax post |
|
460
|
|
|
* @param string $url the request url |
|
461
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
462
|
|
|
* @param string $params JSON parameters |
|
463
|
|
|
* @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
464
|
|
|
*/ |
|
465
|
|
|
public function post($url, $params="{}",$responseElement="", $parameters=[]) { |
|
466
|
|
|
return $this->_post($url, $params,$responseElement, $parameters); |
|
467
|
|
|
} |
|
468
|
|
|
|
|
469
|
|
|
/** |
|
470
|
|
|
* Prepares a delayed ajax POST |
|
471
|
|
|
* to use on an event |
|
472
|
|
|
* @param string $url the request url |
|
473
|
|
|
* @param string $params JSON parameters |
|
474
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
475
|
|
|
* @param array $parameters default : array("jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"immediatly"=>true,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
476
|
|
|
*/ |
|
477
|
|
|
public function postDeferred($url, $params="{}",$responseElement="", $parameters=[]) { |
|
478
|
|
|
$parameters["immediatly"]=false; |
|
479
|
|
|
return $this->_post($url, $params, $responseElement, $parameters); |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* Performs a post to $url on the event $event fired on $element and pass the parameters $params |
|
484
|
|
|
* Display the result in $responseElement |
|
485
|
|
|
* @param string $event |
|
486
|
|
|
* @param string $element |
|
487
|
|
|
* @param string $url The url of the request |
|
488
|
|
|
* @param string $params The parameters to send |
|
489
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
490
|
|
|
* @param array $parameters 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) |
|
491
|
|
|
*/ |
|
492
|
|
|
public function postOn($event, $element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
493
|
|
|
$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
|
494
|
|
|
return $this->_add_event($element, $this->postDeferred($url, $params, $responseElement, $parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
/** |
|
498
|
|
|
* Performs a post to $url on the click event fired on $element and pass the parameters $params |
|
499
|
|
|
* Display the result in $responseElement |
|
500
|
|
|
* @param string $element |
|
501
|
|
|
* @param string $url The url of the request |
|
502
|
|
|
* @param string $params The parameters to send |
|
503
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
504
|
|
|
* @param array $parameters 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) |
|
505
|
|
|
*/ |
|
506
|
|
|
public function postOnClick($element, $url, $params="{}", $responseElement="", $parameters=array()) { |
|
507
|
|
|
return $this->postOn("click", $element, $url, $params, $responseElement, $parameters); |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
private function _postForm($url, $form, $responseElement, $parameters=[]) { |
|
511
|
|
|
if(isset($this->params["ajax"])){ |
|
512
|
|
|
extract($this->params["ajax"]); |
|
513
|
|
|
} |
|
514
|
|
|
$params="{}";$validation=false; |
|
515
|
|
|
\extract($parameters); |
|
516
|
|
|
$async=($async)?"true":"false"; |
|
517
|
|
|
$jsCallback=isset($jsCallback) ? $jsCallback : ""; |
|
518
|
|
|
$retour=$this->_getAjaxUrl($url, $attr); |
|
519
|
|
|
$retour.="\n$('#".$form."').trigger('ajaxSubmit');"; |
|
520
|
|
|
$retour.="\nvar params=$('#".$form."').serialize();\n"; |
|
521
|
|
|
if(isset($params)){ |
|
522
|
|
|
$retour.="params+='&'+".self::_correctParams($params).";\n"; |
|
523
|
|
|
} |
|
524
|
|
|
$responseElement=$this->_getResponseElement($responseElement); |
|
525
|
|
|
$retour.="var self=this;\n"; |
|
526
|
|
|
if($hasLoader===true){ |
|
527
|
|
|
$this->addLoading($retour, $responseElement,$ajaxLoader); |
|
528
|
|
|
}elseif($hasLoader==="internal"){ |
|
529
|
|
|
$retour.="\n$(this).addClass('loading');"; |
|
530
|
|
|
} |
|
531
|
|
|
$ajaxParameters=["url"=>"url","method"=>"'POST'","data"=>"params","async"=>$async]; |
|
532
|
|
|
if(isset($headers)){ |
|
533
|
|
|
$ajaxParameters["headers"]=$headers; |
|
534
|
|
|
} |
|
535
|
|
|
$this->createAjaxParameters($ajaxParameters, $parameters); |
|
536
|
|
|
$retour.="$.ajax({".$this->implodeAjaxParameters($ajaxParameters)."}).done(function( data ) {\n"; |
|
537
|
|
|
$retour.=$this->_getOnAjaxDone($responseElement, $jqueryDone,$ajaxTransition,$jsCallback,$hasLoader)."});\n"; |
|
538
|
|
|
|
|
539
|
|
|
if ($validation) { |
|
540
|
|
|
$retour="$('#".$form."').validate({submitHandler: function(form) { |
|
541
|
|
|
".$retour." |
|
542
|
|
|
}});\n"; |
|
543
|
|
|
$retour.="$('#".$form."').submit();\n"; |
|
544
|
|
|
} |
|
545
|
|
|
$retour=$this->_addJsCondition($jsCondition, $retour); |
|
546
|
|
|
if ($immediatly) |
|
547
|
|
|
$this->jquery_code_for_compile[]=$retour; |
|
548
|
|
|
return $retour; |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
/** |
|
552
|
|
|
* Performs a post form with ajax |
|
553
|
|
|
* @param string $url The url of the request |
|
554
|
|
|
* @param string $form The form HTML id |
|
555
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
556
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
557
|
|
|
*/ |
|
558
|
|
|
public function postForm($url, $form, $responseElement, $parameters=[]) { |
|
559
|
|
|
$parameters["immediatly"]=true; |
|
560
|
|
|
return $this->_postForm($url, $form, $responseElement, $parameters); |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
/** |
|
564
|
|
|
* Performs a delayed post form with ajax |
|
565
|
|
|
* For use on an event |
|
566
|
|
|
* @param string $url The url of the request |
|
567
|
|
|
* @param string $form The form HTML id |
|
568
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
569
|
|
|
* @param array $parameters default : array("params"=>"{}","jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxLoader"=>null,"jqueryDone"=>"html","ajaxTransition"=>null,"jsCondition"=>NULL,"headers"=>null,"historize"=>false) |
|
570
|
|
|
*/ |
|
571
|
|
|
public function postFormDeferred($url, $form, $responseElement, $parameters=[]) { |
|
572
|
|
|
$parameters["immediatly"]=false; |
|
573
|
|
|
return $this->_postForm($url, $form, $responseElement, $parameters); |
|
574
|
|
|
} |
|
575
|
|
|
|
|
576
|
|
|
/** |
|
577
|
|
|
* Performs a post form with ajax in response to an event $event on $element |
|
578
|
|
|
* display the result in $responseElement |
|
579
|
|
|
* @param string $event |
|
580
|
|
|
* @param string $element |
|
581
|
|
|
* @param string $url |
|
582
|
|
|
* @param string $form |
|
583
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
584
|
|
|
* @param array $parameters 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) |
|
585
|
|
|
*/ |
|
586
|
|
|
public function postFormOn($event, $element, $url, $form, $responseElement="", $parameters=array()) { |
|
587
|
|
|
$this->setDefaultParameters($parameters, ["preventDefault"=>true,"stopPropagation"=>true,"immediatly"=>true]); |
|
588
|
|
|
return $this->_add_event($element, $this->postFormDeferred($url, $form, $responseElement,$parameters), $event, $parameters["preventDefault"], $parameters["stopPropagation"],$parameters["immediatly"]); |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* Performs a post form with ajax in response to the click event on $element |
|
593
|
|
|
* display the result in $responseElement |
|
594
|
|
|
* @param string $element |
|
595
|
|
|
* @param string $url |
|
596
|
|
|
* @param string $form |
|
597
|
|
|
* @param string $responseElement selector of the HTML element displaying the answer |
|
598
|
|
|
* @param array $parameters 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) |
|
599
|
|
|
*/ |
|
600
|
|
|
public function postFormOnClick($element, $url, $form, $responseElement="", $parameters=array()) { |
|
601
|
|
|
return $this->postFormOn("click", $element, $url, $form, $responseElement, $parameters); |
|
602
|
|
|
} |
|
603
|
|
|
} |
|
604
|
|
|
|