From a3c0e160a2a7f836b5ba6b90a06f416ef398d88c Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Sat, 19 Sep 2020 12:36:04 -0500 Subject: [PATCH] convert state modal to ajax request --- web/ajax/modals/state.php | 86 ++++++++++++++++++++++++ web/skins/classic/includes/functions.php | 6 +- web/skins/classic/js/skin.js | 83 ++++++++++++++++++++++- web/skins/classic/js/skin.js.php | 1 + 4 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 web/ajax/modals/state.php diff --git a/web/ajax/modals/state.php b/web/ajax/modals/state.php new file mode 100644 index 000000000..a3b6734a3 --- /dev/null +++ b/web/ajax/modals/state.php @@ -0,0 +1,86 @@ +' .translate('Stop'). ''.PHP_EOL; + $content .= ''.PHP_EOL; +} else { + $content .= ''.PHP_EOL; +} + +$states = dbFetchAll('SELECT * FROM States'); +foreach ( $states as $state ) { + $selected = $state['IsActive'] ? 'selected="selected"' : ''; + + $content .= ''.PHP_EOL; +} + +?> + + diff --git a/web/skins/classic/includes/functions.php b/web/skins/classic/includes/functions.php index 584924c2d..377a97943 100644 --- a/web/skins/classic/includes/functions.php +++ b/web/skins/classic/includes/functions.php @@ -721,7 +721,7 @@ function getStatusBtnHTML($status) { if ( canEdit('System') ) { //$result .= ''.PHP_EOL; @@ -830,9 +830,6 @@ function xhtmlFooter() { global $view; global $skin; global $basename; - if ( canEdit('System') ) { - include("skins/$skin/views/state.php"); - } $skinJsPhpFile = getSkinFile('js/skin.js.php'); $cssJsFile = getSkinFile('js/'.$css.'.js'); $viewJsFile = getSkinFile('views/js/'.$basename.'.js'); @@ -856,7 +853,6 @@ function xhtmlFooter() { - diff --git a/web/skins/classic/js/skin.js b/web/skins/classic/js/skin.js index 264ddfaff..578b7f9d4 100644 --- a/web/skins/classic/js/skin.js +++ b/web/skins/classic/js/skin.js @@ -312,8 +312,9 @@ if ( currentView != 'none' && currentView != 'login' ) { $j.ajaxSetup({timeout: AJAX_TIMEOUT}); //sets timeout for all getJSON. $j(document).ready(function() { - // Load the lgoout modal into the dom + // Load the Logout and State modals into the dom getLogoutModal(); + if ( canEditSystem ) $j('#stateModalBtn').click(getStateModal); // Trigger autorefresh of the widget bar stats on the navbar if ( $j('.navbar').length ) { @@ -764,3 +765,83 @@ function getLogoutModal() { console.log("Response Text: " + jqxhr.responseText); }); } + +function getStateModal() { + $j.getJSON(thisUrl + '?request=modal&modal=state') + .done(function(data) { + if ( $j('#modalState').length ) { + $j('#modalState').replaceWith(data.html); + } else { + $j("body").append(data.html); + } + $j('#modalState').modal('show'); + manageStateModalBtns(); + }) + .fail(function(jqxhr, textStatus, error) { + console.log("Request Failed: " + textStatus + ", " + error); + console.log("Response Text: " + jqxhr.responseText); + }); +} + +function manageStateModalBtns() { + // Enable or disable the Delete button depending on the selected run state + $j("#runState").change(function() { + runstate = $j(this).val(); + + if ( (runstate == 'stop') || (runstate == 'restart') || (runstate == 'start') || (runstate == 'default') ) { + $j("#btnDelete").prop("disabled", true); + } else { + $j("#btnDelete").prop("disabled", false); + } + }); + + // Enable or disable the Save button when entering a new state + $j("#newState").keyup(function() { + length = $j(this).val().length; + if ( length < 1 ) { + $j("#btnSave").prop("disabled", true); + } else { + $j("#btnSave").prop("disabled", false); + } + }); + + + // Delete a state + $j("#btnDelete").click(function() { + stateStuff('delete', $j("#runState").val()); + }); + + + // Save a new state + $j("#btnSave").click(function() { + stateStuff('save', undefined, $j("#newState").val()); + }); + + // Change state + $j("#btnApply").click(function() { + stateStuff('state', $j("#runState").val()); + }); +} + +function stateStuff(action, runState, newState) { + // the state action will redirect to console + var formData = { + 'view': 'state', + 'action': action, + 'apply': 1, + 'runState': runState, + 'newState': newState + }; + + $j("#pleasewait").toggleClass("hidden"); + + $j.ajax({ + type: 'POST', + url: thisUrl, + data: formData, + dataType: 'html', + timeout: 0 + }).done(function(data) { + location.reload(); + }); +} diff --git a/web/skins/classic/js/skin.js.php b/web/skins/classic/js/skin.js.php index 2855b404d..08ed2d57d 100644 --- a/web/skins/classic/js/skin.js.php +++ b/web/skins/classic/js/skin.js.php @@ -72,3 +72,4 @@ var imagePrefix = ""; var auth_hash = ''; var auth_relay = ''; +var running = ;