1
|
|
|
define(function(require) { |
2
|
|
|
'use strict'; |
3
|
|
|
|
4
|
|
|
var ProductUnitPrecisionLimitationsComponent; |
5
|
|
|
var $ = require('jquery'); |
6
|
|
|
var _ = require('underscore'); |
7
|
|
|
var BaseComponent = require('oroui/js/app/components/base/component'); |
8
|
|
|
var __ = require('orotranslation/js/translator'); |
9
|
|
|
|
10
|
|
|
ProductUnitPrecisionLimitationsComponent = BaseComponent.extend({ |
11
|
|
|
/** |
12
|
|
|
* @property {Object} |
13
|
|
|
*/ |
14
|
|
|
options: { |
15
|
|
|
selectSelector: 'select[name^="oro_product[prices]"][name$="[unit]"]', |
16
|
|
|
unitsAttribute: 'units', |
17
|
|
|
unitRemovedSuffix: __('oro.product.productunit.removed.suffix') |
18
|
|
|
}, |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @property {Object} |
22
|
|
|
*/ |
23
|
|
|
listen: { |
24
|
|
|
'product:precision:remove mediator': 'onChange', |
25
|
|
|
'product:precision:add mediator': 'onChange', |
26
|
|
|
'product:primary:precision:change mediator': 'onChange' |
27
|
|
|
}, |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
32
|
|
|
constructor: function ProductUnitPrecisionLimitationsComponent() { |
33
|
|
|
ProductUnitPrecisionLimitationsComponent.__super__.constructor.apply(this, arguments); |
34
|
|
|
}, |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
initialize: function(options) { |
40
|
|
|
this.options = _.defaults(options || {}, this.options); |
41
|
|
|
|
42
|
|
|
this.options._sourceElement |
43
|
|
|
.on('content:changed', _.bind(this.onChange, this)); |
44
|
|
|
|
45
|
|
|
this.options._sourceElement.trigger('content:changed'); |
46
|
|
|
}, |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Change options in selects |
50
|
|
|
*/ |
51
|
|
|
onChange: function() { |
52
|
|
|
var self = this; |
53
|
|
|
var units = this.getUnits(); |
54
|
|
|
|
55
|
|
|
_.each(this.getSelects(), function(select) { |
56
|
|
|
var $select = $(select); |
57
|
|
|
var clearChangeRequired = self.clearOptions(units, $select); |
58
|
|
|
var addChangeRequired = self.addOptions(units, $select); |
59
|
|
|
if (clearChangeRequired || addChangeRequired) { |
60
|
|
|
$select.trigger('change'); |
61
|
|
|
} |
62
|
|
|
}); |
63
|
|
|
}, |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Clear options from selects |
67
|
|
|
* |
68
|
|
|
* @param {Array} units |
69
|
|
|
* @param {jQuery.Element} $select |
70
|
|
|
* |
71
|
|
|
* @return {Boolean} |
72
|
|
|
*/ |
73
|
|
|
clearOptions: function(units, $select) { |
74
|
|
|
var updateRequired = false; |
75
|
|
|
var self = this; |
76
|
|
|
|
77
|
|
|
_.each($select.find('option'), function(option) { |
78
|
|
|
if (!option.value) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
var $option = $(option); |
83
|
|
|
if (!units.hasOwnProperty(option.value)) { |
84
|
|
|
if (option.selected !== true) { |
85
|
|
|
$option.remove(); |
86
|
|
|
} else if (option.text.indexOf(' - ') < 0) { |
87
|
|
|
$option.text($option.text() + ' - ' + self.options.unitRemovedSuffix); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
updateRequired = true; |
91
|
|
|
} |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
return updateRequired; |
95
|
|
|
}, |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add options based on units configuration |
99
|
|
|
* |
100
|
|
|
* @param {Array} units |
101
|
|
|
* @param {jQuery.Element} $select |
102
|
|
|
* |
103
|
|
|
* @return {Boolean} |
104
|
|
|
*/ |
105
|
|
|
addOptions: function(units, $select) { |
106
|
|
|
var updateRequired = false; |
107
|
|
|
var emptyOption = $select.find('option[value=""]'); |
108
|
|
|
|
109
|
|
|
if (_.isEmpty(units)) { |
110
|
|
|
emptyOption.show(); |
111
|
|
|
} else { |
112
|
|
|
emptyOption.hide(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
_.each($select.find('option:contains( - )'), function(option) { |
116
|
|
|
if (units.hasOwnProperty(option.value)) { |
117
|
|
|
var oldText = option.text; |
118
|
|
|
var newText = oldText.substring(0, oldText.indexOf(' - ')); |
119
|
|
|
$(option).text(newText); |
120
|
|
|
$select.closest('.oro-multiselect-holder').find('.validation-failed').hide(); |
121
|
|
|
updateRequired = true; |
122
|
|
|
} |
123
|
|
|
}); |
124
|
|
|
|
125
|
|
|
_.each(units, function(text, value) { |
126
|
|
|
if (!$select.find('option[value="' + value + '"]').length) { |
127
|
|
|
$select.append('<option value="' + value + '">' + text + '</option>'); |
128
|
|
|
updateRequired = true; |
129
|
|
|
} |
130
|
|
|
}); |
131
|
|
|
|
132
|
|
|
if ($select.val() === '' && !_.isEmpty(units)) { |
133
|
|
|
var value = _.keys(units)[0]; |
134
|
|
|
$select.val(value); |
135
|
|
|
updateRequired = true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return updateRequired; |
139
|
|
|
}, |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return selects to update |
143
|
|
|
* |
144
|
|
|
* @returns {jQuery.Element} |
145
|
|
|
*/ |
146
|
|
|
getSelects: function() { |
147
|
|
|
return this.options._sourceElement.find(this.options.selectSelector); |
148
|
|
|
}, |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return units from data attribute |
152
|
|
|
* |
153
|
|
|
* @returns {Object} |
154
|
|
|
*/ |
155
|
|
|
getUnits: function() { |
156
|
|
|
var units = {}; |
157
|
|
|
var attribute = this.options.unitsAttribute; |
158
|
|
|
_.each($(':data(' + attribute + ')'), function(container) { |
159
|
|
|
var unit = $(container).data(attribute) || {}; |
160
|
|
|
_.each(unit, function(val, key) { |
161
|
|
|
units[key] = val; |
162
|
|
|
}); |
163
|
|
|
}); |
164
|
|
|
|
165
|
|
|
return units; |
166
|
|
|
}, |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return units from data attribute |
170
|
|
|
* |
171
|
|
|
* @returns {Object} |
172
|
|
|
*/ |
173
|
|
|
getUnitsWithPrices: function() { |
174
|
|
|
var unitsWithPrice = {}; |
175
|
|
|
_.each(this.getSelects(), function(select) { |
176
|
|
|
var selected = $(select).find('option:selected'); |
177
|
|
|
unitsWithPrice[selected.val()] = selected.text(); |
178
|
|
|
}); |
179
|
|
|
|
180
|
|
|
return unitsWithPrice; |
181
|
|
|
}, |
182
|
|
|
|
183
|
|
|
dispose: function() { |
184
|
|
|
if (this.disposed) { |
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
this.options._sourceElement.off(); |
189
|
|
|
|
190
|
|
|
ProductUnitPrecisionLimitationsComponent.__super__.dispose.call(this); |
191
|
|
|
} |
192
|
|
|
}); |
193
|
|
|
|
194
|
|
|
return ProductUnitPrecisionLimitationsComponent; |
195
|
|
|
}); |
196
|
|
|
|