/** * Login Shortcode JavaScript * * Production-optimized login form interactions and functionality * * @package Yatra * @version 1.0.0 */ // Translation helpers — idempotent shim matching the other frontend bundles. (function () { if (typeof window.__ === 'function') return; if (window.wp && window.wp.i18n && typeof window.wp.i18n.__ === 'function') { window.__ = function (text, domain) { return window.wp.i18n.__(text, domain || 'yatra'); }; window._n = function (s, p, n, domain) { return window.wp.i18n._n(s, p, n, domain || 'yatra'); }; window._x = function (text, ctx, domain) { return window.wp.i18n._x(text, ctx, domain || 'yatra'); }; window.sprintf = window.sprintf || (window.wp.i18n.sprintf || function (fmt) { return fmt; }); } else { window.__ = function (text) { return text; }; window._n = function (s, p, n) { return n === 1 ? s : p; }; window._x = function (text) { return text; }; window.sprintf = window.sprintf || function (fmt) { return fmt; }; } })(); (function($) { 'use strict'; // Performance: Cache DOM elements let $document = $(document); let $window = $(window); // Performance: Debounce function for performance function debounce(func, wait) { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } // Performance: Throttle function for scroll events function throttle(func, limit) { let inThrottle; return function() { const args = arguments; const context = this; if (!inThrottle) { func.apply(context, args); inThrottle = true; setTimeout(() => inThrottle = false, limit); } }; } // Initialize when DOM is ready $document.ready(function() { // Performance: Check if login form exists before initializing if ($('.yatra-login-form').length === 0) { return; } // Initialize components initPasswordToggles(); initAjaxLogin(); initAccessibility(); initPerformanceOptimizations(); }); /** * Initialize password toggle functionality with performance optimizations */ function initPasswordToggles() { const passwordToggles = $('.yatra-password-toggle'); // Performance: Use event delegation for better performance $document.on('click', '.yatra-password-toggle', function(e) { e.preventDefault(); const $toggle = $(this); const targetId = $toggle.data('target'); const $input = targetId ? $('#' + targetId) : $toggle.siblings('.yatra-form-input'); if (!$input.length) { console.warn('Password input not found for toggle'); return; } const $eyeIcon = $toggle.find('.yatra-eye-icon'); const $eyeOffIcon = $toggle.find('.yatra-eye-off-icon'); try { const isPassword = $input.attr('type') === 'password'; if (isPassword) { $input.attr('type', 'text'); $eyeIcon.hide(); $eyeOffIcon.show(); $toggle.attr('aria-label', __('Hide password', 'yatra')); } else { $input.attr('type', 'password'); $eyeIcon.show(); $eyeOffIcon.hide(); $toggle.attr('aria-label', __('Show password', 'yatra')); } } catch (error) { console.error('Error toggling password visibility:', error); } }); } /** * Enhanced form validation function */ function validateLoginForm($username, $password) { const username = $username.val().trim(); const password = $password.val().trim(); if (!username) { return { valid: false, message: yatra_ajax?.strings?.validation_error || __('Please enter your email or username.', 'yatra') }; } if (!password) { return { valid: false, message: yatra_ajax?.strings?.validation_error || __('Please enter your password.', 'yatra') }; } if (username.length > 60) { return { valid: false, message: __('Username is too long.', 'yatra') }; } if (password.length > 72) { return { valid: false, message: __('Password is too long.', 'yatra') }; } return { valid: true }; } /** * Initialize accessibility features */ function initAccessibility() { // Add ARIA labels and improve keyboard navigation $document.on('keydown', '.yatra-form-input', function(e) { if (e.key === 'Enter') { const $form = $(this).closest('form'); if ($form.length) { $form.trigger('submit'); } } }); // Focus management for error messages $document.on('click', '.yatra-form-error', function() { $(this).fadeOut(200, function() { $(this).remove(); }); }); } /** * Initialize performance optimizations */ function initPerformanceOptimizations() { // Lazy load error styles if (!$('#yatra-login-error-styles').length) { $('