From 37e58e70b0768b5b5ca02064f3df751c8e733654 Mon Sep 17 00:00:00 2001 From: Misode Date: Mon, 17 Jun 2019 18:48:51 +0200 Subject: [PATCH] Implement entries --- index.html | 93 +++++++++++++++++++++++++++++++++++++---------------- script.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 156 insertions(+), 31 deletions(-) diff --git a/index.html b/index.html index 46a8a00c..5e9a69c8 100644 --- a/index.html +++ b/index.html @@ -22,7 +22,7 @@
-
+
Type @@ -36,11 +36,11 @@
-
+
-
+
Indentation @@ -59,40 +59,79 @@
-
+
- +
-
+
Rolls - + - - Min - - Max - - n - - p -
+ + Min + + Max + + n + + p +
+
+
+ + + +
+
+
+
+ Type +
+ +
+
+
+ Name +
+ +
+
+
+ Weight +
+ +
+
+ +
+
+
+
- - - - - - + + + + + + diff --git a/script.js b/script.js index ff0bd38b..543cac15 100644 --- a/script.js +++ b/script.js @@ -8,6 +8,18 @@ let table = { pools: [] }; addPool(); +addEntry($('.pool').get()); + +function getPool(el) { + let poolIndex = parseInt($(el).closest('.pool').attr('data-index')); + return table.pools[poolIndex]; +} + +function getEntry(el) { + let poolIndex = parseInt($(el).closest('.pool').attr('data-index')); + let entryIndex = parseInt($(el).closest('.entry').attr('data-index')); + return table.pools[poolIndex].entries[entryIndex]; +} function addPool(el) { table.pools.push({ @@ -18,11 +30,29 @@ function addPool(el) { } function removePool(el) { - let index = parseInt($(el).closest('.pool').attr('data-index')); - if (index === 0) { + let poolIndex = parseInt($(el).closest('.pool').attr('data-index')); + if (poolIndex === 0) { table.pools.shift(); } else { - table.pools.splice(index, index); + table.pools.splice(poolIndex, poolIndex); + } + invalidated(); +} + +function addEntry(el) { + getPool(el).entries.push({ + type: "minecraft:item", + name: "minecraft:stone" + }); + invalidated(); +} + +function removeEntry(el) { + let entryIndex = parseInt($(el).closest('.entry').attr('data-index')); + if (entryIndex === 0) { + getPool(el).entries.shift(); + } else { + getPool(el).entries.splice(entryIndex, entryIndex); } invalidated(); } @@ -55,7 +85,36 @@ function switchRollsType(el, type) { function updateRollsField(el) { let type = $(el).closest('.rolls').attr('data-type'); let data = getRangeField($(el).closest('.rolls'), type); - table.pools[$(el).closest('.pool').attr('data-index')].rolls = data; + getPool(el).rolls = data; + invalidated(); +} + +function updateEntryType(el) { + let entry = getEntry(el); + entry.type = $(el).val(); + if (entry.type === 'minecraft:dynamic') { + entry.name = 'minecraft:contents'; + } + invalidated(); +} + +function updateEntryName(el) { + let entry = getEntry(el); + if (entry.type === 'minecraft:dynamic') { + entry.name = 'minecraft:contents'; + } else { + entry.name = $(el).val(); + } + invalidated(); +} + +function updateEntryWeight(el) { + let weight = parseInt($(el).val()); + if (isNaN(weight)) { + delete getEntry(el).weight; + } else { + getEntry(el).weight = weight; + } invalidated(); } @@ -101,6 +160,7 @@ function generateStructure() { let pool = table.pools[i]; let $pool = $('#poolTemplate').clone(); $pool.removeAttr('id').attr('data-index', i); + // Rolls let $rolls = $pool.find('.rolls'); if (typeof pool.rolls === 'object') { @@ -120,6 +180,32 @@ function generateStructure() { $rolls.find('.exact').removeClass('d-none'); $rolls.find('.exact').val(pool.rolls); } + + // Entries + for (let j = 0; j < pool.entries.length; j += 1) { + let entry = pool.entries[j]; + let $entry = $('#entryTemplate').clone(); + $entry.removeAttr('id').attr('data-index', j); + + $entry.find('.entry-type').val(entry.type); + if (entry.type === 'minecraft:item' || entry.type === 'minecraft:tag' || entry.type === 'minecraft:loot_table' || entry.type === 'minecraft:dynamic') { + $entry.find('.entry-name').removeClass('d-none'); + if (entry.type === 'minecraft:dynamic') { + entry.name = 'minecraft:contents'; + } + $entry.find('.entry-name input').val(entry.name); + } + $entry.find('.entry-weight').removeClass('d-none'); + if (entry.weight) { + $entry.find('.entry-weight input').val(entry.weight); + } + if (entry.type === 'minecraft:alternatives' || entry.type === 'minecraft:sequence' || entry.type === 'minecraft:group') { + delete entry.name; + $entry.find('.entry-children').removeClass('d-none'); + } + + $pool.children('.card-body').append($entry); + } $('#structure').append($pool); } }