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

base-product-view.js ➔ ... ➔ BaseProductView   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 BaseProductView;
5
    var BaseView = require('oroui/js/app/views/base/view');
6
    var ElementsHelper = require('orofrontend/js/app/elements-helper');
7
    var BaseModel = require('oroui/js/app/models/base/model');
8
    var mediator = require('oroui/js/mediator');
9
    var routing = require('routing');
10
    var $ = require('jquery');
11
    var _ = require('underscore');
12
13
    BaseProductView = BaseView.extend(_.extend({}, ElementsHelper, {
14
        optionNames: BaseView.prototype.optionNames.concat(['normalizeQuantityField']),
15
16
        normalizeQuantityField: true,
17
18
        elements: {
19
            productItem: '[data-role="product-item"]',
20
            quantity: ['lineItem', '[data-name="field__quantity"]:first'],
21
            unit: ['lineItem', '[data-name="field__unit"]:first'],
22
            lineItem: '[data-role="line-item-form-container"]',
23
            lineItemFields: ':input[data-name]'
24
        },
25
26
        elementsEvents: {
27
            'quantity input': ['input', 'onQuantityChange']
28
        },
29
30
        modelElements: {
31
            quantity: 'quantity',
32
            unit: 'unit'
33
        },
34
35
        modelAttr: {
36
            id: 0,
37
            quantity: 0,
38
            unit: '',
39
            product_units: {},
40
            line_item_form_enable: true
41
        },
42
43
        modelEvents: {
44
            id: ['change', 'onProductChanged'],
45
            line_item_form_enable: ['change', 'onLineItemFormEnableChanged'],
46
            unit_label: ['change', 'changeUnitLabel'],
47
            unit: ['change', 'onUnitChange']
48
        },
49
50
        originalProductId: null,
51
52
        /**
53
         * @inheritDoc
54
         */
55
        constructor: function BaseProductView() {
56
            BaseProductView.__super__.constructor.apply(this, arguments);
57
        },
58
59
        /**
60
         * @inheritDoc
61
         */
62
        initialize: function(options) {
63
            BaseProductView.__super__.initialize.apply(this, arguments);
64
65
            this.rowId = this.$el.parent().data('row-id');
66
            this.initModel(options);
67
            this.initializeElements(options);
68
            this.setPrecision();
69
70
            this.originalProductId = this.model.get('parentProduct');
71
72
            this.initializeSubviews({
73
                productModel: this.model,
74
                options: {
75
                    productModel: this.model
76
                }
77
            });
78
        },
79
80
        initModel: function(options) {
81
            this.modelAttr = $.extend(true, {}, this.modelAttr, options.modelAttr || {});
82
            if (!this.model) {
83
                this.model = _.isObject(this.collection) && this.collection.get(this.rowId)
84
                    ? this.collection.get(this.rowId) : new BaseModel();
85
            }
86
87
            _.each(this.modelAttr, function(value, attribute) {
88
                if (!this.model.has(attribute)) {
89
                    this.model.set(attribute, value);
90
                }
91
            }, this);
92
        },
93
94
        onProductChanged: function() {
95
            var modelProductId = this.model.get('id');
96
            this.model.set('line_item_form_enable', Boolean(modelProductId));
97
98
            var productId = modelProductId || this.originalProductId;
99
            mediator.trigger('layout-subtree:update:product', {
100
                layoutSubtreeUrl: routing.generate('oro_product_frontend_product_view', {
101
                    id: productId,
102
                    parentProductId: this.model.get('parentProduct'),
103
                    ignoreProductVariant: true
104
                }),
105
                layoutSubtreeCallback: _.bind(this.afterProductChanged, this)
106
            });
107
        },
108
109
        onQuantityChange: function(e) {
110
            this.setModelValueFromElement(e, 'quantity', 'quantity');
111
        },
112
113
        onUnitChange: function() {
114
            this.setPrecision();
115
        },
116
117
        setPrecision: function() {
118
            var precision = this.model.get('product_units')[this.model.get('unit')];
119
            this.getElement('quantity')
120
                .data('precision', precision)
121
                .inputWidget('refresh');
122
        },
123
124
        changeUnitLabel: function() {
125
            var $unit = this.getElement('unit');
126
            var unitLabel = this.model.get('unit_label');
127
128
            $unit.find('option').each(function() {
129
                var $option = $(this);
130
                if (!$option.data('originalText')) {
131
                    $option.data('originalText', this.text);
132
                }
133
134
                if (unitLabel && this.selected) {
135
                    this.text = unitLabel;
136
                } else {
137
                    this.text = $option.data('originalText');
138
                }
139
            });
140
            $unit.inputWidget('refresh');
141
        },
142
143
        afterProductChanged: function() {
144
            this.undelegateElementsEvents();
145
            this.clearElementsCache();
146
            this.setModelValueFromElements();
147
            this.delegateElementsEvents();
148
149
            this.onLineItemFormEnableChanged();
150
        },
151
152
        onLineItemFormEnableChanged: function() {
153
            if (this.model.get('line_item_form_enable')) {
154
                this.enableLineItemForm();
155
            } else {
156
                this.disableLineItemForm();
157
            }
158
        },
159
160
        enableLineItemForm: function() {
161
            this.getLineItemFields().prop('disabled', false).inputWidget('refresh');
162
            this.getLineItem().removeClass('disabled');
163
        },
164
165
        disableLineItemForm: function() {
166
            this.getLineItemFields().prop('disabled', true).inputWidget('refresh');
167
            this.getLineItem().addClass('disabled');
168
        },
169
170
        getLineItem: function() {
171
            var $innerLineItem = this.getElement('productItem').find(this.elements.lineItem);
172
            return this.getElement('lineItem').not($innerLineItem);
173
        },
174
175
        getLineItemFields: function() {
176
            return this.getLineItem().find(this.elements.lineItem);
177
        },
178
179
        dispose: function() {
180
            delete this.modelAttr;
181
            delete this.rowId;
182
            this.disposeElements();
183
            BaseProductView.__super__.dispose.apply(this, arguments);
184
        }
185
    }));
186
187
    return BaseProductView;
188
});
189