diff --git a/assets/script.js b/assets/script.js index ef9704d..c334866 100644 --- a/assets/script.js +++ b/assets/script.js @@ -20,88 +20,42 @@ function isEdited(form) { return false; } -const dialogTemplate = { - login: "", - alert: "", - confirm:"" -}; +function instantiate(templateId) { + return document.getElementById(templateId).firstElementChild.cloneNode(true); +} -function createPopup(templateId) { - const dialog = document.createElement("div"); - dialog.className = "modal-block"; - dialog.innerHTML = dialogTemplate[templateId]; - return dialog; +function popup(dialog) { + document.body.appendChild(dialog); + dialog.querySelector(".primary").focus(); + + return new Promise((resolve, reject) => { + function handler(ev) { + document.body.removeChild(dialog); + resolve(ev.target.getAttribute("data-value")); + } + + const buttons = dialog.querySelectorAll('.btn-row>*'); + for (let i = 0; i < buttons.length; ++i) + buttons[i].addEventListener("click", handler); + }); } function loginDialog(loginUrl) { - const dialog = createPopup("login"); - const remove = () => document.body.removeChild(dialog); - - const loginLink = dialog.querySelector("a"); - const dismiss = dialog.querySelector("button"); - - loginLink.setAttribute("href", loginUrl); - - document.body.appendChild(dialog); - loginLink.focus(); - - return new Promise((resolve, reject) => { - loginLink.addEventListener("click", () => { - remove(); - resolve(); - }); - - dismiss.addEventListener("click", () => { - remove(); - resolve(); - }); - }); + const dialog = instantiate("login"); + dialog.querySelector("a").setAttribute("href", loginUrl); + return popup(dialog); } function alertAsync(message) { - const dialog = createPopup("alert"); - const remove = () => document.body.removeChild(dialog); - - const messageNode = dialog.querySelector(".message"); - const dismiss = dialog.querySelector("button"); - - messageNode.textContent = message; - - document.body.appendChild(dialog); - dismiss.focus(); - - return new Promise((resolve, reject) => { - dismiss.addEventListener("click", () => { - remove(); - resolve(); - }); - }); + const dialog = instantiate("alert"); + dialog.querySelector(".message").textContent = message; + return popup(dialog); } function confirmAsync(message) { - const dialog = createPopup("confirm"); - const remove = () => document.body.removeChild(dialog); - - const messageNode = dialog.querySelector(".message"); - const btnNo = dialog.querySelector('button[name="no"]'); - const btnYes = dialog.querySelector('button[name="yes"]'); - - messageNode.textContent = message; - - document.body.appendChild(dialog); - btnNo.focus(); - - return new Promise((resolve, reject) => { - btnNo.addEventListener("click", () => { - remove(); - resolve(false); - }); - - btnYes.addEventListener("click", () => { - remove(); - resolve(true); - }); - }); + const dialog = instantiate("confirm"); + dialog.querySelector(".message").textContent = message; + return popup(dialog); } let hasBeenOpen = false; diff --git a/assets/style.css b/assets/style.css index 2b564b7..77e4fbb 100644 --- a/assets/style.css +++ b/assets/style.css @@ -459,8 +459,33 @@ input[type="search"] { } .popup>.btn-row { - padding: 16px; - background: #ccc; + display: flex; + flex-direction: row; + + font-family: -apple-system, BlinkMacSystemFont, + "Segoe UI", "Roboto", "Oxygen", + "Ubuntu", "Cantarell", "Fira Sans", + "Droid Sans", "Helvetica Neue", sans-serif; +} + +.btn-row>* { + padding: 8px 16px; + margin: 0; + background: white; + color: black; + border: none; + flex: 1; + + font-family: inherit; + cursor: pointer; +} +.btn-row>*:hover { + text-decoration: underline; +} + +.primary { + background: #337ab7; + color: white; } @media print { diff --git a/templates/article.html b/templates/article.html index b9093af..a398aa9 100644 --- a/templates/article.html +++ b/templates/article.html @@ -38,3 +38,5 @@ > {{>footer/items.html}} + +{{>dialog_prototypes.html}} diff --git a/templates/dialog_prototypes.html b/templates/dialog_prototypes.html new file mode 100644 index 0000000..31e5c61 --- /dev/null +++ b/templates/dialog_prototypes.html @@ -0,0 +1,35 @@ +
+ +
+ +
+ +
+ +
+ +