DataTools3/elements/RandomNumberGenerator.js

53 lines
1.2 KiB
JavaScript

class RandomNumberGenerator extends Box {
min;
max;
constructor() {
super("Random Number Generator");
this.outputs.push(new Connector(Connector.OUTPUT, "number"));
let minInput = new Connector(Connector.INPUT, "number", "Min");
minInput.addListener((d) => {
this.min.value = d
this.sendNewNumber();
});
let maxInput = new Connector(Connector.INPUT, "number", "Max");
maxInput.addListener((d) => {
this.max.value = d
this.sendNewNumber();
});
this.inputs.push(minInput);
this.inputs.push(maxInput);
}
buildElement() {
super.buildElement();
this.min = document.createElement("input");
this.min.type = "number";
this.min.value = 1;
this.max = document.createElement("input");
this.max.type = "number";
this.max.value = 100;
this.content.appendChild(this.min);
this.content.appendChild(this.max);
let generateButton = document.createElement("button");
generateButton.innerText = "Generate";
generateButton.addEventListener("click", () => this.sendNewNumber());
this.content.appendChild(generateButton);
return this.element;
}
sendNewNumber() {
this.outputs[0].send(Math.floor(Math.random() * (this.max.value - this.min.value + 1)) + parseFloat(this.min.value));
}
}