@@ -44,6 +44,8 @@ protected function _ajax($method, $url, $responseElement = '', $parameters = [])
44
44
$ this ->addLoading ($ retour , '$(this).closest(".item, .step") ' , $ ajaxLoader );
45
45
} elseif ($ hasLoader === 'internal ' ) {
46
46
$ retour .= "\n$(this).addClass('loading'); " ;
47
+ } elseif (\is_string ($ hasLoader )) {
48
+ $ this ->addLoading ($ retour , $ hasLoader , $ ajaxLoader );
47
49
}
48
50
$ ajaxParameters = [
49
51
"url " => "url " ,
@@ -170,6 +172,8 @@ protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition
170
172
$ retour .= "\n$(self).removeClass('loading'); " ;
171
173
} elseif ($ hasLoader === 'internal-x ' ) {
172
174
$ retour .= "\n$(self).children('.ajax-loader').remove(); " ;
175
+ } else {
176
+ $ retour .= "\n$(self).find('.loading').removeClass('loading'); " ;
173
177
}
174
178
$ retour .= "\t" . $ jsCallback . "\n" ;
175
179
return $ retour ;
@@ -182,6 +186,13 @@ protected function _getResponseElement($responseElement) {
182
186
return $ responseElement ;
183
187
}
184
188
189
+ protected function _getFormElement ($ formElement ) {
190
+ if (JString::isNotNull ($ formElement )) {
191
+ $ formElement = Javascript::prep_value ($ formElement );
192
+ }
193
+ return $ formElement ;
194
+ }
195
+
185
196
protected function _correctAjaxUrl ($ url ) {
186
197
if ($ url !== "/ " && JString::endsWith ($ url , "/ " ) === true )
187
198
$ url = substr ($ url , 0 , strlen ($ url ) - 1 );
@@ -646,31 +657,29 @@ public function postHref($element, $responseElement = "", $parameters = array())
646
657
* Uses a form action to make an ajax post request
647
658
*
648
659
* @param string $element
649
- * a form submit selector
650
- * @param string $formId
651
- * the form identifier
660
+ * a form selector
652
661
* @param string $responseElement
653
662
* the target of the ajax request (data-target attribute of the element is used if responseElement is omited)
654
663
* @param array $parameters
655
664
* 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,"listenerOn"=>false)
656
665
* @return $this
657
666
*/
658
- public function postFormAction ($ element , $ formId = "" , $ responseElement = "" , $ parameters = array ()) {
667
+ public function postFormAction ($ element , $ responseElement = "" , $ parameters = array ()) {
659
668
$ parameters ['attr ' ] = 'action ' ;
660
669
if (JString::isNull ($ responseElement )) {
661
- $ responseElement = '%$(self).closest("form").attr("data-target")% ' ;
662
- } else {
663
- $ responseElement = '%$(self).closest("form").attr("data-target") || " ' . $ responseElement . '"% ' ;
664
- }
665
- if (JString::isNull ($ formId )) {
666
- $ formId = '%$(self).closest("form").attr("id")% ' ;
670
+ $ responseElement = '%$(self).attr("data-target")% ' ;
667
671
} else {
668
- $ formId = '%$(self).closest("form"). attr("id ") || " ' . $ formId . '"% ' ;
672
+ $ responseElement = '%$(self).attr("data-target ") || " ' . $ responseElement . '"% ' ;
669
673
}
674
+ $ formId = '%$(this).attr("id")% ' ;
670
675
if (! isset ($ parameters ['historize ' ])) {
671
676
$ parameters ['historize ' ] = true ;
672
677
}
673
- return $ this ->postFormOnClick ($ element , '' , $ formId , $ responseElement , $ parameters );
678
+ $ parameters ['preventDefault ' ] = true ;
679
+ if (! isset ($ parameters ['hasLoader ' ])) {
680
+ $ parameters ['hasLoader ' ] = '$(self).find("button, input[type=submit], input[type=button]") ' ;
681
+ }
682
+ return $ this ->postFormOn ('submit ' , $ element , '' , $ formId , $ responseElement , $ parameters );
674
683
}
675
684
676
685
private function _post ($ url , $ params = '{} ' , $ responseElement = '' , $ parameters = []) {
@@ -762,14 +771,15 @@ private function _postForm($url, $form, $responseElement, $parameters = []) {
762
771
$ async = ($ async ) ? 'true ' : 'false ' ;
763
772
$ jsCallback = isset ($ jsCallback ) ? $ jsCallback : "" ;
764
773
$ retour = $ this ->_getAjaxUrl ($ url , $ attr );
765
- $ retour .= "\n$('# " . $ form . "').trigger('ajaxSubmit'); " ;
774
+ $ form = $ this ->_getFormElement ($ form );
775
+ $ retour .= "\n$('#'+ " . $ form . ").trigger('ajaxSubmit'); " ;
766
776
if (! isset ($ contentType ) || $ contentType != 'false ' ) {
767
- $ retour .= "\nvar params=$('# " . $ form . "' ).serialize(); \n" ;
777
+ $ retour .= "\nvar params=$('#'+ " . $ form . ").serialize(); \n" ;
768
778
if (isset ($ params )) {
769
779
$ retour .= "params+='&'+ " . self ::_correctParams ($ params ) . "; \n" ;
770
780
}
771
781
} else {
772
- $ retour .= "\nvar params=new FormData($('# " . $ form . "' )[0]); \n" ;
782
+ $ retour .= "\nvar params=new FormData($('#'+ " . $ form . ")[0]); \n" ;
773
783
}
774
784
$ responseElement = $ this ->_getResponseElement ($ responseElement );
775
785
$ retour .= "var self=this; \n" ;
@@ -779,8 +789,12 @@ private function _postForm($url, $form, $responseElement, $parameters = []) {
779
789
$ this ->addLoading ($ retour , $ responseElement , $ ajaxLoader );
780
790
} elseif ($ hasLoader === 'response ' ) {
781
791
$ this ->addResponseLoading ($ retour , $ responseElement , $ ajaxLoader );
792
+ } elseif ($ hasLoader === 'internal-x ' ) {
793
+ $ this ->addLoading ($ retour , '$(this).closest(".item, .step") ' , $ ajaxLoader );
782
794
} elseif ($ hasLoader === 'internal ' ) {
783
795
$ retour .= "\n$(this).addClass('loading'); " ;
796
+ } elseif (\is_string ($ hasLoader )) {
797
+ $ retour .= "\n$ hasLoader.addClass('loading'); " ;
784
798
}
785
799
$ ajaxParameters = [
786
800
"url " => "url " ,
@@ -800,10 +814,10 @@ private function _postForm($url, $form, $responseElement, $parameters = []) {
800
814
$ retour .= $ this ->_getOnAjaxDone ($ responseElement , $ jqueryDone , $ ajaxTransition , $ jsCallback , $ hasLoader ) . "}); \n" ;
801
815
802
816
if ($ validation ) {
803
- $ retour = "$('# " . $ form . "' ).validate({submitHandler: function(form) {
817
+ $ retour = "$('#'+ " . $ form . ").validate({submitHandler: function(form) {
804
818
" . $ retour . "
805
819
}}); \n" ;
806
- $ retour .= "$('# " . $ form . "' ).submit(); \n" ;
820
+ $ retour .= "$('#'+ " . $ form . ").submit(); \n" ;
807
821
}
808
822
$ retour = $ this ->_addJsCondition ($ jsCondition , $ retour );
809
823
if ($ immediatly )
0 commit comments