| 1 |
/** |
| 2 |
* MLSImport Onboarding Wizard JavaScript |
| 3 |
* |
| 4 |
* Handles all the frontend functionality for the onboarding wizard including |
| 5 |
* navigation, form validation, and AJAX requests. |
| 6 |
*/ |
| 7 |
|
| 8 |
(function($) { |
| 9 |
'use strict'; |
| 10 |
|
| 11 |
// Store wizard state |
| 12 |
var MLSImportWizard = { |
| 13 |
currentStep: '', |
| 14 |
steps: {}, |
| 15 |
formData: {}, |
| 16 |
init: function() { |
| 17 |
// Set initial state from localized data |
| 18 |
this.currentStep = mlsimportOnboarding.current_step; |
| 19 |
this.steps = mlsimportOnboarding.steps; |
| 20 |
|
| 21 |
// Initialize event listeners |
| 22 |
this.initEvents(); |
| 23 |
|
| 24 |
// Initialize step-specific functionality |
| 25 |
this.initCurrentStep(); |
| 26 |
}, |
| 27 |
|
| 28 |
initEvents: function() { |
| 29 |
// Form submission |
| 30 |
$('#mlsimport-wizard-form').on('submit', this.handleFormSubmit); |
| 31 |
|
| 32 |
// Back button handling |
| 33 |
$('.mlsimport-wizard-back').on('click', this.handleBackClick); |
| 34 |
|
| 35 |
// Save data when navigating away |
| 36 |
$(window).on('beforeunload', this.saveCurrentData); |
| 37 |
|
| 38 |
// Step navigation |
| 39 |
$('.mlsimport-wizard-step').on('click', this.handleStepClick); |
| 40 |
}, |
| 41 |
handleStepClick: function(e) { |
| 42 |
e.preventDefault(); |
| 43 |
|
| 44 |
// Save current data |
| 45 |
MLSImportWizard.saveCurrentData(); |
| 46 |
|
| 47 |
// Get clicked step index |
| 48 |
var $step = $(this); |
| 49 |
var stepIndex = $step.index(); |
| 50 |
var stepKeys = Object.keys(MLSImportWizard.steps); |
| 51 |
var targetStep = stepKeys[stepIndex]; |
| 52 |
|
| 53 |
// Don't allow skipping ahead - only go to completed steps or next step |
| 54 |
var currentIndex = stepKeys.indexOf(MLSImportWizard.currentStep); |
| 55 |
if (stepIndex > currentIndex + 1) { |
| 56 |
alert(mlsimportOnboarding.strings.complete_current_step || 'Please complete the current step first.'); |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
// Navigate to the step |
| 61 |
window.location.href = 'admin.php?page=mlsimport-onboarding&step=' + targetStep; |
| 62 |
}, |
| 63 |
|
| 64 |
|
| 65 |
initCurrentStep: function() { |
| 66 |
// Step-specific initialization |
| 67 |
switch(this.currentStep) { |
| 68 |
case 'welcome': |
| 69 |
// Nothing special for welcome step |
| 70 |
break; |
| 71 |
case 'account': |
| 72 |
// Initialize autocomplete already handled in template |
| 73 |
break; |
| 74 |
case 'field-mapping': |
| 75 |
// Template selection handler already in template |
| 76 |
break; |
| 77 |
case 'import-config': |
| 78 |
// Initialize Select2 if available already in template |
| 79 |
break; |
| 80 |
case 'test-import': |
| 81 |
// Test import handlers already in template |
| 82 |
break; |
| 83 |
case 'success': |
| 84 |
// Success page doesn't need special handling |
| 85 |
break; |
| 86 |
} |
| 87 |
}, |
| 88 |
|
| 89 |
handleFormSubmit: function(e) { |
| 90 |
// Save current form data |
| 91 |
MLSImportWizard.saveCurrentData(); |
| 92 |
|
| 93 |
// Let the form submit normally - PHP will handle the processing |
| 94 |
return true; |
| 95 |
}, |
| 96 |
|
| 97 |
handleBackClick: function(e) { |
| 98 |
// Save current form data before going back |
| 99 |
MLSImportWizard.saveCurrentData(); |
| 100 |
|
| 101 |
// Let the link work normally |
| 102 |
return true; |
| 103 |
}, |
| 104 |
|
| 105 |
saveCurrentData: function() { |
| 106 |
// Collect form data |
| 107 |
var formData = $('#mlsimport-wizard-form').serializeArray(); |
| 108 |
var data = {}; |
| 109 |
|
| 110 |
// Convert to object |
| 111 |
$.each(formData, function(i, field) { |
| 112 |
if (field.name.indexOf('[]') !== -1) { |
| 113 |
// Handle array values |
| 114 |
var name = field.name.replace('[]', ''); |
| 115 |
if (!data[name]) { |
| 116 |
data[name] = []; |
| 117 |
} |
| 118 |
data[name].push(field.value); |
| 119 |
} else { |
| 120 |
data[field.name] = field.value; |
| 121 |
} |
| 122 |
}); |
| 123 |
|
| 124 |
// Save via AJAX |
| 125 |
$.ajax({ |
| 126 |
url: mlsimportOnboarding.ajaxurl, |
| 127 |
method: 'POST', |
| 128 |
data: { |
| 129 |
action: 'mlsimport_save_step_data', |
| 130 |
step: MLSImportWizard.currentStep, |
| 131 |
data: data, |
| 132 |
nonce: mlsimportOnboarding.nonce |
| 133 |
}, |
| 134 |
async: false // Make sure data is saved before page unloads |
| 135 |
}); |
| 136 |
}, |
| 137 |
|
| 138 |
// Utility function to show step-specific sections |
| 139 |
showStepSection: function(selector) { |
| 140 |
$('.mlsimport-step-section').hide(); |
| 141 |
$(selector).show(); |
| 142 |
}, |
| 143 |
|
| 144 |
// Utility function to validate current step |
| 145 |
validateStep: function() { |
| 146 |
var isValid = true; |
| 147 |
var requiredFields = $('#mlsimport-wizard-form').find('[required]'); |
| 148 |
|
| 149 |
requiredFields.each(function() { |
| 150 |
if (!$(this).val()) { |
| 151 |
isValid = false; |
| 152 |
$(this).addClass('mlsimport-field-error'); |
| 153 |
} else { |
| 154 |
$(this).removeClass('mlsimport-field-error'); |
| 155 |
} |
| 156 |
}); |
| 157 |
|
| 158 |
return isValid; |
| 159 |
}, |
| 160 |
|
| 161 |
// Utility function to show error message |
| 162 |
showError: function(message) { |
| 163 |
if (!$('.mlsimport-error-notice').length) { |
| 164 |
$('<div class="mlsimport-error-notice"></div>').insertBefore('#mlsimport-wizard-form'); |
| 165 |
} |
| 166 |
|
| 167 |
$('.mlsimport-error-notice').html('<p>' + message + '</p>').show(); |
| 168 |
|
| 169 |
// Scroll to error |
| 170 |
$('html, body').animate({ |
| 171 |
scrollTop: $('.mlsimport-error-notice').offset().top - 50 |
| 172 |
}, 200); |
| 173 |
}, |
| 174 |
|
| 175 |
// Utility function to hide error message |
| 176 |
hideError: function() { |
| 177 |
$('.mlsimport-error-notice').hide(); |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
// Initialize the wizard on document ready |
| 182 |
$(document).ready(function() { |
| 183 |
MLSImportWizard.init(); |
| 184 |
}); |
| 185 |
|
| 186 |
// Add utility functions for step templates that run inline JS |
| 187 |
window.MLSImportWizard = MLSImportWizard; |
| 188 |
|
| 189 |
})(jQuery); |
| 190 |
|
| 191 |
/** |
| 192 |
* Helper function to get a URL parameter by name |
| 193 |
*/ |
| 194 |
function getUrlParameter(name) { |
| 195 |
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); |
| 196 |
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); |
| 197 |
var results = regex.exec(location.search); |
| 198 |
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Helper function to format large numbers with commas |
| 203 |
*/ |
| 204 |
function formatNumber(num) { |
| 205 |
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Helper function to show a loading state on a button |
| 210 |
*/ |
| 211 |
function showButtonLoading(button, loadingText) { |
| 212 |
button.data('original-text', button.html()); |
| 213 |
button.html(loadingText || mlsimportOnboarding.strings.loading); |
| 214 |
button.prop('disabled', true); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Helper function to restore a button from loading state |
| 219 |
*/ |
| 220 |
function hideButtonLoading(button) { |
| 221 |
button.html(button.data('original-text')); |
| 222 |
button.prop('disabled', false); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
jQuery(document).ready(function (jQuery) { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
function showButtonStatus(button, statusText, reset = true) { |
| 241 |
const originalText = button.data('original-text') || button.text(); |
| 242 |
if (!button.data('original-text')) { |
| 243 |
button.data('original-text', originalText); |
| 244 |
} |
| 245 |
button.text(statusText); |
| 246 |
if (reset) { |
| 247 |
setTimeout(() => button.text(originalText), 2000); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
jQuery('.mlsimport-save-account').on('click', function (e) { |
| 252 |
e.preventDefault(); |
| 253 |
const button = jQuery(this); |
| 254 |
showButtonStatus(button, mlsimportOnboarding.strings.saving, false); |
| 255 |
|
| 256 |
const username = jQuery('#mlsimport_admin_options-mlsimport_username').val(); |
| 257 |
const password = jQuery('#mlsimport_admin_options-mlsimport_password').val(); |
| 258 |
|
| 259 |
jQuery.post(mlsimportOnboarding.ajaxurl, { |
| 260 |
action: 'mlsimport_save_account', |
| 261 |
security: mlsimportOnboarding.nonce, |
| 262 |
mlsimport_username: username, |
| 263 |
mlsimport_password: password |
| 264 |
}, function (response) { |
| 265 |
if (response.success) { |
| 266 |
showButtonStatus(button, mlsimportOnboarding.strings.success); |
| 267 |
|
| 268 |
// Replace the status feedback message |
| 269 |
jQuery('.mlsimport_warning').remove(); |
| 270 |
jQuery('#mlsimport_admin_options-mlsimport_username') |
| 271 |
.closest('fieldset') |
| 272 |
.before(response.data.html); |
| 273 |
} else { |
| 274 |
showButtonStatus(button, mlsimportOnboarding.strings.error); |
| 275 |
} |
| 276 |
}).fail(function () { |
| 277 |
showButtonStatus(button, mlsimportOnboarding.strings.error); |
| 278 |
}); |
| 279 |
}); |
| 280 |
|
| 281 |
jQuery('.mlsimport-save-mls-data').on('click', function (e) { |
| 282 |
e.preventDefault(); |
| 283 |
|
| 284 |
const button = jQuery(this); |
| 285 |
const originalText = button.text(); |
| 286 |
button.text(mlsimportOnboarding.strings.saving).prop('disabled', true); |
| 287 |
|
| 288 |
const data = { |
| 289 |
action: 'mlsimport_save_mls_data', |
| 290 |
security: mlsimportOnboarding.nonce |
| 291 |
}; |
| 292 |
|
| 293 |
jQuery('[name^="mlsimport_admin_options"]').each(function () { |
| 294 |
const name = jQuery(this).attr('name').replace('mlsimport_admin_options[', '').replace(']', ''); |
| 295 |
if (name !== 'mlsimport_username' && name !== 'mlsimport_password') { |
| 296 |
data[name] = jQuery(this).val(); |
| 297 |
} |
| 298 |
}); |
| 299 |
|
| 300 |
jQuery.post(mlsimportOnboarding.ajaxurl, data, function (response) { |
| 301 |
button.text(originalText).prop('disabled', false); |
| 302 |
|
| 303 |
if (response.success) { |
| 304 |
// Remove all .mlsimport_warning except the validated one (account message) |
| 305 |
jQuery('.mlsimport_warning').not('.mlsimport_validated').remove(); |
| 306 |
|
| 307 |
// Insert new MLS connection message before MLS input |
| 308 |
jQuery('#mlsimport_mls_name_front') |
| 309 |
.closest('fieldset') |
| 310 |
.before(response.data.html); |
| 311 |
} else { |
| 312 |
button.text(mlsimportOnboarding.strings.error); |
| 313 |
} |
| 314 |
}).fail(function () { |
| 315 |
button.text(mlsimportOnboarding.strings.error).prop('disabled', false); |
| 316 |
}); |
| 317 |
}); |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
// code for the acocunt page |
| 322 |
|
| 323 |
if (jQuery('.mlsimport-wizard-content-account').length) { |
| 324 |
updateContinueButton(); |
| 325 |
|
| 326 |
// Add continue button navigation |
| 327 |
jQuery('.mlsimport-wizard-content-account .mlsimport-wizard-next').on('click', function(e) { |
| 328 |
e.preventDefault(); |
| 329 |
if (!jQuery(this).prop('disabled')) { |
| 330 |
window.location.href = ajaxurl.replace('admin-ajax.php', 'admin.php') + '?page=mlsimport-onboarding&step=field-mapping'; |
| 331 |
} |
| 332 |
}); |
| 333 |
|
| 334 |
// Update after AJAX calls |
| 335 |
jQuery(document).ajaxComplete(function() { |
| 336 |
setTimeout(updateContinueButton, 500); |
| 337 |
}); |
| 338 |
} |
| 339 |
|
| 340 |
}); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
function updateContinueButton() { |
| 346 |
const validatedCount = jQuery('.mlsimport_warning.mlsimport_validated').length; |
| 347 |
console.log('nwe thing'); |
| 348 |
const continueButton = jQuery('.mlsimport-wizard-content-account .mlsimport-wizard-next'); |
| 349 |
|
| 350 |
if (validatedCount >= 2) { |
| 351 |
continueButton.prop('disabled', false).removeClass('disabled'); |
| 352 |
} else { |
| 353 |
continueButton.prop('disabled', true).addClass('disabled'); |
| 354 |
} |
| 355 |
} |
| 356 |
|