diff --git a/index.html b/index.html index b507f5eb..fd2ba0c4 100644 --- a/index.html +++ b/index.html @@ -7886,7 +7886,7 @@ - + diff --git a/modules/dynamic/editors/states-editor.js b/modules/dynamic/editors/states-editor.js index 41241340..11d9581d 100644 --- a/modules/dynamic/editors/states-editor.js +++ b/modules/dynamic/editors/states-editor.js @@ -114,6 +114,7 @@ function insertEditorHtml() { + `; @@ -140,6 +141,7 @@ function addListeners() { byId("statesManuallyApply").on("click", applyStatesManualAssignent); byId("statesManuallyCancel").on("click", () => exitStatesManualAssignment(false)); byId("statesAdd").on("click", enterAddStateMode); + byId("statesMerge").on("click", openStateMergeDialog); byId("statesExport").on("click", downloadStatesCsv); $body.on("click", event => { @@ -1327,6 +1329,142 @@ function exitAddStateMode() { if (statesAdd.classList.contains("pressed")) statesAdd.classList.remove("pressed"); } +function openStateMergeDialog() { + const emblem = i => /* html */ ``; + const validStates = pack.states.filter(s => s.i && !s.removed); + + const statesSelector = validStates + .map( + s => /* html */ ` +
The following states will be removed: ${statesToMerge.map(stateId => `${emblem(stateId)}${pack.states[stateId].name}`).join(", ")}.
+Removed states data (burgs, provinces, regiments) will be assigned to ${emblem(rullingState.i)}${rullingState.name}.
+Are you sure you want to merge states? This action cannot be reverted.
`, + confirm: "Merge", + onConfirm: () => { + mergeStates(statesToMerge, rulingStateId); + $(this).dialog("close"); + } + }); + }, + Cancel: function () { + $(this).dialog("close"); + } + } + }); + + function mergeStates(statesToMerge, rulingStateId) { + const rulingState = pack.states[rulingStateId]; + + // remove states to be merged + statesToMerge.forEach(stateId => { + const state = pack.states[stateId]; + state.removed = true; + + statesBody.select("#state" + stateId).remove(); + statesBody.select("#state-gap" + stateId).remove(); + statesHalo.select("#state-border" + stateId).remove(); + labels.select("#stateLabel" + stateId).remove(); + defs.select("#textPath_stateLabel" + stateId).remove(); + + byId("stateCOA" + stateId).remove(); + emblems.select(`#stateEmblems > use[data-i='${stateId}']`).remove(); + + // add merged state regiments to the ruling state + state.military.forEach(m => { + const oldId = `regiment${stateId}-${m.i}`; + + const newRegiment = {...m, i: rulingState.military.length}; + rulingState.military.push(newRegiment); + + const newId = `regiment${rulingStateId}-${newRegiment.i}`; + + const note = notes.find(n => n.id === oldId); + if (note) note.id = newId; + + const rulingStateArmy = armies.select("g#army" + rulingStateId); + armies + .select("g#army" + stateId) + .selectAll("g") + .each(function () { + this.setAttribute("id", newId); + rulingStateArmy.node().appendChild(this); + }); + armies.select("g#army" + stateId).remove(); + }); + }); + + // reassing burgs + pack.burgs.forEach(b => { + if (statesToMerge.includes(b.state)) { + if (b.capital) { + moveBurgToGroup(b.i, "towns"); + b.capital = 0; + } + b.state = rulingStateId; + } + }); + + // reassign provinces + pack.provinces.forEach((p, i) => { + if (statesToMerge.includes(p.state)) p.state = rulingStateId; + }); + + // reassing cells + pack.cells.state.forEach((s, i) => { + if (statesToMerge.includes(s)) pack.cells.state[i] = rulingStateId; + }); + + unfog(); + debug.selectAll(".highlight").remove(); + + layerIsOn("toggleStates") ? drawStates() : toggleStates(); + layerIsOn("toggleBorders") ? drawBorders() : toggleBorders(); + layerIsOn("toggleProvinces") && drawProvinces(); + BurgsAndStates.drawStateLabels([rulingStateId]); + + refreshStatesEditor(); + } +} + function downloadStatesCsv() { const unit = getAreaUnit("2"); const headers = `Id,State,Full Name,Form,Color,Capital,Culture,Type,Expansionism,Cells,Burgs,Area ${unit},Total Population,Rural Population,Urban Population`; diff --git a/modules/ui/diplomacy-editor.js b/modules/ui/diplomacy-editor.js index d5b7b183..0f363b7b 100644 --- a/modules/ui/diplomacy-editor.js +++ b/modules/ui/diplomacy-editor.js @@ -214,7 +214,6 @@ function editDiplomacy() { function selectRelation(subjectId, objectId, currentRelation) { const states = pack.states; const subject = states[subjectId]; - const object = states[objectId]; const relationsSelector = Object.entries(relations) .map( diff --git a/modules/ui/editors.js b/modules/ui/editors.js index 49a525eb..c3e72b35 100644 --- a/modules/ui/editors.js +++ b/modules/ui/editors.js @@ -1176,7 +1176,7 @@ function refreshAllEditors() { // dynamically loaded editors async function editStates() { if (customization) return; - const Editor = await import("../dynamic/editors/states-editor.js?v=1.89.05"); + const Editor = await import("../dynamic/editors/states-editor.js?v=1.89.35"); Editor.open(); } diff --git a/versioning.js b/versioning.js index 44b0de0a..b4c896a2 100644 --- a/versioning.js +++ b/versioning.js @@ -1,7 +1,7 @@ "use strict"; // version and caching control -const version = "1.89.34"; // generator version, update each time +const version = "1.89.35"; // generator version, update each time { document.title += " v" + version;