1/*! 2 * Curry currency conversion jQuery Plugin v0.8.3 3 * https://bitbucket.org/netyou/curry-currency-ddm 4 * 5 * Copyright 2017, NetYou (http://curry.netyou.co.il) 6 * Dual licensed under the MIT or GPL Version 2 licenses. 7 * http://www.opensource.org/licenses/mit-license.php 8 * http://www.opensource.org/licenses/GPL-2.0 9 */ 10(function($) { 11 12 $.fn.curry = function(options) { 13 14 // Setup a global cache for other curry plugins 15 if (!window.jQCurryPluginCache) 16 window.jQCurryPluginCache = [{}, false]; 17 18 var output = '', 19 rates = {}, 20 t = this, 21 requestedCurrency = window.jQCurryPluginCache[1], 22 $document = $(document), 23 dropDownMenu, value, 24 item, keyName, 25 i, l, rate; 26 27 // Create some defaults, extending them with any options that were provided 28 var settings = $.extend({ 29 target: '.price', 30 change: true, 31 base: 'USD', 32 symbols: {} 33 }, options); 34 35 this.each(function() { 36 37 var $this = $(this), 38 id = $this.attr('id'), 39 classes = $this.attr('class'), 40 attrs = '', 41 tempHolder; 42 43 // Add class or id if replaced element had either of them 44 attrs += id ? ' id="' + id + '"' : ''; 45 46 if (classes) { 47 48 attrs += ' class="curry-ddm'; 49 50 if (classes) 51 attrs += ' ' + classes + '"'; 52 else 53 attrs += '"'; 54 55 } else { 56 57 attrs += ''; 58 59 } 60 61 // keep any classes attached to the original element 62 output = '<select' + attrs + '></select>'; 63 64 // Replace element with generated select 65 tempHolder = $(output).insertAfter($this); 66 $this.detach(); 67 68 // Add new drop down to jquery list (jquery object) 69 dropDownMenu = !dropDownMenu ? tempHolder : dropDownMenu.add(tempHolder); 70 71 }); 72 73 // Create the html for the drop down menu 74 var generateDDM = function(rates) { 75 76 output = ''; 77 78 // Change all target elements to drop downs 79 dropDownMenu.each(function() { 80 81 for (i in rates) { 82 83 rate = rates[i]; 84 85 output += '<option value="' + i + '" data-rate="' + rate + '">' + i + '</option>'; 86 87 } 88 89 $(output).appendTo(this); 90 91 }); 92 93 }; 94 95 if (!settings.customCurrency) { 96 97 // Only get currency hash once 98 if (!requestedCurrency) { 99 100 // Request currencies from yahoo finance 101 var jqxhr = $.ajax({ 102 //url: 'https://api.fixer.io/latest', 103 url: 'http://data.fixer.io/api/latest?access_key=bba4f5cd18037c36bb5c174f6b70a435&symbols=USD,GBP,CAD,AUD,JPY,RUB,INR,ILS&format=1', 104 dataType: 'jsonp', 105 data: { 106 symbols: 'INR,EUR,CAD,GBP,ILS', 107 base: settings.base 108 } 109 }); 110 111 // Set global flag so we know we made a request 112 window.jQCurryPluginCache[1] = true; 113 114 jqxhr 115 .done(function(data) { 116 117 var initrates = data.rates; 118 119 // Add the base currency to the rates 120 rates[settings.base] = 1; 121 122 for ( var currency in initrates ) { 123 124 value = initrates[currency]; 125 126 rates[currency] = value; 127 128 } 129 130 generateDDM(rates); 131 132 window.jQCurryPluginCache[0] = rates; 133 $document.trigger('jQCurryPlugin.gotRates'); 134 135 }) 136 .fail(function(err) { 137 138 console.log(err); 139 140 }); 141 142 } else { 143 144 $document.on('jQCurryPlugin.gotRates', function() { 145 146 generateDDM(window.jQCurryPluginCache[0]); 147 148 }); 149 150 } 151 152 } else { 153 154 generateDDM(settings.customCurrency); 155 156 } 157 158 // only change target when change is set by user 159 if (settings.change) { 160 161 // Add default currency symbols 162 var symbols = $.extend({ 163 'USD': '$', 164 'GBP': '£', 165 'EUR': '€', 166 'JPY': '¥' 167 }, settings.symbols), 168 $priceTag, symbol; 169 170 $document.on('change', this.selector, function() { 171 172 var $target = $(settings.target), 173 $option = $(this).find(':selected'), 174 rate = $option.data('rate'), 175 has_comma = false, 176 money, result, l = $target.length; 177 178 for (var i = 0; i < l; i++) { 179 180 $price = $($target[i]); 181 money = $price.text(); 182 183 // Check if field has comma instead of decimal and replace with decimal 184 if ( money.indexOf(',') !== -1 ){ 185 has_comma = true; 186 money = money.replace( ',' , '.' ); 187 } 188 189 // Remove anything but the numbers and decimals and convert string to Number 190 money = Number(money.replace(/[^0-9\.]+/g, '')); 191 192 if ($price.data('base-figure')) { 193 194 // If the client changed the currency there should be a base stored on the element 195 result = rate * $price.data('base-figure'); 196 197 } else { 198 199 // Store the base price on the element
200 $price.data('base-figure', money); 201 result = rate * money; 202 203 } 204 205 // Parse as two decimal number with . 206 result = Number(result.toString().match(/^\d+(?:\.\d{0,2})?/)); 207 208 // Replace decimal with comma after calculations 209 if ( has_comma ){ 210 result = result.toString().replace( '.' , ',' ); 211 has_comma = false; 212 } 213 214 symbol = symbols[$option.val()] || $option.val(); 215 216 $price.html('<span class="symbol">' + symbol + '</span>' + result); 217 218 } 219 220 }); 221 222 } 223 224 // Returns jQuery object for chaining 225 return dropDownMenu; 226 227 }; 228 229})(jQuery);
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.