viewof attribsCount = {
selectedAttributes;
function getNewAttr(allAttrs, currentAttribs) {
const remainingAttrs = allAttrs.filter((a) => !currentAttribs.includes(a));
return remainingAttrs.at(Math.floor(Math.random() * remainingAttrs.length));
}
const addAttrib = html`<button>+</button>`;
const removeAttrib = html`<button>-</button>`;
const spanCount = html`<span style="margin: 1px 2px">${
viewof selectedAttributes.value.length
}</span>`;
const target = ReactiveWidget(html`<span id="plusMinus">
<style>
#plusMinus {
display: inline-flex;
align-items: center;
}
#plusMinus button {
font-size: 1.2rem;
margin: 1px 10px
}
</style>
${removeAttrib} <div> ${spanCount} attributes </div> ${addAttrib}
</span>`);
function update(stopAnimation = false) {
spanCount.textContent = viewof selectedAttributes.value.length;
viewof selectedAttributes.dispatchEvent(new Event("input"));
if (stopAnimation) {
viewof animate.value = false;
viewof animate.dispatchEvent(new Event("input"));
}
}
target.incr = (evt) => {
evt && evt.preventDefault();
evt && evt.stopPropagation();
viewof selectedAttributes.value = [
...viewof selectedAttributes.value,
getNewAttr(attrs, viewof selectedAttributes.value)
];
update(!!evt);
};
target.decr = (evt) => {
evt && evt.preventDefault();
evt && evt.stopPropagation();
viewof selectedAttributes.value = viewof selectedAttributes.value.slice(
0,
viewof selectedAttributes.value.length - 1
);
update(!!evt);
};
addAttrib.addEventListener("click", target.incr);
removeAttrib.addEventListener("click", target.decr);
return target;
}