| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  | const settings = (function() { | 
					
						
							|  |  |  |     const dialogEl = $("#settings-dialog"); | 
					
						
							|  |  |  |     const tabsEl = $("#settings-tabs"); | 
					
						
							| 
									
										
										
										
											2017-09-12 23:04:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     const settingModules = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function addModule(module) { | 
					
						
							|  |  |  |         settingModules.push(module); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-10 23:10:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     async function showDialog() { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:03:15 -04:00
										 |  |  |         glob.activeDialog = dialogEl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |         const settings = await $.ajax({ | 
					
						
							|  |  |  |             url: baseApiUrl + 'settings', | 
					
						
							|  |  |  |             type: 'GET', | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |             error: () => showError("Error getting settings.") | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-09-10 23:10:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |         dialogEl.dialog({ | 
					
						
							|  |  |  |             modal: true, | 
					
						
							| 
									
										
										
										
											2017-11-13 23:35:23 -05:00
										 |  |  |             width: 800 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-09-12 22:23:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |         tabsEl.tabs(); | 
					
						
							| 
									
										
										
										
											2017-09-12 22:23:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 21:02:56 -04:00
										 |  |  |         for (const module of settingModules) { | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |             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: () => { | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |                 showMessage("Settings change have been saved."); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             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) { | 
					
						
							| 
									
										
										
										
											2017-09-12 22:23:57 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     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) { | 
					
						
							| 
									
										
										
										
											2017-11-06 19:48:02 -05:00
										 |  |  |                     alert("Password has been changed. Trilium will be reloaded after you press OK."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |                     // encryption password changed so current encryption session is invalid and needs to be cleared
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:36:36 -05:00
										 |  |  |                     protected_session.resetProtectedSession(); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |                     showError(result.message); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2017-11-08 22:33:08 -05:00
										 |  |  |             error: () => showError("Error occurred during changing password.") | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2017-09-12 22:23:57 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-10 23:10:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     return { | 
					
						
							|  |  |  |         settingsLoaded | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | })()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | settings.addModule((function() { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |     const formEl = $("#protected-session-timeout-form"); | 
					
						
							|  |  |  |     const protectedSessionTimeoutEl = $("#protected-session-timeout-in-seconds"); | 
					
						
							|  |  |  |     const settingName = 'protected_session_timeout'; | 
					
						
							| 
									
										
										
										
											2017-09-12 23:04:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     function settingsLoaded(settings) { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |         protectedSessionTimeoutEl.val(settings[settingName]); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-12 23:04:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     formEl.submit(() => { | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |         const protectedSessionTimeout = protectedSessionTimeoutEl.val(); | 
					
						
							| 
									
										
										
										
											2017-09-23 10:59:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-14 22:44:45 -05:00
										 |  |  |         settings.saveSettings(settingName, protectedSessionTimeout).then(() => { | 
					
						
							|  |  |  |             protected_session.setProtectedSessionTimeout(protectedSessionTimeout); | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2017-09-12 23:04:17 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     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; | 
					
						
							| 
									
										
										
										
											2017-09-24 20:50:14 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-13 23:35:23 -05:00
										 |  |  |     return { | 
					
						
							|  |  |  |         settingsLoaded | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | })()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | settings.addModule((function () { | 
					
						
							|  |  |  |     const buildDateEl = $("#build-date"); | 
					
						
							|  |  |  |     const buildRevisionEl = $("#build-revision"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function settingsLoaded(settings) { | 
					
						
							|  |  |  |         buildDateEl.html(settings['buildDate']); | 
					
						
							|  |  |  |         buildRevisionEl.html(settings['buildRevision']); | 
					
						
							|  |  |  |         buildRevisionEl.attr('href', 'https://github.com/zadam/trilium/commit/' + settings['buildRevision']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 14:31:53 -04:00
										 |  |  |     return { | 
					
						
							|  |  |  |         settingsLoaded | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | })()); |