populate the settings page correctly with existing values

This commit is contained in:
Stephen Birarda 2014-06-25 13:55:32 -07:00
parent f8dccad6ec
commit 6519d4028a
4 changed files with 54 additions and 24 deletions

View file

@ -25,11 +25,11 @@ setup_hifi_project(${TARGET_NAME} TRUE)
# remove and then copy the files for the webserver # remove and then copy the files for the webserver
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E remove_directory COMMAND "${CMAKE_COMMAND}" -E remove_directory
$<TARGET_FILE_DIR:${TARGET_NAME}>/resources) $<TARGET_FILE_DIR:${TARGET_NAME}>/resources/web)
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy_directory COMMAND "${CMAKE_COMMAND}" -E copy_directory
"${PROJECT_SOURCE_DIR}/resources" "${PROJECT_SOURCE_DIR}/resources/web"
$<TARGET_FILE_DIR:${TARGET_NAME}>/resources) $<TARGET_FILE_DIR:${TARGET_NAME}>/resources/web)
# link the shared hifi library # link the shared hifi library
include(${MACRO_DIR}/LinkHifiLibrary.cmake) include(${MACRO_DIR}/LinkHifiLibrary.cmake)

View file

@ -1,8 +1,12 @@
var Settings = {}; var Settings = {};
$(document).ready(function(){ $(document).ready(function(){
Handlebars.registerHelper('setKey', function(value){ Handlebars.registerHelper('setGroup', function(value){
this.key = value; this.group = value;
});
Handlebars.registerHelper('settingsValue', function(values, key, group){
return values[group][key];
}); });
var source = $('#template').html(); var source = $('#template').html();
@ -12,11 +16,13 @@ $(document).ready(function(){
}); });
function reloadSettings() { function reloadSettings() {
$.getJSON('describe.json', function(data){ $.getJSON('/settings.json', function(data){
$('#settings').html(Settings.template(data)); $('#settings').html(Settings.template(data));
}); });
} }
var SETTINGS_ERROR_MESSAGE = "There was a problem saving domain settings. Please try again!";
$('#settings').on('click', 'button', function(e){ $('#settings').on('click', 'button', function(e){
// grab a JSON representation of the form via form2js // grab a JSON representation of the form via form2js
var formJSON = form2js('settings-form', ".", false, null, true); var formJSON = form2js('settings-form', ".", false, null, true);
@ -26,17 +32,26 @@ $('#settings').on('click', 'button', function(e){
data: JSON.stringify(formJSON), data: JSON.stringify(formJSON),
contentType: 'application/json', contentType: 'application/json',
type: 'POST' type: 'POST'
}).done(function(){ }).done(function(data){
if (data.status == "success") {
showAlertMessage("Domain settings saved.", true);
} else {
showAlertMessage(SETTINGS_ERROR_MESSAGE, false);
}
reloadSettings();
}).fail(function(){ }).fail(function(){
var alertBox = $('.alert'); showAlertMessage(SETTINGS_ERROR_MESSAGE, false);
alertBox.attr('class', 'alert');
alertBox.addClass('alert-danger');
alertBox.html("There was a problem saving domain settings. Please try again!");
alertBox.fadeIn();
reloadSettings(); reloadSettings();
}); });
return false; return false;
}); });
function showAlertMessage(message, isSuccess) {
var alertBox = $('.alert');
alertBox.attr('class', 'alert');
alertBox.addClass(isSuccess ? 'alert-success' : 'alert-danger');
alertBox.html(message);
alertBox.fadeIn();
}

View file

@ -5,8 +5,8 @@
<form class="form-horizontal" id="settings-form" role="form"> <form class="form-horizontal" id="settings-form" role="form">
<script id="template" type="text/x-handlebars-template"> <script id="template" type="text/x-handlebars-template">
{{#each .}} {{#each descriptions}}
{{setKey @key}} {{setGroup @key}}
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">
<h3 class="panel-title">{{label}}</h3> <h3 class="panel-title">{{label}}</h3>
@ -15,7 +15,7 @@
{{#each settings}} {{#each settings}}
<div class="form-group"> <div class="form-group">
<label for="{{../key}}.{{@key}}" class="col-sm-2 control-label">{{label}}</label> <label for="{{../key}}.{{@key}}" class="col-sm-2 control-label">{{label}}</label>
<div class="col-sm-10"><input type="text" class="form-control" id="{{../key}}.{{@key}}" placeholder="{{placeholder}}"></div> <div class="col-sm-10"><input type="text" class="form-control" id="{{../group}}.{{@key}}" placeholder="{{placeholder}}" value="{{settingsValue ../../values @key ../group}}"></div>
<p class="help-block col-sm-offset-2 col-sm-10">{{help}}</p> <p class="help-block col-sm-offset-2 col-sm-10">{{help}}</p>
</div> </div>
{{/each}} {{/each}}

View file

@ -19,6 +19,7 @@
#include "DomainServerSettingsManager.h" #include "DomainServerSettingsManager.h"
const QString SETTINGS_DESCRIPTION_RELATIVE_PATH = "/resources/web/settings/describe.json"; const QString SETTINGS_DESCRIPTION_RELATIVE_PATH = "/resources/web/settings/describe.json";
const QString SETTINGS_CONFIG_FILE_RELATIVE_PATH = "/resources/config.json";
DomainServerSettingsManager::DomainServerSettingsManager() : DomainServerSettingsManager::DomainServerSettingsManager() :
_descriptionObject(), _descriptionObject(),
@ -29,7 +30,12 @@ DomainServerSettingsManager::DomainServerSettingsManager() :
descriptionFile.open(QIODevice::ReadOnly); descriptionFile.open(QIODevice::ReadOnly);
_descriptionObject = QJsonDocument::fromJson(descriptionFile.readAll()).object(); _descriptionObject = QJsonDocument::fromJson(descriptionFile.readAll()).object();
qDebug() << _descriptionObject;
// load the existing config file to get the current values
QFile configFile(QCoreApplication::applicationDirPath() + SETTINGS_CONFIG_FILE_RELATIVE_PATH);
configFile.open(QIODevice::ReadOnly);
_settingsMap = QJsonDocument::fromJson(configFile.readAll()).toVariant().toMap();
} }
bool DomainServerSettingsManager::handleHTTPRequest(HTTPConnection* connection, const QUrl &url) { bool DomainServerSettingsManager::handleHTTPRequest(HTTPConnection* connection, const QUrl &url) {
@ -41,12 +47,23 @@ bool DomainServerSettingsManager::handleHTTPRequest(HTTPConnection* connection,
// we recurse one level deep below each group for the appropriate setting // we recurse one level deep below each group for the appropriate setting
recurseJSONObjectAndOverwriteSettings(postedObject, _settingsMap, _descriptionObject); recurseJSONObjectAndOverwriteSettings(postedObject, _settingsMap, _descriptionObject);
// now let's look at the settingsMap? // store whatever the current _settingsMap is to file
qDebug() << QJsonDocument::fromVariant(_settingsMap).toJson();
persistToFile(); persistToFile();
return false; // return success to the caller
QString jsonSuccess = "{\"status\": \"success\"}";
connection->respond(HTTPConnection::StatusCode200, jsonSuccess.toUtf8(), "application/json");
return true;
} else if (connection->requestOperation() == QNetworkAccessManager::GetOperation && url.path() == "/settings.json") {
// this is a GET operation for our settings
// combine the description object and our current settings map
QJsonObject combinedObject;
combinedObject["descriptions"] = _descriptionObject;
combinedObject["values"] = QJsonDocument::fromVariant(_settingsMap).object();
connection->respond(HTTPConnection::StatusCode200, QJsonDocument(combinedObject).toJson(), "application/json");
return true;
} }
return false; return false;
@ -86,10 +103,8 @@ void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJ
} }
} }
const QString SETTINGS_JSON_FILE_RELATIVE_PATH = "/resources/config.json";
void DomainServerSettingsManager::persistToFile() { void DomainServerSettingsManager::persistToFile() {
QFile settingsFile(QCoreApplication::applicationDirPath() + SETTINGS_JSON_FILE_RELATIVE_PATH); QFile settingsFile(QCoreApplication::applicationDirPath() + SETTINGS_CONFIG_FILE_RELATIVE_PATH);
if (settingsFile.open(QIODevice::WriteOnly)) { if (settingsFile.open(QIODevice::WriteOnly)) {
settingsFile.write(QJsonDocument::fromVariant(_settingsMap).toJson()); settingsFile.write(QJsonDocument::fromVariant(_settingsMap).toJson());