/******/ (() => { // webpackBootstrap /******/ "use strict"; $(function () { var $input = $("#amount"); var $progressBar = $("#slider-range-max"); var initialAmount = 1000; var config = null; $.ajax({ method: "GET", url: "https://8oknim7zqi.execute-api.eu-west-1.amazonaws.com/prod" + "/config",// "https://ebcce4e5-2018-47d2-a4bc-7d2c8a3bf47f.mock.pstmn.io/config", dataType: "json", success: function (data) { config = data.config.pricePerCredits; initSlider(); }, }); function filterConfig(creditVolume, config) { return config.filter((item) => { if (creditVolume === 0 && item.from === 0) return item; if (item.from < creditVolume && creditVolume <= item.to) return item; if (item.from < creditVolume && !item.to) return item; return false; }); } function configSorting(a, b) { if (a.from > b.from) return 1; if (a.from === b.from) return 0; return -1; } function calculatePrice(creditVolume, config) { var price = 0; var sortedConfig = config.sort(configSorting); sortedConfig.some((item, i, configArrey) => { if (creditVolume <= 1000) { return true; } if (creditVolume <= item.to) { price = configArrey[i - 1].cumulative + ((creditVolume - item.from) / 1000) * item.value; price = Math.ceil(price * 100) / 100; return true; } if (i === config.length - 1 && creditVolume > item.to) { price = configArrey[i - 1].cumulative + ((creditVolume - item.from) / 1000) * item.value; price = Math.ceil(price * 100) / 100; return true; } return false; }); return price; } function roundCreditVolume(credits) { var roundedCreditVolume = Math.ceil(credits / 1000) * 1000; roundedCreditVolume <= 0 && (roundedCreditVolume = 1000); roundedCreditVolume > 2000000 && (roundedCreditVolume = 2000000); return roundedCreditVolume; // onChangeCreditVolume(roundedCreditVolume); // setCredits(roundedCreditVolume); } function commaSeparateNumber(val) { while (/(\d+)(\d{3})/.test(val.toString())) { val = val.toString().replace(/(\d+)(\d{3})/, "$1" + "," + "$2"); } return val; } function setSubscriptionPrice(value) { var subscriptionPrice = calculatePrice(value, config); if (subscriptionPrice > 0) { var price = commaSeparateNumber(subscriptionPrice.toFixed(2)); $("#js-subscription-price").text("$ " + price + " / month"); } else { $("#js-subscription-price").text("FREE"); } } function setOverage(value) { var overage = filterConfig(value, config); $("#js-overage-fee").text("$ " + overage[0].overage + " / thousand emails"); } function setOrderValue(value) { setSubscriptionPrice(value); setOverage(value); } function changeInputHandler(inputValue) { var roundedValue = roundCreditVolume(inputValue); var price = commaSeparateNumber(roundedValue); setOrderValue(roundedValue); $input.val(price); $progressBar.slider("value", roundedValue); } function isNumberKey(evt) { var charCode = evt.which ? evt.which : event.keyCode; if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } function setInputMask(price) { $input.val(commaSeparateNumber(price)); } function initSlider() { setOrderValue(initialAmount); setInputMask(initialAmount); setInputMask(initialAmount); $progressBar.slider({ range: "max", min: 1000, max: 2000000, value: 1000, step: 1000, slide: function (event, ui) { setInputMask(ui.value); setOrderValue(ui.value); }, }); $input.on("keypress", isNumberKey); $input.on("blur", function () { var inputValue = $(this).val().replace(/,/g, ""); changeInputHandler(inputValue); }); $input.on("keydown", function (event) { if (event.key === "Enter") { var inputValue = $(this).val().replace(/,/g, ""); changeInputHandler(inputValue); } }); } }); /******/ })() ;