Test Setup Failed
Push — master ( cd1dab...2a689d )
by
unknown
03:51
created

CurrencySwitcherComponent   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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