1/** 2 * frontend.js 3 * 4 * @author YITH <[email protected]> 5 * @package YITH WooCommerce Waiting List 6 * @version 1.0.0 7 */ 8 9jQuery( document ).ready( function( $ ) { 10 "use strict"; 11 12 var addUrlParameter = function( key, value, t ) { 13 key = encodeURI( key ); 14 value = encodeURI( value ); 15 16 var url_elem = t.parents( '.yith-wcwtl-output' ).find( 'a.button, form' ), 17 prop = typeof url_elem.attr( 'action' ) != 'undefined' ? 'action' : 'href', 18 url = url_elem.attr( prop ), 19 kvp = url.split( '&' ); 20 21 var i = kvp.length; 22 var x; 23 24 // if exists change it! 25 while (i--) { 26 x = kvp[i].split( '=' ); 27 if ( x[0] == key ) { 28 x[1] = value; 29 kvp[i] = x.join( '=' ); 30 break; 31 } 32 } 33 34 // if not exists add 35 if ( i < 0 ) { 36 kvp[kvp.length] = [key, value].join( '=' ); 37 } 38 url_elem.prop( prop, kvp.join( '&' ) ); 39 }, 40 getUrlParameter = function( sURL, data ) { // function to get param from url 41 var sURLVariables = sURL.split( '?' )[1].split( '&' ), 42 sParameterName, 43 i; 44 45 for (i = 0; i < sURLVariables.length; i++) { 46 sParameterName = sURLVariables[i].split( '=' ); 47 data.push( {name: sParameterName[0], value: sParameterName[1]} ); 48 } 49 50 return data; 51 }, 52 submit = function( url, button ) { 53 54 $(document).trigger('yith-wcwtl-submit'); 55 56 // build Data 57 var ajaxData = [ 58 {name: 'action', value: 'yith_wcwtl_submit'}, 59 {name: 'context', value: 'frontend'} 60 ], 61 form = button.closest( '.yith-wcwtl-output' ); 62 63 ajaxData = getUrlParameter( url, ajaxData ); 64 65 form.addClass( 'active' ); 66 67 form.block({ 68 message: null, 69 overlayCSS: { 70 background: '#fff', 71 opacity: 0.6 72 } 73 }); 74 75 76 const register = ! form.find( '.yith-wcwtl-submit' ).hasClass( 'button-leave' ); 77 78 isSubmitAllowed( register ).then(function( data ) { 79 80 if( data !== true ){ 81 ajaxData.push({name: 'gtoken', value: data}) 82 } 83 84 // Run this when your request was successful 85 $.ajax( { 86 url: woocommerce_params.ajax_url, 87 data: $.param( ajaxData ), 88 method: 'POST', 89 dataType: 'json', 90 error: function( e ) { 91 }, 92 93 success: function( res ) { 94 95 // add message 96 $( document ).find( '.yith-wcwtl-ajax-message' ).remove(); 97 98 if( 'error' === res.type ){ 99 // replace form 100 form.replaceWith( res.form ); 101 102 $( document ).find('.yith-wcwtl-notices').show().append( res.msg ); 103 104 $( document ).find( '#yith-wcwtl-user-email' ).trigger( 'input' ); 105 }else{ 106 $( document ).find( '.yith-wcwtl-output' ).addClass( 'success' ); 107 $( document ).find( '.yith-wcwtl-output' ).empty().html( res.msg ); 108 } 109 110 $( document ).find( '.yith-wcwtl-output' ).removeClass( 'active' ); 111 $( document ).find( '.yith-wcwtl-output.active' ).unblock(); 112 113 $( document ).trigger( 'yith-wcwtl-submit-success' ); 114 } 115 } ); 116 }).catch(function(err) { 117 // Run this when promise was rejected via reject() 118 console.log(err) 119 $( document ).find( '.yith-wcwtl-output' ).removeClass( 'active' ); 120 $( document ).find( '.yith-wcwtl-output' ).unblock(); 121 }) 122 123 124 }; 125 126 const ywcwtlIsAjaxEnabled = yith_wcwtl.is_ajax_enabled === 'yes'; 127 128 $( document ).on( 'click', '.yith-wcwtl-output a.yith-wcwtl-submit', function( ev ) { 129 ev.preventDefault(); 130 if( ywcwtlIsAjaxEnabled ){ 131 submit( $( this ).attr( 'href' ), $( this ) ); 132 }else{ 133 const register = ! $(this).hasClass( 'button-leave' ); 134 isSubmitAllowed(register).then( function(){ 135 window.location.href = $( '.yith-wcwtl-submit' ).attr('href'); 136 } ) 137 } 138 } ); 139 140 $( document ).on( 'submit', '.yith-wcwtl-output form', function( ev ) { 141 ev.preventDefault(); 142 submit( $( this ).attr( 'action' ), $( this ) ); 143 } ); 144 145 $( document ).on( 'input', '#yith-wcwtl-user-email', function() { 146 var t = $( this ), 147 val = t.val(), 148 name = t.attr( 'name' ); 149 150 addUrlParameter( name, val, t ); 151 } ); 152 153 $( document ).on( 'change', '.yith-wcwtl-policy-check', function() { 154 var t = $( this ), 155 name = t.attr( 'name' ), 156 val = t.is( ':checked' ) ? 'yes' : 'no'; 157 158 addUrlParameter( name, val, t ); 159 } ); 160 161 var isSubmitAllowed = function( register= true ) { 162 if( ! register ){ 163 return new Promise(function(resolve) { 164 resolve(true); 165 }); 166 } 167 168 var emailInput = $( '#yith-wcwtl-user-email' ); 169 var emailValue = emailInput.val(); 170 171 emailInput.removeClass('error'); 172 emailInput.next('.yith-wcwtl-error-message').remove(); 173 174 if ( !emailValue ) { 175 176 var errorMessage = '<div class="yith-wcwtl-error-message" style="color: red; font-size: 12px;margin: 20px 0; position: absolute; bottom: 0;">' + yith_wcwtl.email_empty_error + '</div>'; 177 emailInput.addClass('error').after(errorMessage); 178 179 return new Promise(function(resolve, reject) { 180 reject( yith_wcwtl.email_empty ); 181 }); 182 } 183 184 if ( yith_wcwtl.recaptcha_site_key === undefined || !yith_wcwtl.recaptcha_site_key ) { 185 return new Promise(function(resolve) { 186 resolve(true); 187 }); 188 } else { 189 return ywtwlGetCaptchaToken(); 190 } 191 }; 192 193 194 $( document ).on( 'input', '#yith-wcwtl-user-email', function() { 195 var emailInput = $( this ); 196 emailInput.removeClass('error'); 197 emailInput.next('.yith-wcwtl-error-message').remove(); 198 }); 199} );
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.