From 6d56ad0001fc902c5d8c83284d1fe4c7d93f23ca Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Sat, 28 Oct 2017 15:21:47 +0200 Subject: [PATCH] Ask user for confirmation when they abandon an edit in progress. This fixes #17 --- assets/script.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/assets/script.js b/assets/script.js index d6b2c6d..d4fc3cd 100644 --- a/assets/script.js +++ b/assets/script.js @@ -13,6 +13,13 @@ function queryArgsFromForm(form) { return items.join('&'); } +function isEdited(form) { + for (const {name, value, defaultValue} of form.elements) { + if (name && (value !== defaultValue)) return true; + } + return false; +} + let hasBeenOpen = false; function openEditor() { const container = document.querySelector(".container"); @@ -91,8 +98,10 @@ function openEditor() { ev.preventDefault(); ev.stopPropagation(); - container.classList.remove('edit'); - form.reset(); + if (!isEdited(form) || confirm("Discard changes?")) { + container.classList.remove('edit'); + form.reset(); + } }); }