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

OrderLineItemTaxesComponent   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 OrderLineItemTaxesComponent;
5
    var $ = require('jquery');
6
    var _ = require('underscore');
7
    var mediator = require('oroui/js/mediator');
8
    var BaseComponent = require('oroui/js/app/components/base/component');
9
    var LoadingMaskView = require('oroui/js/app/views/loading-mask-view');
10
    var TaxFormatter = require('orotax/js/formatter/tax');
11
12
    /**
13
     * @export orotax/js/app/components/order-line-item-taxes-component
14
     * @extends oroui.app.components.base.Component
15
     * @class orotax.app.components.OrderLineItemTaxesComponent
16
     */
17
    OrderLineItemTaxesComponent = BaseComponent.extend({
18
        /**
19
         * @property {Object}
20
         */
21
        options: {
22
            selectors: {
23
                appliedTaxesTemplate: '#line-item-taxes-template',
24
                tableContainer: '[data-table-container]',
25
                lineItemDataAttr: 'data-tax-item',
26
                lineItemDataAttrSelector: '[data-tax-item]'
27
            },
28
            result: null
29
        },
30
31
        /**
32
         * @property {Object}
33
         */
34
        appliedTaxesTemplate: null,
35
36
        /**
37
         * @property {Object}
38
         */
39
        emptyData: {
40
            unit: {},
41
            row: {},
42
            taxes: {}
43
        },
44
45
        /**
46
         * @property {LoadingMaskView}
47
         */
48
        loadingMaskView: null,
49
50
        /**
51
         * @property {jQuery.Element}
52
         */
53
        $tableContainer: null,
54
55
        /**
56
         * @inheritDoc
57
         */
58
        constructor: function OrderLineItemTaxesComponent() {
59
            OrderLineItemTaxesComponent.__super__.constructor.apply(this, arguments);
60
        },
61
62
        /**
63
         * @inheritDoc
64
         */
65
        initialize: function(options) {
66
            this.options = _.defaults(options || {}, this.options);
67
            this.options._sourceElement
68
                .attr(
69
                    this.options.selectors.lineItemDataAttr,
70
                    $(this.options.selectors.lineItemDataAttrSelector).length
71
                );
72
73
            mediator.on('entry-point:order:load:before', this.initializeAttribute, this);
74
            mediator.on('entry-point:order:load:before', this.showLoadingMask, this);
75
            mediator.on('entry-point:order:load', this.setOrderTaxes, this);
76
            mediator.on('entry-point:order:load:after', this.hideLoadingMask, this);
77
78
            this.appliedTaxesTemplate = _.template($(this.options.selectors.appliedTaxesTemplate).html());
79
80
            this.$tableContainer = this.options._sourceElement.find(this.options.selectors.tableContainer);
81
82
            this.loadingMaskView = new LoadingMaskView({container: this.options._sourceElement});
83
84
            this.render(this.options.result);
85
        },
86
87
        initializeAttribute: function() {
88
            var self = this;
89
            $(this.options.selectors.lineItemDataAttrSelector).each(function(index) {
90
                $(this).attr(self.options.selectors.lineItemDataAttr, index);
91
            });
92
        },
93
94
        showLoadingMask: function() {
95
            this.loadingMaskView.show();
96
        },
97
98
        hideLoadingMask: function() {
99
            this.loadingMaskView.hide();
100
        },
101
102
        render: function(result) {
103
            result = _.defaults(result, this.emptyData);
104
            result.row = TaxFormatter.formatItem(result.row);
105
            result.unit = TaxFormatter.formatItem(result.unit);
106
            result.taxes = _.map(result.taxes, TaxFormatter.formatTax);
107
108
            this.$tableContainer.html(this.appliedTaxesTemplate(result));
109
        },
110
111
        /**
112
         * @param {Object} response
113
         */
114
        setOrderTaxes: function(response) {
115
            var result = _.defaults(response, {taxItems: {}});
116
            var itemId = this.options._sourceElement.attr(this.options.selectors.lineItemDataAttr);
117
118
            if (!_.has(result.taxItems, itemId)) {
119
                return;
120
            }
121
122
            var itemData = _.defaults(response.taxItems[itemId], this.emptyData);
123
124
            this.render(itemData);
125
        },
126
127
        /**
128
         * @inheritDoc
129
         */
130
        dispose: function() {
131
            if (this.disposed) {
132
                return;
133
            }
134
135
            mediator.off('entry-point:order:load:before', this.showLoadingMask, this);
136
            mediator.off('entry-point:order:load:before', this.initializeAttribute, this);
137
            mediator.off('entry-point:order:load', this.setOrderTaxes, this);
138
            mediator.off('entry-point:order:load:after', this.hideLoadingMask, this);
139
140
            OrderLineItemTaxesComponent.__super__.dispose.call(this);
141
        }
142
    });
143
144
    return OrderLineItemTaxesComponent;
145
});
146