convert state modal to ajax request

This commit is contained in:
Andrew Bauer 2020-09-19 12:36:04 -05:00
parent bf29357455
commit a3c0e160a2
4 changed files with 170 additions and 6 deletions

86
web/ajax/modals/state.php Normal file
View File

@ -0,0 +1,86 @@
<?php
//
// ZoneMinder web run state view file, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
if ( !canEdit('System') ) return;
$running = daemonCheck();
$content = '';
if ( $running ) {
$content .= '<option value="stop" selected="selected">' .translate('Stop'). '</option>'.PHP_EOL;
$content .= '<option value="restart">' .translate('Restart'). '</option>'.PHP_EOL;
} else {
$content .= '<option value="start" selected="selected">' .translate('Start'). '</option>'.PHP_EOL;
}
$states = dbFetchAll('SELECT * FROM States');
foreach ( $states as $state ) {
$selected = $state['IsActive'] ? 'selected="selected"' : '';
$content .= '<option value="' .validHtmlStr($state["Name"]). '" ' .$selected. '>'.PHP_EOL;
$content .= validHtmlStr($state['Name']).PHP_EOL;
$content .= '</option>'.PHP_EOL;
}
?>
<div id="modalState" class="modal fade" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Run State</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form class="" name="contentForm" method="get" action="?view=state">
<?php
// We have to manually insert the csrf key into the form when using a modal generated via ajax call
echo getCSRFinputHTML();
?>
<input type="hidden" name="view" value="state"/>
<input type="hidden" name="action" value="state"/>
<input type="hidden" name="apply" value="1"/>
<div class="form-group">
<label for="runState" class="col-md-3 col-form-label float-left">Change State</label>
<div class="col-md-9">
<select id="runState" name="runState" class="form-control">
<?php echo $content ?>
</select>
</div><!--col-md-9-->
</div><!--form-group-->
<div class="form-group">
<label for="newState" class="col-md-3 col-form-label float-left"><?php echo translate('NewState') ?></label>
<div class="col-md-9">
<input class="form-control" type="text" id="newState"/>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" id="btnApply"><?php echo translate('Apply') ?></button>
<button class="btn btn-primary" type="button" id="btnSave" disabled><?php echo translate('Save') ?></button>
<button class="btn btn-danger" type="button" id="btnDelete" disabled><?php echo translate('Delete') ?></button>
<p class="pull-left hidden" id="pleasewait"><?php echo translate('PleaseWait') ?></p>
</div>
</div>
</div>
</div>

View File

@ -721,7 +721,7 @@ function getStatusBtnHTML($status) {
if ( canEdit('System') ) {
//$result .= '<li class="nav-item dropdown">'.PHP_EOL;
$result .= '<form id="getStatusBtnHTML" class="form-inline">'.PHP_EOL;
$result .= '<button type="button" class="btn btn-default navbar-btn" data-toggle="modal" data-target="#modalState">' .$status. '</button>'.PHP_EOL;
$result .= '<button type="button" class="btn btn-default navbar-btn" id="stateModalBtn">' .$status. '</button>'.PHP_EOL;
$result .= '</form>'.PHP_EOL;
//$result .= '</li>'.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() {
<script src="<?php echo cache_bust('js/Server.js'); ?>"></script>
<script nonce="<?php echo $cspNonce; ?>">var $j = jQuery.noConflict();</script>
<script src="<?php echo cache_bust('skins/'.$skin.'/views/js/state.js') ?>"></script>
<?php
if ( $view == 'event' ) {
?>

View File

@ -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();
});
}

View File

@ -72,3 +72,4 @@ var imagePrefix = "<?php echo '?view=image&eid=' ?>";
var auth_hash = '<?php echo generateAuthHash(ZM_AUTH_HASH_IPS) ?>';
var auth_relay = '<?php echo get_auth_relay() ?>';
var running = <?php echo daemonCheck()?'true':'false' ?>;