| 1 |
/** |
| 2 |
* Simple/Advanced Mode Toggle Handler |
| 3 |
* Handles switching between settings modes via AJAX. |
| 4 |
*/ |
| 5 |
|
| 6 |
(function($) { |
| 7 |
'use strict'; |
| 8 |
|
| 9 |
$(document).ready(function() { |
| 10 |
initModeToggle(); |
| 11 |
}); |
| 12 |
|
| 13 |
/** |
| 14 |
* Initialize the mode toggle buttons. |
| 15 |
*/ |
| 16 |
function initModeToggle() { |
| 17 |
var $toggleContainer = $('.abj404-mode-toggle'); |
| 18 |
if (!$toggleContainer.length) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
var $buttons = $toggleContainer.find('.abj404-mode-btn'); |
| 23 |
var nonce = $toggleContainer.data('nonce'); |
| 24 |
|
| 25 |
// Handle "Switch to Advanced Mode" link in the hint |
| 26 |
$(document).on('click', '.abj404-switch-to-advanced', function(e) { |
| 27 |
e.preventDefault(); |
| 28 |
// Trigger click on the Advanced Mode button |
| 29 |
var $advancedBtn = $toggleContainer.find('.abj404-mode-btn[data-mode="advanced"]'); |
| 30 |
if ($advancedBtn.length && !$advancedBtn.hasClass('active')) { |
| 31 |
$advancedBtn.trigger('click'); |
| 32 |
} |
| 33 |
}); |
| 34 |
|
| 35 |
$buttons.on('click', function(e) { |
| 36 |
e.preventDefault(); |
| 37 |
|
| 38 |
var $btn = $(this); |
| 39 |
var mode = $btn.data('mode'); |
| 40 |
|
| 41 |
// Don't do anything if already active |
| 42 |
if ($btn.hasClass('active')) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
// Disable buttons during request |
| 47 |
$buttons.prop('disabled', true); |
| 48 |
$btn.addClass('loading'); |
| 49 |
|
| 50 |
$.ajax({ |
| 51 |
url: ajaxurl, |
| 52 |
type: 'POST', |
| 53 |
// A request with no deadline never reaches the error handler that |
| 54 |
// re-enables the control this call disabled, so the page stays stuck. |
| 55 |
timeout: 30000, |
| 56 |
data: { |
| 57 |
action: 'abj404_toggle_settings_mode', |
| 58 |
mode: mode, |
| 59 |
nonce: nonce |
| 60 |
}, |
| 61 |
success: function(response) { |
| 62 |
// Validate shape before reading fields: an upstream |
| 63 |
// gateway / WAF / plugin conflict can return HTML or |
| 64 |
// a non-object body even when the request asked for |
| 65 |
// JSON. Treat any malformed body as a failed save. |
| 66 |
if (response && typeof response === 'object' && response.success === true) { |
| 67 |
// Reload the page to show the new mode |
| 68 |
window.location.reload(); |
| 69 |
return; |
| 70 |
} |
| 71 |
var serverMsg = ''; |
| 72 |
if (response && typeof response === 'object' && response.data) { |
| 73 |
if (typeof response.data === 'string') { |
| 74 |
serverMsg = response.data; |
| 75 |
} else if (response.data.message) { |
| 76 |
serverMsg = response.data.message; |
| 77 |
} |
| 78 |
} |
| 79 |
alert(serverMsg || 'Failed to update settings mode'); |
| 80 |
$buttons.prop('disabled', false); |
| 81 |
$btn.removeClass('loading'); |
| 82 |
}, |
| 83 |
error: function(jqXHR, textStatus, errorThrown) { |
| 84 |
// Shared describe-and-record seam (abj404-admin-ajax.js), guarded |
| 85 |
// so a missing asset degrades to today's generic message instead |
| 86 |
// of throwing out of the error handler and showing nothing at all. |
| 87 |
var errMsg = 'An error occurred while updating settings mode'; |
| 88 |
if (typeof abj404AdminAjaxErrorMessage === 'function') { |
| 89 |
errMsg = abj404AdminAjaxErrorMessage(jqXHR, { |
| 90 |
fallback: errMsg, |
| 91 |
source: 'settings-mode-toggle', |
| 92 |
errorThrown: errorThrown |
| 93 |
}); |
| 94 |
} |
| 95 |
alert(errMsg); |
| 96 |
$buttons.prop('disabled', false); |
| 97 |
$btn.removeClass('loading'); |
| 98 |
} |
| 99 |
}); |
| 100 |
}); |
| 101 |
|
| 102 |
// Keyboard accessibility |
| 103 |
$buttons.on('keydown', function(e) { |
| 104 |
if (e.key === 'Enter' || e.key === ' ') { |
| 105 |
e.preventDefault(); |
| 106 |
$(this).trigger('click'); |
| 107 |
} |
| 108 |
}); |
| 109 |
} |
| 110 |
|
| 111 |
})(jQuery); |
| 112 |
|