(function () { "use strict"; function VerifyForm( elem ) { this.wrapper = elem; this.firstName = elem.querySelector( '[data-role="firstName"]' ); this.lastName = elem.querySelector( '[data-role="lastName"]' ); this.email = elem.querySelector( '[data-role="email"]' ); this.purchase = elem.querySelector( '[data-role="purchase"]' ); this.item = elem.querySelector( '[data-role="itemId"]' ); this.nonce = elem.querySelector( '[name="verifyNonce"]' ); this.responseArea = elem.querySelector( '[data-role="response-area"]' ); this.init(); } VerifyForm.prototype.init = function () { var _this = this; this.clearMessages(); [].forEach.call( _this.wrapper.querySelectorAll( '[data-event="click"]' ), function ( target ) { var actionName = target.getAttribute( 'data-action' ).trim() + 'Action'; if ( _this[ actionName ] && typeof _this[ actionName ] === 'function' ) { target.addEventListener( 'click', _this[ actionName ].bind( _this ) ); } } ); }; VerifyForm.prototype.clearMessages = function () { this.showMessage( '', '' ); }; VerifyForm.prototype.showMessage = function ( message, type ) { var response = this.responseArea; response.innerHTML = message; response.setAttribute( 'data-type', type ); }; VerifyForm.prototype.sendFormAction = function ( event ) { var _this = this; _this.clearMessages(); _this.lock(); var data = _this.load(); var validated = _this.validate( data ); if ( validated !== true ) { _this.showMessage( validated.join( '
' ), 'error' ); return false; } wp.ajax.send( 'wpway_verify', { data : data, success: function ( response ) { if ( response.responseText ) { response = JSON.parse( response.responseText ); } _this.showMessage( response.message, 'success' ); _this.unlock(); }, error : function ( response ) { if ( response.responseText ) { response = JSON.parse( response.responseText ); } _this.showMessage( response.data.message, 'error' ); _this.unlock(); console.error( response ); } } ); }; VerifyForm.prototype.lock = function () { this.wrapper.classList.add( 'loading' ); }; VerifyForm.prototype.unlock = function () { this.wrapper.classList.remove( 'loading' ); }; VerifyForm.prototype.load = function () { var _this = this; return { 'firstName' : _this.firstName.value || _this.firstName.getAttribute( 'value' ), 'lastName' : _this.lastName.value || _this.lastName.getAttribute( 'value' ), 'email' : _this.email.value || _this.email.getAttribute( 'value' ), 'purchase' : _this.purchase.value || _this.purchase.getAttribute( 'value' ), "item" : _this.item.value || _this.item.getAttribute( 'value' ), 'verifyNonce': _this.nonce.value || _this.nonce.getAttribute( 'value' ) }; }; VerifyForm.prototype.validate = function ( data ) { var errors = []; if ( !data.firstName || !data.firstName.match( /^\w{3,}$/g ) ) { errors.push( 'First Name is not valid!' ); } if ( !data.lastName || !data.lastName.match( /^\w{3,}$/g ) ) { errors.push( 'Last Name is not valid!' ); } if ( !data.email || !data.email.match( /^([\w.-]+@([\w-]+)\.+\w{2,})\S+$/g ) ) { errors.push( 'Email is not valid!' ); } if ( !data.purchase || !data.purchase.match( /^[\w\d-]{5,}$/g ) ) { errors.push( 'Purchase Code is not valid!' ); } if ( !data.item || !data.item.match( /^\w{3,}$/g ) ) { errors.push( 'Item ID is not valid!' ); } return errors.length > 0 ? errors : true; }; /** * Manager * @constructor */ function VerifyFormManager() { } VerifyFormManager.prototype.init = function ( context ) { if ( !context ) { context = document.body; } [].forEach.call( context.querySelectorAll( '[data-component="verify-form"]' ), function ( form ) { new VerifyForm( form ); } ); }; document.addEventListener( 'DOMContentLoaded', function () { var formManager = new VerifyFormManager(); formManager.init(); }, true ); })();