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

  A

Complexity

Conditions 1
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
define(function(require) {
2
    'use strict';
3
4
    var $ = require('jquery');
5
    var _ = require('underscore');
6
    var BaseView = require('oroui/js/app/views/base/view');
7
    var LoadingMaskView = require('oroui/js/app/views/loading-mask-view');
8
    var mediator = require('oroui/js/mediator');
9
    var AppliedPromotionCollectionView;
10
11
    AppliedPromotionCollectionView = BaseView.extend({
12
        /**
13
         * @property {Object}
14
         */
15
        options: {
16
            sourcePromotionIdDataAttribute: 'source-promotion-id',
17
            sourceCouponIdDataAttribute: 'source-coupon-id',
18
            delimiter: ',',
19
            selectors: {
20
                appliedPromotionElement: '[data-role="applied-promotion-element"]',
21
                appliedPromotionTableRow: '[data-role="applied-discount-table-row"]',
22
                appliedPromotionActiveField: '[data-role="applied-promotion-active"]',
23
                changeActiveButton: '[data-role="applied-promotion-change-active-button"]',
24
                removeButton: '[data-role="applied-promotion-remove-button"]'
25
            }
26
        },
27
28
        /**
29
         * @inheritDoc
30
         */
31
        constructor: function AppliedPromotionCollectionView(options) {
32
            this.options = $.extend(true, {}, this.options, options || {});
33
            this._checkOptions();
34
            AppliedPromotionCollectionView.__super__.constructor.call(this, options);
35
        },
36
37
        /**
38
         * @inheritDoc
39
         */
40
        events: function() {
41
            var events = {};
42
            events['click ' + this.options.selectors.changeActiveButton] = 'changeActiveStatus';
43
            events['click ' + this.options.selectors.removeButton] = 'removeAppliedPromotion';
44
45
            return events;
46
        },
47
48
        /**
49
         * @inheritDoc
50
         */
51
        initialize: function() {
52
            var handlers = {};
53
            handlers['entry-point:order:load:before'] = this.showLoadingMask;
54
            handlers['entry-point:order:load'] = this.refreshCollectionBlock;
55
            handlers['entry-point:order:load:after'] = this.hideLoadingMask;
56
57
            this.listenTo(mediator, handlers);
58
        },
59
60
        /**
61
         * @param {jQuery.Event} event
62
         */
63
        changeActiveStatus: function(event) {
64
            var self = this;
65
            var $tableRow = $(event.target).closest(this.options.selectors.appliedPromotionTableRow);
66
            var sourcePromotionId = $tableRow.data(this.options.sourcePromotionIdDataAttribute);
67
68
            this.$(this.options.selectors.appliedPromotionElement).each(function() {
69
                if ($(this).data(self.options.sourcePromotionIdDataAttribute) === sourcePromotionId) {
70
                    var $activeField = $(this).find(self.options.selectors.appliedPromotionActiveField);
71
                    var activeState = +$activeField.val();
72
                    var newActiveState = !activeState;
73
                    $activeField.val(+newActiveState);
74
                }
75
            });
76
77
            mediator.trigger('entry-point:order:trigger');
78
        },
79
80
        /**
81
         * @param {jQuery.Event} event
82
         */
83
        removeAppliedPromotion: function(event) {
84
            var self = this;
85
            var $tableRow = $(event.target).closest(this.options.selectors.appliedPromotionTableRow);
86
            var sourcePromotionId = $tableRow.data(this.options.sourcePromotionIdDataAttribute);
87
            var sourceCouponId = $tableRow.data(this.options.sourceCouponIdDataAttribute);
88
89
            this.$(this.options.selectors.appliedPromotionElement).each(function() {
90
                if ($(this).data(self.options.sourcePromotionIdDataAttribute) === sourcePromotionId) {
91
                    $(this).remove();
92
                    mediator.trigger('applied-coupon:remove', sourceCouponId);
93
                }
94
            });
95
96
            mediator.trigger('entry-point:order:trigger');
97
        },
98
99
        /**
100
         * @param {Object} response
101
         */
102
        refreshCollectionBlock: function(response) {
103
            if (!_.isUndefined(response.appliedPromotions)) {
104
                var $content = $(response.appliedPromotions);
105
                this.$el.html($content.html());
106
                this.$el.trigger('content:changed');
107
                this._removeLoadingMask();
108
            }
109
        },
110
111
        /**
112
         * @private
113
         */
114
        showLoadingMask: function() {
115
            this._ensureLoadingMaskLoaded();
116
117
            if (!this.subview('loadingMask').isShown()) {
118
                this.subview('loadingMask').show();
119
            }
120
        },
121
122
        /**
123
         * @private
124
         */
125
        hideLoadingMask: function() {
126
            this._ensureLoadingMaskLoaded();
127
128
            if (this.subview('loadingMask').isShown()) {
129
                this.subview('loadingMask').hide();
130
            }
131
        },
132
133
        /**
134
         * @private
135
         */
136
        _ensureLoadingMaskLoaded: function() {
137
            if (!this.subview('loadingMask')) {
138
                this.subview('loadingMask', new LoadingMaskView({container: this.$el}));
139
            }
140
        },
141
142
        /**
143
         * @private
144
         */
145
        _removeLoadingMask: function() {
146
            if (this.subview('loadingMask')) {
147
                this.removeSubview('loadingMask');
148
            }
149
        },
150
151
        /**
152
         * @private
153
         */
154
        _checkOptions: function() {
155
            var requiredSelectors = [];
156
            _.each(this.options.selectors, function(selector, selectorName) {
157
                if (!selector) {
158
                    requiredSelectors.push(selectorName);
159
                }
160
            });
161
            if (requiredSelectors.length) {
162
                throw new TypeError('Missing required selectors(s): ' + requiredSelectors.join(', '));
163
            }
164
        }
165
    });
166
167
    return AppliedPromotionCollectionView;
168
});
169