PluginProbe
404 Solution / trunk
404 Solution vtrunk
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / js / themePreview.js

themePreview.js in 404 Solution trunk, at includes/js/themePreview.js

65 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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