Added option to reset the currency conversion

This commit is contained in:
Felipe Martín 2015-09-09 23:06:37 +02:00
parent e91759adc5
commit 78b642fe5d
2 changed files with 19 additions and 4 deletions

View File

@ -1,7 +1,7 @@
(function() {
var MoneyComponent = function() {
this.DEBUG = DEBUG;
this.currency = 'EUR';
this.currency = 'reset';
this.currencies = CURRENCIES;
this.ratesSet = false;
this.initialize();
@ -59,16 +59,30 @@
MoneyComponent.prototype.setCurrency = function(currency) {
var self = this;
if (this.currencies.indexOf(currency) !== -1) {
if (this.currencies.indexOf(currency) !== -1 || currency === 'reset') {
if (this.DEBUG) console.log('[money] set user currency: ' + currency)
$('[data-currency-change]').removeClass('active');
$('[data-currency-change="' + currency + '"]').addClass('active');
window.localStorage.setItem('currency', currency);
this.currency = currency;
this.convertPrices();
if (currency === 'reset') {
window.localStorage.removeItem('currency');
this.resetConversion();
} else {
window.localStorage.setItem('currency', currency);
this.convertPrices();
}
}
};
MoneyComponent.prototype.resetConversion = function() {
var self = this;
[].forEach.call(document.querySelectorAll('[data-money]'), function(item) {
var price = item.getAttribute('data-price');
var currency = item.getAttribute('data-currency');
item.innerHTML = price + ' ' + currency;
});
}
MoneyComponent.prototype.convertPrices = function() {
var self = this;
if (!this.ratesSet) {

View File

@ -128,6 +128,7 @@
{% for currency in CURRENCIES %}
<a class="item" href="#" data-currency-change="{{ currency }}">{{ currency }}</a>
{% endfor %}
<a class="item" href="#" data-currency-change="reset">{% trans 'No convert' %}</a>
</div>
</div>
<div class="four wide column left aligned">