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 @@ - +