AjaxSubmit.js
2 years ago
BaseSubmit.js
2 years ago
FormSubmit.js
2 years ago
ReloadSubmit.js
2 years ago
functions.js
2 years ago
FormSubmit.js
109 lines
| 1 | import LoadingReactiveVar from '../reactive/LoadingReactiveVar'; |
| 2 | import AjaxSubmit from './AjaxSubmit'; |
| 3 | import ReloadSubmit from './ReloadSubmit'; |
| 4 | import { focusOnInvalidInput } from '../functions'; |
| 5 | import { populateInputs } from '../inputs/functions'; |
| 6 | |
| 7 | /** |
| 8 | * @param observable {Observable} |
| 9 | * @constructor |
| 10 | */ |
| 11 | function FormSubmit( observable ) { |
| 12 | |
| 13 | this.observable = observable; |
| 14 | this.lockState = new LoadingReactiveVar( false ); |
| 15 | this.lockState.make(); |
| 16 | this.autoFocus = window.JetFormBuilderSettings?.auto_focus; |
| 17 | |
| 18 | /** |
| 19 | * @param event {Event} |
| 20 | */ |
| 21 | this.onSubmit = function ( event ) { |
| 22 | event.preventDefault(); |
| 23 | |
| 24 | this.submit(); |
| 25 | }; |
| 26 | |
| 27 | this.submit = function () { |
| 28 | this.observable.inputsAreValid().then( () => { |
| 29 | this.clearErrors(); |
| 30 | this.toggle(); |
| 31 | |
| 32 | this.submitter.submit(); |
| 33 | } ).catch( () => { |
| 34 | this.autoFocus && focusOnInvalidInput( |
| 35 | populateInputs( this.observable.getInputs() ), |
| 36 | ); |
| 37 | } ); |
| 38 | }; |
| 39 | |
| 40 | this.clearErrors = function () { |
| 41 | const messages = this.observable.rootNode.querySelectorAll( |
| 42 | '.jet-form-builder-messages-wrap', |
| 43 | ); |
| 44 | |
| 45 | for ( const message of messages ) { |
| 46 | message.remove(); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | this.toggle = function () { |
| 51 | this.lockState.toggle(); |
| 52 | this.toggleLoading(); |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * @private |
| 57 | */ |
| 58 | this.handleButtons = function () { |
| 59 | /** |
| 60 | * @type {NodeListOf<Element>} |
| 61 | */ |
| 62 | const buttons = this.observable.rootNode.querySelectorAll( |
| 63 | '.jet-form-builder__submit', |
| 64 | ); |
| 65 | |
| 66 | this.lockState.watch( () => { |
| 67 | for ( const button of buttons ) { |
| 68 | button.disabled = this.lockState.current; |
| 69 | } |
| 70 | } ); |
| 71 | }; |
| 72 | |
| 73 | this.toggleLoading = function () { |
| 74 | this.observable.rootNode.classList.toggle( 'is-loading' ); |
| 75 | }; |
| 76 | |
| 77 | this.createSubmitter = function () { |
| 78 | const { classList } = this.observable.rootNode; |
| 79 | const isAjax = classList.contains( 'submit-type-ajax' ); |
| 80 | |
| 81 | return isAjax ? new AjaxSubmit( this ) : new ReloadSubmit( this ); |
| 82 | }; |
| 83 | |
| 84 | this.getFormId = function () { |
| 85 | const { rootNode } = this.observable; |
| 86 | |
| 87 | return +rootNode.dataset.formId; |
| 88 | }; |
| 89 | |
| 90 | this.onEndSubmit = function ( callable ) { |
| 91 | this.submitter.hasOwnProperty( 'status' ) |
| 92 | ? this.submitter.status.watch( callable ) |
| 93 | : this.submitter.onFailSubmit( callable ); |
| 94 | }; |
| 95 | |
| 96 | this.observable.rootNode.addEventListener( |
| 97 | 'submit', |
| 98 | ( event ) => this.onSubmit( event ), |
| 99 | ); |
| 100 | |
| 101 | /** |
| 102 | * @type {AjaxSubmit|ReloadSubmit} |
| 103 | */ |
| 104 | this.submitter = this.createSubmitter(); |
| 105 | |
| 106 | this.handleButtons(); |
| 107 | } |
| 108 | |
| 109 | export default FormSubmit; |