| 1 |
/** |
| 2 |
* Restore Defaults Handler |
| 3 |
* Confirms with the admin, then calls the AJAX endpoint that overwrites |
| 4 |
* abj404_settings with PluginLogic::getDefaultOptions(). |
| 5 |
*/ |
| 6 |
|
| 7 |
(function($) { |
| 8 |
'use strict'; |
| 9 |
|
| 10 |
$(document).ready(function() { |
| 11 |
initRestoreDefaults(); |
| 12 |
}); |
| 13 |
|
| 14 |
function initRestoreDefaults() { |
| 15 |
var $button = $('#abj404-restore-defaults'); |
| 16 |
if (!$button.length) { |
| 17 |
return; |
| 18 |
} |
| 19 |
var nonce = $button.data('nonce'); |
| 20 |
var $modal = $('#abj404-restore-defaults-modal'); |
| 21 |
var $confirm = $('#abj404-restore-defaults-confirm'); |
| 22 |
var $cancel = $('#abj404-restore-defaults-cancel, #abj404-restore-defaults-cancel-2'); |
| 23 |
|
| 24 |
$button.on('click', function(e) { |
| 25 |
e.preventDefault(); |
| 26 |
$modal.addClass('active'); |
| 27 |
$confirm.trigger('focus'); |
| 28 |
}); |
| 29 |
|
| 30 |
function closeModal() { |
| 31 |
$modal.removeClass('active'); |
| 32 |
} |
| 33 |
|
| 34 |
$cancel.on('click', closeModal); |
| 35 |
$modal.on('click', function(e) { |
| 36 |
if (e.target === this) { |
| 37 |
closeModal(); |
| 38 |
} |
| 39 |
}); |
| 40 |
|
| 41 |
$(document).on('keydown.abj404RestoreDefaults', function(e) { |
| 42 |
if (e.key === 'Escape' && $modal.hasClass('active')) { |
| 43 |
closeModal(); |
| 44 |
} |
| 45 |
}); |
| 46 |
|
| 47 |
$confirm.on('click', function(e) { |
| 48 |
e.preventDefault(); |
| 49 |
$confirm.prop('disabled', true).addClass('loading'); |
| 50 |
$cancel.prop('disabled', true); |
| 51 |
|
| 52 |
$.ajax({ |
| 53 |
url: ajaxurl, |
| 54 |
type: 'POST', |
| 55 |
// A request with no deadline never reaches the error handler that |
| 56 |
// re-enables the control this call disabled, so the page stays stuck. |
| 57 |
timeout: 30000, |
| 58 |
data: { |
| 59 |
action: 'abj404_restore_defaults', |
| 60 |
nonce: nonce |
| 61 |
}, |
| 62 |
success: function(response) { |
| 63 |
if (response && typeof response === 'object' && response.success === true) { |
| 64 |
window.location.reload(); |
| 65 |
return; |
| 66 |
} |
| 67 |
var serverMsg = ''; |
| 68 |
if (response && typeof response === 'object' && response.data) { |
| 69 |
if (typeof response.data === 'string') { |
| 70 |
serverMsg = response.data; |
| 71 |
} else if (response.data.message) { |
| 72 |
serverMsg = response.data.message; |
| 73 |
} |
| 74 |
} |
| 75 |
alert(serverMsg || 'Failed to restore defaults'); |
| 76 |
$confirm.prop('disabled', false).removeClass('loading'); |
| 77 |
$cancel.prop('disabled', false); |
| 78 |
}, |
| 79 |
error: function(jqXHR, textStatus, errorThrown) { |
| 80 |
// Shared describe-and-record seam (abj404-admin-ajax.js), guarded |
| 81 |
// so a missing asset degrades to today's generic message instead |
| 82 |
// of throwing out of the error handler and showing nothing at all. |
| 83 |
var errMsg = 'An error occurred while restoring defaults'; |
| 84 |
if (typeof abj404AdminAjaxErrorMessage === 'function') { |
| 85 |
errMsg = abj404AdminAjaxErrorMessage(jqXHR, { |
| 86 |
fallback: errMsg, |
| 87 |
source: 'restore-defaults', |
| 88 |
errorThrown: errorThrown |
| 89 |
}); |
| 90 |
} |
| 91 |
alert(errMsg); |
| 92 |
$confirm.prop('disabled', false).removeClass('loading'); |
| 93 |
$cancel.prop('disabled', false); |
| 94 |
} |
| 95 |
}); |
| 96 |
}); |
| 97 |
} |
| 98 |
|
| 99 |
})(jQuery); |
| 100 |
|