aboutsummaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
Diffstat (limited to 'assets')
-rw-r--r--assets/js/edit.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/assets/js/edit.js b/assets/js/edit.js
new file mode 100644
index 0000000..6a652ad
--- /dev/null
+++ b/assets/js/edit.js
@@ -0,0 +1,60 @@
+let form;
+let unsavedChanges = false;
+
+window.onload = () => {
+ form = document.getElementById('update');
+ let inputs = form.elements;
+ for (i = 0; i < inputs.length; i++) {
+ inputs[i].onchange = changeHandler;
+ inputs[i].oninput = changeHandler;
+ }
+
+ form.onsubmit = submitHandler;
+ window.onkeydown = keyboardHandler;
+ window.onbeforeunload = confirmExit;
+};
+
+/**
+ * Manage a keyboardEvent
+ * @param {KeyboardEvent} e
+ */
+function keyboardHandler(e) {
+ if (e.composed) {
+ if (e.ctrlKey) {
+ // console.log(e.key);
+ switch (e.key) {
+ case 's':
+ form.submit();
+ return false;
+ }
+ }
+ }
+}
+
+/**
+ * Manage change event
+ * @param {Event} e
+ */
+function changeHandler(e) {
+ unsavedChanges = true;
+ // console.log({unsavedChanges});
+}
+
+/**
+ * Manage submit event
+ * @param {Event} e
+ */
+function submitHandler(e) {
+ unsavedChanges = false;
+}
+
+/**
+ * Manage a beforeUnloadEvent
+ * @param {BeforeUnloadEvent} e
+ */
+function confirmExit(e) {
+ // console.log({unsavedChanges});
+ if (unsavedChanges) {
+ return "You have attempted to leave this page. Are you sure?";
+ }
+} \ No newline at end of file