1( function( $ ) { 2 3 'use strict'; 4 5 // Document ready. 6 $( function() { 7 8 /** 9 * Handle submission of volunteer opportunity sign up form including 10 * validation and AJAX processing. 11 */ 12 $( '#wivm-sign-up-form input[type=submit]' ).click( function( e ) { 13 14 e.preventDefault(); 15 var $this = $( this ), 16 form_valid; 17 18 $( this ).prop( "disabled", true ); 19 20 form_valid = validate_sign_up_form(); 21 if ( form_valid === true ) { 22 23 submit_sign_up_form( $this ); 24 25 } else { // Allow submission again if there were errors. 26 27 $this.prop( "disabled", false ); 28 } 29 } ); 30 31 }); 32 33 /** 34 * Validate the volunteer opportunity sign up form. 35 * 36 * @return {bool} Whether the form is valid. 37 */ 38 function validate_sign_up_form(){ 39 40 var has_errors = false; 41 42 // Show an error and don't submit if the honeypot exists and is filled in. 43 var hp = $( '#wivm_hp' ); 44 if ( hp.length && hp.val() !== '' ) { 45 46 has_errors = true; 47 } 48 49 // Make sure each field is filled in and that email addresses are valid. 50 $( '#wivm-sign-up-form input[type=text]:not(#wivm_hp), #wivm-sign-up-form input[type=email]' ).each( function() { 51 52 if ( this.value === '' ) { 53 54 $( this ).addClass( 'field-error' ); 55 has_errors = true; 56 57 } else if ( this.type === 'email' && ! validate_email( this.value ) ) { 58 59 $( this ).addClass( 'field-error' ); 60 has_errors = true; 61 62 } else { 63 64 $( this ).removeClass( 'field-error' ); 65 } 66 } ); 67 68 // If not valid return false. 69 if ( has_errors === true ) { 70 71 $( '.volunteer-opp-message.loading, .volunteer-opp-message.success' ).slideUp(); 72 $( '.volunteer-opp-message.error' ).slideDown(); 73 74 return false; 75 76 } else { 77 78 $( '.volunteer-opp-message' ).slideUp(); 79 80 return true; 81 } 82 } 83 84 /** 85 * Validates a provided email address. 86 * 87 * @param {string} email The provided email address. 88 * @return {bool} Whether the provided email address is valid. 89 */ 90 function validate_email( email ){ 91 92 var email_regex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; 93 94 if ( email_regex.test( email ) ) { 95 96 return true; 97 98 } else { 99 100 return false; 101 } 102 } 103 104 /** 105 * Submit the sign up form for processing on the backend. 106 */ 107 function submit_sign_up_form( submit_button ){ 108 109 // Show messages to user. 110 $( '.volunteer-opp-message.error' ).slideUp(); 111 $( '.volunteer-opp-message.loading' ).slideDown(); 112 113 jQuery.post( wivm_ajax.ajaxurl, 114 { 115 action: 'wivm_sign_up', 116 data: $( '#wivm-sign-up-form' ).serialize() 117 }, 118 function( response ){ 119 120 $( '.volunteer-opp-message.loading' ).slideUp(); 121 122 if ( response === 'rsvped' ) { // If submitter was RSVPed successfully. 123 124 $( '.volunteer-opp-message.success' ).slideDown(); 125 submit_button.prop( "disabled", false ); 126 track_google_analytics( 'Success' ); 127 128 } else if ( response === 'already_rsvped') { // If submitter had already RSVPed. 129 130 $( '.volunteer-opp-message.already-rsvped' ).slideDown(); 131 submit_button.prop( "disabled", false ); 132 track_google_analytics( 'Failure: Already Signed Up' ); 133 134 } else if ( response === 'rsvp_closed' ) { // If submitter tried to sign up, but there are no spots left. 135 136 $( '.volunteer-opp-message.rsvp-closed' ).slideDown(); 137 $( '#wivm-sign-up-form' ).slideUp(); 138 track_google_analytics( 'Failure: No More Open Spots' ); 139 } 140 } 141 ); 142 } 143 144 /** 145 * Track an event within Google Analytics when volunteers sign up. 146 * 147 * @param {string} sign_up_result The result of the sign up attempt. 148 */ 149 function track_google_analytics( sign_up_result ){ 150 151 if ( typeof gtag === 'function' ) { 152 153 gtag( 'event', 'volunteer_opportunity_submit', { 154 'sign_up_result': sign_up_result, 155 } ); 156 } 157 } 158 159})( jQuery );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.