| 1 |
/** |
| 2 |
* 404 Solution Theme Preview |
| 3 |
* |
| 4 |
* Provides live preview of theme changes on the Options page. |
| 5 |
* When the user changes the theme dropdown, the theme is applied immediately |
| 6 |
* to the current page for preview. The theme is not persisted until the user |
| 7 |
* clicks the "Save Settings" button. |
| 8 |
*/ |
| 9 |
|
| 10 |
(function($) { |
| 11 |
'use strict'; |
| 12 |
|
| 13 |
// Wait for DOM to be ready |
| 14 |
$(document).ready(function() { |
| 15 |
var themeSelect = $('#admin_theme'); |
| 16 |
|
| 17 |
if (themeSelect.length === 0) { |
| 18 |
// Theme selector not found, probably not on options page |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Apply theme to the html and body elements |
| 24 |
* @param {string} theme - Theme name (default, calm, mono, neon, obsidian) |
| 25 |
*/ |
| 26 |
function applyTheme(theme) { |
| 27 |
// Validate theme value |
| 28 |
var allowedThemes = ['default', 'calm', 'mono', 'neon', 'obsidian']; |
| 29 |
if (allowedThemes.indexOf(theme) === -1) { |
| 30 |
console.warn('Invalid theme selected:', theme); |
| 31 |
theme = 'default'; // Default fallback |
| 32 |
} |
| 33 |
|
| 34 |
// For 'default' theme, remove data-theme attribute to use WordPress defaults |
| 35 |
if (theme === 'default') { |
| 36 |
$('html, body').removeAttr('data-theme'); |
| 37 |
} else { |
| 38 |
// Apply the theme to both html and body elements to match CSS selectors |
| 39 |
$('html, body').attr('data-theme', theme); |
| 40 |
} |
| 41 |
|
| 42 |
// Also update the select value if it doesn't match |
| 43 |
if (themeSelect.val() !== theme) { |
| 44 |
themeSelect.val(theme); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle theme selection change |
| 50 |
*/ |
| 51 |
themeSelect.on('change', function() { |
| 52 |
var selectedTheme = $(this).val(); |
| 53 |
applyTheme(selectedTheme); |
| 54 |
}); |
| 55 |
|
| 56 |
// Initialize: ensure both html and body have the correct theme on page load |
| 57 |
// This is redundant with the PHP script but provides a fallback |
| 58 |
var initialTheme = themeSelect.val() || 'default'; |
| 59 |
if (!$('html').attr('data-theme') && initialTheme !== 'default') { |
| 60 |
applyTheme(initialTheme); |
| 61 |
} |
| 62 |
}); |
| 63 |
|
| 64 |
})(jQuery); |
| 65 |
|