mirror of https://github.com/lgc-4/DataTools3.git
54 lines
1.2 KiB
JavaScript
54 lines
1.2 KiB
JavaScript
class Converter extends Box {
|
|
optionSelect;
|
|
options;
|
|
selectedOption = 0;
|
|
|
|
|
|
/**
|
|
* options is array like
|
|
* [
|
|
* {"label" : "Encode", "conversionFunction" : btoa},
|
|
* {"label" : "Decode", "conversionFunction" : atob}
|
|
* ]
|
|
*/
|
|
constructor(label, options) {
|
|
super(label);
|
|
|
|
this.options = options;
|
|
|
|
let input = new Connector(Connector.INPUT, "any");
|
|
let output = new Connector(Connector.OUTPUT, "any");
|
|
|
|
input.addListener((data) => {
|
|
output.send(this.options[this.selectedOption].conversionFunction(data));
|
|
});
|
|
|
|
this.inputs.push(input);
|
|
this.outputs.push(output);
|
|
}
|
|
|
|
buildElement() {
|
|
super.buildElement();
|
|
|
|
this.optionSelect = document.createElement("select");
|
|
|
|
for (let i = 0; i < this.options.length; i++) {
|
|
let optionElement = document.createElement("option");
|
|
optionElement.innerText = this.options[i].label;
|
|
optionElement.value = i;
|
|
this.optionSelect.appendChild(optionElement);
|
|
}
|
|
|
|
if (this.options.length == 1) {
|
|
this.optionSelect.style.display = "none";
|
|
}
|
|
|
|
this.optionSelect.addEventListener("input", () => {
|
|
this.selectedOption = this.optionSelect.value;
|
|
});
|
|
|
|
this.content.appendChild(this.optionSelect);
|
|
|
|
return this.element;
|
|
}
|
|
} |