var $orderActionForm = $(".order-action-form"); var $cryptoStat = $(".crypto-stat"); var $orderType = $orderActionForm.find(".order-type"); var $orderDirection = $orderActionForm.find(".order-direction"); var $orderDetails = $orderActionForm.find(".order-details"); var $orderTotal = $orderActionForm.find(".order-total"); var $directionList = $orderDetails.find(".amount .drop-down-group"); var $unitPriceList = $orderDetails.find(".price .drop-down-group"); var $totalPriceList = $orderTotal.find(".total-price .drop-down-group"); var $baseList = $cryptoStat.find(".base-currency .drop-down-group"); // -- Bind Currencies to drop down list $(function () { var Sources = []; for (var i = 0; i < AvailableCryptos.length; i++) Sources[Sources.length] = { text: AvailableCryptos[i], value: AvailableCryptos[i] }; $directionList[0].bind(Sources); }); $(function () { var Sources = []; for (var i = 0; i < AvailableFiatMoney.length; i++) Sources[Sources.length] = { text: AvailableFiatMoney[i], value: AvailableFiatMoney[i] }; $unitPriceList[0].bind(Sources); $totalPriceList[0].bind(Sources); }); $(function () { var Sources = []; for (var i = 0; i < AvailableFiatMoney.length; i++) Sources[Sources.length] = { text: AvailableFiatMoney[i], value: AvailableFiatMoney[i] }; for (var i = 0; i < AvailableCryptos.length; i++) Sources[Sources.length] = { text: AvailableCryptos[i], value: AvailableCryptos[i] }; if ($baseList.length > 0) $baseList[0].bind(Sources); }); // -- Bind events // ---- Order Actions $(function () { var $marketButton = $orderType.find(".market-order"); var $limitButton = $orderType.find(".limit-order"); var $stopButton = $orderType.find(".stop-order"); var $amount = $orderDetails.find(".amount"); var $amountInput = $amount.find("input"); var $unitPrice = $orderDetails.find(".price"); var $unitPriceInput = $unitPrice.find("input"); var $totalPrice = $orderTotal.find(".total-price"); var $totalPriceInput = $totalPrice.find("input"); function GetMarketPrice() { $amount.attr("disabled", "disabled").find("*").attr("disabled", "disabled"); $marketButton.attr("disabled", "disabled"); $limitButton.attr("disabled", "disabled"); $stopButton.attr("disabled", "disabled"); var baseAsset = CryptoAssets[$amount.find("select option:selected").val()]; var fiatAsset = FiatAssets[$unitPrice.find("select option:selected").val()]; if (!baseAsset || !fiatAsset) { setTimeout(function () { GetMarketPrice(); }, 500); return; } $.get(GetCoinAPIUrl("v1/exchangerate/" + baseAsset.asset_id + "/" + fiatAsset.asset_id)) .done(function (response) { var Rate = JSON.parse(response); $unitPrice.find("input").val(new Number(Rate.rate).toFixed(2)); }) .always(function () { $amount.removeAttr("disabled").find("*").removeAttr("disabled"); $marketButton.removeAttr("disabled"); $limitButton.removeAttr("disabled"); $stopButton.removeAttr("disabled"); $amountInput.trigger("keyup"); }); } $marketButton.on("click", function () { $unitPrice.attr("disabled", "disabled").find("*").attr("disabled", "disabled"); $totalPrice.attr("disabled", "disabled").find("*").attr("disabled", "disabled"); GetMarketPrice(); }); $limitButton.on("click", function () { $unitPrice.removeAttr("disabled").find("*").removeAttr("disabled"); $totalPrice.removeAttr("disabled").find("*").removeAttr("disabled"); }); $stopButton.on("click", function () { $unitPrice.removeAttr("disabled").find("*").removeAttr("disabled"); $totalPrice.removeAttr("disabled").find("*").removeAttr("disabled"); }); $marketButton.trigger("click"); $amount.find("select").on("change", function () { if ($marketButton.hasClass("active")) $marketButton.trigger("click"); }); $amountInput.on("keyup", function () { if (!$unitPriceInput.val()) { $totalPriceInput.val(""); return; } $totalPriceInput.val(new Number( parseFloat($amountInput.val()) * parseFloat($unitPriceInput.val()) ).toFixed(2)); }); $unitPriceInput.on("keyup", function () { if (!$totalPriceInput.val()) { $amountInput.val(""); return; } $amountInput.val(new Number( parseFloat($totalPriceInput.val()) / parseFloat($unitPriceInput.val()) ).toFixed(2)); }); $totalPriceInput.on("keyup", function () { if (!$unitPriceInput.val()) { $amountInput.val(""); return; } $amountInput.val(new Number( parseFloat($totalPriceInput.val()) / parseFloat($unitPriceInput.val()) ).toFixed(2)); }); }); // ---- Stat $(function () { var $baseSelect = $baseList.find("select"); function BindCryptosStat(selectedBase) { var Asset = CryptoAssets[selectedBase]; if (!Asset) Asset = FiatAssets[selectedBase]; if (!Asset) { setTimeout(function () { BindCryptosStat(selectedBase); }, 500); return; } var CreateElement = function (value) { var $current = $("
").addClass("current"); var $compareSolid = $("").addClass("compare-solid text-left"); var $comparePercent = $("").addClass("compare-percent text-right"); if (value.toUpperCase() != selectedBase.toUpperCase()) $("
") .addClass("col-xl-3 col-lg-4 col-md-2 col-sm-3 col-4") .append( $("
") .addClass("crypto") .append($("
").addClass("title").text(selectedBase + "/" + value)) .append($current) .append( $("
") .append($compareSolid) .append($comparePercent) ) ) .appendTo($cryptoStat); return { current: $current, solid: $compareSolid, percent: $comparePercent }; } $cryptoStat.find("> :not(.base-currency)").remove(); var $elements = []; for (var i = 0; i < AvailableFiatMoney.length; i++) $elements[AvailableFiatMoney[i]] = CreateElement(AvailableFiatMoney[i]); for (var i = 0; i < AvailableCryptos.length; i++) $elements[AvailableCryptos[i]] = CreateElement(AvailableCryptos[i]); $.get(GetCoinAPIUrl("v1/exchangerate/" + Asset.asset_id)) .done(function (response) { var Rates = JSON.parse(response).rates; for (var i = 0; i < Rates.length; i++) { if (AvailableCryptos.indexOf(Rates[i].asset_id_quote) >= 0 || AvailableFiatMoney.indexOf(Rates[i].asset_id_quote) >= 0 ) { var $ele = $elements[Rates[i].asset_id_quote]; if ($ele) { $ele.current.text(new Number(Rates[i].rate).toFixed(4)); $ele.solid.text("+0.002"); $ele.percent.text("-0.01%"); } } } }) .always(function () { $baseSelect .prop("disabled", false) .siblings() .removeAttr("disabled"); }); } $baseSelect.on("change", function () { $baseSelect.prop("disabled", true).siblings().attr("disabled", "disabled"); BindCryptosStat($baseSelect.val()); }); $baseSelect.trigger("change"); });