Fix form input highlights on initial load/ajax requests - add alert settings - slack integration

This commit is contained in:
Akkadius 2017-09-02 09:59:36 -05:00
parent 8a73d5bb5d
commit e4c8faaa57
10 changed files with 186 additions and 8 deletions

2
app.js
View File

@ -33,6 +33,8 @@ app.use('/dhcp_config_save', require('./routes/dhcp_config_save'));
app.use('/dhcp_start_stop_restart', require('./routes/dhcp_start_stop_restart'));
app.use('/api_examples', require('./routes/api_examples'));
app.use('/glass_settings', require('./routes/glass_settings'));
app.use('/glass_alerts', require('./routes/glass_alerts'));
app.use('/glass_alert_settings_save', require('./routes/glass_alert_settings_save'));
app.use('/glass_settings_save', require('./routes/glass_settings_save'));
/* API Routes */

View File

@ -2,6 +2,11 @@
"admin_user": "glassadmin",
"admin_password": "glassadmin",
"leases_file": "/var/lib/dhcp/dhcpd.leases",
"log_file": "/var/lib/dhcp.log",
"config_file": "/etc/dhcp/dhcpd.conf"
"log_file": "/var/log/dhcp.log",
"config_file": "/etc/dhcp/dhcpd.conf",
"shared_network_critical_threshold": 95,
"shared_network_warning_threshold": 80,
"lease_per_second_threshold": 0,
"slack_webhook_url": "",
"slack_alert_channel": ""
}

View File

@ -24,7 +24,7 @@ module.exports = {
return template.replace("[" + variable + "]", value);
},
form_body: function (id, inputs) {
return '<div class="form-group" id="' + id + '">' + inputs + '</div>';
return '<div id="' + id + '">' + inputs + '</div>';
},
form_input: function (title, input) {
return '<label>' + title + '</label><div class="form-group"><div class="form-line">' + input + '</div></div>';

View File

@ -4,6 +4,45 @@
$( document ).ajaxComplete(function( event, request, settings ) {
check_dashboard_active();
/*
* Form input highlighting events
*/
//On focus event
$('.form-control').focus(function () {
$(this).parent().addClass('focused');
});
//On focusout event
$('.form-control').focusout(function () {
var $this = $(this);
if ($this.parents('.form-group').hasClass('form-float')) {
if ($this.val() == '') { $this.parents('.form-line').removeClass('focused'); }
}
else {
$this.parents('.form-line').removeClass('focused');
}
});
//On label click
$('body').on('click', '.form-float .form-line .form-label', function () {
$(this).parent().find('input').focus();
});
//Not blank form
$('.form-control').each(function () {
if ($(this).val() !== '') {
$(this).parents('.form-line').addClass('focused');
}
});
$('.form-line').removeClass("focused");
});
$( document ).ready(function() {
$('.form-line').removeClass("focused");
});
function check_dashboard_active() {

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

@ -0,0 +1,46 @@
<div id="alerts_config">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="card">
<div class="header">
<h2>
Glass Alerts
</h2>
</div>
<div class="body">
[c_content]
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="card">
<div class="header">
<h2>
Notification Settings
</h2>
</div>
<div class="body">
[n_content]
</div>
</div>
</div>
</div>
<div id="glass_alerts_result"></div>
</div>
[save_button]
<script type="text/javascript">
function save_alarm_settings() {
glass_alert_settings = get_form_query_string("alerts_config");
$.post( "/glass_alert_settings_save", glass_alert_settings, function( data ) {
$( "#glass_alerts_result" ).html( data );
});
}
</script>

View File

@ -203,9 +203,6 @@
<!-- Custom Js -->
<script src="assets/js/admin.js"></script>
<!-- Demo Js -->
<script src="assets/js/demo.js"></script>
<script src="assets/js/pjax.js"></script>
<script src="assets/js/api-examples.js"></script>

View File

@ -0,0 +1,26 @@
/**
* Created by cmiles on 8/9/2017.
*/
var express = require('express');
var router = express.Router();
router.post('/', function(req, res, next) {
var request = req.body;
var json_file = require('jsonfile');
var glass_config = json_file.readFileSync('config/glass_config.json');
glass_config.shared_network_critical_threshold = request.shared_network_critical_threshold;
glass_config.shared_network_warning_threshold = request.shared_network_warning_threshold;
glass_config.lease_per_second_threshold = request.lease_per_second_threshold;
glass_config.slack_webhook_url = request.slack_webhook_url;
glass_config.slack_alert_channel = request.slack_alert_channel;
json_file.writeFile('./config/glass_config.json', glass_config, {spaces: 2}, function(err) {
console.error(err);
});
res.send('<script type="text/javascript">notification(\'Saved Alert Settings!\')</script>');
});
module.exports = router;

55
routes/glass_alerts.js Normal file
View File

@ -0,0 +1,55 @@
/**
* Created by cmiles on 8/9/2017.
*/
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
router.get('/', function(req, res, next) {
glass_settings_template = template_render.get_template("glass_alerts");
var json_file = require('jsonfile');
/* Read Config */
glass_config = json_file.readFileSync('config/glass_config.json');
/* Shared Network Alert Threshold (Critical) */
input = template_render.form_input('Shared Network Alert Threshold (Critical)', '<input type="input" class="form-control" id="shared_network_critical_threshold" placeholder="95%" value="' + glass_config.shared_network_critical_threshold + '">');
/* Shared Network Alert Threshold (Warning) */
input = input + template_render.form_input('Shared Network Alert Threshold (Warning)', '<input type="input" class="form-control" id="shared_network_warning_threshold" placeholder="80" value="' + glass_config.shared_network_warning_threshold + '">');
/* Leases Per Second Threshold */
input = input + template_render.form_input('Alert when Leases Per Second Reaches Below this Number', '<input type="input" class="form-control" id="lease_per_second_threshold" placeholder="0" value="' + glass_config.lease_per_second_threshold + '">');
// <div id="glass_settings_result"></div>
form_data = template_render.form_body("glass-alerts-form", input);
glass_settings_template = template_render.set_template_variable(glass_settings_template, "c_content", form_data);
/* Shared Network Alert Threshold (Critical) */
input = template_render.form_input('Slack Webhook URL <img src="images/slack-icon.png" style="height:25px; width: auto;"> ', '<input type="input" class="form-control" id="slack_webhook_url" placeholder="https://hooks.slack.com/services/xxx/xxx/xxx" value="' + glass_config.slack_webhook_url + '">');
/* Shared Network Alert Threshold (Warning) */
input = input + template_render.form_input('Slack Channel <img src="images/slack-icon.png" style="height:25px; width: auto;"> ', '<input type="input" class="form-control" id="slack_alert_channel" placeholder="#channel" value="' + glass_config.slack_alert_channel + '">');
// <div id="glass_settings_result"></div>
form_data = template_render.form_body("glass-notifications-form", input);
glass_settings_template = template_render.set_template_variable(glass_settings_template, "n_content", form_data);
glass_settings_template = template_render.set_template_variable(
glass_settings_template,
"save_button",
'<button type="button" class="btn btn-primary waves-effect" onclick="save_alarm_settings()"><i class="material-icons">add_alert</i> <span>Save Alarm Settings</span></button>'
);
res.send(template_render.get_index_template(glass_settings_template, req.url));
});
module.exports = router;

View File

@ -9,8 +9,16 @@ router.post('/', function(req, res, next) {
var request = req.body;
var json_file = require('jsonfile');
json_file.writeFile('./config/glass_config.json', request, {spaces: 2}, function(err) {
console.error(err);
var glass_config = json_file.readFileSync('config/glass_config.json');
glass_config.admin_user = request.admin_user;
glass_config.admin_password = request.admin_password;
glass_config.leases_file = request.leases_file;
glass_config.log_file = request.log_file;
glass_config.config_file = request.config_file;
json_file.writeFile('./config/glass_config.json', glass_config, {spaces: 2}, function(err) {
console.error(err)
});
res.send('<script type="text/javascript">notification(\'Saved Config!\')</script>');