From 35d13308b99191a7168711b4fb4aac04cba9602e Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Fri, 1 Dec 2017 16:37:41 +0100 Subject: [PATCH] Replace use of `alert()`, for #47 --- assets/script.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/assets/script.js b/assets/script.js index 3eb9f8f..f4457f1 100644 --- a/assets/script.js +++ b/assets/script.js @@ -52,6 +52,33 @@ function loginDialog(loginUrl) { }); } +function alertAsync(message) { + const dialogHtml = ""; + + const dialog = document.createElement("div"); + dialog.className = "modal-block"; + dialog.innerHTML = dialogHtml; + + const messageNode = dialog.querySelector(".message"); + const dismiss = dialog.querySelector("button"); + + messageNode.textContent = message; + + document.body.appendChild(dialog); + dismiss.focus(); + + function remove() { + document.body.removeChild(dialog); + } + + return new Promise((resolve, reject) => { + dismiss.addEventListener("click", () => { + remove(); + resolve(); + }); + }); +} + let hasBeenOpen = false; function openEditor() { const container = document.querySelector(".container"); @@ -139,14 +166,15 @@ function openEditor() { autosizeTextarea(textarea, shadow); if (result.conflict) { - alert("Your edit came into conflict with another change and has not been saved.\n" + + return alertAsync("Your edit came into conflict with another change " + + "and has not been saved.\n" + "Please resolve the merge conflict and save again."); } }); }).catch(err => { textarea.disabled = false; console.error(err); - alert(err); + return alertAsync(err.toString()); }); });