1 | View Code Duplication | define(function(require) { |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
2 | 'use strict'; |
||
3 | |||
4 | var OrderLineItemAppliedDiscountsComponent; |
||
5 | var _ = require('underscore'); |
||
6 | var mediator = require('oroui/js/mediator'); |
||
7 | var BaseComponent = require('oroui/js/app/components/base/component'); |
||
8 | var LoadingMaskView = require('oroui/js/app/views/loading-mask-view'); |
||
9 | var NumberFormatter = require('orolocale/js/formatter/number'); |
||
10 | |||
11 | /** |
||
12 | * @export orotax/js/app/components/order-line-item-discounts-no-tax-component |
||
13 | * @extends oroui.app.components.base.Component |
||
14 | * @class orotax.app.components.OrderLineItemAppliedDiscountsComponent |
||
15 | */ |
||
16 | OrderLineItemAppliedDiscountsComponent = BaseComponent.extend({ |
||
17 | /** |
||
18 | * @property {Object} |
||
19 | */ |
||
20 | options: { |
||
21 | selectors: { |
||
22 | rowTotalAfterDiscount: '.applied-discount-row-total-after-discount', |
||
23 | appliedDiscountsAmount: '.applied-discount-row-total-discount-amount' |
||
24 | } |
||
25 | }, |
||
26 | |||
27 | /** |
||
28 | * @property {LoadingMaskView} |
||
29 | */ |
||
30 | loadingMaskView: null, |
||
31 | |||
32 | /** |
||
33 | * @inheritDoc |
||
34 | */ |
||
35 | constructor: function OrderLineItemAppliedDiscountsComponent() { |
||
36 | OrderLineItemAppliedDiscountsComponent.__super__.constructor.apply(this, arguments); |
||
37 | }, |
||
38 | |||
39 | /** |
||
40 | * @inheritDoc |
||
41 | */ |
||
42 | initialize: function(options) { |
||
43 | this.options = _.defaults(options || {}, this.options); |
||
44 | |||
45 | mediator.on('entry-point:order:load:before', this.showLoadingMask, this); |
||
46 | mediator.on('entry-point:order:load', this.setDiscounts, this); |
||
47 | mediator.on('entry-point:order:load:after', this.hideLoadingMask, this); |
||
48 | |||
49 | this.loadingMaskView = new LoadingMaskView({container: this.options._sourceElement}); |
||
50 | }, |
||
51 | |||
52 | showLoadingMask: function() { |
||
53 | this.loadingMaskView.show(); |
||
54 | }, |
||
55 | |||
56 | hideLoadingMask: function() { |
||
57 | this.loadingMaskView.hide(); |
||
58 | }, |
||
59 | |||
60 | /** |
||
61 | * @param {Object} response |
||
62 | */ |
||
63 | setDiscounts: function(response) { |
||
64 | var itemId = this.options._sourceElement.closest('tr.order-line-item').index(); |
||
65 | if (!_.has(response.appliedDiscounts, itemId)) { |
||
66 | return; |
||
67 | } |
||
68 | var discounts = response.appliedDiscounts[itemId]; |
||
69 | var appliedDiscountsAmount = NumberFormatter.formatCurrency( |
||
70 | discounts.appliedDiscountsAmount, |
||
71 | discounts.currency |
||
72 | ); |
||
73 | var rowTotalAfterDiscount = NumberFormatter.formatCurrency( |
||
74 | discounts.rowTotalAfterDiscount, |
||
75 | discounts.currency |
||
76 | ); |
||
77 | this.options._sourceElement |
||
78 | .find(this.options.selectors.appliedDiscountsAmount) |
||
79 | .text(appliedDiscountsAmount); |
||
80 | this.options._sourceElement |
||
81 | .find(this.options.selectors.rowTotalAfterDiscount) |
||
82 | .text(rowTotalAfterDiscount); |
||
83 | }, |
||
84 | |||
85 | /** |
||
86 | * @inheritDoc |
||
87 | */ |
||
88 | dispose: function() { |
||
89 | if (this.disposed) { |
||
90 | return; |
||
91 | } |
||
92 | |||
93 | mediator.off('entry-point:order:load:before', this.showLoadingMask, this); |
||
94 | mediator.off('entry-point:order:load', this.setDiscounts, this); |
||
95 | mediator.off('entry-point:order:load:after', this.hideLoadingMask, this); |
||
96 | |||
97 | OrderLineItemAppliedDiscountsComponent.__super__.dispose.call(this); |
||
98 | } |
||
99 | }); |
||
100 | |||
101 | return OrderLineItemAppliedDiscountsComponent; |
||
102 | }); |
||
103 |