converted settings to module(s)

This commit is contained in:
azivner 2017-11-04 14:31:53 -04:00
parent fd48a97c6d
commit 819c01bb04
2 changed files with 134 additions and 85 deletions

View File

@ -1,103 +1,152 @@
function displaySettings() { const settings = (function() {
$.ajax({ const dialogEl = $("#settings-dialog");
url: baseApiUrl + 'settings', const tabsEl = $("#settings-tabs");
type: 'GET',
success: result => {
$("#encryption-timeout-in-seconds").val(result['encryption_session_timeout']);
$("#history-snapshot-time-interval-in-seconds").val(result['history_snapshot_time_interval']);
},
error: () => alert("Error getting settings.")
});
$("#settings-dialog").dialog({ const settingModules = [];
modal: true,
width: 600
});
$("#settings-tabs").tabs(); function addModule(module) {
} settingModules.push(module);
$("#change-password-form").submit(() => {
const oldPassword = $("#old-password").val();
const newPassword1 = $("#new-password1").val();
const newPassword2 = $("#new-password2").val();
$("#old-password").val('');
$("#new-password1").val('');
$("#new-password2").val('');
if (newPassword1 !== newPassword2) {
alert("New passwords are not the same.");
return false;
} }
$.ajax({ async function showDialog() {
url: baseApiUrl + 'password/change', const settings = await $.ajax({
type: 'POST', url: baseApiUrl + 'settings',
data: JSON.stringify({ type: 'GET',
'current_password': oldPassword, error: () => error("Error getting settings.")
'new_password': newPassword1 });
}),
contentType: "application/json",
success: result => {
if (result.success) {
// encryption password changed so current encryption session is invalid and needs to be cleared
resetEncryptionSession();
glob.encryptedDataKey = result.new_encrypted_data_key; dialogEl.dialog({
modal: true,
width: 600
});
alert("Password has been changed."); tabsEl.tabs();
$("#settings-dialog").dialog('close'); for (module of settingModules) {
} module.settingsLoaded(settings);
else { }
alert(result.message); }
}
}, function saveSettings(settingName, settingValue) {
error: () => alert("Error occurred during changing password.") return $.ajax({
url: baseApiUrl + 'settings',
type: 'POST',
data: JSON.stringify({
name: settingName,
value: settingValue
}),
contentType: "application/json",
success: () => {
message("Settings change have been saved.");
},
error: () => alert("Error occurred during saving settings change.")
});
}
return {
showDialog,
saveSettings,
addModule
};
})();
settings.addModule((function() {
const formEl = $("#change-password-form");
const oldPasswordEl = $("#old-password");
const newPassword1El = $("#new-password1");
const newPassword2El = $("#new-password2");
function settingsLoaded(settings) {
}
formEl.submit(() => {
const oldPassword = oldPasswordEl.val();
const newPassword1 = newPassword1El.val();
const newPassword2 = newPassword2El.val();
oldPasswordEl.val('');
newPassword1El.val('');
newPassword2El.val('');
if (newPassword1 !== newPassword2) {
alert("New passwords are not the same.");
return false;
}
$.ajax({
url: baseApiUrl + 'password/change',
type: 'POST',
data: JSON.stringify({
'current_password': oldPassword,
'new_password': newPassword1
}),
contentType: "application/json",
success: result => {
if (result.success) {
// encryption password changed so current encryption session is invalid and needs to be cleared
resetEncryptionSession();
glob.encryptedDataKey = result.new_encrypted_data_key;
alert("Password has been changed.");
$("#settings-dialog").dialog('close');
}
else {
alert(result.message);
}
},
error: () => alert("Error occurred during changing password.")
});
return false;
}); });
return false; return {
}); settingsLoaded
};
})());
$("#encryption-timeout-form").submit(() => { settings.addModule((function() {
const encryptionTimeout = $("#encryption-timeout-in-seconds").val(); const formEl = $("#encryption-timeout-form");
const encryptionTimeoutEl = $("#encryption-timeout-in-seconds");
const settingName = 'encryption_session_timeout';
$.ajax({ function settingsLoaded(settings) {
url: baseApiUrl + 'settings', encryptionTimeoutEl.val(settings[settingName]);
type: 'POST', }
data: JSON.stringify({
name: 'encryption_session_timeout',
value: encryptionTimeout
}),
contentType: "application/json",
success: () => {
alert("Encryption timeout has been changed.");
formEl.submit(() => {
const encryptionTimeout = encryptionTimeoutEl.val();
settings.saveSettings(settingName, encryptionTimeout).then(() => {
glob.encryptionSessionTimeout = encryptionTimeout; glob.encryptionSessionTimeout = encryptionTimeout;
}, });
error: () => alert("Error occurred during changing encryption timeout.")
return false;
}); });
return false; return {
}); settingsLoaded
};
})());
$("#history-snapshot-time-interval-form").submit(() => { settings.addModule((function () {
const historySnapshotTimeInterval = $("#history-snapshot-time-interval-in-seconds").val(); const formEl = $("#history-snapshot-time-interval-form");
const timeIntervalEl = $("#history-snapshot-time-interval-in-seconds");
const settingName = 'history_snapshot_time_interval';
$.ajax({ function settingsLoaded(settings) {
url: baseApiUrl + 'settings', timeIntervalEl.val(settings[settingName]);
type: 'POST', }
data: JSON.stringify({
name: 'history_snapshot_time_interval', formEl.submit(() => {
value: historySnapshotTimeInterval settings.saveSettings(settingName, timeIntervalEl.val());
}),
contentType: "application/json", return false;
success: () => {
alert("History snapshot time interval has been changed.");
},
error: () => alert("Error occurred during changing history snapshot time interval.")
}); });
return false; return {
}); settingsLoaded
};
})());

View File

@ -24,7 +24,7 @@
<button class="btn btn-xs" onclick="syncNow();">Sync now (<span id="changesToPushCount">0</span>)</button> <button class="btn btn-xs" onclick="syncNow();">Sync now (<span id="changesToPushCount">0</span>)</button>
<button class="btn btn-xs" onclick="displaySettings();">Settings</button> <button class="btn btn-xs" onclick="settings.showDialog();">Settings</button>
<form action="logout" method="POST" style="display: inline;"> <form action="logout" method="POST" style="display: inline;">
<input type="submit" class="btn btn-xs" value="Logout"> <input type="submit" class="btn btn-xs" value="Logout">