From 563d5fbc3067f5ea0ab96e2bd3d5664bc638e271 Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Wed, 25 Mar 2015 19:28:33 +0100 Subject: [PATCH] moved javascript to its own file --- dencoder.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 75 +---------------------------------------------------- 2 files changed, 73 insertions(+), 74 deletions(-) create mode 100644 dencoder.js diff --git a/dencoder.js b/dencoder.js new file mode 100644 index 0000000..1699363 --- /dev/null +++ b/dencoder.js @@ -0,0 +1,72 @@ +(function($) { + var template = "
" + + "
"; + $(document).ready(function() { + var $new = $(template.replace(/\{index\}/g, "0").replace(/\{content\}/g, "Please enter your input here.")); + $("body").append($new); + }); + + function convert(select) { + var $select = $(select); + var selectid = $select.attr("id"); + var inputIndex = +selectid.split("-")[1]; + var outputIndex = inputIndex + 1; + var $input = $("#input-" + inputIndex); + var $output = $("#input-" + outputIndex); + var appendNewOutput = false; + var input = $input.val(); + var conversion = $select.find(":selected").attr("name"); + var output; + if ($output.length == 0) { + appendNewOutput = true; + } + switch (conversion) { + case "CHOOSE": + output = ""; + break; + case "base64encode": + output = btoa(input); + break; + case "base64decode": + output = atob(input); + break; + default: + alert("Unknown: " + conversion); + output = "ERROR!"; + break; + } + if (appendNewOutput) { + $output = $(template.replace(/\{index\}/g, "" + outputIndex).replace(/\{content\}/g, output)); + $("body").append($output); + } else { + $output.val(output); + } + + clean(outputIndex); + } + + function clean(index) { + var found = true; + var $wrapper; + $("#type-" + index + " option[name=CHOOSE]").prop("selected", true); + index++; + do { + $wrapper = $("#wrapper-" + index); + if ($wrapper.length > 0) { + $wrapper.remove(); + index++; + } else { + found = false; + } + } while (found); + } + + var den = {}; + den.convert = convert; + window.den = window.den || den; + +})(jQuery); diff --git a/index.html b/index.html index 2a7eb51..d0c1781 100644 --- a/index.html +++ b/index.html @@ -3,80 +3,7 @@ encoder-decoder - +