| 1 |
/** |
| 2 |
* MetaSync tab switcher for redirections / 404-monitor page. |
| 3 |
* |
| 4 |
* Extracted from admin/class-metasync-admin.php (Phase 5, #887). |
| 5 |
*/ |
| 6 |
jQuery(document).ready(function($) { |
| 7 |
// Function to switch to a specific tab |
| 8 |
function switchToTab(targetTab) { |
| 9 |
// Update active tab |
| 10 |
$('.metasync-tab-nav a').removeClass('active'); |
| 11 |
$('.metasync-tab-nav a[data-tab="' + targetTab + '"]').addClass('active'); |
| 12 |
|
| 13 |
// Show target content |
| 14 |
$('.metasync-tab-content').removeClass('active'); |
| 15 |
$('#' + targetTab + '-content').addClass('active'); |
| 16 |
} |
| 17 |
|
| 18 |
// Initialize tabs immediately |
| 19 |
function initializeTabs() { |
| 20 |
// Check URL parameter on page load |
| 21 |
var urlParams = new URLSearchParams(window.location.search); |
| 22 |
var currentTab = urlParams.get('tab'); |
| 23 |
|
| 24 |
// Always ensure a tab is active |
| 25 |
if (currentTab && (currentTab === 'redirections' || currentTab === '404-monitor')) { |
| 26 |
// Switch to the tab specified in URL |
| 27 |
switchToTab(currentTab); |
| 28 |
} else { |
| 29 |
// No tab parameter or invalid tab, ensure redirections is active |
| 30 |
switchToTab('redirections'); |
| 31 |
currentTab = 'redirections'; |
| 32 |
} |
| 33 |
|
| 34 |
// Clean up irrelevant pagination parameters based on active tab |
| 35 |
var needsCleanup = false; |
| 36 |
if (currentTab === '404-monitor') { |
| 37 |
if (urlParams.has('paged') || urlParams.has('paged_redir')) { |
| 38 |
urlParams.delete('paged'); |
| 39 |
urlParams.delete('paged_redir'); |
| 40 |
needsCleanup = true; |
| 41 |
} |
| 42 |
} else if (currentTab === 'redirections') { |
| 43 |
if (urlParams.has('paged') || urlParams.has('paged_404')) { |
| 44 |
urlParams.delete('paged'); |
| 45 |
urlParams.delete('paged_404'); |
| 46 |
needsCleanup = true; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Update URL if cleanup was needed |
| 51 |
if (needsCleanup) { |
| 52 |
var newUrl = window.location.pathname + '?' + urlParams.toString(); |
| 53 |
window.history.replaceState({}, '', newUrl); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Initialize tabs immediately |
| 58 |
initializeTabs(); |
| 59 |
|
| 60 |
// Also initialize after a short delay as backup |
| 61 |
setTimeout(initializeTabs, 100); |
| 62 |
|
| 63 |
// Handle tab switching on click |
| 64 |
$('.metasync-tab-nav a').on('click', function(e) { |
| 65 |
e.preventDefault(); |
| 66 |
|
| 67 |
var targetTab = $(this).data('tab'); |
| 68 |
switchToTab(targetTab); |
| 69 |
|
| 70 |
// Update URL without page reload and clean up pagination parameters |
| 71 |
var url = new URL(window.location); |
| 72 |
url.searchParams.set('tab', targetTab); |
| 73 |
|
| 74 |
// Remove all pagination parameters when switching tabs |
| 75 |
url.searchParams.delete('paged'); |
| 76 |
url.searchParams.delete('paged_404'); |
| 77 |
url.searchParams.delete('paged_redir'); |
| 78 |
|
| 79 |
window.history.pushState({}, '', url); |
| 80 |
}); |
| 81 |
}); |
| 82 |
|