diff --git a/modules/ui/resources-editor.js b/modules/ui/resources-editor.js index be8cc542..fb60e075 100644 --- a/modules/ui/resources-editor.js +++ b/modules/ui/resources-editor.js @@ -63,6 +63,19 @@ function editResources() { if (bonus === 'cavalry') return ``; } + body.addEventListener("click", function(ev) { + const el = ev.target, cl = el.classList, line = el.parentNode, i = +line.dataset.id; + const resource = Resources.get(+line.dataset.id); + if (cl.contains("resourceCategory")) return changeCategory(resource, line, el); + if (cl.contains("resourceModel")) return changeModel(resource, line, el); + }); + + body.addEventListener("change", function(ev) { + const el = ev.target, cl = el.classList, line = el.parentNode; + const resource = Resources.get(+line.dataset.id); + if (cl.contains("resourceName")) return changeName(resource, el.value, line); + }); + // add line for each resource function resourcesEditorAddLines() { const addTitle = (string, max) => (string.length < max ? '' : `title="${string}"`); @@ -451,6 +464,106 @@ function editResources() { confirmationDialog({title: 'Restore default resources', message, confirm: 'Restore', onConfirm}); } + function changeName(resource, name, line) { + resource.name = line.dataset.name = name; + } + + function changeCategory(resource, line, el) { + const categories = [...new Set(pack.resources.map(r => r.category))].sort(); + const categoryOptions = category => categories.map(c => ``).join(""); + + alertMessage.innerHTML = ` +
+
Select category:
+ +
+ +
+
Custom category:
+ +
+ `; + + $("#alert").dialog({resizable: false, title: "Change category", + buttons: { + Cancel: function() {$(this).dialog("close");}, + Apply: function() {applyChanges(); $(this).dialog("close");} + } + }); + + function applyChanges() { + const custom = document.getElementById("resouceCategoryAdd").value; + const select = document.getElementById("resouceCategorySelect").value; + const category = custom ? capitalize(custom) : select; + resource.category = line.dataset.category = el.innerHTML = category; + } + } + + function changeModel(resource, line, el) { + const defaultModels = Resources.defaultModels; + const model = line.dataset.model; + const modelOptions = Object.keys(defaultModels).map(m => ``).join(""); + const wikiURL = "https://github.com/Azgaar/Fantasy-Map-Generator/wiki/Resources:-spread-functions"; + + alertMessage.innerHTML = ` +
+ Predefined models +
+
Name:
+ +
+ +
+
Function:
+
${defaultModels[model]}
+
+
+ +
+ Custom model +
+
Name:
+ +
+ +
+
Function:
+ +
+
+ +
+ `; + + $("#alert").dialog({resizable: false, title: "Change spread model", + buttons: { + Help: () => openURL(wikiURL), + Cancel: function() {$(this).dialog("close");}, + Apply: function() {applyChanges(this);} + } + }); + + function applyChanges(dialog) { + const customName = document.getElementById("resouceModelCustomName").value; + const customFn = document.getElementById("resouceModelCustomFunction").value; + + const message = document.getElementById("resourceModelMessage"); + if (customName && !customFn) return message.innerHTML = "Error. Custom model function is required"; + if (!customName && customFn) return message.innerHTML = "Error. Custom model name is required"; + message.innerHTML = ""; + + if (customName && customFn) { + resource.model = line.dataset.model = el.innerHTML = customName; + resource.custom = customFn; + return; + } + + const model = document.getElementById("resouceModelSelect").value; + resource.model = line.dataset.model = el.innerHTML = model; + $(dialog).dialog("close"); + } + } + function resourceChangeColor() { const circle = this.querySelector("circle"); const resource = Resources.get(+this.parentNode.dataset.id);