diff --git a/src/public/assets/js/modules/events.module.js b/src/public/assets/js/modules/events.module.js index 88eed3f..c6f9f02 100644 --- a/src/public/assets/js/modules/events.module.js +++ b/src/public/assets/js/modules/events.module.js @@ -1,3 +1,6 @@ +import * as OreParser from './parser/ore.module.js'; +import * as StoneParser from './parser/stone.module.js'; + export const preInit = () => { switch112Listener(); }; @@ -8,136 +11,17 @@ export const init = () => { const generateJSONListener = () => { const btn = document.getElementById('generate-btn'); - const oreCfg = document.getElementById('oreconfig'); - const stoneCfg = document.getElementById('stoneconfig'); btn.addEventListener('click', async () => { btn.attributes.disabled = true; - let ores = Array(); - let err = false; - - oreCfg.querySelectorAll('form').forEach((form) => { - const ore = parseForm(form); - - if (ore == -1) { - err = true; - return; - } else { - ores.push(ore); - } - }); - - if (err) { - return; - } - - const asJson = JSON.parse( - JSON.stringify({ - "ores": ores - }) - ); - - console.log(asJson); - - const resp = await fetch('/', { - method: 'PUT', - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - name: '', - config: asJson, - }) - }); - - const newDoc = await resp.json(); - makeNotification('Config Upload Complete!', ` -
- You can view your JSON at -
- ${location.origin}/configs/${newDoc._id} -
- `); + await OreParser.read(); + await StoneParser.read(); btn.attributes.disabled = false; }); }; - -const makeNotification = (title, contents) => { - const modal = document.getElementById('notification'); - - modal.querySelector('.modal-title').innerHTML = title; - modal.querySelector('.modal-body').innerHTML = contents; - - new bootstrap.Modal.getInstance(modal).show(); -}; - -const parseForm = (form) => { - let ore = { - 'blocks': Array(), - 'samples': Array() - }; - - const allInputs = Array.from(form.querySelectorAll('input, select[name="type"]')); - - for (let [idx, inp] of allInputs.entries()) { - let err = false; - if (inp.required && !inp.value || (inp.type == 'number' && (parsed(inp.value) > parsed(inp.max) || parsed(inp.value) < parsed(inp.min)))) { - err = true; - inp.classList.add('is-invalid'); - } else { - if (inp.classList.contains('is-invalid')) { - inp.classList.remove('is-invalid'); - } - } - - if (err) { - alert("There was an error in your form (see outlined in red). Your JSON will not be generated until you correct this."); - return -1; - } - - if (!inp.value) { /* Skip over *optional*, empty attrs */ - continue; - } - - if (inp.name.includes('weight-')) { /* Skip weight items because they're parsed using idx+1 */ - continue; - } - - if (inp.name.includes('block-ore-')) { /* Parse out ores */ - ore.blocks.push(inp.value, parseInt(allInputs[idx + 1].value)); - } else if (inp.name.includes('block-sample-')) { /* Parse out samples*/ - ore.samples.push(inp.value, parseInt(allInputs[idx + 1].value)); - } else if (inp.name.includes('-')) { /* parse out any JSONArray() objs */ - const key = inp.name.substr(0, inp.name.indexOf('-')); - - if (Object.keys(ore).includes(key)) { - ore[key].push(parsed(inp.value)); - } else { - ore[key] = [parsed(inp.value)]; - } - } else { /* parse anything else */ - if (inp.type == 'checkbox') { - ore[inp.name] = inp.checked; - } else { - ore[inp.name] = parsed(inp.value); - } - } - } - - return ore; -}; - -const parsed = (any) => { - if (!any) { - return; - } else if (isNaN(Number(any))) { - return String(any); - } else { - return Number(any); - } -}; - const switch112Listener = () => { const switch112 = document.getElementById('switch-112-mode'); diff --git a/src/public/assets/js/modules/parser/ore.module.js b/src/public/assets/js/modules/parser/ore.module.js new file mode 100644 index 0000000..24ea030 --- /dev/null +++ b/src/public/assets/js/modules/parser/ore.module.js @@ -0,0 +1,116 @@ +import { parsed, makeNotification } from '../util.module.js'; + +/** + * Reads through every form on the oreconfig element + * + * @returns {null} + */ +export const read = async () => { + const oreCfg = document.getElementById('oreconfig'); + + let ores = Array(); + let err = false; + + oreCfg.querySelectorAll('form').forEach((form) => { + const ore = parse(form); + + if (ore == -1) { + err = true; + return; + } else { + ores.push(ore); + } + }); + + if (err) { + return; + } + + const asJson = JSON.parse( + JSON.stringify({ + "ores": ores + }) + ); + + const resp = await fetch('/', { + method: 'PUT', + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + json: asJson, + }) + }); + + const newDoc = await resp.json(); + makeNotification('Config Upload Complete!', ` +
+ You can view your JSON at +
+ ${location.origin}/configs/${newDoc._id} +
+ `); +}; + +/** + * Internal method for parsing a form into an object + * + * Arguments: + * @param {HTMLFormElement} form: The form to parse away at - selects all [select[name='type'], input] subtypes + * + * @returns {object} + */ +const parse = (form) => { + let ore = { + 'blocks': Array(), + 'samples': Array() + }; + + const allInputs = Array.from(form.querySelectorAll('input, select[name="type"]')); + + for (let [idx, inp] of allInputs.entries()) { + let err = false; + if (inp.required && !inp.value || (inp.type == 'number' && (parsed(inp.value) > parsed(inp.max) || parsed(inp.value) < parsed(inp.min)))) { + err = true; + inp.classList.add('is-invalid'); + } else { + if (inp.classList.contains('is-invalid')) { + inp.classList.remove('is-invalid'); + } + } + + if (err) { + alert("There was an error in your form (see outlined in red). Your JSON will not be generated until you correct this."); + return -1; + } + + if (!inp.value) { /* Skip over *optional*, empty attrs */ + continue; + } + + if (inp.name.includes('weight-')) { /* Skip weight items because they're parsed using idx+1 */ + continue; + } + + if (inp.name.includes('block-ore-')) { /* Parse out ores */ + ore.blocks.push(inp.value, parseInt(allInputs[idx + 1].value)); + } else if (inp.name.includes('block-sample-')) { /* Parse out samples*/ + ore.samples.push(inp.value, parseInt(allInputs[idx + 1].value)); + } else if (inp.name.includes('-')) { /* parse out any JSONArray() objs */ + const key = inp.name.substr(0, inp.name.indexOf('-')); + + if (Object.keys(ore).includes(key)) { + ore[key].push(parsed(inp.value)); + } else { + ore[key] = [parsed(inp.value)]; + } + } else { /* parse anything else */ + if (inp.type == 'checkbox') { + ore[inp.name] = inp.checked; + } else { + ore[inp.name] = parsed(inp.value); + } + } + } + + return ore; +}; + diff --git a/src/public/assets/js/modules/parser/stone.module.js b/src/public/assets/js/modules/parser/stone.module.js new file mode 100644 index 0000000..5ebc260 --- /dev/null +++ b/src/public/assets/js/modules/parser/stone.module.js @@ -0,0 +1,6 @@ +export const read = () => { + const stoneCfg = document.getElementById('stoneconfig'); + + console.warn('stone.module.read was called but has not been implemented'); +}; + diff --git a/src/public/assets/js/modules/util.module.js b/src/public/assets/js/modules/util.module.js new file mode 100644 index 0000000..64a5bb6 --- /dev/null +++ b/src/public/assets/js/modules/util.module.js @@ -0,0 +1,18 @@ +export const parsed = (any) => { + if (!any) { + return; + } else if (isNaN(Number(any))) { + return String(any); + } else { + return Number(any); + } +}; + +export const makeNotification = (title, contents) => { + const modal = document.getElementById('notification'); + + modal.querySelector('.modal-title').innerHTML = title; + modal.querySelector('.modal-body').innerHTML = contents; + + new bootstrap.Modal.getInstance(modal).show(); +}; \ No newline at end of file diff --git a/src/public/assets/js/modules/utils.module.js b/src/public/assets/js/modules/utils.module.js deleted file mode 100644 index e69de29..0000000