-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
product-summary.js
123 lines (110 loc) · 3.98 KB
/
product-summary.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
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* @api
*/
define([
'jquery',
'mage/template',
'jquery-ui-modules/widget',
'Magento_Bundle/js/price-bundle'
], function ($, mageTemplate) {
'use strict';
/**
* Widget product Summary:
* Handles rendering of Bundle options and displays them in the Summary box
*/
$.widget('mage.productSummary', {
options: {
mainContainer: '#product_addtocart_form',
templates: {
summaryBlock: '[data-template="bundle-summary"]',
optionBlock: '[data-template="bundle-option"]'
},
optionSelector: '[data-container="options"]',
summaryContainer: '[data-container="product-summary"]',
bundleSummaryContainer: '.bundle-summary'
},
cache: {},
/**
* Method attaches event observer to the product form
* @private
*/
_create: function () {
this.element
.closest(this.options.mainContainer)
.on('updateProductSummary', $.proxy(this._renderSummaryBox, this))
.priceBundle({})
;
},
/**
* Method extracts data from the event and renders Summary box
* using jQuery template mechanism
* @param {Event} event
* @param {Object} data
* @private
*/
_renderSummaryBox: function (event, data) {
this.cache.currentElement = data.config;
this.cache.currentElementCount = 0;
// Clear Summary box
this.element.html('');
this.cache.currentElement.positions.forEach(function (optionId) {
this._renderOption(optionId, this.cache.currentElement.selected[optionId]);
}, this);
this.element
.parents(this.options.bundleSummaryContainer)
.toggleClass('empty', !this.cache.currentElementCount); // Zero elements equal '.empty' container
},
/**
* @param {String} key
* @param {String} row
* @private
*/
_renderOption: function (key, row) {
var template;
if (row && row.length > 0 && row[0] !== null) {
template = this.element
.closest(this.options.summaryContainer)
.find(this.options.templates.summaryBlock)
.html();
template = mageTemplate(template.trim(), {
data: {
_label_: this.cache.currentElement.options[key].title
}
});
this.cache.currentKey = key;
this.cache.summaryContainer = $(template);
this.element.append(this.cache.summaryContainer);
$.each(row, this._renderOptionRow.bind(this));
this.cache.currentElementCount += row.length;
//Reset Cache
this.cache.currentKey = null;
}
},
/**
* @param {String} key
* @param {String} optionIndex
* @private
*/
_renderOptionRow: function (key, optionIndex) {
var template;
template = this.element
.closest(this.options.summaryContainer)
.find(this.options.templates.optionBlock)
.html();
template = mageTemplate(template.trim(), {
data: {
_quantity_: this.cache.currentElement.options[this.cache.currentKey].selections[optionIndex].qty,
_label_: this.cache.currentElement.options[this.cache.currentKey].selections[optionIndex].name
}
});
this.cache.summaryContainer
.find(this.options.optionSelector)
.append(template);
}
});
return $.mage.productSummary;
});