Skip to content

Build your personal knowledge base with Trilium Notes

converted settings to module(s)

azivner819c01bb04d7
diff --git a/public/javascripts/settings.js b/public/javascripts/settings.js
index d87d95d1ff..144f8f8ef1 100644
--- a/public/javascripts/settings.js
+++ b/public/javascripts/settings.js
@@ -1,103 +1,152 @@
-function displaySettings() {
- $.ajax({
- url: baseApiUrl + 'settings',
- 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.")
- });
+const settings = (function() {
+ const dialogEl = $("#settings-dialog");
+ const tabsEl = $("#settings-tabs");
- $("#settings-dialog").dialog({
- modal: true,
- width: 600
- });
+ const settingModules = [];
+
+ function addModule(module) {
+ settingModules.push(module);
+ }
- $("#settings-tabs").tabs();
-}
+ async function showDialog() {
+ const settings = await $.ajax({
+ url: baseApiUrl + 'settings',
+ type: 'GET',
+ error: () => error("Error getting settings.")
+ });
-$("#change-password-form").submit(() => {
- const oldPassword = $("#old-password").val();
- const newPassword1 = $("#new-password1").val();
- const newPassword2 = $("#new-password2").val();
+ dialogEl.dialog({
+ modal: true,
+ width: 600
+ });
- $("#old-password").val('');
- $("#new-password1").val('');
- $("#new-password2").val('');
+ tabsEl.tabs();
- if (newPassword1 !== newPassword2) {
- alert("New passwords are not the same.");
- return false;
+ for (module of settingModules) {
+ module.settingsLoaded(settings);
+ }
+ }
+
+ function saveSettings(settingName, settingValue) {
+ 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) {
}
- $.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.")
+ 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
+ };
+})());
+
+settings.addModule((function() {
+ const formEl = $("#encryption-timeout-form");
+ const encryptionTimeoutEl = $("#encryption-timeout-in-seconds");
+ const settingName = 'encryption_session_timeout';
-$("#encryption-timeout-form").submit(() => {
- const encryptionTimeout = $("#encryption-timeout-in-seconds").val();
+ function settingsLoaded(settings) {
+ encryptionTimeoutEl.val(settings[settingName]);
+ }
- $.ajax({
- url: baseApiUrl + 'settings',
- 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;
- },
- error: () => alert("Error occurred during changing encryption timeout.")
+ });
+
+ return false;
});
- return false;
-});
-
-$("#history-snapshot-time-interval-form").submit(() => {
- const historySnapshotTimeInterval = $("#history-snapshot-time-interval-in-seconds").val();
-
- $.ajax({
- url: baseApiUrl + 'settings',
- type: 'POST',
- data: JSON.stringify({
- name: 'history_snapshot_time_interval',
- value: historySnapshotTimeInterval
- }),
- contentType: "application/json",
- success: () => {
- alert("History snapshot time interval has been changed.");
- },
- error: () => alert("Error occurred during changing history snapshot time interval.")
+ return {
+ settingsLoaded
+ };
+})());
+
+settings.addModule((function () {
+ const formEl = $("#history-snapshot-time-interval-form");
+ const timeIntervalEl = $("#history-snapshot-time-interval-in-seconds");
+ const settingName = 'history_snapshot_time_interval';
+
+ function settingsLoaded(settings) {
+ timeIntervalEl.val(settings[settingName]);
+ }
+
+ formEl.submit(() => {
+ settings.saveSettings(settingName, timeIntervalEl.val());
+
+ return false;
});
- return false;
-});
\ No newline at end of file
+ return {
+ settingsLoaded
+ };
+})());
\ No newline at end of file
diff --git a/views/index.ejs b/views/index.ejs
index 74257a8d5f..a8e2a87000 100644
--- a/views/index.ejs
+++ b/views/index.ejs
@@ -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="displaySettings();">Settings</button>
+ <button class="btn btn-xs" onclick="settings.showDialog();">Settings</button>
<form action="logout" method="POST" style="display: inline;">
<input type="submit" class="btn btn-xs" value="Logout">