-
Notifications
You must be signed in to change notification settings - Fork 83
/
list-item-product-prices-subview.js
130 lines (108 loc) · 4.01 KB
/
list-item-product-prices-subview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
define(function(require) {
'use strict';
const BaseView = require('oroui/js/app/views/base/view');
const mediator = require('oroui/js/mediator');
const numeral = require('numeral');
const localeSettings = require('orolocale/js/locale-settings');
const PricesHelper = require('oropricing/js/app/prices-helper');
const _ = require('underscore');
const ListItemProductPricesSubview = BaseView.extend({
template: require('tpl-loader!oropricing/templates/product/list-item-product-prices.html'),
modelAttr: {
listedPrice: null
},
/**
* @inheritdoc
*/
constructor: function ListItemProductPricesSubview(options) {
ListItemProductPricesSubview.__super__.constructor.call(this, options);
},
/**
* @inheritdoc
*/
initialize: function(options) {
ListItemProductPricesSubview.__super__.initialize.call(this, options);
this.showListedPrice = options.showListedPrice;
this.showValuePrice = options.showValuePrice;
this.changeUnitLabel = options.changeUnitLabel;
_.each(this.modelAttr, function(attrValue, attrCode) {
this.model.set(attrCode, attrValue);
}, this);
this.model.on('change:pricesByUnit', this.render, this);
this.model.on('change:quantity', this.render, this);
this.model.on('change:unit', this.render, this);
},
/**
* @inheritdoc
*/
dispose: function() {
if (this.model) {
this.model.off(null, null, this);
}
ListItemProductPricesSubview.__super__.dispose.call(this);
},
/**
* @inheritdoc
*/
getTemplateData: function() {
return {
listedPrice: this.findListedPrice(),
unit: this.model.get('unit'),
price: this.findPrice(),
localeSettings: localeSettings,
numeral: numeral,
showValuePrice: this.showValuePrice,
showListedPrice: this.showListedPrice
};
},
/**
* Add the current price to unit label to improve UI on product view
* Changes of 'unit_label' listen 'oroproduct/js/app/views/base-product-view'
*/
setUnitLabel: function() {
if (!this.changeUnitLabel) {
return;
}
const price = this.findPrice();
let unitLabel = null;
if (price) {
unitLabel = _.__('oro.pricing.price.formatted.unit', {
formattedUnit: _(price.formatted_unit).capitalize(),
formattedPrice: price.formatted_price
});
}
this.model.set('unit_label', unitLabel);
},
/**
* @inheritdoc
*/
render: function() {
ListItemProductPricesSubview.__super__.render.call(this);
this.setUnitLabel();
mediator.trigger('layout:reposition');
return this;
},
findPrice: function() {
if (!this.showValuePrice) {
return null;
}
const quantity = this.model.get('quantity');
const unit = this.model.get('unit');
const prices = this.model.get('pricesByUnit');
return PricesHelper.findPrice(prices, unit, quantity);
},
findListedPrice: function() {
const prices = this.model.get('pricesByUnit');
let listedPrice = this.model.get('listedPrice');
if (null === listedPrice) {
listedPrice = {};
_.each(prices, function(unitData, unitKey) {
listedPrice[unitKey] = _.last(unitData);
});
this.model.set('listedPrice', listedPrice);
}
return listedPrice;
}
});
return ListItemProductPricesSubview;
});