From eb60be8acbc3dd8b00b3311ff91e7248b6ed15e7 Mon Sep 17 00:00:00 2001 From: Misode Date: Sun, 29 Dec 2019 15:13:40 +0100 Subject: [PATCH 1/5] Restructure files --- advancement/index.html | 31 +++++++++++++++++++++++++++++++ custom.css => css/custom.css | 0 i18n.js => js/i18n.js | 2 +- model.js => js/model.js | 3 --- view.js => js/view.js | 2 +- loot-table/index.html | 31 +++++++++++++++++++++++++++++++ predicate/index.html | 31 +++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 advancement/index.html rename custom.css => css/custom.css (100%) rename i18n.js => js/i18n.js (91%) rename model.js => js/model.js (99%) rename view.js => js/view.js (99%) create mode 100644 loot-table/index.html create mode 100644 predicate/index.html diff --git a/advancement/index.html b/advancement/index.html new file mode 100644 index 00000000..e003a009 --- /dev/null +++ b/advancement/index.html @@ -0,0 +1,31 @@ + + + + + + + + + + + Advancement Generator Minecraft 1.15 + + + + + + + + + + + + + + + diff --git a/custom.css b/css/custom.css similarity index 100% rename from custom.css rename to css/custom.css diff --git a/i18n.js b/js/i18n.js similarity index 91% rename from i18n.js rename to js/i18n.js index faee506f..97ac84b1 100644 --- a/i18n.js +++ b/js/i18n.js @@ -10,7 +10,7 @@ i18next .use(i18nextBrowserLanguageDetector) .use(i18nextXHRBackend) .init({ - backend: { loadPath: 'locales/{{lng}}.json' }, + backend: { loadPath: '../locales/{{lng}}.json' }, fallbackLng: 'en', whitelist: lngs.map(v => v[0]), keySeparator: false diff --git a/model.js b/js/model.js similarity index 99% rename from model.js rename to js/model.js index 991998cd..422c6259 100644 --- a/model.js +++ b/js/model.js @@ -219,10 +219,7 @@ function addToSet(el, array) { function removeFromSet(el, array) { let parent = getParent(el); - console.warn(parent[array]); - console.log($(el).attr('value')); let index = parent[array].indexOf($(el).attr('value')); - console.log(parent, index); if (index > -1) { parent[array].splice(index, 1); invalidated(); diff --git a/view.js b/js/view.js similarity index 99% rename from view.js rename to js/view.js index 298af75d..fbada69a 100644 --- a/view.js +++ b/js/view.js @@ -15,7 +15,7 @@ generators[generator].forEach(v => { changeVersion('1.15'); function changeVersion(version) { - $.getJSON('schemas/' + version + '.json', json => { + $.getJSON('../schemas/' + version + '.json', json => { if (json.root) { structure = json.root; } else if (json.roots) { diff --git a/loot-table/index.html b/loot-table/index.html new file mode 100644 index 00000000..cfe77f8b --- /dev/null +++ b/loot-table/index.html @@ -0,0 +1,31 @@ + + + + + + + + + + + Loot Table Generator Minecraft 1.15 + + + + + + + + + + + + + + + diff --git a/predicate/index.html b/predicate/index.html new file mode 100644 index 00000000..e1c6f4d6 --- /dev/null +++ b/predicate/index.html @@ -0,0 +1,31 @@ + + + + + + + + + + + Predicate Generator Minecraft 1.15 + + + + + + + + + + + + + + + From 9cbe9ce7dbbdae9189cce6f17b2595ba5e519569 Mon Sep 17 00:00:00 2001 From: Misode Date: Sun, 29 Dec 2019 16:14:39 +0100 Subject: [PATCH 2/5] Allow multiple views + smarter initialization --- advancement/index.html | 31 +++++++++++++++-- js/i18n.js | 27 +++++++-------- js/model.js | 76 +++++++++++++++++++++++++++++------------- js/view.js | 44 +++--------------------- loot-table/index.html | 31 +++++++++++++++-- predicate/index.html | 31 +++++++++++++++-- 6 files changed, 157 insertions(+), 83 deletions(-) diff --git a/advancement/index.html b/advancement/index.html index e003a009..314d97d1 100644 --- a/advancement/index.html +++ b/advancement/index.html @@ -16,7 +16,34 @@ Advancement Generator Minecraft 1.15 - + + +
+ @@ -25,7 +52,7 @@ - + diff --git a/js/i18n.js b/js/i18n.js index 97ac84b1..013d1d29 100644 --- a/js/i18n.js +++ b/js/i18n.js @@ -6,19 +6,20 @@ const lngs = [ lngs.forEach(v => $('#lngList').append(`${v[1]}`)) -i18next - .use(i18nextBrowserLanguageDetector) - .use(i18nextXHRBackend) - .init({ - backend: { loadPath: '../locales/{{lng}}.json' }, - fallbackLng: 'en', - whitelist: lngs.map(v => v[0]), - keySeparator: false - }) - .then(() => { - jqueryI18next.init(i18next, $, { parseDefaultValueFromContent: false }) - updateView() - }) +function initLng() { + return i18next + .use(i18nextBrowserLanguageDetector) + .use(i18nextXHRBackend) + .init({ + backend: { loadPath: '../locales/{{lng}}.json' }, + fallbackLng: 'en', + whitelist: lngs.map(v => v[0]), + keySeparator: false + }) + .then(() => { + jqueryI18next.init(i18next, $, { parseDefaultValueFromContent: false }); + }); +} function changeLng(code) { i18next.changeLanguage(code).then(() => { diff --git a/js/model.js b/js/model.js index 422c6259..af0da7a4 100644 --- a/js/model.js +++ b/js/model.js @@ -1,30 +1,58 @@ +let indentation = 2; +let luckBased = false; +let historyBuffer = 100; +let history = ['{}']; +let historyIndex = 0; + +let structure; +let components; +let collections; +let table = {}; +let listeners = []; + +const generators = { + 'advancement': ['1.15'], + 'loot-table': ['1.13', '1.14', '1.15'], + 'predicate': ['1.15'] +} + +function addListener(listener) { + listeners.push(listener); + listener(); +} + +loadGenerator($('[data-generator]').attr('data-generator')); +function loadGenerator(generator) { + const versions = generators[generator] || []; + versions.forEach(v => { + $('#versionList').append(`${v}`) + }); + const promises = [initLng(), loadVersion(generator, '1.15')]; + Promise.all(promises).then(() => { + invalidated() + }); +} + +function loadVersion(generator, version) { + return $.getJSON('../schemas/' + version + '.json', json => { + structure = json.roots.find(e => e.id === generator); + components = json.components; + collections = json.collections; + }).fail((jqXHR, textStatus, errorThrown) => { + let message = 'Failed loading ' + version + ' schema'; + structure = {}; + console.error(message + '\n' + errorThrown); + }).always(() => { + $('#versionLabel').text(version); + }); +} + $("#source").val(''); $('#luckBased').prop('checked', false); $('#tableType').val("minecraft:generic"); $('#indentationSelect').val("2"); -let indentation = 2; -let luckBased = false; -let table = { - type: "minecraft:generic", - pools: [ - { - "rolls": 1, - "entries": [ - { - "type": "minecraft:item", - "name": "minecraft:stone" - } - ] - } - ] -}; -let historyBuffer = 100; -let history = ['{}']; -let historyIndex = 0; -invalidated(); - const params = new URLSearchParams(window.location.search); if (params.has('q')) { $('#source').val(atob(params.get('q'))); @@ -49,7 +77,7 @@ function undo() { if (historyIndex > 0) { historyIndex -= 1; table = JSON.parse(history[historyIndex]); - updateView(); + listeners.forEach(l => l()); } } @@ -57,7 +85,7 @@ function redo() { if (historyIndex < history.length - 1) { historyIndex += 1; table = JSON.parse(history[historyIndex]); - updateView(); + listeners.forEach(l => l()); } } @@ -73,7 +101,7 @@ function invalidated() { history = history.slice(0, historyIndex); history.push(JSON.stringify(table)); } - updateView(); + listeners.forEach(l => l()); } function updateTableType() { diff --git a/js/view.js b/js/view.js index fbada69a..da1a9698 100644 --- a/js/view.js +++ b/js/view.js @@ -1,45 +1,9 @@ -let structure; -let components; -let collections; -const generators = { - 'advancement': ['1.15'], - 'loot-table': ['1.13', '1.14', '1.15'], - 'predicate': ['1.15'] -} +const themes = ["light", "dark"]; -const generator = window.location.pathname.replace(/\/$/, '').replace(/^\//, ''); -generators[generator].forEach(v => { - $('#versionList').append(`${v}`) -}); +themes.forEach(v => $('#themeList').append(``)); -changeVersion('1.15'); -function changeVersion(version) { - $.getJSON('../schemas/' + version + '.json', json => { - if (json.root) { - structure = json.root; - } else if (json.roots) { - structure = json.roots.find(e => e.id === generator) || json.roots[0] ; - } - components = json.components; - collections = json.collections; - }).fail((jqXHR, textStatus, errorThrown) => { - let message = 'Failed loading ' + version + ' schema'; - structure = { - fields: [ - { - id: 'pools', - type: 'error', - message: message - } - ] - }; - console.error(message + '\n' + errorThrown); - }).always(() => { - $('#versionLabel').text(version); - updateView(); - }); -} +addListener(updateView); changeTheme(localStorage.getItem('theme')) function changeTheme(theme) { @@ -65,7 +29,7 @@ function updateView() { } function generateSourceAndView(data, struct) { - if (generator === 'loot-table') { + if (struct.id === 'loot-table') { $('#lootTableToolbar').removeClass('d-none'); $('#structure').attr('data-index', 'pools'); return generateTable(data, struct); diff --git a/loot-table/index.html b/loot-table/index.html index cfe77f8b..52ed7715 100644 --- a/loot-table/index.html +++ b/loot-table/index.html @@ -16,7 +16,34 @@ Loot Table Generator Minecraft 1.15 - + + +
+ @@ -25,7 +52,7 @@ - + diff --git a/predicate/index.html b/predicate/index.html index e1c6f4d6..ab6dbf38 100644 --- a/predicate/index.html +++ b/predicate/index.html @@ -16,7 +16,34 @@ Predicate Generator Minecraft 1.15 - + + +
+ @@ -25,7 +52,7 @@ - + From cab0885fc02e333231842d30c0bf672684335eb6 Mon Sep 17 00:00:00 2001 From: Misode Date: Sun, 29 Dec 2019 17:03:58 +0100 Subject: [PATCH 3/5] Load shared html --- components.html | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ js/model.js | 9 +++- shared.html | 47 +++++++++++++++++ 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 components.html create mode 100644 shared.html diff --git a/components.html b/components.html new file mode 100644 index 00000000..a40bad4f --- /dev/null +++ b/components.html @@ -0,0 +1,130 @@ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ + + +
+ + + + + + + + + +
+
+
+ + + +
+ + + + + +
+
+
+ +
+ + + + +
+
+
+ +
+
+ + +
+
+
+
+ +
+ +
+
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+ + +
+
+
+
+
+
+ +
+ +
+ +
+
+
+
diff --git a/js/model.js b/js/model.js index af0da7a4..3895a394 100644 --- a/js/model.js +++ b/js/model.js @@ -28,7 +28,7 @@ function loadGenerator(generator) { versions.forEach(v => { $('#versionList').append(`${v}`) }); - const promises = [initLng(), loadVersion(generator, '1.15')]; + const promises = [initShared(), initLng(), loadVersion(generator, '1.15')]; Promise.all(promises).then(() => { invalidated() }); @@ -48,6 +48,13 @@ function loadVersion(generator, version) { }); } +async function initShared() { + const components = await fetch('../components.html').then(r => r.text()); + const shared = await fetch('../shared.html').then(r => r.text()); + $('body').append(components); + $('div.container').append(shared); +} + $("#source").val(''); $('#luckBased').prop('checked', false); $('#tableType').val("minecraft:generic"); diff --git a/shared.html b/shared.html new file mode 100644 index 00000000..2c721169 --- /dev/null +++ b/shared.html @@ -0,0 +1,47 @@ +
+
+
+ +
+ +
+ +
+ + +
+
+ +
+
+
+
+
+
+
+
+
+ + +
+ + +
+
+
+ + +
+ +
+
From 303205aea908082b6364805e86ea106fa04deaf1 Mon Sep 17 00:00:00 2001 From: Misode Date: Sun, 29 Dec 2019 18:30:34 +0100 Subject: [PATCH 4/5] Add home menu --- advancement/index.html | 2 +- css/custom.css | 17 ++++ index.html | 200 ++++++----------------------------------- js/i18n.js | 6 +- js/model.js | 2 + js/view.js | 1 + locales/en.json | 2 + loot-table/index.html | 2 +- predicate/index.html | 2 +- schemas/1.15.json | 31 ++++++- 10 files changed, 87 insertions(+), 178 deletions(-) diff --git a/advancement/index.html b/advancement/index.html index 314d97d1..dfac209d 100644 --- a/advancement/index.html +++ b/advancement/index.html @@ -17,7 +17,7 @@