mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-24 23:56:51 +00:00
Further abstraction of indices
This commit is contained in:
@@ -53,7 +53,7 @@
|
||||
<button type="button" class="btn btn-secondary" onclick="showSource()" data-i18n="show_source"></button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="structure" class="loot-table mt-3" data-field="table">
|
||||
<div id="structure" class="loot-table mt-3" data-index="pools">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12 col-lg-5 source-container">
|
||||
|
||||
233
model.js
233
model.js
@@ -145,88 +145,69 @@ function copySource(el) {
|
||||
document.execCommand('copy');
|
||||
}
|
||||
|
||||
function getParent(el) {
|
||||
let $node = $(el).closest('[data-field]');
|
||||
let fields = $node.attr('data-field').split('.');
|
||||
if (fields.length === 1 && fields[0] === 'table') {
|
||||
return table;
|
||||
}
|
||||
if ($node.attr('data-type')) {
|
||||
fields = fields.slice(0, -1);
|
||||
}
|
||||
let node = getParent($node.parent());
|
||||
for (let f of fields) {
|
||||
if (f.endsWith('[]')) {
|
||||
f = f.slice(0, -2);
|
||||
let index = $node.attr('data-index');
|
||||
node = node[f][index];
|
||||
} else {
|
||||
if (node[f] === undefined) {
|
||||
node[f] = {};
|
||||
}
|
||||
node = node[f];
|
||||
function getPath(el) {
|
||||
let $node = $(el).closest('[data-index]');
|
||||
let index = $node.attr('data-index');
|
||||
if (index === 'pools') return ['pools'];
|
||||
let parent = getPath($node.parent());
|
||||
parent.push(index);
|
||||
return parent;
|
||||
}
|
||||
|
||||
function getNode(path) {
|
||||
let node = table;
|
||||
for (let index of path) {
|
||||
if (!isNaN(index)) {
|
||||
index = +index;
|
||||
} else if (node[index] === undefined) {
|
||||
node[index] = {};
|
||||
}
|
||||
node = node[index];
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
function getType(el) {
|
||||
let $field = $(el).closest('[data-index]');
|
||||
if ($field) {
|
||||
return $field.attr('data-type');
|
||||
}
|
||||
}
|
||||
|
||||
function getParent(el) {
|
||||
let path = getPath(el);
|
||||
path.pop();
|
||||
return getNode(path);
|
||||
}
|
||||
|
||||
function getSuperParent(el) {
|
||||
let $parent = $(el).closest('[data-field]');
|
||||
return getParent($parent.parent());
|
||||
}
|
||||
|
||||
function setField(node, field, value) {
|
||||
let fields = field.split('.');
|
||||
let last = fields.splice(-1)[0];
|
||||
for (let f of fields) {
|
||||
node = node[f];
|
||||
}
|
||||
node[last] = value;
|
||||
}
|
||||
|
||||
function deleteField(node, field) {
|
||||
let fields = field.split('.');
|
||||
let last = fields.splice(-1)[0];
|
||||
for (let f of fields) {
|
||||
node = node[f];
|
||||
}
|
||||
delete node[last];
|
||||
}
|
||||
|
||||
function getField(node, field) {
|
||||
let fields = field.split('.');
|
||||
let last = fields.splice(-1)[0];
|
||||
for (let f of fields) {
|
||||
node = node[f];
|
||||
}
|
||||
return node[last];
|
||||
}
|
||||
|
||||
function getIndex(el) {
|
||||
let $parent = $(el).closest('[data-field]');
|
||||
return parseInt($parent.attr('data-index'));
|
||||
let path = getPath(el);
|
||||
path.pop();
|
||||
path.pop();
|
||||
return getNode(path);
|
||||
}
|
||||
|
||||
function addComponent(el, array) {
|
||||
let parent = getParent(el);
|
||||
if (!parent[array]) {
|
||||
parent[array] = [];
|
||||
let node = getNode(getPath(el));
|
||||
if (!node[array]) {
|
||||
node[array] = [];
|
||||
}
|
||||
parent[array].push({});
|
||||
node[array].push({});
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function removeComponent(el) {
|
||||
let node = getSuperParent(el);
|
||||
let $field = $(el).closest('[data-field]');
|
||||
let index = $field.attr('data-index');
|
||||
let last = $field.attr('data-field').slice(0, -2);
|
||||
node[last].splice(index, 1);
|
||||
if (node[last].length === 0) {
|
||||
delete node[last];
|
||||
let path = getPath(el);
|
||||
let index = path.pop();
|
||||
let array = path.pop();
|
||||
let node = getNode(path);
|
||||
node[array].splice(index, 1);
|
||||
if (node[array].length === 0) {
|
||||
delete node[array];
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function addToSet(el, array) {
|
||||
let parent = getParent(el);
|
||||
if (!parent[array]) {
|
||||
@@ -257,11 +238,11 @@ function toggleCollapseObject(el) {
|
||||
}
|
||||
|
||||
function updateField(el) {
|
||||
let $field = $(el).closest('[data-field]');
|
||||
let fields = $field.attr('data-field');
|
||||
let field = fields.split('.').slice(-1)[0];
|
||||
let type = $field.attr('data-type');
|
||||
let node = getParent(el);
|
||||
let path = getPath(el);
|
||||
let $field = $(el).closest('[data-index]');
|
||||
let field = path.pop();
|
||||
let node = getNode(path);
|
||||
let type = getType(el);
|
||||
let value = undefined;
|
||||
|
||||
if (type === 'string' || type === 'int' || type === 'float' || type === 'enum' || type === 'json' || type === 'nbt') {
|
||||
@@ -307,23 +288,24 @@ function updateField(el) {
|
||||
value = getBooleanValue(node[field], ($(el).val() === 'true'));
|
||||
}
|
||||
if (value === '') {
|
||||
deleteField(node, field);
|
||||
delete node[field];
|
||||
} else {
|
||||
setField(node, field, value);
|
||||
node[field] = value;
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function updateRangeType(el) {
|
||||
let $field = $(el).closest('[data-field]');
|
||||
let field = $field.attr('data-field');
|
||||
let path = getPath(el);
|
||||
let field = path.pop();
|
||||
let node = getNode(path);
|
||||
let type = $(el).attr('value');
|
||||
if (type === 'range') {
|
||||
setField(getParent(el), field, {});
|
||||
node[field] = {};
|
||||
} else if (type === 'binomial') {
|
||||
setField(getParent(el), field, {type: "minecraft:binomial"});
|
||||
node[field] = {type: "minecraft:binomial"};
|
||||
} else {
|
||||
setField(getParent(el), field, 0);
|
||||
node[field] = 0;
|
||||
}
|
||||
updateField(el);
|
||||
}
|
||||
@@ -453,91 +435,6 @@ function updateBlockPropertyField(el) {
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function addTerm(el) {
|
||||
let condition = getParent(el);
|
||||
if (!condition.terms) {
|
||||
condition.terms = [];
|
||||
}
|
||||
condition.terms.push({
|
||||
condition: "minecraft:random_chance",
|
||||
chance: 0.5
|
||||
});
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function togglePosition(el) {
|
||||
let parent = getParent(el);
|
||||
if (parent.position) {
|
||||
delete parent.position;
|
||||
} else {
|
||||
parent.position = {};
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function toggleEntityLocation(el) {
|
||||
let parent = getParent(el);
|
||||
if (parent.location) {
|
||||
delete parent.location;
|
||||
} else {
|
||||
parent.location = {};
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function updateItemType(el, type) {
|
||||
let $predicate = $(el).closest('.predicate');
|
||||
if (type === 'item') {
|
||||
$predicate.find('.item').removeClass('d-none');
|
||||
$predicate.find('.tag').addClass('d-none');
|
||||
} else {
|
||||
$predicate.find('.tag').removeClass('d-none');
|
||||
$predicate.find('.item').addClass('d-none');
|
||||
}
|
||||
}
|
||||
|
||||
function updateItemField(el, type) {
|
||||
let parent = getParent(el);
|
||||
if (type === 'item') {
|
||||
parent.item = $(el).closest('.predicate').find('input.item').val();
|
||||
delete parent.tag;
|
||||
} else {
|
||||
parent.tag = $(el).closest('.predicate').find('input.tag').val();
|
||||
delete parent.item;
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function toggleDamageFlags(el) {
|
||||
let parent = getParent(el);
|
||||
if (parent.type) {
|
||||
delete parent.type;
|
||||
} else {
|
||||
parent.type = {};
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function toggleSourceEntity(el) {
|
||||
let parent = getParent(el);
|
||||
if (parent.source_entity) {
|
||||
delete parent.source_entity;
|
||||
} else {
|
||||
parent.source_entity = {};
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function toggleDirectEntity(el) {
|
||||
let parent = getParent(el);
|
||||
if (parent.direct_entity) {
|
||||
delete parent.direct_entity;
|
||||
} else {
|
||||
parent.direct_entity = {};
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function updateChancesField(el) {
|
||||
let parent = getParent(el);
|
||||
let chances = '[' + $(el).val() + ']';
|
||||
@@ -553,15 +450,3 @@ function updateChancesField(el) {
|
||||
}
|
||||
invalidated();
|
||||
}
|
||||
|
||||
function addConditionEnchantment(el) {
|
||||
let condition = getParent(el);
|
||||
if (!condition.enchantments) {
|
||||
condition.enchantments = [];
|
||||
}
|
||||
condition.enchantments.push({
|
||||
enchantment: 'minecraft:silk_touch',
|
||||
level: 1
|
||||
});
|
||||
invalidated();
|
||||
}
|
||||
|
||||
27
view.js
27
view.js
@@ -76,7 +76,7 @@ function generateComponent(data, struct) {
|
||||
|
||||
function generateString(data, struct) {
|
||||
let $el = $('#components').find('[data-type="string"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
$el.find('input').val(data);
|
||||
return $el;
|
||||
@@ -84,7 +84,7 @@ function generateString(data, struct) {
|
||||
|
||||
function generateBoolean(data, struct) {
|
||||
let $el = $('#components').find('[data-type="boolean"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
if (data === true) {
|
||||
$el.find('[value="true"]').addClass('active');
|
||||
@@ -96,7 +96,7 @@ function generateBoolean(data, struct) {
|
||||
|
||||
function generateRandom(data, struct) {
|
||||
let $el = $('#components').find('[data-type="random"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
if (typeof data === 'object') {
|
||||
if (data.type && data.type.match(/(minecraft:)?binomial/)) {
|
||||
@@ -117,7 +117,7 @@ function generateRandom(data, struct) {
|
||||
|
||||
function generateRange(data, struct) {
|
||||
let $el = $('#components').find('[data-type="range"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
if (typeof data === 'object') {
|
||||
$el.find('.range').removeClass('d-none');
|
||||
@@ -132,7 +132,7 @@ function generateRange(data, struct) {
|
||||
|
||||
function generateBoundary(data, struct) {
|
||||
let $el = $('#components').find('[data-type="boundary"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
if (data) {
|
||||
$el.find('.range.min').val(data.min);
|
||||
@@ -143,7 +143,7 @@ function generateBoundary(data, struct) {
|
||||
|
||||
function generateEnum(data, struct) {
|
||||
let $el = $('#components').find('[data-type="enum"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
let collection = struct.values;
|
||||
if (typeof struct.values === 'string') {
|
||||
@@ -167,7 +167,7 @@ function generateEnum(data, struct) {
|
||||
|
||||
function generateSet(data, struct) {
|
||||
let $el = $('#components').find('[data-type="set"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
let collection = struct.values;
|
||||
if (typeof struct.values === 'string') {
|
||||
@@ -198,7 +198,7 @@ function setValueAndName($el, value, source) {
|
||||
|
||||
function generateJson(data, struct) {
|
||||
let $el = $('#components').find('[data-type="json"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
if (typeof data !== 'string') {
|
||||
data = JSON.stringify(data);
|
||||
@@ -209,7 +209,7 @@ function generateJson(data, struct) {
|
||||
|
||||
function generateJsonList(data, struct) {
|
||||
let $el = $('#components').find('[data-type="json-list"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
let jsonList = "";
|
||||
if (data) {
|
||||
@@ -230,7 +230,7 @@ function generateJsonList(data, struct) {
|
||||
|
||||
function generateNbt(data, struct) {
|
||||
let $el = $('#components').find('[data-type="nbt"]').clone();
|
||||
$el.attr('data-field', struct.id);
|
||||
$el.attr('data-index', struct.id);
|
||||
$el.find('[data-name]').attr('data-i18n', struct.id);
|
||||
$el.find('textarea').val(data).keydown(e => preventNewline(e));
|
||||
return $el;
|
||||
@@ -256,7 +256,6 @@ function generateArray(data, struct) {
|
||||
let child = components.find(e => e.id === struct.values);
|
||||
for (let i = 0; i < data.length; i += 1) {
|
||||
let $child = generateObject(data[i], child, true);
|
||||
$child.attr('data-field', struct.id + '[]');
|
||||
$child.attr('data-index', i);
|
||||
$child.removeAttr('data-type');
|
||||
$el.append($child);
|
||||
@@ -280,7 +279,7 @@ function generateObject(data, struct, header) {
|
||||
for (let field of struct.fields) {
|
||||
let $field;
|
||||
if (field.collapse) {
|
||||
$body.append('<button type="button" class="btn btn-light mt-3 dropdown-toggle" onclick="toggleCollapseObject(this)" data-field="' + field.id + '" data-i18n="' + field.id + '"></button>');
|
||||
$body.append('<button type="button" class="btn btn-light mt-3 dropdown-toggle" onclick="toggleCollapseObject(this)" data-index="' + field.id + '" data-i18n="' + field.id + '"></button>');
|
||||
if (data[field.id] === undefined) {
|
||||
break;
|
||||
}
|
||||
@@ -362,9 +361,7 @@ function generateField(data, field, parent) {
|
||||
if (field.class) {
|
||||
$field.addClass(field.class);
|
||||
}
|
||||
if (field.type !== 'array') {
|
||||
$field.attr('data-field', field.id);
|
||||
}
|
||||
$field.attr('data-index', field.id);
|
||||
}
|
||||
return $field;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user