Reduce complexity of checking whether system should be rebooted after RAM Disk option change.

Also fix the issue when RAM Disk option is changed, but there is an input error and thus on subsequent form submitting there is no question about rebooting.
Small JavaScript cleanup and refactoring.
This commit is contained in:
Nazar Mokrynskyi 2017-07-04 09:32:01 +03:00
parent 2504e3f1bc
commit 19be2fc28b
2 changed files with 37 additions and 44 deletions

View File

@ -748,38 +748,44 @@ function interceptGET() {
});
}
// Convert a GET argument list such as ?name=fred&action=delete into an array of POST
// parameters such as [[name, fred],[action, delete]]
// Convert a GET argument list such as ?name=fred&action=delete into an object of POST
// parameters such as {name : fred, action : delete}
function get2post(getargs) {
var arglist = new Array();
var argdict = {};
var argarray = getargs.split('&');
for (var i=0;i<argarray.length;i++) {
var thisarg = argarray[i].split('=');
var arg = new Array(thisarg[0], thisarg[1]);
arglist[i] = arg;
}
argarray.forEach(function(arg) {
arg = arg.split('=');
argdict[arg[0]] = arg[1];
});
return arglist;
return argdict;
}
// Create a form, add, the POST data and submit it
function postSubmit(data, target) {
var $form = $('<form>');
var form = $(document.createElement('form'));
$(form).attr("method", "POST");
$(form).attr("action", target);
for (var i=0;i<data.length;i++) {
var input = $("<input>").attr("type", "hidden").attr("name", data[i][0]).val(data[i][1]);
$(form).append($(input));
for (var name in data) {
$form.append(
$("<input>")
.attr("type", "hidden")
.attr("name", name)
.val(data[name])
);
}
// The CSRF magic is required because we will be viewing the results of the POST
var input = $("<input>").attr("type", "hidden").attr("name", "__csrf_magic").val(csrfMagicToken);
$(form).append($(input));
$(form).appendTo('body').submit();
$form
.attr("method", "POST")
.attr("action", target)
// The CSRF magic is required because we will be viewing the results of the POST
.append(
$("<input>")
.attr("type", "hidden")
.attr("name", "__csrf_magic")
.val(csrfMagicToken)
)
.appendTo('body')
.submit();
}

View File

@ -57,6 +57,8 @@ $pconfig['use_mfs_tmp_size'] = $config['system']['use_mfs_tmp_size'];
$pconfig['use_mfs_var_size'] = $config['system']['use_mfs_var_size'];
$pconfig['do_not_send_uniqueid'] = isset($config['system']['do_not_send_uniqueid']);
$use_mfs_tmpvar_before = $pconfig['use_mfs_tmpvar'];
$pconfig['powerd_ac_mode'] = "hadp";
if (!empty($config['system']['powerd_ac_mode'])) {
$pconfig['powerd_ac_mode'] = $config['system']['powerd_ac_mode'];
@ -291,6 +293,7 @@ if ($_POST) {
}
}
}
$use_mfs_tmpvar_after = $pconfig['use_mfs_tmpvar'];
$pgtitle = array(gettext("System"), gettext("Advanced"), gettext("Miscellaneous"));
$pglinks = array("", "system_advanced_admin.php", "@self");
@ -298,7 +301,6 @@ include("head.inc");
if ($input_errors) {
print_input_errors($input_errors);
unset($pconfig['doreboot']);
}
if ($changes_applied) {
@ -594,31 +596,16 @@ $form->add($section);
print $form;
$ramdisk_msg = gettext('The \"Use Ramdisk\" setting has been changed. This will cause the firewall\nto reboot immediately after the new setting is saved.\n\nPlease confirm.');?>
$ramdisk_msg = gettext('The \"Use Ramdisk\" setting has been changed. This will cause the firewall\nto reboot immediately after the new setting is saved.\n\nPlease confirm.');
$use_mfs_tmpvar_changed = $use_mfs_tmpvar_before !== $use_mfs_tmpvar_after && !$input_errors;
?>
<script type="text/javascript">
//<![CDATA[
events.push(function() {
// Record the state of the Use Ramdisk checkbox on page load
use_ramdisk = $('#use_mfs_tmpvar').prop('checked');
$('form').submit(function(event) {
// Has the Use ramdisk checkbox changed state?
if ($('#use_mfs_tmpvar').prop('checked') != use_ramdisk) {
if (confirm("<?=$ramdisk_msg?>")) {
$('form').append('<input type="hidden" name="doreboot" id="doreboot" value="yes"/>');
} else {
event.preventDefault();
}
}
});
drb = "<?=$pconfig['doreboot']?>";
if (drb == "yes") {
$('form').append("<input type=\"hidden\" name=\"override\" value=\"yes\" />");
$('form').get(0).setAttribute('action', 'diag_reboot.php');
$(form).submit();
// Has the Use ramdisk checkbox changed state?
if (<?=(int)$use_mfs_tmpvar_changed?> && confirm("<?=$ramdisk_msg?>")) {
postSubmit({override : 'yes'}, 'diag_reboot.php')
}
// source track timeout field is disabled if sticky connections not enabled