"use strict";
function editBurgGroups() {
if (customization) return;
addLines();
$("#burgGroupsEditor").dialog({
title: "Configure Burg groups",
resizable: false,
position: {my: "center", at: "center", of: "svg"},
buttons: {
Apply: () => {
byId("burgGroupsForm").requestSubmit();
},
Add: () => {
byId("burgGroupsBody").innerHTML += createLine({name: "", active: true, preview: null});
},
Restore: () => {
options.burgs.groups = Burgs.getDefaultGroups();
addLines();
},
Cancel: function () {
$(this).dialog("close");
}
}
});
if (modules.editBurgGroups) return;
modules.editBurgGroups = true;
// add listeners
byId("burgGroupsForm").on("change", validateForm).on("submit", submitForm);
byId("burgGroupsBody").on("click", ev => {
const el = ev.target;
const line = el.closest("tr");
if (!line) return;
if (el.name === "biomes") {
const biomes = Array(biomesData.i.length)
.fill(null)
.map((_, i) => ({i, name: biomesData.name[i], color: biomesData.color[i]}));
return selectLimitation(el, biomes);
}
if (el.name === "states") return selectLimitation(el, pack.states);
if (el.name === "cultures") return selectLimitation(el, pack.cultures);
if (el.name === "religions") return selectLimitation(el, pack.religions);
if (el.name === "features") return selectFeaturesLimitation(el);
if (el.name === "up") return line.parentNode.insertBefore(line, line.previousElementSibling);
if (el.name === "down") return line.parentNode.insertBefore(line.nextElementSibling, line);
if (el.name === "remove") return removeLine(line);
});
function addLines() {
const lines = options.burgs.groups.map(createLine);
byId("burgGroupsBody").innerHTML = lines.join("");
}
function createLine(group) {
const count = pack.burgs.filter(burg => !burg.removed && burg.group === group.name).length;
// prettier-ignore
return /* html */ `
|
|
|
|
|
|
|
|
|
|
${count} |
|
|
|
|
|
`;
}
function selectLimitation(el, data) {
const value = el.previousElementSibling.value;
const initial = value ? value.split(",").map(v => +v) : [];
const filtered = data.filter(datum => datum.i && !datum.removed);
const lines = filtered.map(
({i, name, fullName, color}) => /* html */ `
|
⬤
|
|
`
);
alertMessage.innerHTML = /* html */ `Limit group by ${el.name}:
`;
$("#alert").dialog({
width: fitContent(),
title: "Limit group",
buttons: {
Invert: function () {
alertMessage.querySelectorAll("input").forEach(el => (el.checked = !el.checked));
},
Apply: function () {
const inputs = Array.from(alertMessage.querySelectorAll("input"));
const selected = inputs.reduce((acc, input) => {
if (input.checked) acc.push(input.dataset.i);
return acc;
}, []);
if (!selected.length) return tip("Select at least one element", false, "error");
const allAreSelected = selected.length === inputs.length;
el.previousElementSibling.value = allAreSelected ? "" : selected.join(",");
el.innerHTML = allAreSelected ? "all" : "some";
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
}
function selectFeaturesLimitation(el) {
const value = el.previousElementSibling.value;
const initial = value ? JSON.parse(value) : {};
const features = [
{name: "capital", icon: "icon-star"},
{name: "port", icon: "icon-anchor"},
{name: "citadel", icon: "icon-chess-rook"},
{name: "walls", icon: "icon-fort-awesome"},
{name: "plaza", icon: "icon-store"},
{name: "temple", icon: "icon-chess-bishop"},
{name: "shanty", icon: "icon-campground"}
];
const lines = features.map(
// prettier-ignore
({name, icon}) => /* html */ `
|
${name}
|
|
|
|
`
);
alertMessage.innerHTML = /* html */ `
`;
$("#alert").dialog({
width: fitContent(),
title: "Limit group by features",
buttons: {
Apply: function () {
const form = byId("featuresLimitationForm");
const values = features.reduce((acc, {name}) => {
const value = form[name].value;
if (value !== "undefined") acc[name] = value === "true";
return acc;
}, {});
el.previousElementSibling.value = JSON.stringify(values);
el.innerHTML = Object.keys(values).length ? "some" : "any";
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
}
function removeLine(line) {
const lines = byId("burgGroupsBody").children;
if (lines.length < 2) return tip("At least one group should be defined", false, "error");
confirmationDialog({
title: this.dataset.tip,
message:
"Are you sure you want to remove the group?
This WON'T change the burgs unless the changes are applied",
confirm: "Remove",
onConfirm: () => {
line.remove();
validateForm();
}
});
}
function validateForm() {
const form = byId("burgGroupsForm");
if (form.name.length) {
const names = Array.from(form.name).map(input => input.value);
form.name.forEach(nameInput => {
const value = nameInput.value;
const isUnique = names.filter(n => n === value).length === 1;
nameInput.setCustomValidity(isUnique ? "" : "Group name should be unique");
nameInput.reportValidity();
});
}
if (form.active.length) {
const active = Array.from(form.active).map(input => input.checked);
form.active[0].setCustomValidity(active.includes(true) ? "" : "At least one group should be active");
form.active[0].reportValidity();
} else {
const active = form.active.checked;
form.active.setCustomValidity(active ? "" : "At least one group should be active");
form.active.reportValidity();
}
if (form.isDefault.length) {
const checked = Array.from(form.isDefault).map(input => input.checked);
form.isDefault[0].setCustomValidity(checked.includes(true) ? "" : "At least one group should be default");
form.isDefault[0].reportValidity();
} else {
const checked = form.isDefault.checked;
form.isDefault.setCustomValidity(checked ? "" : "At least one group should be default");
form.isDefault.reportValidity();
}
}
function submitForm(event) {
event.preventDefault();
const lines = Array.from(byId("burgGroupsBody").children);
if (!lines.length) return tip("At least one group should be defined", false, "error");
function parseInput(input) {
if (input.name === "name") return sanitizeId(input.value);
if (input.name === "features") {
const isValid = JSON.isValid(input.value);
const parsed = isValid ? JSON.parse(input.value) : {};
if (Object.keys(parsed).length) return parsed;
return null;
}
if (input.type === "hidden") return input.value || null;
if (input.type === "radio") return input.checked;
if (input.type === "checkbox") return input.checked;
if (input.type === "number") {
const value = input.valueAsNumber;
if (value === 0 || isNaN(value)) return null;
return value;
}
return input.value || null;
}
options.burgs.groups = lines.map(line => {
const inputs = line.querySelectorAll("input");
const group = Array.from(inputs).reduce((obj, input) => {
const value = parseInput(input);
if (value !== null) obj[input.name] = value;
return obj;
}, {});
return group;
});
// put burgs to new groups
const validBurgs = pack.burgs.filter(b => b.i && !b.removed);
const populations = validBurgs.map(b => b.population).sort((a, b) => a - b);
validBurgs.forEach(burg => Burgs.defineGroup(burg, populations));
if (layerIsOn("toggleBurgIcons")) drawBurgIcons();
if (layerIsOn("toggleLabels")) drawBurgLabels();
$("#burgGroupsEditor").dialog("close");
}
}