@@ -56,8 +56,12 @@ async function fetchActionDoRequest(actionElem: HTMLElement, url: string, opt: R
5656 actionElem . classList . remove ( 'is-loading' , 'loading-icon-2px' ) ;
5757}
5858
59- async function formFetchAction ( formEl : HTMLFormElement , e : SubmitEvent ) {
59+ async function onFormFetchActionSubmit ( formEl : HTMLFormElement , e : SubmitEvent ) {
6060 e . preventDefault ( ) ;
61+ await submitFormFetchAction ( formEl , submitEventSubmitter ( e ) ) ;
62+ }
63+
64+ export async function submitFormFetchAction ( formEl : HTMLFormElement , formSubmitter ?: HTMLElement ) {
6165 if ( formEl . classList . contains ( 'is-loading' ) ) return ;
6266
6367 formEl . classList . add ( 'is-loading' ) ;
@@ -68,7 +72,6 @@ async function formFetchAction(formEl: HTMLFormElement, e: SubmitEvent) {
6872 const formMethod = formEl . getAttribute ( 'method' ) || 'get' ;
6973 const formActionUrl = formEl . getAttribute ( 'action' ) ;
7074 const formData = new FormData ( formEl ) ;
71- const formSubmitter = submitEventSubmitter ( e ) ;
7275 const [ submitterName , submitterValue ] = [ formSubmitter ?. getAttribute ( 'name' ) , formSubmitter ?. getAttribute ( 'value' ) ] ;
7376 if ( submitterName ) {
7477 formData . append ( submitterName , submitterValue || '' ) ;
@@ -96,7 +99,7 @@ async function formFetchAction(formEl: HTMLFormElement, e: SubmitEvent) {
9699 await fetchActionDoRequest ( formEl , reqUrl , reqOpt ) ;
97100}
98101
99- async function linkAction ( el : HTMLElement , e : Event ) {
102+ async function onLinkActionClick ( el : HTMLElement , e : Event ) {
100103 // A "link-action" can post AJAX request to its "data-url"
101104 // Then the browser is redirected to: the "redirect" in response, or "data-redirect" attribute, or current URL by reloading.
102105 // If the "link-action" has "data-modal-confirm" attribute, a confirm modal dialog will be shown before taking action.
@@ -126,6 +129,6 @@ async function linkAction(el: HTMLElement, e: Event) {
126129}
127130
128131export function initGlobalFetchAction ( ) {
129- addDelegatedEventListener ( document , 'submit' , '.form-fetch-action' , formFetchAction ) ;
130- addDelegatedEventListener ( document , 'click' , '.link-action' , linkAction ) ;
132+ addDelegatedEventListener ( document , 'submit' , '.form-fetch-action' , onFormFetchActionSubmit ) ;
133+ addDelegatedEventListener ( document , 'click' , '.link-action' , onLinkActionClick ) ;
131134}
0 commit comments