1/** 2 * JBZoo Application 3 * 4 * This file is part of the JBZoo CCK package. 5 * For the full copyright and license information, please view the LICENSE 6 * file that was distributed with this source code. 7 * 8 * @package Application 9 * @license GPL-2.0 10 * @copyright Copyright (C) JBZoo.com, All rights reserved. 11 * @link https://github.com/JBZoo/JBZoo 12 */ 13 14; 15(function ($, window, document, undefined) { 16 17 /** 18 * Currency toggle (widget with flags) 19 */ 20 JBZoo.widget('JBZoo.Money', 21 { 22 duration : 400, 23 easing : 'swing', 24 onAfterUpdate : $.noop, 25 onBeforeUpdate: function (value) { 26 return value; 27 } 28 }, 29 { 30 // default data 31 '_defaultRound': 9, 32 '_defaultCur' : 'default_cur', 33 34 // currenct money data 35 'currency': 'eur', 36 'value' : .0, 37 'showplus': 0, 38 39 init: function ($this) { 40 $this.currency = this._cleanCur(this.data('currency')); 41 $this.value = JBZoo.toFloat(this.data('value')); 42 $this.showplus = this.data('showplus'); 43 }, 44 45 /** 46 * Convert money to another format 47 * @param currency 48 * @param isAnimate 49 */ 50 convert: function (currency, isAnimate) { 51 var $this = this, 52 isAnimate = $this._def(isAnimate, true), 53 currency = $this._cleanCur(currency), 54 from = $this._getCurInfo($this.currency), 55 to = $this._getCurInfo(currency); 56 57 if (currency == '%' || $this.currency == '%') { 58 return; 59 } 60 61 !from && $this.error('Currency from "' + $this.currency + '" is undefined'); 62 !to && $this.error('Currency to "' + currency + '" is undefined'); 63 64 var newValue = ($this.value / from.value) * to.value; 65 if (isAnimate) { 66 $this.setValue(newValue, currency); 67 } else { 68 $this._update(newValue, currency); 69 } 70 $this.currency = currency; 71 $this.value = newValue; 72 }, 73 74 /** 75 * Get current value 76 */ 77 getValue: function () { 78 return [this.value, this.currency] 79 }, 80 81 /** 82 * Set new value 83 * @param value 84 * @param currency 85 */ 86 setValue: function (value, currency) { 87 88 var $this = this, 89 value = JBZoo.toFloat(value), 90 currency = $this._cleanCur((currency || $this.currency)); 91 92 $this.currency = currency; 93 94 if (currency == $this.currency) { 95 96 if ($this.value != value) { 97 98 $({value: $this.value}) 99 .stop() 100 .animate({value: value}, { 101 duration: $this.options.duration, 102 easing : $this.options.easing, 103 step : function () { 104 $this._update(this.value, $this.currency); 105 }, 106 complete: function () { 107 $this._update(value, $this.currency); 108 $this.value = value; 109 } 110 }); 111 } else { 112 $this._update(value, $this.currency); 113 $this.value = value; 114 } 115 } 116 117 }, 118 119 /** 120 * Update view 121 * @param value 122 * @param currency 123 * @private 124 */ 125 _update: function (value, currency) { 126 127 var $this = this, 128 format = $this._getCurInfo(currency).format, 129 isPositive = (value >= 0); 130 131 value = $this._round(currency, value); 132 133 if ($.isFunction($this.options.onBeforeUpdate)) { 134 value = $this.options.onBeforeUpdate.apply($this, [value]); 135 } 136 137 var formated = JBZoo.numberFormat(Math.abs(value), format.num_decimals, format.decimal_sep, format.thous
137ands_sep), 138 template = isPositive ? format.format_positive : format.format_negative; 139 140 formated = template 141 .replace('%v', '<span class="jbcurrency-value">' + formated + '</span>') 142 .replace('%s', '<span class="jbcurrency-symbol">' + format.symbol + '</span>'); 143 144 if ($this.showplus) { 145 formated = '+' + formated; 146 } 147 148 //$this.currency = currency; 149 if ($this.el.is('input')) { 150 $this.el.val(JBZoo.stripTags(formated)); 151 } else { 152 $this.el.html(formated); 153 } 154 155 if ($.isFunction($this.options.onAfterUpdate)) { 156 $this.options.onAfterUpdate.apply($this, [value]); 157 } 158 }, 159 160 /** 161 * @param currency 162 * @param value 163 * @returns {string} 164 * @private 165 */ 166 _round: function (currency, value) { 167 168 // TODO smart rounding 169 var $this = this, 170 format = $this._getCurInfo(currency).format, 171 roundType = format.round_type, 172 roundValue = format.round_value; 173 174 if (roundType == 'ceil') { 175 var base = Math.pow(10, roundValue); 176 value = Math.ceil(value * base) / base; 177 178 } else if (roundType == 'classic') { 179 value = $this.jbzoo.round(value, roundValue); 180 181 } else if (roundType == 'floor') { 182 var base = Math.pow(10, roundValue); 183 value = Math.floor(value * base) / base; 184 185 } else { 186 value = $this.jbzoo.round(value, $this._defaultRound); 187 } 188 189 return value; 190 }, 191 192 /** 193 * @param currecny 194 * @returns {string} 195 * @private 196 */ 197 _cleanCur: function (currency) { 198 199 var $this = this, 200 rates = JBZoo.getVar('currencyList'); 201 202 currency = $.trim(currency).toLowerCase(); 203 204 if (currency == '%') { 205 return currency; 206 } 207 208 if (currency == this._defaultCur) { 209 return $.trim($this.data('currency')).toLowerCase(); 210 } 211 212 if (!rates[currency]) { 213 $this.error('Undefined currency - ' + currency); 214 } 215 216 return currency; 217 }, 218 219 /** 220 * @param currency 221 * @returns {*} 222 * @private 223 */ 224 _getCurInfo: function (currency) { 225 var $this = this, 226 rates = JBZoo.getVar('currencyList'); 227 228 return rates[$this._cleanCur(currency)]; 229 }, 230 231 'change {element}': function (e, $this) { 232 $this.setInputValue($(this).val()); 233 }, 234 235 'keypress {element}': function (e, $this) { 236 if ($this._key(e, 'enter')) { 237 $this.setInputValue($(this).val()); 238 return false; 239 } 240 }, 241 242 setInputValue: function (newValue) { 243 newValue = JBZoo.toFloat(newValue); 244 this._update(newValue, this.currency); 245 } 246 } 247 ); 248 249})(jQuery, window, document);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.