| 1 |
/** |
| 2 |
* Yatra Setup Wizard JavaScript |
| 3 |
* |
| 4 |
* @package Yatra |
| 5 |
*/ |
| 6 |
|
| 7 |
(function($) { |
| 8 |
'use strict'; |
| 9 |
|
| 10 |
var YatraSetupWizard = { |
| 11 |
/** |
| 12 |
* Initialize |
| 13 |
*/ |
| 14 |
init: function() { |
| 15 |
this.bindEvents(); |
| 16 |
this.initFormValidation(); |
| 17 |
this.ensureRedirectField(); |
| 18 |
}, |
| 19 |
|
| 20 |
/** |
| 21 |
* Ensure redirect field exists so links can autosave + navigate. |
| 22 |
*/ |
| 23 |
ensureRedirectField: function() { |
| 24 |
$('.yatra-setup-content form.wizard-step').each(function() { |
| 25 |
var $form = $(this); |
| 26 |
if ($form.find('input[name="yatra_redirect_to"]').length === 0) { |
| 27 |
$form.append('<input type="hidden" name="yatra_redirect_to" value="" />'); |
| 28 |
} |
| 29 |
}); |
| 30 |
}, |
| 31 |
|
| 32 |
/** |
| 33 |
* Bind events |
| 34 |
*/ |
| 35 |
bindEvents: function() { |
| 36 |
// Form submission with loading state |
| 37 |
$('.yatra-setup-content form').on('submit', function() { |
| 38 |
var $form = $(this); |
| 39 |
var $submitBtn = $form.find('button[type="submit"]'); |
| 40 |
|
| 41 |
// Disable submit button and add loading class |
| 42 |
$submitBtn.prop('disabled', true); |
| 43 |
$submitBtn.css('opacity', '0.6'); |
| 44 |
$submitBtn.css('pointer-events', 'none'); |
| 45 |
}); |
| 46 |
|
| 47 |
// Currency preview (decimal field id matches currency.php: #decimal_places) |
| 48 |
$('#currency, #currency_position, #thousand_separator, #decimal_separator, #decimal_places, #number_of_decimals').on('change input', function() { |
| 49 |
YatraSetupWizard.updateCurrencyPreview(); |
| 50 |
}); |
| 51 |
|
| 52 |
// Skip step confirmation (matches .btn-skip links in wizard templates) |
| 53 |
$(document).on('click', '.yatra-setup-content a.btn-skip', function(e) { |
| 54 |
var msg = (typeof yatraSetupWizard !== 'undefined' && yatraSetupWizard.skipConfirmMessage) |
| 55 |
? yatraSetupWizard.skipConfirmMessage |
| 56 |
: 'Skip this step without saving?'; |
| 57 |
if (!window.confirm(msg)) { |
| 58 |
e.preventDefault(); |
| 59 |
return false; |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
// Autosave current step when navigating via links (Back / completed steps). |
| 64 |
$(document).on('click', '.yatra-setup-wrapper .yatra-setup-steps a, .yatra-setup-content a.wizard-footer-back', function(e) { |
| 65 |
var $link = $(this); |
| 66 |
var href = $link.attr('href'); |
| 67 |
if (!href) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
var $form = $('.yatra-setup-content form.wizard-step').first(); |
| 72 |
if ($form.length === 0) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// Do not interfere with modifier keys / new tab. |
| 77 |
if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey || e.button !== 0) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
e.preventDefault(); |
| 82 |
|
| 83 |
// Set redirect target and submit so PHP saves before redirecting. |
| 84 |
$form.find('input[name="yatra_redirect_to"]').val(href); |
| 85 |
$form.trigger('submit'); |
| 86 |
return false; |
| 87 |
}); |
| 88 |
}, |
| 89 |
|
| 90 |
/** |
| 91 |
* Initialize form validation |
| 92 |
*/ |
| 93 |
initFormValidation: function() { |
| 94 |
// Number inputs validation |
| 95 |
$('input[type="number"]').on('input', function() { |
| 96 |
var $input = $(this); |
| 97 |
var min = parseFloat($input.attr('min')); |
| 98 |
var max = parseFloat($input.attr('max')); |
| 99 |
var value = parseFloat($input.val()); |
| 100 |
|
| 101 |
if (!isNaN(min) && value < min) { |
| 102 |
$input.val(min); |
| 103 |
} |
| 104 |
if (!isNaN(max) && value > max) { |
| 105 |
$input.val(max); |
| 106 |
} |
| 107 |
}); |
| 108 |
|
| 109 |
// Required fields |
| 110 |
$('.yatra-setup-content form').on('submit', function(e) { |
| 111 |
var isValid = true; |
| 112 |
var $form = $(this); |
| 113 |
|
| 114 |
// Remove previous error messages |
| 115 |
$form.find('.error-message').remove(); |
| 116 |
|
| 117 |
// Check required fields |
| 118 |
$form.find('[required]').each(function() { |
| 119 |
var $field = $(this); |
| 120 |
if (!$field.val() || $field.val().trim() === '') { |
| 121 |
isValid = false; |
| 122 |
$field.addClass('error'); |
| 123 |
$field.after('<p class="error-message" style="color: #d63638; margin-top: 5px;">This field is required.</p>'); |
| 124 |
} else { |
| 125 |
$field.removeClass('error'); |
| 126 |
} |
| 127 |
}); |
| 128 |
|
| 129 |
if (!isValid) { |
| 130 |
e.preventDefault(); |
| 131 |
// Scroll to first error |
| 132 |
$('html, body').animate({ |
| 133 |
scrollTop: $form.find('.error').first().offset().top - 100 |
| 134 |
}, 500); |
| 135 |
} |
| 136 |
|
| 137 |
return isValid; |
| 138 |
}); |
| 139 |
}, |
| 140 |
|
| 141 |
/** |
| 142 |
* Update currency preview |
| 143 |
*/ |
| 144 |
updateCurrencyPreview: function() { |
| 145 |
var currency = $('#currency').val() || 'USD'; |
| 146 |
var position = $('#currency_position').val() || 'before'; |
| 147 |
var thousandSep = $('#thousand_separator').val() || ','; |
| 148 |
var decimalSep = $('#decimal_separator').val() || '.'; |
| 149 |
var decimalsRaw = $('#decimal_places').val(); |
| 150 |
if (decimalsRaw === undefined || decimalsRaw === '') { |
| 151 |
decimalsRaw = $('#number_of_decimals').val(); |
| 152 |
} |
| 153 |
var decimals = parseInt(decimalsRaw, 10); |
| 154 |
if (isNaN(decimals)) { |
| 155 |
decimals = 2; |
| 156 |
} |
| 157 |
|
| 158 |
// Get currency symbol (simplified) |
| 159 |
var symbols = { |
| 160 |
'USD': '$', |
| 161 |
'EUR': '€', |
| 162 |
'GBP': '£', |
| 163 |
'AUD': '$', |
| 164 |
'CAD': '$', |
| 165 |
'JPY': '¥', |
| 166 |
'INR': '₹', |
| 167 |
'CNY': '¥', |
| 168 |
'CHF': 'CHF', |
| 169 |
'NZD': '$' |
| 170 |
}; |
| 171 |
var symbol = symbols[currency] || currency; |
| 172 |
|
| 173 |
// Format example number |
| 174 |
var number = 1234.56; |
| 175 |
var parts = number.toFixed(decimals).split('.'); |
| 176 |
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandSep); |
| 177 |
var formatted = parts.join(decimalSep); |
| 178 |
|
| 179 |
// Apply position |
| 180 |
var preview = position === 'before' ? symbol + formatted : formatted + symbol; |
| 181 |
|
| 182 |
// Show preview if element exists |
| 183 |
var $anchor = $('#decimal_places').length ? $('#decimal_places').closest('.form-group') : $('#currency').closest('.form-group'); |
| 184 |
if ($('#currency-preview').length === 0 && $anchor.length) { |
| 185 |
$anchor.after( |
| 186 |
'<div class="form-group" id="currency-preview-wrap"><label class="form-label">Preview</label>' + |
| 187 |
'<p class="form-control" style="background:#f9fafb;font-weight:600;" id="currency-preview">' + preview + '</p></div>' |
| 188 |
); |
| 189 |
} else if ($('#currency-preview').length) { |
| 190 |
$('#currency-preview').text(preview); |
| 191 |
} |
| 192 |
}, |
| 193 |
|
| 194 |
/** |
| 195 |
* Show notification |
| 196 |
*/ |
| 197 |
showNotification: function(message, type) { |
| 198 |
type = type || 'success'; |
| 199 |
var $notice = $('<div class="notice notice-' + type + ' is-dismissible"><p>' + message + '</p></div>'); |
| 200 |
|
| 201 |
$('.yatra-setup-content').prepend($notice); |
| 202 |
|
| 203 |
// Auto dismiss after 5 seconds |
| 204 |
setTimeout(function() { |
| 205 |
$notice.fadeOut(function() { |
| 206 |
$(this).remove(); |
| 207 |
}); |
| 208 |
}, 5000); |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
// Initialize on document ready |
| 213 |
$(document).ready(function() { |
| 214 |
YatraSetupWizard.init(); |
| 215 |
}); |
| 216 |
|
| 217 |
})(jQuery); |
| 218 |
|