comparison 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
comparison
equal deleted inserted replaced
3:78658abbb431 4:3be5d1679ce0
54 <li> 54 <li>
55 Bank transfer: 55 Bank transfer:
56 <span id="result-bank"></span> IDR 56 <span id="result-bank"></span> IDR
57 (<span id="rate-text"></span> IDR/JPY) 57 (<span id="rate-text"></span> IDR/JPY)
58 </li> 58 </li>
59
60 <li>
61 Bank transfer (alt):
62 <span id="result-bank-bni"></span> IDR
63 (<span id="rate-text-bni"></span> IDR/JPY)
64 </li>
65
59 <li> 66 <li>
60 PayPal: 67 PayPal:
61 <span id="result-paypal"></span> JPY 68 <span id="result-paypal"></span> JPY
62 </li> 69 </li>
63 </ul> 70 </ul>
72 <li>Berhubung sudah punya timbangan, ongkir (estimasi) bisa dihitung sebelum dikirim.</li> 79 <li>Berhubung sudah punya timbangan, ongkir (estimasi) bisa dihitung sebelum dikirim.</li>
73 <li><a href="http://www.post.japanpost.jp/int/charge/list/ems1_en.html">Tabel ongkir EMS</a>.</li> 80 <li><a href="http://www.post.japanpost.jp/int/charge/list/ems1_en.html">Tabel ongkir EMS</a>.</li>
74 <li>Perhitungan: 81 <li>Perhitungan:
75 <ul> 82 <ul>
76 <li>Bank: <code>jumlah &times; 1.07 &times; nilai_tukar</code>, dibulatkan ke seribuan terdekat.</li> 83 <li>Bank: <code>jumlah &times; 1.07 &times; nilai_tukar</code>, dibulatkan ke seribuan terdekat.</li>
84 <li>Bank (alt): <code>jumlah &times; 1.01 &times; nilai_tukar + 25000</code>, dibulatkan ke seribuan terdekat.</li>
77 <li>PayPal: <code>jumlah &times; 1.05</code>, dibulatkan ke satuan terdekat.</li> 85 <li>PayPal: <code>jumlah &times; 1.05</code>, dibulatkan ke satuan terdekat.</li>
78 </ul> 86 </ul>
79 </li> 87 </li>
80 <li>Jangan lupa memastikan jumlah setelah pajak dan ongkir (lokal).</li> 88 <li>Jangan lupa memastikan jumlah setelah pajak dan ongkir (lokal).</li>
81 <li>Data nilai tukar didapat dari layanan <a href="http://fixer.io/">Fixer.io</a>.</li> 89 <li>Data nilai tukar didapat dari layanan <a href="http://fixer.io/">Fixer.io</a>.</li>
82 </ul> 90 </ul>
83 </div> 91 </div>
84 92
85 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 93 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
86 <script> 94 <script>
87 // best javascript ahead. Proceed with care. 95 var input = function() { return parseFloat($xForm.val()); }
88 var
89 displayResult = function(x, rate) {
90 $("#rate-text").text(rate.toLocaleString())
91 $("#result-bank").text((Math.round(x * 1.07 * rate / 1000) * 1000).toLocaleString())
92 $("#result-paypal").text((Math.round(x * 1.05)).toLocaleString())
93 $("#ok").show()
94 },
95 loading = $("#loading"),
96 doButton = $("button[type=submit]"),
97 clearButton = $("button[type=reset]"),
98 unlock = function() {
99 loading.hide()
100 doButton.attr("disabled", false)
101 },
102 lock = function() {
103 loading.show()
104 doButton.attr("disabled", true)
105 },
106 xForm = $("#x"),
107 action = function(e) {
108 e.preventDefault()
109 $(".js").hide()
110 var x = parseFloat(xForm.val())
111 if (x === NaN || x === undefined) { return false; }
112 96
113 if (window.rate === undefined) { 97 var $loading = $("#loading");
114 lock() 98 var $doButton = $("button[type=submit]");
115 $.getJSON("https://api.fixer.io/latest", { base: "JPY", symbols: "IDR" }) 99 var $xForm = $("#x");
116 .done(function(data) { 100
117 window.rate = data.rates["IDR"] 101 var locks = {};
118 displayResult(x, window.rate) 102 var lock = function(isLoading) {
119 }) 103 if (!isLoading && locks.fixer === true && locks.bni === true) { return; }
120 .fail(function() { 104 $loading.toggle(isLoading);
121 $("#error").show() 105 $doButton.attr("disabled", isLoading);
122 }) 106 };
123 .always(function() { 107
124 unlock() 108 var rates = {};
125 }) 109
126 } else { 110 var displayResult = function() {
127 displayResult(x, window.rate) 111 if (rates.fixer === undefined || rates.bni === undefined) { return; }
128 } 112 var x = input();
129 } 113
130 clearButton.click(function(e) { xForm.focus() }) 114 $("#rate-text").text(rates.fixer.toLocaleString());
131 doButton.click(action) 115 $("#result-bank").text((Math.round(x * 1.07 * rates.fixer / 1000) * 1000).toLocaleString());
116
117 if (rates.bni !== 0) {
118 $("#rate-text-bni").text(rates.bni.toLocaleString());
119 $("#result-bank-bni").text((Math.ceil(x * 1.01 * rates.bni / 1000) * 1000 + 25000).toLocaleString());
120 } else {
121 $("#rate-text-bni").text('x');
122 $("#result-bank-bni").text('x');
123 };
124
125 $("#result-paypal").text((Math.round(x * 1.05)).toLocaleString());
126 $("#ok").show();
127 lock(false);
128 };
129
130 var action = function(e) {
131 e.preventDefault()
132 $(".js").hide()
133
134 var x = input();
135
136 if (x === NaN || x === undefined) { return false; };
137
138 if (rates.fixer === undefined) {
139 lock(true);
140 locks.fixer = true;
141
142 $.getJSON("https://api.fixer.io/latest", { base: "JPY", symbols: "IDR" })
143 .done(function(data) {
144 rates.fixer = parseFloat(data.rates["IDR"]);
145
146 displayResult();
147 })
148 .fail(function() {
149 $("#error").show();
150 })
151 .always(function() {
152 locks.fixer = false;
153 lock(false);
154 });
155 };
156
157 if (rates.bni === undefined) {
158 lock(true);
159 locks.bni = true;
160
161 $.get("bni-jpyidr.txt")
162 .done(function(data) {
163 if (data === "") {
164 rates.bni = 0;
165 } else {
166 rates.bni = parseFloat(data);
167 }
168
169 displayResult();
170 })
171 .fail(function() {
172 $("#error").show();
173 })
174 .always(function() {
175 locks.bni = false;
176 lock(false);
177 });
178 };
179
180 displayResult();
181 };
182
132 $("form").submit(action) 183 $("form").submit(action)
184 $("button[type=reset]").click(function(e) { xForm.focus(); });
133 </script> 185 </script>
134 </body> 186 </body>