@@ -44,6 +44,8 @@ protected function _ajax($method, $url, $responseElement = '', $parameters = [])
4444 $ this ->addLoading ($ retour , '$(this).closest(".item, .step") ' , $ ajaxLoader );
4545 } elseif ($ hasLoader === 'internal ' ) {
4646 $ retour .= "\n$(this).addClass('loading'); " ;
47+ } elseif (\is_string ($ hasLoader )) {
48+ $ this ->addLoading ($ retour , $ hasLoader , $ ajaxLoader );
4749 }
4850 $ ajaxParameters = [
4951 "url " => "url " ,
@@ -170,6 +172,8 @@ protected function _getOnAjaxDone($responseElement, $jqueryDone, $ajaxTransition
170172 $ retour .= "\n$(self).removeClass('loading'); " ;
171173 } elseif ($ hasLoader === 'internal-x ' ) {
172174 $ retour .= "\n$(self).children('.ajax-loader').remove(); " ;
175+ } else {
176+ $ retour .= "\n$(self).find('.loading').removeClass('loading'); " ;
173177 }
174178 $ retour .= "\t" . $ jsCallback . "\n" ;
175179 return $ retour ;
@@ -182,6 +186,13 @@ protected function _getResponseElement($responseElement) {
182186 return $ responseElement ;
183187 }
184188
189+ protected function _getFormElement ($ formElement ) {
190+ if (JString::isNotNull ($ formElement )) {
191+ $ formElement = Javascript::prep_value ($ formElement );
192+ }
193+ return $ formElement ;
194+ }
195+
185196 protected function _correctAjaxUrl ($ url ) {
186197 if ($ url !== "/ " && JString::endsWith ($ url , "/ " ) === true )
187198 $ url = substr ($ url , 0 , strlen ($ url ) - 1 );
@@ -646,31 +657,29 @@ public function postHref($element, $responseElement = "", $parameters = array())
646657 * Uses a form action to make an ajax post request
647658 *
648659 * @param string $element
649- * a form submit selector
650- * @param string $formId
651- * the form identifier
660+ * a form selector
652661 * @param string $responseElement
653662 * the target of the ajax request (data-target attribute of the element is used if responseElement is omited)
654663 * @param array $parameters
655664 * 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)
656665 * @return $this
657666 */
658- public function postFormAction ($ element , $ formId = "" , $ responseElement = "" , $ parameters = array ()) {
667+ public function postFormAction ($ element , $ responseElement = "" , $ parameters = array ()) {
659668 $ parameters ['attr ' ] = 'action ' ;
660669 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")% ' ;
667671 } else {
668- $ formId = '%$(self).closest("form"). attr("id ") || " ' . $ formId . '"% ' ;
672+ $ responseElement = '%$(self).attr("data-target ") || " ' . $ responseElement . '"% ' ;
669673 }
674+ $ formId = '%$(this).attr("id")% ' ;
670675 if (! isset ($ parameters ['historize ' ])) {
671676 $ parameters ['historize ' ] = true ;
672677 }
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 );
674683 }
675684
676685 private function _post ($ url , $ params = '{} ' , $ responseElement = '' , $ parameters = []) {
@@ -762,14 +771,15 @@ private function _postForm($url, $form, $responseElement, $parameters = []) {
762771 $ async = ($ async ) ? 'true ' : 'false ' ;
763772 $ jsCallback = isset ($ jsCallback ) ? $ jsCallback : "" ;
764773 $ retour = $ this ->_getAjaxUrl ($ url , $ attr );
765- $ retour .= "\n$('# " . $ form . "').trigger('ajaxSubmit'); " ;
774+ $ form = $ this ->_getFormElement ($ form );
775+ $ retour .= "\n$('#'+ " . $ form . ").trigger('ajaxSubmit'); " ;
766776 if (! isset ($ contentType ) || $ contentType != 'false ' ) {
767- $ retour .= "\nvar params=$('# " . $ form . "' ).serialize(); \n" ;
777+ $ retour .= "\nvar params=$('#'+ " . $ form . ").serialize(); \n" ;
768778 if (isset ($ params )) {
769779 $ retour .= "params+='&'+ " . self ::_correctParams ($ params ) . "; \n" ;
770780 }
771781 } else {
772- $ retour .= "\nvar params=new FormData($('# " . $ form . "' )[0]); \n" ;
782+ $ retour .= "\nvar params=new FormData($('#'+ " . $ form . ")[0]); \n" ;
773783 }
774784 $ responseElement = $ this ->_getResponseElement ($ responseElement );
775785 $ retour .= "var self=this; \n" ;
@@ -779,8 +789,12 @@ private function _postForm($url, $form, $responseElement, $parameters = []) {
779789 $ this ->addLoading ($ retour , $ responseElement , $ ajaxLoader );
780790 } elseif ($ hasLoader === 'response ' ) {
781791 $ this ->addResponseLoading ($ retour , $ responseElement , $ ajaxLoader );
792+ } elseif ($ hasLoader === 'internal-x ' ) {
793+ $ this ->addLoading ($ retour , '$(this).closest(".item, .step") ' , $ ajaxLoader );
782794 } elseif ($ hasLoader === 'internal ' ) {
783795 $ retour .= "\n$(this).addClass('loading'); " ;
796+ } elseif (\is_string ($ hasLoader )) {
797+ $ retour .= "\n$ hasLoader.addClass('loading'); " ;
784798 }
785799 $ ajaxParameters = [
786800 "url " => "url " ,
@@ -800,10 +814,10 @@ private function _postForm($url, $form, $responseElement, $parameters = []) {
800814 $ retour .= $ this ->_getOnAjaxDone ($ responseElement , $ jqueryDone , $ ajaxTransition , $ jsCallback , $ hasLoader ) . "}); \n" ;
801815
802816 if ($ validation ) {
803- $ retour = "$('# " . $ form . "' ).validate({submitHandler: function(form) {
817+ $ retour = "$('#'+ " . $ form . ").validate({submitHandler: function(form) {
804818 " . $ retour . "
805819 }}); \n" ;
806- $ retour .= "$('# " . $ form . "' ).submit(); \n" ;
820+ $ retour .= "$('#'+ " . $ form . ").submit(); \n" ;
807821 }
808822 $ retour = $ this ->_addJsCondition ($ jsCondition , $ retour );
809823 if ($ immediatly )
0 commit comments