From e65370d9c441f0d17184cdbf9d1fb603f568df6e Mon Sep 17 00:00:00 2001 From: Azgaar Date: Fri, 12 Nov 2021 23:00:21 +0300 Subject: [PATCH] burgs overview - intert lock state --- index.html | 1 + modules/ui/burgs-overview.js | 153 +++++++++++++++-------------------- 2 files changed, 64 insertions(+), 90 deletions(-) diff --git a/index.html b/index.html index b8d13e3e..0a0720aa 100644 --- a/index.html +++ b/index.html @@ -3260,6 +3260,7 @@
Culture 
Population 
Type 
+
diff --git a/modules/ui/burgs-overview.js b/modules/ui/burgs-overview.js index 9b6e0d9e..97093035 100644 --- a/modules/ui/burgs-overview.js +++ b/modules/ui/burgs-overview.js @@ -34,6 +34,7 @@ function overviewBurgs() { uploadFile(this, importBurgNames); }); document.getElementById("burgsRemoveAll").addEventListener("click", triggerAllBurgsRemove); + document.getElementById("burgsInvertLock").addEventListener("click", invertLock); function refreshBurgsEditor() { updateFilter(); @@ -152,8 +153,8 @@ function overviewBurgs() { function zoomIntoBurg() { const burg = +this.parentNode.dataset.id; const label = document.querySelector("#burgLabels [data-id='" + burg + "']"); - const x = +label.getAttribute("x"), - y = +label.getAttribute("y"); + const x = +label.getAttribute("x"); + const y = +label.getAttribute("y"); zoomTo(x, y, 8, 2000); } @@ -214,24 +215,15 @@ function overviewBurgs() { function triggerBurgRemove() { const burg = +this.parentNode.dataset.id; - if (pack.burgs[burg].capital) { - tip("You cannot remove the capital. Please change the capital first", false, "error"); - return; - } + if (pack.burgs[burg].capital) return tip("You cannot remove the capital. Please change the capital first", false, "error"); - alertMessage.innerHTML = "Are you sure you want to remove the burg?"; - $("#alert").dialog({ - resizable: false, + confirmationDialog({ title: "Remove burg", - buttons: { - Remove: function () { - $(this).dialog("close"); - removeBurg(burg); - burgsOverviewAddLines(); - }, - Cancel: function () { - $(this).dialog("close"); - } + message: "Are you sure you want to remove the burg? This actiove cannot be reverted", + confirm: "Remove", + onConfirm: () => { + removeBurg(burg); + burgsOverviewAddLines(); } }); } @@ -239,22 +231,19 @@ function overviewBurgs() { function regenerateNames() { body.querySelectorAll(":scope > div").forEach(function (el) { const burg = +el.dataset.id; - //if (pack.burgs[burg].lock) return; + if (pack.burgs[burg].lock) return; + const culture = pack.burgs[burg].culture; const name = Names.getCulture(culture); - if (!pack.burgs[burg].lock) { - el.querySelector(".burgName").value = name; - pack.burgs[burg].name = el.dataset.name = name; - burgLabels.select("[data-id='" + burg + "']").text(name); - } + + el.querySelector(".burgName").value = name; + pack.burgs[burg].name = el.dataset.name = name; + burgLabels.select("[data-id='" + burg + "']").text(name); }); } function enterAddBurgMode() { - if (this.classList.contains("pressed")) { - exitAddBurgMode(); - return; - } + if (this.classList.contains("pressed")) return exitAddBurgMode(); customization = 3; this.classList.add("pressed"); tip("Click on the map to create a new burg. Hold Shift to add multiple", true, "warn"); @@ -264,14 +253,9 @@ function overviewBurgs() { function addBurgOnClick() { const point = d3.mouse(this); const cell = findCell(point[0], point[1]); - if (pack.cells.h[cell] < 20) { - tip("You cannot place state into the water. Please click on a land cell", false, "error"); - return; - } - if (pack.cells.burg[cell]) { - tip("There is already a burg in this cell. Please select a free cell", false, "error"); - return; - } + if (pack.cells.h[cell] < 20) return tip("You cannot place state into the water. Please click on a land cell", false, "error"); + if (pack.cells.burg[cell]) return tip("There is already a burg in this cell. Please select a free cell", false, "error"); + addBurg(point); // add new burg if (d3.event.shiftKey === false) { @@ -315,8 +299,8 @@ function overviewBurgs() { .sum(d => d.population) .sort((a, b) => b.value - a.value); - const width = 150 + 200 * uiSizeOutput.value, - height = 150 + 200 * uiSizeOutput.value; + const width = 150 + 200 * uiSizeOutput.value; + const height = 150 + 200 * uiSizeOutput.value; const margin = {top: 0, right: -50, bottom: -10, left: -50}; const w = width - margin.left - margin.right; const h = height - margin.top - margin.bottom; @@ -415,14 +399,14 @@ function overviewBurgs() { if (this.value === "provinces") return d.province; }; - const base = - this.value === "states" - ? getStatesData() - : this.value === "cultures" - ? getCulturesData() - : this.value === "parent" - ? getParentData() - : getProvincesData(); + const mapping = { + states: getStatesData, + cultures: getCulturesData, + parent: getParentData, + provinces: getProvincesData + }; + + const base = mapping[this.value](); burgs.forEach(b => (b.id = b.i + base.length - 1)); const data = base.concat(burgs); @@ -449,9 +433,7 @@ function overviewBurgs() { width: fitContent(), position: {my: "left bottom", at: "left+10 bottom-10", of: "svg"}, buttons: {}, - close: () => { - alertMessage.innerHTML = ""; - } + close: () => (alertMessage.innerHTML = "") }); } @@ -522,19 +504,14 @@ function overviewBurgs() { } function importBurgNames(dataLoaded) { - if (!dataLoaded) { - tip("Cannot load the file, please check the format", false, "error"); - return; - } + if (!dataLoaded) return tip("Cannot load the file, please check the format", false, "error"); const data = dataLoaded.split("\r\n"); - if (!data.length) { - tip("Cannot parse the list, please check the file format", false, "error"); - return; - } + if (!data.length) return tip("Cannot parse the list, please check the file format", false, "error"); - let change = [], - message = `Burgs will be renamed as below. Please confirm`; + let change = []; + let message = `Burgs to be renamed as below:`; message += ``; + const burgs = pack.burgs.filter(b => b.i && !b.removed); for (let i = 0; i < data.length && i <= burgs.length; i++) { const v = data[i]; @@ -543,45 +520,36 @@ function overviewBurgs() { message += ``; } message += `
IdCurrent nameNew Name
${burgs[i].i}${burgs[i].name}${v}
`; + if (!change.length) message = "No changes found in the file. Please change some names to get a result"; alertMessage.innerHTML = message; - $("#alert").dialog({ - title: "Burgs bulk renaming", - width: "22em", - position: {my: "center", at: "center", of: "svg"}, - buttons: { - Cancel: function () { - $(this).dialog("close"); - }, - Confirm: function () { - for (let i = 0; i < change.length; i++) { - const id = change[i].id; - pack.burgs[id].name = change[i].name; - burgLabels.select("[data-id='" + id + "']").text(change[i].name); - } - $(this).dialog("close"); - burgsOverviewAddLines(); - } + const onConfirm = () => { + for (let i = 0; i < change.length; i++) { + const id = change[i].id; + pack.burgs[id].name = change[i].name; + burgLabels.select("[data-id='" + id + "']").text(change[i].name); } + burgsOverviewAddLines(); + }; + + confirmationDialog({ + title: "Burgs bulk renaming", + message, + confirm: "Rename", + onConfirm }); } function triggerAllBurgsRemove() { - alertMessage.innerHTML = `Are you sure you want to remove all unlocked burgs except for capitals? -
To remove a capital you have to remove a state first`; - $("#alert").dialog({ - resizable: false, - title: "Remove all burgs", - buttons: { - Remove: function () { - $(this).dialog("close"); - removeAllBurgs(); - }, - Cancel: function () { - $(this).dialog("close"); - } - } + const number = pack.burgs.filter(b => b.i && !b.removed && !b.capital && !b.lock).length; + confirmationDialog({ + title: `Remove ${number} burgs`, + message: ` + Are you sure you want to remove all unlocked burgs except for capitals? +
To remove a capital you have to remove a state first`, + confirm: "Remove", + onConfirm: removeAllBurgs }); } @@ -589,4 +557,9 @@ function overviewBurgs() { pack.burgs.filter(b => b.i && !(b.capital || b.lock)).forEach(b => removeBurg(b.i)); burgsOverviewAddLines(); } + + function invertLock() { + pack.burgs = pack.burgs.map(burg => ({...burg, lock: !burg.lock})); + burgsOverviewAddLines(); + } }