Mercurial > titip
diff index.html @ 4:3be5d1679ce0
Add bni, rewrite javascript
author | nanaya <me@myconan.net> |
---|---|
date | Thu, 19 Nov 2015 02:19:03 +0900 |
parents | 9e5f60aae01c |
children | c1086c23ef29 |
line wrap: on
line diff
--- a/index.html Fri Aug 14 21:20:56 2015 +0900 +++ b/index.html Thu Nov 19 02:19:03 2015 +0900 @@ -56,6 +56,13 @@ <span id="result-bank"></span> IDR (<span id="rate-text"></span> IDR/JPY) </li> + + <li> + Bank transfer (alt): + <span id="result-bank-bni"></span> IDR + (<span id="rate-text-bni"></span> IDR/JPY) + </li> + <li> PayPal: <span id="result-paypal"></span> JPY @@ -74,6 +81,7 @@ <li>Perhitungan: <ul> <li>Bank: <code>jumlah × 1.07 × nilai_tukar</code>, dibulatkan ke seribuan terdekat.</li> + <li>Bank (alt): <code>jumlah × 1.01 × nilai_tukar + 25000</code>, dibulatkan ke seribuan terdekat.</li> <li>PayPal: <code>jumlah × 1.05</code>, dibulatkan ke satuan terdekat.</li> </ul> </li> @@ -84,51 +92,95 @@ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> - // best javascript ahead. Proceed with care. - var - displayResult = function(x, rate) { - $("#rate-text").text(rate.toLocaleString()) - $("#result-bank").text((Math.round(x * 1.07 * rate / 1000) * 1000).toLocaleString()) - $("#result-paypal").text((Math.round(x * 1.05)).toLocaleString()) - $("#ok").show() - }, - loading = $("#loading"), - doButton = $("button[type=submit]"), - clearButton = $("button[type=reset]"), - unlock = function() { - loading.hide() - doButton.attr("disabled", false) - }, - lock = function() { - loading.show() - doButton.attr("disabled", true) - }, - xForm = $("#x"), - action = function(e) { - e.preventDefault() - $(".js").hide() - var x = parseFloat(xForm.val()) - if (x === NaN || x === undefined) { return false; } + var input = function() { return parseFloat($xForm.val()); } + + var $loading = $("#loading"); + var $doButton = $("button[type=submit]"); + var $xForm = $("#x"); + + var locks = {}; + var lock = function(isLoading) { + if (!isLoading && locks.fixer === true && locks.bni === true) { return; } + $loading.toggle(isLoading); + $doButton.attr("disabled", isLoading); + }; + + var rates = {}; + + var displayResult = function() { + if (rates.fixer === undefined || rates.bni === undefined) { return; } + var x = input(); + + $("#rate-text").text(rates.fixer.toLocaleString()); + $("#result-bank").text((Math.round(x * 1.07 * rates.fixer / 1000) * 1000).toLocaleString()); + + if (rates.bni !== 0) { + $("#rate-text-bni").text(rates.bni.toLocaleString()); + $("#result-bank-bni").text((Math.ceil(x * 1.01 * rates.bni / 1000) * 1000 + 25000).toLocaleString()); + } else { + $("#rate-text-bni").text('x'); + $("#result-bank-bni").text('x'); + }; + + $("#result-paypal").text((Math.round(x * 1.05)).toLocaleString()); + $("#ok").show(); + lock(false); + }; + + var action = function(e) { + e.preventDefault() + $(".js").hide() + + var x = input(); + + if (x === NaN || x === undefined) { return false; }; - if (window.rate === undefined) { - lock() - $.getJSON("https://api.fixer.io/latest", { base: "JPY", symbols: "IDR" }) - .done(function(data) { - window.rate = data.rates["IDR"] - displayResult(x, window.rate) - }) - .fail(function() { - $("#error").show() - }) - .always(function() { - unlock() - }) - } else { - displayResult(x, window.rate) - } - } - clearButton.click(function(e) { xForm.focus() }) - doButton.click(action) + if (rates.fixer === undefined) { + lock(true); + locks.fixer = true; + + $.getJSON("https://api.fixer.io/latest", { base: "JPY", symbols: "IDR" }) + .done(function(data) { + rates.fixer = parseFloat(data.rates["IDR"]); + + displayResult(); + }) + .fail(function() { + $("#error").show(); + }) + .always(function() { + locks.fixer = false; + lock(false); + }); + }; + + if (rates.bni === undefined) { + lock(true); + locks.bni = true; + + $.get("bni-jpyidr.txt") + .done(function(data) { + if (data === "") { + rates.bni = 0; + } else { + rates.bni = parseFloat(data); + } + + displayResult(); + }) + .fail(function() { + $("#error").show(); + }) + .always(function() { + locks.bni = false; + lock(false); + }); + }; + + displayResult(); + }; + $("form").submit(action) + $("button[type=reset]").click(function(e) { xForm.focus(); }); </script> </body>