| @@ -6,19 +6,61 @@ | ||
| 6 | 6 | * @package Visualizer |
| 7 | 7 | */ |
| 8 | 8 | /* jshint unused:false */ |
| 9 | 9 | jQuery(function ($) { |
| 10 | + | |
| 11 | + /** | |
| 12 | + * Check if the user is in live preview mode. | |
| 13 | + * Live Preview is used in WordPress Playground to test the plugin. | |
| 14 | + * Use this check to deactivate any process that does not showcase the plugin features (e.g. subscription to the newsletter, etc.) | |
| 15 | + * | |
| 16 | + * @type {boolean} | |
| 17 | + */ | |
| 18 | + const isLivePreview = window.location.search.includes('env=preview'); | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Redirect to draft page after subscribe (optional). | |
| 22 | + * | |
| 23 | + * @param {Object} postData - Post data. | |
| 24 | + */ | |
| 25 | + const goToDraftPage = function ( postData = {} ) { | |
| 26 | + postData ??= {}; | |
| 27 | + postData.action ??= 'visualizer_wizard_step_process'; | |
| 28 | + postData.security ??= visualizerSetupWizardData.ajax.security; | |
| 29 | + postData.step ??= 'step_subscribe'; | |
| 30 | + | |
| 31 | + // Subscribe the user using the email if provided. Redirect to the draft page. | |
| 32 | + $.post(visualizerSetupWizardData.ajax.url, postData, function (res) { | |
| 33 | + | |
| 34 | + // Toggle the redirect popup. | |
| 35 | + $('.redirect-popup').find('h3.popup-title').html(res.message); | |
| 36 | + $('.redirect-popup').show(); | |
| 37 | + | |
| 38 | + if (1 === res.status) { | |
| 39 | + setTimeout(function () { | |
| 40 | + window.location.href = res.redirect_to; | |
| 41 | + }, 5000); | |
| 42 | + } else { | |
| 43 | + $('.redirect-popup').hide(); | |
| 44 | + } | |
| 45 | + currentStep.find('.spinner').removeClass('is-active'); | |
| 46 | + }).fail(function () { | |
| 47 | + $('.redirect-popup').hide(); | |
| 48 | + currentStep.find('.spinner').removeClass('is-active'); | |
| 49 | + }); | |
| 50 | + } | |
| 51 | + | |
| 10 | 52 | var provideContent = function(id, stepDirection, stepPosition, selStep, callback) { |
| 11 | 53 | // Import chart data. |
| 12 | 54 | if ( 1 == id ) { |
| 13 | 55 | var chartType = $(".vz-radio-btn:checked").val(); |
| 14 | - var percentBarWidth = 100; | |
| 56 | + var percentBarWidth = 0; | |
| 15 | 57 | var loaderDelay; |
| 16 | 58 | $.ajax( { |
| 17 | 59 | beforeSend: function() { |
| 18 | 60 | loaderDelay = setInterval(function () { |
| 19 | 61 | $('.vz-progress-bar').css({ |
| 20 | - width: percentBarWidth-- + '%' | |
| 62 | + width: percentBarWidth++ + '%' | |
| 21 | 63 | }); |
| 22 | 64 | }, 1000); |
| 23 | 65 | }, |
| 24 | 66 | type: 'POST', |
| @@ -33,11 +75,11 @@ | ||
| 33 | 75 | success: function (data) { |
| 34 | 76 | clearInterval( loaderDelay ); |
| 35 | 77 | loaderDelay = setInterval(function () { |
| 36 | 78 | $('.vz-progress-bar').css({ |
| 37 | - width: percentBarWidth-- + '%' | |
| 79 | + width: percentBarWidth++ + '%' | |
| 38 | 80 | }); |
| 39 | - if ( percentBarWidth <= 0 ) { | |
| 81 | + if ( percentBarWidth >= 100 ) { | |
| 40 | 82 | if ( 1 === data.success ) { |
| 41 | 83 | var importMessage = jQuery('[data-import_message]'); |
| 42 | 84 | importMessage |
| 43 | 85 | .html( importMessage.data('import_message') ) |
| @@ -52,13 +94,16 @@ | ||
| 52 | 94 | } else { |
| 53 | 95 | $('#smartwizard').smartWizard('reset'); |
| 54 | 96 | } |
| 55 | 97 | $('.vz-progress-bar').css({ |
| 56 | - width: 0 | |
| 98 | + width: 100 + '%' | |
| 57 | 99 | }); |
| 100 | + $('.vz-progress').css({ | |
| 101 | + visibility: 'hidden' | |
| 102 | + }); | |
| 58 | 103 | clearInterval( loaderDelay ); |
| 59 | 104 | } |
| 60 | - }, 100 ); | |
| 105 | + }, 36 ); | |
| 61 | 106 | }, |
| 62 | 107 | error: function() { |
| 63 | 108 | $('#step-2').find('.vz-progress-bar').animate({ width: '0' }); |
| 64 | 109 | } |
| @@ -118,8 +163,9 @@ | ||
| 118 | 163 | "click", |
| 119 | 164 | ".btn-primary:not(.next-btn,.vz-create-page,.vz-subscribe)", |
| 120 | 165 | function (e) { |
| 121 | 166 | var stepNumber = $(this).data("step_number"); |
| 167 | + | |
| 122 | 168 | switch (stepNumber) { |
| 123 | 169 | case 1: |
| 124 | 170 | if ($(".vz-radio-btn").is(":checked")) { |
| 125 | 171 | $('#smartwizard').smartWizard('next'); |
| @@ -125,12 +171,16 @@ | ||
| 125 | 171 | $('#smartwizard').smartWizard('next'); |
| 126 | 172 | } |
| 127 | 173 | break; |
| 128 | 174 | case 3: |
| 129 | - var urlParams = new URLSearchParams(window.location.search); | |
| 130 | - urlParams.set('preview_chart', Date.now()); | |
| 131 | - window.location.hash = "#step-3"; | |
| 132 | - window.location.search = urlParams; | |
| 175 | + if ( isLivePreview ) { | |
| 176 | + $('#smartwizard').smartWizard('next'); | |
| 177 | + } else { | |
| 178 | + var urlParams = new URLSearchParams(window.location.search); | |
| 179 | + urlParams.set('preview_chart', Date.now()); | |
| 180 | + window.location.hash = "#step-3"; | |
| 181 | + window.location.search = urlParams; | |
| 182 | + } | |
| 133 | 183 | break; |
| 134 | 184 | case 4: |
| 135 | 185 | $('#step-4').find('.spinner').addClass('is-active'); |
| 136 | 186 | $('#step-4').find('.vz-error-notice').addClass('hidden'); |
| @@ -180,9 +230,13 @@ | ||
| 180 | 230 | add_basic_shortcode: $("#insert_shortcode").is(":checked"), |
| 181 | 231 | }, |
| 182 | 232 | function (res) { |
| 183 | 233 | if (res.status > 0) { |
| 184 | - $("#smartwizard").smartWizard("next"); | |
| 234 | + if ( isLivePreview ) { | |
| 235 | + goToDraftPage(); | |
| 236 | + } else { | |
| 237 | + $("#smartwizard").smartWizard("next"); | |
| 238 | + } | |
| 185 | 239 | } |
| 186 | 240 | _this.next(".spinner").removeClass("is-active"); |
| 187 | 241 | } |
| 188 | 242 | ).fail(function () { |
| @@ -234,23 +288,9 @@ | ||
| 234 | 288 | } |
| 235 | 289 | var currentStep = $( '.vz-wizard-wrap .tab-pane:last-child' ); |
| 236 | 290 | currentStep.find(".spinner").addClass("is-active"); |
| 237 | 291 | |
| 238 | - $.post(visualizerSetupWizardData.ajax.url, postData, function (res) { | |
| 239 | - $('.redirect-popup').find('h3.popup-title').html(res.message); | |
| 240 | - $('.redirect-popup').show(); | |
| 241 | - if (1 === res.status) { | |
| 242 | - setTimeout(function () { | |
| 243 | - window.location.href = res.redirect_to; | |
| 244 | - }, 5000); | |
| 245 | - } else { | |
| 246 | - $('.redirect-popup').hide(); | |
| 247 | - } | |
| 248 | - currentStep.find('.spinner').removeClass('is-active'); | |
| 249 | - }).fail(function () { | |
| 250 | - $('.redirect-popup').hide(); | |
| 251 | - currentStep.find('.spinner').removeClass('is-active'); | |
| 252 | - }); | |
| 292 | + goToDraftPage( postData ); | |
| 253 | 293 | e.preventDefault(); |
| 254 | 294 | }); |
| 255 | 295 | |
| 256 | 296 | // Click to copy. |