|
1
|
|
|
define(function(require) { |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
var CurrencySwitcherComponent; |
|
5
|
|
|
var BaseComponent = require('oroui/js/app/components/base/component'); |
|
6
|
|
|
var mediator = require('oroui/js/mediator'); |
|
7
|
|
|
var _ = require('underscore'); |
|
8
|
|
|
var $ = require('jquery'); |
|
9
|
|
|
var routing = require('routing'); |
|
10
|
|
|
|
|
11
|
|
|
CurrencySwitcherComponent = BaseComponent.extend({ |
|
12
|
|
|
/** |
|
13
|
|
|
* @property {Object} |
|
14
|
|
|
*/ |
|
15
|
|
|
options: { |
|
16
|
|
|
currencySwitcherRoute: 'oro_pricing_frontend_set_current_currency', |
|
17
|
|
|
currencyElement: '[data-currency]', |
|
18
|
|
|
selectedCurrency: null |
|
19
|
|
|
}, |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritDoc |
|
23
|
|
|
*/ |
|
24
|
|
|
constructor: function CurrencySwitcherComponent() { |
|
25
|
|
|
CurrencySwitcherComponent.__super__.constructor.apply(this, arguments); |
|
26
|
|
|
}, |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritDoc |
|
30
|
|
|
*/ |
|
31
|
|
|
initialize: function(options) { |
|
32
|
|
|
this.options = _.defaults(options || {}, this.options); |
|
33
|
|
|
|
|
34
|
|
|
this.options._sourceElement |
|
35
|
|
|
.on('click', this.options.currencyElement, _.bind(this.onCurrencyChange, this)); |
|
36
|
|
|
}, |
|
37
|
|
|
|
|
38
|
|
|
onCurrencyChange: function(e) { |
|
39
|
|
|
e.preventDefault(); |
|
40
|
|
|
var $el = $(e.target); |
|
41
|
|
|
|
|
42
|
|
|
var currency = $el.data('currency'); |
|
43
|
|
|
if (currency !== this.options.selectedCurrency) { |
|
44
|
|
|
mediator.execute('showLoading'); |
|
45
|
|
|
$.post( |
|
46
|
|
|
routing.generate(this.options.currencySwitcherRoute), |
|
47
|
|
|
{currency: currency}, |
|
48
|
|
|
function() { |
|
49
|
|
|
mediator.execute('refreshPage'); |
|
50
|
|
|
} |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
}, |
|
54
|
|
|
|
|
55
|
|
|
dispose: function() { |
|
56
|
|
|
if (this.disposed) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
this.options._sourceElement.off(); |
|
61
|
|
|
|
|
62
|
|
|
CurrencySwitcherComponent.__super__.dispose.call(this); |
|
63
|
|
|
} |
|
64
|
|
|
}); |
|
65
|
|
|
|
|
66
|
|
|
return CurrencySwitcherComponent; |
|
67
|
|
|
}); |
|
68
|
|
|
|