1
|
|
|
define(function(require) { |
2
|
|
|
'use strict'; |
3
|
|
|
|
4
|
|
|
var ProductPrimaryUnitLimitationsComponent; |
5
|
|
|
var $ = require('jquery'); |
6
|
|
|
var _ = require('underscore'); |
7
|
|
|
var BaseComponent = require('oroui/js/app/components/base/component'); |
8
|
|
|
var mediator = require('oroui/js/mediator'); |
9
|
|
|
|
10
|
|
|
ProductPrimaryUnitLimitationsComponent = BaseComponent.extend({ |
11
|
|
|
/** |
12
|
|
|
* @property {Object} |
13
|
|
|
*/ |
14
|
|
|
options: { |
15
|
|
|
unitsAttribute: 'units', |
16
|
|
|
allUnitsAttribute: 'all_units', |
17
|
|
|
deleteMessage: 'oro.product.productunit.delete.confirmation', |
18
|
|
|
addButtonSelector: 'a.add-list-item', |
19
|
|
|
selectParent: '.oro-multiselect-holder', |
20
|
|
|
dataContent: '*[data-content]', |
21
|
|
|
unitSelect: 'select[name$="[unit]"]', |
22
|
|
|
hiddenUnitClass: 'hidden-unit', |
23
|
|
|
precisions: {}, |
24
|
|
|
initialAdditionalUnits: {} |
25
|
|
|
}, |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @property {Object} |
29
|
|
|
*/ |
30
|
|
|
listen: { |
31
|
|
|
'product:precision:remove mediator': 'onAdditionalPrecisionsChange', |
32
|
|
|
'product:precision:add mediator': 'onAdditionalPrecisionsChange' |
33
|
|
|
}, |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
|
|
constructor: function ProductPrimaryUnitLimitationsComponent() { |
39
|
|
|
ProductPrimaryUnitLimitationsComponent.__super__.constructor.apply(this, arguments); |
40
|
|
|
}, |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
initialize: function(options) { |
46
|
|
|
this.options = _.defaults(options || {}, this.options); |
47
|
|
|
this.$select = this.options._sourceElement.find(this.options.unitSelect); |
48
|
|
|
|
49
|
|
|
this.options._sourceElement |
50
|
|
|
.on('change', _.bind(this.onChange, this)); |
51
|
|
|
this.saveInitialOptions(); |
52
|
|
|
this.options._sourceElement.trigger('change'); |
53
|
|
|
this.onAdditionalPrecisionsChange(this.options.initialAdditionalUnits); |
54
|
|
|
}, |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Handle change select |
58
|
|
|
*/ |
59
|
|
|
onChange: function() { |
60
|
|
|
this.$select.on('change', _.bind(this.onSelectChange, this)); |
61
|
|
|
var option = this.$select.find('option:selected'); |
62
|
|
|
var changes = {}; |
63
|
|
|
changes.removed = this.getData() || {}; |
64
|
|
|
this.saveData({}); |
65
|
|
|
var storedData = this.getData(); |
66
|
|
|
|
67
|
|
|
if (option.val() !== undefined) { |
68
|
|
|
storedData[option.val()] = option.text(); |
69
|
|
|
} else { |
70
|
|
|
storedData = changes.removed; |
71
|
|
|
} |
72
|
|
|
this.saveData(storedData); |
73
|
|
|
changes.added = storedData; |
74
|
|
|
|
75
|
|
|
if (changes.added !== changes.removed) { |
76
|
|
|
this.triggerChangeEvent(changes); |
77
|
|
|
} |
78
|
|
|
}, |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Handle select change |
82
|
|
|
* |
83
|
|
|
* @param {jQuery.Event} e |
84
|
|
|
*/ |
85
|
|
|
onSelectChange: function(e) { |
86
|
|
|
var select = $(e.target); |
87
|
|
|
var option = select.find('option:selected'); |
88
|
|
|
var value = this.options.precisions[option.val()]; |
89
|
|
|
select.closest('tr').find('input[class="precision"]').val(value); |
90
|
|
|
}, |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Handle changes in additional precisions |
94
|
|
|
*/ |
95
|
|
|
onAdditionalPrecisionsChange: function(e) { |
96
|
|
|
var additionalPrecisions = e; |
97
|
|
|
var precisions = this.getInitialOptions(); |
98
|
|
|
_.each(additionalPrecisions, function(val, key) { |
99
|
|
|
delete precisions[key]; |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
var options = this.$select.find('option'); |
103
|
|
|
var selected = this.$select.find('option:selected'); |
104
|
|
|
delete precisions[selected.val()]; |
105
|
|
|
|
106
|
|
|
_.each(options, function(option) { |
107
|
|
|
if (option.value !== selected.val()) { |
108
|
|
|
$(option).remove(); |
109
|
|
|
} |
110
|
|
|
}); |
111
|
|
|
var self = this; |
112
|
|
|
_.each(precisions, function(text, val) { |
113
|
|
|
self.$select.append($('<option></option>').val(val).text(text)); |
114
|
|
|
}); |
115
|
|
|
self.$select.find(selected.val()).selected(true).trigger('change'); |
116
|
|
|
}, |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Return units from data attribute |
120
|
|
|
* |
121
|
|
|
* @returns {jQuery.Element} |
122
|
|
|
*/ |
123
|
|
|
getData: function() { |
124
|
|
|
return this.options._sourceElement.data(this.options.unitsAttribute) || {}; |
125
|
|
|
}, |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Return initial full options from data attribute |
129
|
|
|
* |
130
|
|
|
* @returns {jQuery.Element} |
131
|
|
|
*/ |
132
|
|
|
getInitialOptions: function() { |
133
|
|
|
return _.clone(this.options._sourceElement.data(this.options.allUnitsAttribute) || {}); |
134
|
|
|
}, |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Save data to data attribute |
138
|
|
|
* |
139
|
|
|
* @param {Object} data |
140
|
|
|
*/ |
141
|
|
|
saveData: function(data) { |
142
|
|
|
this.options._sourceElement.data(this.options.unitsAttribute, data); |
143
|
|
|
}, |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Save initial full select options to data attribute |
147
|
|
|
*/ |
148
|
|
|
saveInitialOptions: function() { |
149
|
|
|
var options = this.$select.find('option'); |
150
|
|
|
var allUnits = {}; |
151
|
|
|
_.each(options, function(option) { |
152
|
|
|
allUnits[option.value] = option.text; |
153
|
|
|
}); |
154
|
|
|
this.options._sourceElement.data(this.options.allUnitsAttribute, allUnits); |
155
|
|
|
}, |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Trigger add event |
159
|
|
|
* |
160
|
|
|
* @param {Object} data |
161
|
|
|
*/ |
162
|
|
|
triggerChangeEvent: function(data) { |
163
|
|
|
mediator.trigger('product:primary:precision:change', data); |
164
|
|
|
}, |
165
|
|
|
|
166
|
|
|
dispose: function() { |
167
|
|
|
if (this.disposed) { |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
if (this.confirm) { |
171
|
|
|
this.confirm |
172
|
|
|
.off() |
173
|
|
|
.remove(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
this.options._sourceElement.off(); |
177
|
|
|
this.$select.off('change'); |
178
|
|
|
|
179
|
|
|
ProductPrimaryUnitLimitationsComponent.__super__.dispose.call(this); |
180
|
|
|
} |
181
|
|
|
}); |
182
|
|
|
|
183
|
|
|
return ProductPrimaryUnitLimitationsComponent; |
184
|
|
|
}); |
185
|
|
|
|