From e44bfa1c369840a31ebc459ee045b5459c6a1d8e Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Mon, 25 Nov 2019 00:01:11 +0100 Subject: [PATCH] Reformat JavaScript --- index.html | 350 ++++++++++++++++++++++++++--------------------------- 1 file changed, 174 insertions(+), 176 deletions(-) diff --git a/index.html b/index.html index 3020181..5186be3 100644 --- a/index.html +++ b/index.html @@ -19,199 +19,197 @@ -
- -
-
-

Converter

-
-

Convert numbers to and from various bases.

-
-
-
- - - +
+ +
-
-
-
-
-
-
-
-
+

Converter

+
+

Convert numbers to and from various bases.

+
+
- - -
+ +
+
+
+
+
+
+
+
+
+
- - - - + + + class Base64NumeralSystemInput extends NumeralSystemInput { + static defaultAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + static dropdownOptions = {"Standard": ['+', '/'], "Filename": ['-', '_'], "IMAP": ['+', ',']}; + + + constructor(name) { + super(name, new NumeralSystem(64, Base64NumeralSystemInput.defaultAlphabet, true)); + + this.dropdownLabel = document.createElement("label"); + this.dropdownLabel.setAttribute("for", `${this.name}Dropdown`); + this.dropdownLabel.innerHTML = "Character set"; + this.dropdownLabel.classList.add("label-inline"); + + this.dropdown = document.createElement("select"); + this.dropdown.id = `${this.name}Dropdown`; + this.dropdown.onchange = () => { + const selectedOption = Base64NumeralSystemInput.dropdownOptions[this.dropdown.value]; + this.setLastDigits(selectedOption[0], selectedOption[1]); + }; + + this.dropdownDiv = document.createElement("div"); + this.dropdownDiv.classList.add("float-right"); + + this.options = + Object.keys(Base64NumeralSystemInput.dropdownOptions).map(key => { + const option = document.createElement("option"); + option.value = key; + option.text = key + ": " + Base64NumeralSystemInput.dropdownOptions[key].join(""); + return option; + }); + } + + + setLastDigits(c62, c63) { + const oc62 = this.numeralSystem.alphabet[62]; + const oc63 = this.numeralSystem.alphabet[63]; + + this.numeralSystem.alphabet = + stringReplaceAt(stringReplaceAt(this.numeralSystem.alphabet, 62, c62), 63, c63); + this.textarea.value = + stringReplaceAll(stringReplaceAll(this.textarea.value, oc62, c62), oc63, c63); + } + + addToParent(parent) { + this.dropdownDiv.appendChild(this.dropdownLabel); + this.options.forEach(option => this.dropdown.appendChild(option)); + this.dropdownDiv.appendChild(this.dropdown); + parent.appendChild(this.dropdownDiv); + + parent.appendChild(this.label); + parent.appendChild(this.textarea); + } + } + + + const inputs = [ + new NumeralSystemInput("Binary", new NumeralSystem(2, "01")), + new NumeralSystemInput("Octal", new NumeralSystem(8, "01234567")), + new NumeralSystemInput("Decimal", new NumeralSystem(10, "0123456789")), + new NumeralSystemInput("Duodecimal", new NumeralSystem(12, "0123456789ab", caseSensitive = false)), + new NumeralSystemInput("Hexadecimal", new NumeralSystem(16, "0123456789abcdef", caseSensitive = false)), + new Base64NumeralSystemInput("Base64"), + new NumeralSystemInput( + "ASCII", + new NumeralSystem( + 256, + new Array(256).fill(0).map((_, it) => String.fromCharCode(it)).join(""), + caseSensitive = true + ) + ), + ]; + + const updateAllInputs = newValue => { + for (const input of inputs) + input.update(newValue); + }; + + + doAfterLoad(() => { + const inputParent = $("#inputs"); + + for (const input of inputs) + input.addToParent(inputParent); + + updateAllInputs(bigInt(42)); + inputs[0].textarea.focus(); + }); +