' + message + '
' ).removeClass( 'cn-hidden' ); } var cnWelcomeScreen = function( e ) { var screen = $( e.target ).data( 'screen' ); var steps = [1,2,3,4]; var sidebars = ['login', 'register', 'configure', 'payment']; // continue with screen loading var requestData = { action: 'cn_welcome_screen', nonce: cnWelcomeArgs.nonce }; if ( $.inArray( screen, steps ) != -1 ) { var container = $( '.cn-welcome-wrap' ); requestData.screen = screen; } else if ( $.inArray( screen, sidebars ) != -1 ) { var container = $( '.cn-sidebar' ); requestData.screen = screen; } else return false; // add loading overlay $( container ).addClass( 'cn-loading' ); $.ajax( { url: cnWelcomeArgs.ajaxURL, type: 'POST', dataType: 'html', data: requestData } ).done( function( response ) { $( container ).replaceWith( response ); } ).fail( function( jqXHR, textStatus, errorThrown ) { // } ).always( function( response ) { // remove spinner $( container ).removeClass( 'cn-loading' ); // trigger event var event = $.Event( 'screen-loaded' ); $( document ).trigger( event ); } ); return this; }; var cnWelcomeForm = function( form ) { var formAction = $( form[0] ).data( 'action' ); var formResult = null; var formData = { action: 'cn_api_request', nonce: cnWelcomeArgs.nonce }; // clear feedback $( form[0] ).find( '.cn-form-feedback' ).addClass( 'cn-hidden' ); // build request data formData.request = formAction; // convert form data to object $( form[0] ).serializeArray().map( function( x ) { // exception for checkboxes if ( x.name === 'cn_laws' ) { var arrayVal = typeof formData[x.name] !== 'undefined' ? formData[x.name] : []; arrayVal.push( x.value ); formData[x.name] = arrayVal; } else { formData[x.name] = x.value; } } ); formResult = $.ajax( { url: cnWelcomeArgs.ajaxURL, type: 'POST', dataType: 'json', data: formData } ); return formResult; }; // handle screen loading $( document ).on( 'click', '.cn-screen-button', function( e ) { var form = $( e.target ).closest( 'form' ); var result = false; // spin the spinner, if exists if ( $( e.target ).find( '.cn-spinner' ).length ) $( e.target ).find( '.cn-spinner' ).addClass( 'spin' ); // no form? if ( form.length === 0 ) return cnWelcomeScreen( e ); var formData = {}; var formDataset = $( form[0] ).data(); var formAction = formDataset.hasOwnProperty( 'action' ) ? formDataset.action : ''; // get form data $( form[0] ).serializeArray().map( function( x ) { // exception for checkboxes if ( x.name === 'cn_laws' ) { var arrayVal = typeof formData[x.name] !== 'undefined' ? formData[x.name] : []; arrayVal.push( x.value ); formData[x.name] = arrayVal; } else { formData[x.name] = x.value; } } ); // console.log( form[0] ); // console.log( formData ); // console.log( formAction ); // payment? if ( formAction === 'payment' ) { if ( formData.plan !== 'free' ) { // only credit cards if ( $( form[0] ).find( 'input[name="payment_nonce"]' ).val() === '' ) { form.trigger( 'submit' ); return false; } } else { // load screen cnWelcomeScreen( e ); return false; } } else e.preventDefault(); // get form and process it result = cnWelcomeForm( form ); result.done( function( response ) { // error if ( response.hasOwnProperty( 'error' ) ) { cnDisplayError( response.error, $( form[0] ) ); return false; // message } else if ( response.hasOwnProperty( 'message' ) ) { cnDisplayError( response.message, $( form[0] ) ); return false; // all good } else { switch ( formAction ) { // logged in, go to success or billing case 'login' : // register complete, go to success or billing case 'register' : var accountPlan = formData.hasOwnProperty( 'plan' ) ? formData.plan : 'free'; // trigger payment var accordionItem = $( form[0] ).closest( '.cn-accordion-item' ); // collapse account $( accordionItem ).addClass( 'cn-collapsed cn-disabled' ); // show billing $( accordionItem ).next().removeClass( 'cn-disabled' ).removeClass( 'cn-collapsed' ); $( accordionItem ).find( 'form' ).removeClass( 'cn-form-disabled' ); // init braintree after payment screen is loaded via AJAX btInit(); break; case 'configure' : default : // load screen cnWelcomeScreen( e ); break; } } } ); result.always( function( response ) { if ( $( e.target ).find( '.cn-spinner' ).length ) $( e.target ).find( '.cn-spinner' ).removeClass( 'spin' ); // after invalid payment? if ( formAction === 'payment' ) { $( form[0] ).removeClass( 'cn-payment-in-progress' ); $( form[0] ).find( 'input[name="payment_nonce"]' ).val( '' ); } } ); return result; } ); // $( document ).on( 'screen-loaded', function() { var configureFields = $( '#cn-form-configure' ).serializeArray() || []; var frame = window.frames[ 'cn_iframe_id' ]; if ( configureFields.length > 0 ) { $( configureFields ).each( function( index, field ) { } ); } } ); // change payment method $( document ).on( 'change', 'input[name="method"]', function() { var input = $( this ); $( '#cn_payment_method_credit_card, #cn_payment_method_paypal' ).toggle(); input.closest( 'form' ).find( '.cn-form-feedback' ).addClass( 'cn-hidden' ); // init payment method if needed btInitPaymentMethod( input.val() ); } ); // $( document ).on( 'click', '.cn-accordion > .cn-accordion-item .cn-accordion-button', function() { var accordionItem = $( this ).closest( '.cn-accordion-item' ); var activeItem = $( this ).closest( '.cn-accordion' ).find( '.cn-accordion-item:not(.cn-collapsed)' ); if ( $( accordionItem ).hasClass( 'cn-collapsed' ) ) { $( activeItem ).addClass( 'cn-collapsed' ); $( accordionItem ).removeClass( 'cn-collapsed' ); } return false; } ); // live preview $( document ).on( 'change', 'input[name="cn_position"]', function() { var val = $( this ).val(); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'position', value: val } ); } ); $( document ).on( 'change', 'input[name="cn_purposes"]', function() { var val = []; $( 'input[name="cn_purposes"]:checked' ).each( function() { val.push( $( this ).val() ); } ); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'purposes', value: val } ); } ); $( document ).on( 'change', 'input[name="cn_laws"]', function() { var val = []; $( 'input[name="cn_laws"]:checked' ).each( function() { val.push( $( this ).val() ); } ); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'laws', value: val } ); } ); $( document ).on( 'change', 'input[name="cn_color_primary"]', function() { var val = $( this ).val(); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'color_primary', value: val } ); } ); $( document ).on( 'change', 'input[name="cn_color_background"]', function() { var val = $( this ).val(); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'color_background', value: val } ); } ); $( document ).on( 'change', 'input[name="cn_color_border"]', function() { var val = $( this ).val(); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'color_border', value: val } ); } ); $( document ).on( 'change', 'input[name="cn_color_text"]', function() { var val = $( this ).val(); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'color_text', value: val } ); } ); $( document ).on( 'change', 'input[name="cn_color_heading"]', function() { var val = $( this ).val(); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'color_heading', value: val } ); } ); $( document ).on( 'change', 'input[name="cn_color_button_text"]', function() { var val = $( this ).val(); var frame = window.frames['cn_iframe_id']; frame.contentWindow.postMessage( { call: 'color_button_text', value: val } ); } ); // plan selection $( document ).on( 'change', 'input[name="plan"]', function() { var availablePlans = [ 'free', 'monthly', 'yearly' ]; var input = $( this ), inputVal = input.val(); inputVal = availablePlans.indexOf( inputVal ) != -1 ? inputVal : 'free'; if ( inputVal === 'free' ) { $( '#cn_submit_free' ).removeClass( 'cn-hidden' ); $( '#cn_submit_paid' ).addClass( 'cn-hidden' ); } else { $( '#cn_submit_free' ).addClass( 'cn-hidden' ); $( '#cn_submit_paid' ).removeClass( 'cn-hidden' ); } $( document ).find( '.cn-pricing-item input[value="' + inputVal + '"' ).prop('checked', true); } ); // highlight form $( document ).on( 'click', 'input[name="cn_pricing_plan"]', function() { $( '.cn-accordion .cn-accordion-item:first-child:not(.cn-collapsed)' ).focus(); } ); // select plan $( document ).on( 'change', 'input[name="cn_pricing_plan"]', function() { var availablePlans = [ 'free', 'monthly', 'yearly' ]; var input = $( this ), inputVal = input.val(); inputVal = availablePlans.indexOf( inputVal ) != -1 ? inputVal : 'free'; if ( inputVal === 'free' ) { $( '#cn_submit_free' ).removeClass( 'cn-hidden' ); $( '#cn_submit_paid' ).addClass( 'cn-hidden' ); } else { $( '#cn_submit_free' ).addClass( 'cn-hidden' ); $( '#cn_submit_paid' ).removeClass( 'cn-hidden' ); } $( document ).find( '#cn_field_plan_' + inputVal ).prop('checked', true); } ); // color picker initSpectrum(); // init welcome modal if ( cnWelcomeArgs.initModal == true ) initModal(); } ); $( document ).on( 'ajaxComplete', function() { // color picker initSpectrum(); } ); function initSpectrum() { $( '.cn-color-picker' ).spectrum( { showInput: true, showInitial: true, allowEmpty: false, showAlpha: false } ); } function initModal() { var progressbar, timerId, modal = $( "#cn-modal-trigger" ); if ( modal ) { $( "#cn-modal-trigger" ).modaal( { content_source: cnWelcomeArgs.ajaxURL + '?action=cn_welcome_screen' + '&nonce=' + cnWelcomeArgs.nonce + '&screen=1', type: 'ajax', width: 1600, custom_class: 'cn-modal', // is_locked: true ajax_success: function() { progressbar = $( document ).find( '.cn-progressbar' ); if ( progressbar ) { timerId = initProgressBar( progressbar ); } }, before_close: function() { clearInterval( timerId ); var currentStep = $( '.cn-welcome-wrap' ); // reload if on success screen if ( currentStep.length > 0 ) { if ( $( currentStep[0] ).hasClass( 'cn-welcome-step-4' ) === true ) window.location.reload( true ); } }, after_close: function() { progressbar = $( document ).find( '.cn-progressbar' ); $( progressbar ).progressbar( "destroy" ); } } ); $( modal ).trigger( 'click' ); $( document ).on( 'click', '.cn-skip-button', function( e ) { $( '#modaal-close' ).trigger( 'click' ); } ); } } function initProgressBar( progressbar ) { var progressbarObj, progressLabel = $( document ).find( '.cn-progress-label' ), complianceResults = $( document ).find( '.cn-compliance-results' ), currentProgress = 0, timerId; if ( progressbar ) { $( document ).on( 'click', '.cn-screen-button', function( e ) { e.preventDefault(); // console.log( e ); clearInterval( timerId ); } ); $( progressbar ).progressbar( { value: 5, max: 100, create: function ( event, ui ) { // console.log( event ); timerId = setInterval( function() { // increment progress bar currentProgress += 5; // console.log( currentProgress ); // update progressbar progressbar.progressbar( 'value', currentProgress ); var lastItem = $( complianceResults ).find( 'div:visible' ).last(), lastItemText = $( lastItem ).find( '.cn-compliance-status' ).text(); $( lastItem ).find( '.cn-compliance-status' ).text( lastItemText + ' .' ); switch ( currentProgress ) { case 25: $( lastItem ).find( '.cn-compliance-status' ).addClass( 'cn-passed' ).text( cnWelcomeArgs.statusPassed ); $( lastItem ).next().slideDown( 200 ); break; case 50: if ( cnWelcomeArgs.complianceStatus === 'active' ) { $( lastItem ).find( '.cn-compliance-status' ).addClass( 'cn-passed' ).text( cnWelcomeArgs.statusPassed ); } else { $( lastItem ).find( '.cn-compliance-status' ).addClass( 'cn-failed' ).text( cnWelcomeArgs.statusFailed ); } $( lastItem ).next().slideDown( 200 ); break; case 75: if ( cnWelcomeArgs.complianceStatus === 'active' ) { $( lastItem ).find( '.cn-compliance-status' ).addClass( 'cn-passed' ).text( cnWelcomeArgs.statusPassed ); } else { $( lastItem ).find( '.cn-compliance-status' ).addClass( 'cn-failed' ).text( cnWelcomeArgs.statusFailed ); } $( lastItem ).next().slideDown( 200 ); break; case 100: if ( cnWelcomeArgs.complianceStatus === 'active' ) { $( lastItem ).find( '.cn-compliance-status' ).addClass( 'cn-passed' ).text( cnWelcomeArgs.statusPassed ); } else { $( lastItem ).find( '.cn-compliance-status' ).addClass( 'cn-failed' ).text( cnWelcomeArgs.statusFailed ); } break; } // complete if ( currentProgress >= 100 ) { clearInterval( timerId ); } }, 300 ); }, change: function ( event, ui ) { // console.log( event ); progressLabel.text( progressbar.progressbar( 'value' ) + '%' ); }, complete: function ( event, ui ) { // console.log( event ); setTimeout( function () { if ( cnWelcomeArgs.complianceStatus ) $( '.cn-compliance-check' ).find( '.cn-compliance-feedback' ).html( '' ).removeClass( 'cn-hidden' ); else $( '.cn-compliance-check' ).find( '.cn-compliance-feedback' ).html( '' + cnWelcomeArgs.complianceFailed + '
' ).removeClass( 'cn-hidden' ); }, 500 ); // $( progressbar ).progressbar( "destroy" ); } } ); progressbarObj = $( progressbar ).progressbar( "instance" ); return timerId; } } $( document ).on( 'click', '.cn-run-upgrade, .cn-run-welcome', function( e ) { e.preventDefault(); // console.log( e ); // modal initModal(); } ); $( document ).on( 'click', '.cn-sign-up', function( e ) { e.preventDefault(); $( '.cn-screen-button' ).trigger( 'click' ); } ); } )( jQuery );