pvgttm-web/src/assets/scripts/tools.js

77 lines
2.7 KiB
JavaScript

// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT
/****************************************************************************
* Planar Vagabond's Guide to the Multiverse (planarvagabond.com)
* Referee Tools
*
* Copyright 2023 Eric Woodward
* Source released under MIT License
* https://www.itsericwoodward.com/licenses/mit
****************************************************************************/
const // Scripts for the Die Roller
addRollerForm = () => {
const rollerForm = document.getElementById('js-rollerForm'),
rollerInput = document.getElementById('js-rollerInput'),
rollerOutput = document.getElementById('js-rollerOutput');
// double-click [x] to clear list
rollerForm.addEventListener('reset', (e) => {
if (rollerInput.value === '') rollerOutput.replaceChildren();
});
// do the roll and show th (cleaned-up) result
rollerForm.addEventListener('submit', (e) => {
e.preventDefault();
if (!window.dice) return;
const newEl = document.createElement('li');
const result = dice.roll(rollerInput.value);
newEl.innerText = `${dice.stringify(result)
.replaceAll('!!!mods listing not yet complete!!!', '')} = ${+result}`;
rollerOutput.prepend(newEl);
});
// clear on escape key
rollerForm.addEventListener('keydown', (event) => {
if (event.key === 'Escape') rollerForm.reset();
});
},
// Scripts for the Complication Randomizers
shuffleContainer = (parent) => {
const container = document.getElementById(parent),
children = container.children,
length = children.length,
shuffled = [...children];
for (let i = 0; i < length * length; i++)
shuffled.sort(() => 0.5 - Math.random());
for (let i = 0; i < length; i++) container.appendChild(shuffled[i]);
},
addComplicationForm = () => {
const complicationForm = document.getElementById('js-complicationForm');
complicationForm.addEventListener('submit', (e) => {
e.preventDefault();
shuffleContainer('js-complicationList');
});
},
addAstralComplicationForm = () => {
const complicationForm = document.getElementById(
'js-astralComplicationForm'
);
complicationForm.addEventListener('submit', (e) => {
e.preventDefault();
shuffleContainer('js-astralComplicationList');
});
};
export default (() => {
addRollerForm();
addComplicationForm();
addAstralComplicationForm();
})();
// @license-end