|
1
|
|
|
define(function(require) { |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
var ProductPricesEditableView; |
|
5
|
|
|
var pricesHint = require('tpl!oropricing/templates/product/prices-tier-button.html'); |
|
6
|
|
|
var pricesHintContent = require('tpl!oropricing/templates/product/prices-tier-table.html'); |
|
7
|
|
|
var priceOverridden = require('tpl!oropricing/templates/product/prices-price-overridden.html'); |
|
8
|
|
|
var BaseProductPricesView = require('oropricing/js/app/views/base-product-prices-view'); |
|
9
|
|
|
var NumberFormatter = require('orolocale/js/formatter/number'); |
|
10
|
|
|
var layout = require('oroui/js/layout'); |
|
11
|
|
|
var $ = require('jquery'); |
|
12
|
|
|
var _ = require('underscore'); |
|
13
|
|
|
|
|
14
|
|
|
ProductPricesEditableView = BaseProductPricesView.extend({ |
|
15
|
|
|
elements: _.extend({}, BaseProductPricesView.prototype.elements, { |
|
16
|
|
|
pricesHint: null, |
|
17
|
|
|
pricesHintContent: null, |
|
18
|
|
|
priceOverridden: null, |
|
19
|
|
|
priceValue: '[data-name="field__value"]' |
|
20
|
|
|
}), |
|
21
|
|
|
|
|
22
|
|
|
modelAttr: { |
|
23
|
|
|
found_price: null |
|
24
|
|
|
}, |
|
25
|
|
|
|
|
26
|
|
|
elementsEvents: _.extend({}, BaseProductPricesView.prototype.elementsEvents, { |
|
27
|
|
|
'priceValue onPriceSetManually': ['change', 'onPriceSetManually'] |
|
28
|
|
|
}), |
|
29
|
|
|
|
|
30
|
|
|
modelElements: _.extend({}, BaseProductPricesView.prototype.modelElements, { |
|
31
|
|
|
price: 'priceValue' |
|
32
|
|
|
}), |
|
33
|
|
|
|
|
34
|
|
|
options: { |
|
35
|
|
|
matchedPriceEnabled: true, |
|
36
|
|
|
precision: 4, |
|
37
|
|
|
editable: true |
|
38
|
|
|
}, |
|
39
|
|
|
|
|
40
|
|
|
templates: { |
|
41
|
|
|
priceOverridden: priceOverridden, |
|
42
|
|
|
pricesHint: pricesHint, |
|
43
|
|
|
pricesHintContent: pricesHintContent |
|
44
|
|
|
}, |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritDoc |
|
48
|
|
|
*/ |
|
49
|
|
|
constructor: function ProductPricesEditableView() { |
|
50
|
|
|
ProductPricesEditableView.__super__.constructor.apply(this, arguments); |
|
51
|
|
|
}, |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritDoc |
|
55
|
|
|
*/ |
|
56
|
|
|
initialize: function(options) { |
|
57
|
|
|
this.options = $.extend(true, {}, this.options, _.pick(options, _.keys(this.options))); |
|
58
|
|
|
this.templates = $.extend(true, {}, this.templates, options.templates || {}); |
|
59
|
|
|
|
|
60
|
|
|
ProductPricesEditableView.__super__.initialize.apply(this, arguments); |
|
61
|
|
|
}, |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritDoc |
|
65
|
|
|
*/ |
|
66
|
|
|
deferredInitialize: function(options) { |
|
|
|
|
|
|
67
|
|
|
ProductPricesEditableView.__super__.deferredInitialize.apply(this, arguments); |
|
68
|
|
|
}, |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritDoc |
|
72
|
|
|
*/ |
|
73
|
|
|
dispose: function(options) { |
|
|
|
|
|
|
74
|
|
|
delete this.templates; |
|
75
|
|
|
ProductPricesEditableView.__super__.dispose.apply(this, arguments); |
|
76
|
|
|
}, |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritDoc |
|
80
|
|
|
*/ |
|
81
|
|
|
findPrice: function() { |
|
82
|
|
|
var price = ProductPricesEditableView.__super__.findPrice.apply(this, arguments); |
|
83
|
|
|
this.model.set('found_price', price); |
|
84
|
|
|
this.getElement('priceValue').data('found_price', price); |
|
85
|
|
|
return price; |
|
86
|
|
|
}, |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritDoc |
|
90
|
|
|
*/ |
|
91
|
|
|
setFoundPrice: function() { |
|
92
|
|
|
this.findPrice(); |
|
93
|
|
|
if (this.options.matchedPriceEnabled && this.getElement('priceValue').hasClass('matched-price')) { |
|
94
|
|
|
this.setPriceValue(this.findPriceValue()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
this.updateUI(); |
|
98
|
|
|
}, |
|
99
|
|
|
|
|
100
|
|
|
setPriceValue: function(price) { |
|
101
|
|
|
this.model.set('price', this.calcTotalPrice(price)); |
|
102
|
|
|
}, |
|
103
|
|
|
|
|
104
|
|
|
updateUI: function() { |
|
105
|
|
|
this.renderPriceOverridden(); |
|
106
|
|
|
this.renderHint(); |
|
107
|
|
|
}, |
|
108
|
|
|
|
|
109
|
|
|
initPriceOverridden: function() { |
|
110
|
|
|
this.priceOverriddenInitialized = true; |
|
111
|
|
|
if (!this.options.matchedPriceEnabled || !this.options.editable) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
var $priceOverridden = this.createElementByTemplate('priceOverridden'); |
|
116
|
|
|
|
|
117
|
|
|
layout.initPopover($priceOverridden); |
|
118
|
|
|
$priceOverridden.insertBefore(this.getElement('priceValue')) |
|
119
|
|
|
.on('click', 'a', _.bind(this.useFoundPrice, this)); |
|
120
|
|
|
|
|
121
|
|
|
if (_.isEmpty(this.getElement('priceValue').val()) && this.options.matchedPriceEnabled) { |
|
122
|
|
|
this.getElement('priceValue').addClass('matched-price'); |
|
123
|
|
|
} |
|
124
|
|
|
}, |
|
125
|
|
|
|
|
126
|
|
|
initHint: function() { |
|
127
|
|
|
this.hintInitialized = true; |
|
128
|
|
|
|
|
129
|
|
|
if (typeof this.templates.pricesHintContent !== 'function') { |
|
130
|
|
|
this.templates.pricesHintContent = _.template(this.getElement('pricesHintContent').html()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
var $pricesHint = this.createElementByTemplate('pricesHint'); |
|
134
|
|
|
|
|
135
|
|
|
this.getElement('priceValue').after($pricesHint); |
|
136
|
|
|
|
|
137
|
|
|
var clickHandler = _.bind(this.setPriceFromHint, this); |
|
138
|
|
|
$pricesHint |
|
139
|
|
|
.on('shown', function() { |
|
140
|
|
|
$pricesHint.data('popover').tip() |
|
141
|
|
|
.find('a[data-price]') |
|
142
|
|
|
.click(clickHandler); |
|
143
|
|
|
}); |
|
144
|
|
|
}, |
|
145
|
|
|
|
|
146
|
|
|
getHintContent: function() { |
|
147
|
|
|
if (_.isEmpty(this.prices)) { |
|
148
|
|
|
return ''; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $(this.templates.pricesHintContent({ |
|
152
|
|
|
model: this.model.attributes, |
|
153
|
|
|
prices: this.prices, |
|
154
|
|
|
matchedPrice: this.findPrice(), |
|
155
|
|
|
clickable: this.options.editable, |
|
156
|
|
|
formatter: NumberFormatter |
|
157
|
|
|
})); |
|
158
|
|
|
}, |
|
159
|
|
|
|
|
160
|
|
|
renderHint: function() { |
|
161
|
|
|
if (!this.hintInitialized) { |
|
162
|
|
|
this.initHint(); |
|
163
|
|
|
} |
|
164
|
|
|
return ProductPricesEditableView.__super__.renderHint.apply(this, arguments); |
|
165
|
|
|
}, |
|
166
|
|
|
|
|
167
|
|
|
onPriceSetManually: function(e, options) { |
|
168
|
|
|
if (options.manually && this.options.matchedPriceEnabled) { |
|
169
|
|
|
this.getElement('priceValue').removeClass('matched-price'); |
|
170
|
|
|
} |
|
171
|
|
|
}, |
|
172
|
|
|
|
|
173
|
|
|
setPriceFromHint: function(e) { |
|
174
|
|
|
this.getElement('priceValue').removeClass('matched-price'); |
|
175
|
|
|
var $target = $(e.currentTarget); |
|
176
|
|
|
this.model.set('unit', $target.data('unit')); |
|
177
|
|
|
this.setPriceValue($target.data('price')); |
|
178
|
|
|
}, |
|
179
|
|
|
|
|
180
|
|
|
renderPriceOverridden: function() { |
|
181
|
|
|
if (!this.options.matchedPriceEnabled) { |
|
182
|
|
|
return; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (!this.priceOverriddenInitialized) { |
|
186
|
|
|
this.initPriceOverridden(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
var priceValue = this.getElement('priceValue').val(); |
|
190
|
|
|
var price = this.findPriceValue(); |
|
191
|
|
|
|
|
192
|
|
|
if (price !== null && |
|
193
|
|
|
this.calcTotalPrice(price) !== parseFloat(priceValue) |
|
194
|
|
|
) { |
|
195
|
|
|
this.getElement('priceOverridden').show(); |
|
196
|
|
|
} else { |
|
197
|
|
|
this.getElement('priceOverridden').hide(); |
|
198
|
|
|
} |
|
199
|
|
|
}, |
|
200
|
|
|
|
|
201
|
|
|
calcTotalPrice: function(price) { |
|
202
|
|
|
if (price === null) { |
|
203
|
|
|
return price; |
|
204
|
|
|
} |
|
205
|
|
|
var quantity = 1; |
|
206
|
|
|
return +(price * quantity).toFixed(this.options.precision); |
|
207
|
|
|
}, |
|
208
|
|
|
|
|
209
|
|
|
useFoundPrice: function() { |
|
210
|
|
|
if (!this.options.matchedPriceEnabled) { |
|
211
|
|
|
return; |
|
212
|
|
|
} |
|
213
|
|
|
this.setPriceValue(this.findPriceValue()); |
|
214
|
|
|
this.getElement('priceValue').addClass('matched-price'); |
|
215
|
|
|
} |
|
216
|
|
|
}); |
|
217
|
|
|
|
|
218
|
|
|
return ProductPricesEditableView; |
|
219
|
|
|
}); |
|
220
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.