From cacdc4641d17037fc28bcd1bab491507a642ed3a Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Sun, 31 Jan 2021 11:14:52 +0200 Subject: [PATCH] Extracted insertElementBefore to domUtils.js --- .../main/resources/assets/plan/web/js/domUtils.js | 5 +++++ .../src/main/resources/assets/plan/web/js/query.js | 14 ++++++++------ .../src/main/resources/assets/plan/web/query.html | 12 +++++++----- 3 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 Plan/common/src/main/resources/assets/plan/web/js/domUtils.js diff --git a/Plan/common/src/main/resources/assets/plan/web/js/domUtils.js b/Plan/common/src/main/resources/assets/plan/web/js/domUtils.js new file mode 100644 index 000000000..421c9f5ea --- /dev/null +++ b/Plan/common/src/main/resources/assets/plan/web/js/domUtils.js @@ -0,0 +1,5 @@ +function insertElementBefore(elementSelector, createElementFunction) { + const placeBefore = document.querySelector(elementSelector); + const element = createElementFunction(); + placeBefore.insertAdjacentElement('beforebegin', element); +} \ No newline at end of file diff --git a/Plan/common/src/main/resources/assets/plan/web/js/query.js b/Plan/common/src/main/resources/assets/plan/web/js/query.js index 239d7606c..91b256234 100644 --- a/Plan/common/src/main/resources/assets/plan/web/js/query.js +++ b/Plan/common/src/main/resources/assets/plan/web/js/query.js @@ -272,6 +272,7 @@ function runQuery() { } function renderResultPath(json) { + const gotResults = Boolean(json.data); let pathHtml = ``; for (let i = 0; i < json.path.length; i++) { const step = json.path[i]; @@ -282,12 +283,13 @@ function renderResultPath(json) { pathHtml += ` ${step.kind} matched ${step.size} players
` } - const placeBefore = document.querySelector('.tab .row .card'); - const element = document.createElement('div'); - element.id = "result-path" - element.classList.add("alert", "alert-warning", "shadow"); - element.innerHTML = pathHtml - placeBefore.insertAdjacentElement('beforebegin', element); + insertElementBefore('.tab .row .card', () => { + const element = document.createElement('div'); + element.id = "result-path" + element.classList.add("alert", gotResults ? "alert-success" : "alert-warning", "shadow"); + element.innerHTML = pathHtml; + return element; + }); window.scrollTo(0, 0); // Scroll to top } diff --git a/Plan/common/src/main/resources/assets/plan/web/query.html b/Plan/common/src/main/resources/assets/plan/web/query.html index 8012ebbf3..876338ff9 100644 --- a/Plan/common/src/main/resources/assets/plan/web/query.html +++ b/Plan/common/src/main/resources/assets/plan/web/query.html @@ -311,6 +311,7 @@ + @@ -328,14 +329,15 @@ const filters = []; if (location.search.includes("error=")) { - const placeBefore = document.querySelector('.tab .row .card div'); - const element = document.createElement('alert'); - element.classList.add("alert", "alert-danger", "alert-dismissable", "show"); - element.innerHTML = ` + insertElementBefore('.tab .row .card div', () => { + const element = document.createElement('alert'); + element.classList.add("alert", "alert-danger", "alert-dismissable", "show"); + element.innerHTML = ` ` - placeBefore.insertAdjacentElement('beforebegin', element); + return element; + }); document.getElementById('error-text').innerText = new URLSearchParams(location.search).get("error"); }