diff --git a/controllers/element-controller.js b/controllers/element-controller.js index 12a321e..a237cdf 100644 --- a/controllers/element-controller.js +++ b/controllers/element-controller.js @@ -2,6 +2,9 @@ function addElement(type) { if (type == "TextBox") { document.getElementById("playground").appendChild(new TextBox().buildElement()); } + if (type == "Base64Converter") { + document.getElementById("playground").appendChild(new Base64Converter().buildElement()); + } } class ConnectionController { diff --git a/elements/Base64Converter.js b/elements/Base64Converter.js new file mode 100644 index 0000000..3cdfe3f --- /dev/null +++ b/elements/Base64Converter.js @@ -0,0 +1,8 @@ +class Base64Converter extends Converter { + constructor() { + super("Base64 Converter", [ + { label : "Encode", conversionFunction : (d) => btoa(d) }, + { label : "Decode", conversionFunction : (d) => atob(d) }, + ]); + } +} \ No newline at end of file diff --git a/elements/Converter.js b/elements/Converter.js new file mode 100644 index 0000000..6ae8bf6 --- /dev/null +++ b/elements/Converter.js @@ -0,0 +1,54 @@ +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); + let output = new Connector(Connector.OUTPUT); + + 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; + } +} \ No newline at end of file diff --git a/index.html b/index.html index 7c5a69d..271fd16 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,8 @@ + +