@@ -56,8 +56,12 @@ async function fetchActionDoRequest(actionElem: HTMLElement, url: string, opt: R
56
56
actionElem . classList . remove ( 'is-loading' , 'loading-icon-2px' ) ;
57
57
}
58
58
59
- async function formFetchAction ( formEl : HTMLFormElement , e : SubmitEvent ) {
59
+ async function onFormFetchActionSubmit ( formEl : HTMLFormElement , e : SubmitEvent ) {
60
60
e . preventDefault ( ) ;
61
+ await submitFormFetchAction ( formEl , submitEventSubmitter ( e ) ) ;
62
+ }
63
+
64
+ export async function submitFormFetchAction ( formEl : HTMLFormElement , formSubmitter ?: HTMLElement ) {
61
65
if ( formEl . classList . contains ( 'is-loading' ) ) return ;
62
66
63
67
formEl . classList . add ( 'is-loading' ) ;
@@ -68,7 +72,6 @@ async function formFetchAction(formEl: HTMLFormElement, e: SubmitEvent) {
68
72
const formMethod = formEl . getAttribute ( 'method' ) || 'get' ;
69
73
const formActionUrl = formEl . getAttribute ( 'action' ) ;
70
74
const formData = new FormData ( formEl ) ;
71
- const formSubmitter = submitEventSubmitter ( e ) ;
72
75
const [ submitterName , submitterValue ] = [ formSubmitter ?. getAttribute ( 'name' ) , formSubmitter ?. getAttribute ( 'value' ) ] ;
73
76
if ( submitterName ) {
74
77
formData . append ( submitterName , submitterValue || '' ) ;
@@ -96,7 +99,7 @@ async function formFetchAction(formEl: HTMLFormElement, e: SubmitEvent) {
96
99
await fetchActionDoRequest ( formEl , reqUrl , reqOpt ) ;
97
100
}
98
101
99
- async function linkAction ( el : HTMLElement , e : Event ) {
102
+ async function onLinkActionClick ( el : HTMLElement , e : Event ) {
100
103
// A "link-action" can post AJAX request to its "data-url"
101
104
// Then the browser is redirected to: the "redirect" in response, or "data-redirect" attribute, or current URL by reloading.
102
105
// 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) {
126
129
}
127
130
128
131
export 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 ) ;
131
134
}
0 commit comments