44
55use Ajax \service \JString ;
66use Ajax \service \Javascript ;
7- use Ajax \service \JQueryAjaxEffect ;
7+ use Ajax \service \AjaxTransition ;
88
99
1010/**
1414 * @property array $params
1515 */
1616trait JqueryAjaxTrait {
17- protected $ ajaxDataCall ;
17+ protected $ ajaxTransition ;
1818 protected $ ajaxLoader ='<span></span><span></span><span></span><span></span><span></span> ' ;
1919
2020 abstract public function _add_event ($ element , $ js , $ event , $ preventDefault =false , $ stopPropagation =false ,$ immediatly =true );
@@ -30,14 +30,14 @@ protected function addLoading(&$retour, $responseElement) {
3030 $ retour .="\t\t$( {$ responseElement }).prepend(' {$ loading_notifier }'); \n" ;
3131 }
3232
33- public function _get ($ url , $ params ="{} " , $ responseElement ="" , $ jsCallback =NULL , $ attr ="id " , $ hasLoader =true ,$ jqueryDone ="html " ,$ immediatly =false ) {
34- return $ this ->_ajax ("get " , $ url ,$ params ,$ responseElement ,$ jsCallback ,$ attr ,$ hasLoader ,$ jqueryDone ,$ immediatly );
33+ public function _get ($ url , $ params ="{} " , $ responseElement ="" , $ jsCallback =NULL , $ attr ="id " , $ hasLoader =true ,$ jqueryDone ="html " ,$ ajaxTransition = null , $ immediatly =false ) {
34+ return $ this ->_ajax ("get " , $ url ,$ params ,$ responseElement ,$ jsCallback ,$ attr ,$ hasLoader ,$ jqueryDone ,$ ajaxTransition , $ immediatly );
3535 }
36- public function _post ($ url , $ params ="{} " , $ responseElement ="" , $ jsCallback =NULL , $ attr ="id " , $ hasLoader =true ,$ jqueryDone ="html " ,$ immediatly =false ) {
37- return $ this ->_ajax ("post " , $ url ,$ params ,$ responseElement ,$ jsCallback ,$ attr ,$ hasLoader ,$ jqueryDone ,$ immediatly );
36+ public function _post ($ url , $ params ="{} " , $ responseElement ="" , $ jsCallback =NULL , $ attr ="id " , $ hasLoader =true ,$ jqueryDone ="html " ,$ ajaxTransition = null , $ immediatly =false ) {
37+ return $ this ->_ajax ("post " , $ url ,$ params ,$ responseElement ,$ jsCallback ,$ attr ,$ hasLoader ,$ jqueryDone ,$ ajaxTransition , $ immediatly );
3838 }
3939
40- protected function _ajax ($ method ,$ url , $ params ="{} " , $ responseElement ="" , $ jsCallback =NULL , $ attr ="id " , $ hasLoader =true ,$ jqueryDone ="html " ,$ immediatly =false ) {
40+ protected function _ajax ($ method ,$ url , $ params ="{} " , $ responseElement ="" , $ jsCallback =NULL , $ attr ="id " , $ hasLoader =true ,$ jqueryDone ="html " ,$ ajaxTransition = null , $ immediatly =false ) {
4141 if (JString::isNull ($ params )){$ params ="{} " ;}
4242 $ jsCallback =isset ($ jsCallback ) ? $ jsCallback : "" ;
4343 $ retour =$ this ->_getAjaxUrl ($ url , $ attr );
@@ -47,16 +47,20 @@ protected function _ajax($method,$url, $params="{}", $responseElement="", $jsCal
4747 $ this ->addLoading ($ retour , $ responseElement );
4848 }
4949 $ retour .="$. " .$ method ."(url, " .$ params .").done(function( data ) { \n" ;
50- $ retour .=$ this ->_getOnAjaxDone ($ responseElement , $ jqueryDone ,$ jsCallback )."}); \n" ;
50+ $ retour .=$ this ->_getOnAjaxDone ($ responseElement , $ jqueryDone ,$ ajaxTransition , $ jsCallback )."}); \n" ;
5151 if ($ immediatly )
5252 $ this ->jquery_code_for_compile []=$ retour ;
5353 return $ retour ;
5454 }
5555
56- protected function setAjaxDataCall (){
57- $ this ->ajaxDataCall =function ($ responseElement ,$ jqueryDone ="html " ){
58- return JQueryAjaxEffect::{$ this ->params ["ajaxEffect " ]}($ responseElement ,$ jqueryDone );
59- };
56+ protected function setAjaxDataCall ($ params ){
57+ $ result =null ;
58+ if (!\is_callable ($ params ) && \method_exists ("Ajax\service\AjaxTransition " ,$ params )){
59+ $ result =function ($ responseElement ,$ jqueryDone ="html " ) use ($ params ){
60+ return AjaxTransition::{$ params }($ responseElement ,$ jqueryDone );
61+ };
62+ }
63+ return $ result ;
6064 }
6165
6266 protected function _getAjaxUrl ($ url ,$ attr ){
@@ -76,10 +80,18 @@ protected function _getAjaxUrl($url,$attr){
7680 return $ retour ;
7781 }
7882
79- protected function _getOnAjaxDone ($ responseElement ,$ jqueryDone ,$ jsCallback ){
80- $ retour ="" ;$ call =$ this -> ajaxDataCall ;
83+ protected function _getOnAjaxDone ($ responseElement ,$ jqueryDone ,$ ajaxTransition , $ jsCallback ){
84+ $ retour ="" ;$ call =null ;
8185 if ($ responseElement !=="" ) {
82- $ retour =$ call ($ responseElement ,$ jqueryDone );
86+ if (isset ($ ajaxTransition )){
87+ $ call =$ this ->setAjaxDataCall ($ ajaxTransition );
88+ }elseif (isset ($ this ->ajaxTransition )){
89+ $ call =$ this ->ajaxTransition ;
90+ }
91+ if (\is_callable ($ call ))
92+ $ retour ="\t" .$ call ($ responseElement ,$ jqueryDone )."; \n" ;
93+ else
94+ $ retour ="\t$( {$ responseElement }). {$ jqueryDone }( data ); \n" ;
8395 }
8496 $ retour .="\t" .$ jsCallback ."\n" ;
8597 return $ retour ;
@@ -192,7 +204,7 @@ public function _jsonArrayOn($event,$element, $maskSelector,$url,$parameters=arr
192204 return $ this ->_add_event ($ element , $ this ->_jsonArray ($ maskSelector ,$ url ,$ method , $ params ,$ jsCallback , $ attr , $ context ), $ event , $ preventDefault , $ stopPropagation ,$ immediatly );
193205 }
194206
195- public function _postForm ($ url , $ form , $ responseElement , $ validation =false , $ jsCallback =NULL , $ attr ="id " , $ hasLoader =true ,$ jqueryDone ="html " ,$ immediatly =false ) {
207+ public function _postForm ($ url , $ form , $ responseElement , $ validation =false , $ jsCallback =NULL , $ attr ="id " , $ hasLoader =true ,$ jqueryDone ="html " ,$ ajaxTransition = null , $ immediatly =false ) {
196208 $ jsCallback =isset ($ jsCallback ) ? $ jsCallback : "" ;
197209 $ retour =$ this ->_getAjaxUrl ($ url , $ attr );
198210 $ retour .="\nvar params=$('# " .$ form ."').serialize(); \n" ;
@@ -202,7 +214,7 @@ public function _postForm($url, $form, $responseElement, $validation=false, $jsC
202214 $ this ->addLoading ($ retour , $ responseElement );
203215 }
204216 $ retour .="$.post(url,params).done(function( data ) { \n" ;
205- $ retour .=$ this ->_getOnAjaxDone ($ responseElement , $ jqueryDone ,$ jsCallback )."}); \n" ;
217+ $ retour .=$ this ->_getOnAjaxDone ($ responseElement , $ jqueryDone ,$ ajaxTransition , $ jsCallback )."}); \n" ;
206218
207219 if ($ validation ) {
208220 $ retour ="$('# " .$ form ."').validate({submitHandler: function(form) {
@@ -223,7 +235,7 @@ public function _postForm($url, $form, $responseElement, $validation=false, $jsC
223235 * @param string $url
224236 * @param string $params queryString parameters (JSON format). default : {}
225237 * @param string $responseElement
226- * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"jqueryDone"=>"html")
238+ * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxTransition"=>null," jqueryDone"=>"html")
227239 */
228240 public function _getOn ($ event ,$ element , $ url , $ params ="{} " , $ responseElement ="" , $ parameters =array ()) {
229241 $ preventDefault =true ;
@@ -233,8 +245,9 @@ public function _getOn($event,$element, $url, $params="{}", $responseElement="",
233245 $ hasLoader =true ;
234246 $ immediatly =true ;
235247 $ jqueryDone ="html " ;
248+ $ ajaxTransition =null ;
236249 extract ($ parameters );
237- return $ this ->_add_event ($ element , $ this ->_get ($ url , $ params , $ responseElement , $ jsCallback , $ attr ,$ hasLoader ,$ jqueryDone ), $ event , $ preventDefault , $ stopPropagation ,$ immediatly );
250+ return $ this ->_add_event ($ element , $ this ->_get ($ url , $ params , $ responseElement , $ jsCallback , $ attr ,$ hasLoader ,$ jqueryDone, $ ajaxTransition ), $ event , $ preventDefault , $ stopPropagation ,$ immediatly );
238251 }
239252
240253 /**
@@ -245,7 +258,7 @@ public function _getOn($event,$element, $url, $params="{}", $responseElement="",
245258 * @param string $url
246259 * @param string $params queryString parameters (JSON format). default : {}
247260 * @param string $responseElement
248- * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true)
261+ * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxTransition"=>null )
249262 */
250263 public function _postOn ($ event ,$ element , $ url , $ params ="{} " , $ responseElement ="" , $ parameters =array ()) {
251264 $ preventDefault =true ;
@@ -255,8 +268,9 @@ public function _postOn($event,$element, $url, $params="{}", $responseElement=""
255268 $ hasLoader =true ;
256269 $ immediatly =true ;
257270 $ jqueryDone ="html " ;
271+ $ ajaxTransition =null ;
258272 extract ($ parameters );
259- return $ this ->_add_event ($ element , $ this ->_post ($ url , $ params , $ responseElement , $ jsCallback , $ attr ,$ hasLoader ,$ jqueryDone ), $ event , $ preventDefault , $ stopPropagation ,$ immediatly );
273+ return $ this ->_add_event ($ element , $ this ->_post ($ url , $ params , $ responseElement , $ jsCallback , $ attr ,$ hasLoader ,$ jqueryDone, $ ajaxTransition ), $ event , $ preventDefault , $ stopPropagation ,$ immediatly );
260274 }
261275
262276 /**
@@ -267,7 +281,7 @@ public function _postOn($event,$element, $url, $params="{}", $responseElement=""
267281 * @param string $url
268282 * @param string $form
269283 * @param string $responseElement
270- * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"immediatly"=>true)
284+ * @param array $parameters default : array("preventDefault"=>true,"stopPropagation"=>true,"validation"=>false,"jsCallback"=>NULL,"attr"=>"id","hasLoader"=>true,"ajaxTransition"=>null," immediatly"=>true)
271285 */
272286 public function _postFormOn ($ event ,$ element , $ url , $ form , $ responseElement ="" , $ parameters =array ()) {
273287 $ preventDefault =true ;
@@ -278,7 +292,8 @@ public function _postFormOn($event,$element, $url, $form, $responseElement="", $
278292 $ hasLoader =true ;
279293 $ immediatly =true ;
280294 $ jqueryDone ="html " ;
295+ $ ajaxTransition =null ;
281296 extract ($ parameters );
282- return $ this ->_add_event ($ element , $ this ->_postForm ($ url , $ form , $ responseElement , $ validation , $ jsCallback , $ attr ,$ hasLoader ,$ jqueryDone ), $ event , $ preventDefault , $ stopPropagation ,$ immediatly );
297+ return $ this ->_add_event ($ element , $ this ->_postForm ($ url , $ form , $ responseElement , $ validation , $ jsCallback , $ attr ,$ hasLoader ,$ jqueryDone, $ ajaxTransition ), $ event , $ preventDefault , $ stopPropagation ,$ immediatly );
283298 }
284299}
0 commit comments