1
|
|
|
define(function(require) { |
2
|
|
|
'use strict'; |
3
|
|
|
|
4
|
|
|
var PriceListCurrencyLimitationComponent; |
5
|
|
|
var $ = require('jquery'); |
6
|
|
|
var _ = require('underscore'); |
7
|
|
|
var routing = require('routing'); |
8
|
|
|
var LoadingMaskView = require('oroui/js/app/views/loading-mask-view'); |
9
|
|
|
var BaseComponent = require('oroui/js/app/components/base/component'); |
10
|
|
|
|
11
|
|
|
PriceListCurrencyLimitationComponent = BaseComponent.extend({ |
12
|
|
|
/** |
13
|
|
|
* @property {Object} |
14
|
|
|
*/ |
15
|
|
|
options: { |
16
|
|
|
priceListSelector: 'input[name$="[priceList]"]', |
17
|
|
|
currencySelector: 'select[name$="[price][currency]"]', |
18
|
|
|
container: '.oro-item-collection', |
19
|
|
|
currenciesRoute: 'oro_pricing_price_list_currency_list' |
20
|
|
|
}, |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @property {array} |
24
|
|
|
*/ |
25
|
|
|
currencies: {}, |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @property {Object} |
29
|
|
|
*/ |
30
|
|
|
$priceListSelect: null, |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @property {Object} |
34
|
|
|
*/ |
35
|
|
|
$currencySelect: null, |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
|
|
constructor: function PriceListCurrencyLimitationComponent(options) { |
41
|
|
|
PriceListCurrencyLimitationComponent.__super__.constructor.call(this, options); |
42
|
|
|
}, |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
initialize: function(options) { |
48
|
|
|
this.options = _.defaults(options || {}, this.options); |
49
|
|
|
this.$elem = options._sourceElement; |
50
|
|
|
|
51
|
|
|
this.loadingMaskView = new LoadingMaskView({container: this.$elem}); |
52
|
|
|
this.currencies = this.$elem.closest(options.container).data('currencies'); |
53
|
|
|
this.$priceListSelect = this.$elem.find(options.priceListSelector); |
54
|
|
|
this.$currencySelect = this.$elem.find(options.currencySelector); |
55
|
|
|
|
56
|
|
|
this.prepareCurrencySelect(false); |
57
|
|
|
this.$elem.on( |
58
|
|
|
'change', |
59
|
|
|
options.priceListSelector, |
60
|
|
|
_.bind( |
61
|
|
|
function() { |
62
|
|
|
this.prepareCurrencySelect(true); |
63
|
|
|
}, |
64
|
|
|
this |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
}, |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Fetches full list of currency options from the prototype of collection item |
71
|
|
|
* Preserves fetched collection in the collection container for reuse by other collection items |
72
|
|
|
* |
73
|
|
|
* @return {Object.<string, HTMLOptionElement>} |
74
|
|
|
*/ |
75
|
|
|
getSystemSupportedCurrencyOptions: function() { |
76
|
|
|
var $collectionContainer = this.$elem.closest(this.options.container); |
77
|
|
|
var currencyOptions = $collectionContainer.data('systemSupportedCurrencyOptions'); |
78
|
|
|
|
79
|
|
|
if (!currencyOptions) { |
80
|
|
|
currencyOptions = {}; |
81
|
|
|
$($collectionContainer.data('prototype')) |
82
|
|
|
.find(this.options.currencySelector + ' option') |
83
|
|
|
.each(function(i, option) { |
84
|
|
|
var optionClone = option.cloneNode(true); |
85
|
|
|
optionClone.removeAttribute('selected'); |
86
|
|
|
currencyOptions[optionClone.value] = optionClone; |
87
|
|
|
}); |
88
|
|
|
$collectionContainer.data('systemSupportedCurrencyOptions', currencyOptions); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return currencyOptions; |
92
|
|
|
}, |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Prepare currency list select for selected price list |
96
|
|
|
* |
97
|
|
|
* @param {Boolean} selectFirst |
98
|
|
|
*/ |
99
|
|
|
prepareCurrencySelect: function(selectFirst) { |
100
|
|
|
var priceListId = this.$priceListSelect.val(); |
101
|
|
|
var self = this; |
102
|
|
|
|
103
|
|
|
if (!priceListId) { |
104
|
|
|
this.$currencySelect.find('option[value=""]').show(); |
105
|
|
|
this.$currencySelect.attr('disabled', 'disabled'); |
106
|
|
|
this.$currencySelect.val(''); |
107
|
|
|
this.$currencySelect.trigger('change'); |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (_.has(this.currencies, priceListId)) { |
112
|
|
|
this.handleCurrencies(this.currencies[priceListId], selectFirst); |
113
|
|
|
} else { |
114
|
|
|
$.ajax({ |
115
|
|
|
url: routing.generate(this.options.currenciesRoute, {id: priceListId}), |
116
|
|
|
type: 'GET', |
117
|
|
|
beforeSend: function() { |
118
|
|
|
self.loadingMaskView.show(); |
119
|
|
|
}, |
120
|
|
|
success: function(response) { |
121
|
|
|
var priceListCurrencies = _.keys(response); |
122
|
|
|
self.currencies[priceListId] = priceListCurrencies; |
123
|
|
|
self.$elem.closest(self.options.container).data('currencies', self.currencies); |
124
|
|
|
self.handleCurrencies(priceListCurrencies, selectFirst); |
125
|
|
|
}, |
126
|
|
|
complete: function() { |
127
|
|
|
self.loadingMaskView.hide(); |
128
|
|
|
} |
129
|
|
|
}); |
130
|
|
|
} |
131
|
|
|
}, |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param {array} priceListCurrencies |
135
|
|
|
* @param {Boolean} selectFirst |
136
|
|
|
*/ |
137
|
|
|
handleCurrencies: function(priceListCurrencies, selectFirst) { |
138
|
|
|
// Add empty key for empty value placeholder |
139
|
|
|
if (priceListCurrencies.indexOf('') === -1) { |
140
|
|
|
priceListCurrencies.unshift(''); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
var systemSupportedCurrencyOptions = this.getSystemSupportedCurrencyOptions(); |
144
|
|
|
var value = this.$currencySelect.val(); |
145
|
|
|
this.$currencySelect.empty(); |
146
|
|
|
_.each(priceListCurrencies, function(currency) { |
147
|
|
|
if (currency in systemSupportedCurrencyOptions) { |
148
|
|
|
var newOption = systemSupportedCurrencyOptions[currency].cloneNode(true); |
149
|
|
|
if (!_.isEmpty(value) && newOption.value === value) { |
150
|
|
|
newOption.selected = true; |
151
|
|
|
} |
152
|
|
|
this.$currencySelect.append(newOption); |
153
|
|
|
} |
154
|
|
|
}, this); |
155
|
|
|
|
156
|
|
|
this.$currencySelect.find('option[value=""]').hide(); |
157
|
|
|
this.$currencySelect.removeAttr('disabled'); |
158
|
|
|
|
159
|
|
|
if (selectFirst && _.isEmpty(value)) { |
160
|
|
|
this.$currencySelect.val(priceListCurrencies[1]); |
161
|
|
|
this.$currencySelect.trigger('change'); |
162
|
|
|
} |
163
|
|
|
}, |
164
|
|
|
|
165
|
|
|
dispose: function() { |
166
|
|
|
if (this.disposed) { |
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
this.$elem.off(); |
171
|
|
|
|
172
|
|
|
PriceListCurrencyLimitationComponent.__super__.dispose.call(this); |
173
|
|
|
} |
174
|
|
|
}); |
175
|
|
|
|
176
|
|
|
return PriceListCurrencyLimitationComponent; |
177
|
|
|
}); |
178
|
|
|
|