From cbbe5cfb252941fd03d90aeb67f61ed570ece18f Mon Sep 17 00:00:00 2001 From: Mario Dian Date: Fri, 30 Nov 2018 20:01:47 +0800 Subject: [PATCH] - fix currency format for numbers over 999 - fix cart table --- BTCPayServer/wwwroot/cart/js/cart.js | 35 +++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/BTCPayServer/wwwroot/cart/js/cart.js b/BTCPayServer/wwwroot/cart/js/cart.js index 1586a9c2c..f66602ed9 100644 --- a/BTCPayServer/wwwroot/cart/js/cart.js +++ b/BTCPayServer/wwwroot/cart/js/cart.js @@ -163,17 +163,17 @@ Cart.prototype.listItems = function() { list.push($(tableTemplate)); } - tableTemplate = '
' + customTipText + '
' + + tableTemplate = '
' + customTipText + '
' + '
' + '
' + - '' + currencySymbol + '' + + '' + (currencySymbol != 'null' ? currencySymbol : '') + '' + '
' + - '' + + '' + '
' + '
'; list.push($(tableTemplate)); - tableTemplate = 'Total' + this.formatCurrency(this.getTotal()) + ''; + tableTemplate = 'Total' + this.formatCurrency(this.getTotal()) + ''; list.push($(tableTemplate)); // Add the list to DOM @@ -239,16 +239,34 @@ Cart.prototype.emptyList = function() { $table.html('The cart is empty.'); } -/* Get the currency symbol from an existing amount and use it with the new amount*/ +/* Get the currency symbol from an existing amount and use it with the new amount */ Cart.prototype.formatCurrency = function(amount, example) { - var regex = /([0-9.]+)/gm; + var regex = /[0-9 .,]/gm, + curSign = '', + amt = '', + sep = ''; - // Get the first item's formated price + // Get the first item's format if (typeof example == 'undefined' && typeof srvModel != 'undefined') { example = srvModel.items[0].price.formatted; } - return example.replace(regex, amount.toFixed(2)); + // Have the currency sign on a proper side (e.g. left for usd, right for eur) + curSign = example.replace(regex, ' ').replace(/\s\s+/g, 'n'); + amt = curSign.replace('n', amount.toFixed(2)); + + if (example.indexOf('.') === -1) { + // Separate decimal by comma for EUR and other currencies + amt = amt.replace('.', ','); + sep = ' '; + } else { + sep = ','; + } + + // Add thousands separator + amt = amt.replace(/\B(?=(\d{3})+(?!\d))/g, sep); + + return amt; } Cart.prototype.toCents = function(num) { @@ -279,4 +297,5 @@ Cart.prototype.destroy = function() { this.content = []; this.items = 0; this.totalAmount = 0; + this.tip = 0; } \ No newline at end of file