Test Setup Failed
Push — master ( dce20e...632f0c )
by
unknown
03:35
created

  A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
nop 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
        initialize: function(options) {
53
            BaseProductView.__super__.initialize.apply(this, arguments);
54
55
            this.rowId = this.$el.parent().data('row-id');
56
            this.initModel(options);
57
            this.initializeElements(options);
58
            this.setPrecision();
59
60
            this.originalProductId = this.model.get('parentProduct');
61
62
            this.initializeSubviews({
63
                productModel: this.model,
64
                options: {
65
                    productModel: this.model
66
                }
67
            });
68
        },
69
70
        initModel: function(options) {
71
            this.modelAttr = $.extend(true, {}, this.modelAttr, options.modelAttr || {});
72
            if (!this.model) {
73
                this.model = (_.isObject(this.collection) && this.collection.get(this.rowId)) ?
74
                    this.collection.get(this.rowId) : new BaseModel();
75
            }
76
77
            _.each(this.modelAttr, function(value, attribute) {
78
                if (!this.model.has(attribute)) {
79
                    this.model.set(attribute, value);
80
                }
81
            }, this);
82
        },
83
84
        onProductChanged: function() {
85
            var modelProductId = this.model.get('id');
86
            this.model.set('line_item_form_enable', Boolean(modelProductId));
87
88
            var productId = modelProductId || this.originalProductId;
89
            mediator.trigger('layout-subtree:update:product', {
90
                layoutSubtreeUrl: routing.generate('oro_product_frontend_product_view', {
91
                    id: productId,
92
                    parentProductId: this.model.get('parentProduct'),
93
                    ignoreProductVariant: true
94
                }),
95
                layoutSubtreeCallback: _.bind(this.afterProductChanged, this)
96
            });
97
        },
98
99
        onQuantityChange: function(e) {
100
            this.setModelValueFromElement(e, 'quantity', 'quantity');
101
        },
102
103
        onUnitChange: function() {
104
            this.setPrecision();
105
        },
106
107
        setPrecision: function() {
108
            var precision = this.model.get('product_units')[this.model.get('unit')];
109
            this.getElement('quantity')
110
                .data('precision', precision)
111
                .inputWidget('refresh');
112
        },
113
114
        changeUnitLabel: function() {
115
            var $unit = this.getElement('unit');
116
            var unitLabel = this.model.get('unit_label');
117
118
            $unit.find('option').each(function() {
119
                var $option = $(this);
120
                if (!$option.data('originalText')) {
121
                    $option.data('originalText', this.text);
122
                }
123
124
                if (unitLabel && this.selected) {
125
                    this.text = unitLabel;
126
                } else {
127
                    this.text = $option.data('originalText');
128
                }
129
            });
130
            $unit.inputWidget('refresh');
131
        },
132
133
        afterProductChanged: function() {
134
            this.undelegateElementsEvents();
135
            this.clearElementsCache();
136
            this.setModelValueFromElements();
137
            this.delegateElementsEvents();
138
139
            this.onLineItemFormEnableChanged();
140
        },
141
142
        onLineItemFormEnableChanged: function() {
143
            if (this.model.get('line_item_form_enable')) {
144
                this.enableLineItemForm();
145
            } else {
146
                this.disableLineItemForm();
147
            }
148
        },
149
150
        enableLineItemForm: function() {
151
            this.getLineItemFields().prop('disabled', false).inputWidget('refresh');
152
            this.getLineItem().removeClass('disabled');
153
        },
154
155
        disableLineItemForm: function() {
156
            this.getLineItemFields().prop('disabled', true).inputWidget('refresh');
157
            this.getLineItem().addClass('disabled');
158
        },
159
160
        getLineItem: function() {
161
            var $innerLineItem = this.getElement('productItem').find(this.elements.lineItem);
162
            return this.getElement('lineItem').not($innerLineItem);
163
        },
164
165
        getLineItemFields: function() {
166
            return this.getLineItem().find(this.elements.lineItem);
167
        },
168
169
        dispose: function() {
170
            delete this.modelAttr;
171
            delete this.rowId;
172
            this.disposeElements();
173
            BaseProductView.__super__.dispose.apply(this, arguments);
174
        }
175
    }));
176
177
    return BaseProductView;
178
});
179