Advertisement
animdenis

/bitrix/js/currency/core_currency.js

Jun 30th, 2016
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(window) {
  2.  
  3. if (BX.Currency)
  4. {
  5.     return;
  6. }
  7.  
  8. BX.Currency = {
  9.     currencyList: [],
  10.     defaultFormat: {
  11.         'FORMAT_STRING': '#',
  12.         'DEC_POINT': '.',
  13.         'THOUSANDS_SEP': ' ',
  14.         'DECIMALS': 2,
  15.         'HIDE_ZERO': 'N'
  16.     },
  17.  
  18.     setCurrencyFormat: function(currency, format, replace)
  19.     {
  20.         var index = this.getCurrencyIndex(currency),
  21.             currencyFormat = BX.clone(this.defaultFormat, true),
  22.             i;
  23.  
  24.         replace = !!replace;
  25.         if (index > -1 && !replace)
  26.         {
  27.             return;
  28.         }
  29.         if (index === -1)
  30.         {
  31.             index = this.currencyList.length;
  32.         }
  33.  
  34.         for (i in currencyFormat)
  35.         {
  36.             if (currencyFormat.hasOwnProperty(i) && typeof format[i] !== 'undefined')
  37.             {
  38.                 currencyFormat[i] = format[i];
  39.             }
  40.         }
  41.         this.currencyList[index] = {
  42.             'currency': currency,
  43.             'format': BX.clone(currencyFormat, true)
  44.         };
  45.     },
  46.  
  47.     setCurrencies: function(currencies, replace)
  48.     {
  49.         var i;
  50.         if (!!currencies && BX.type.isArray(currencies))
  51.         {
  52.             for (i = 0; i < currencies.length; i++)
  53.             {
  54.                 if(currencies[i] === undefined) continue;
  55.                 if (!!currencies[i].CURRENCY && !!currencies[i].FORMAT)
  56.                 {
  57.                     this.setCurrencyFormat(currencies[i].CURRENCY, currencies[i].FORMAT, replace);
  58.                 }
  59.             }
  60.         }
  61.     },
  62.  
  63.     getCurrencyFormat: function(currency)
  64.     {
  65.         var index = this.getCurrencyIndex(currency);
  66.         return (index > -1 ? this.currencyList[index].format : false);
  67.     },
  68.  
  69.     getCurrencyIndex: function(currency)
  70.     {
  71.         var i, index = -1;
  72.         if (this.currencyList.length === 0)
  73.         {
  74.             return index;
  75.         }
  76.         for (i = 0; i < this.currencyList.length; i++)
  77.         {
  78.             if (this.currencyList[i].currency === currency)
  79.             {
  80.                 index = i;
  81.                 break;
  82.             }
  83.         }
  84.         return index;
  85.     },
  86.  
  87.     clearCurrency: function(currency)
  88.     {
  89.         var index = this.getCurrencyIndex(currency);
  90.         if (index > -1)
  91.             this.currencyList = BX.util.deleteFromArray(this.currencyList, index);
  92.     },
  93.  
  94.     clean: function()
  95.     {
  96.         this.currencyList = [];
  97.     },
  98.  
  99.     currencyFormat: function (price, currency, useTemplate)
  100.     {
  101.         var result = '',
  102.             format;
  103.         useTemplate = !!useTemplate;
  104.         format = this.getCurrencyFormat(currency);
  105.         if (!!format && typeof format === 'object')
  106.         {
  107.             format.CURRENT_DECIMALS = format.DECIMALS;
  108.             if (format.HIDE_ZERO === 'Y' && price == parseInt(price, 10))
  109.                 format.CURRENT_DECIMALS = 0;
  110.  
  111.             result = BX.util.number_format(price, format.CURRENT_DECIMALS, format.DEC_POINT, format.THOUSANDS_SEP);
  112.             if (useTemplate)
  113.                 result = format.FORMAT_STRING.replace(/(^|[^&])#/, '$1' + result);
  114.         }
  115.         return result;
  116.     }
  117. };
  118. })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement