Skip to content

Commit 924ee46

Browse files
author
b__b
committed
12273 move lable replace logic to separete js
1 parent 5569413 commit 924ee46

File tree

2 files changed

+69
-53
lines changed

2 files changed

+69
-53
lines changed

view/frontend/web/js/label.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright © Magefan (support@magefan.com). All rights reserved.
3+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
4+
*/
5+
6+
var MagefanPL = {
7+
processConfigurableProductLabel: function (lableEl, maintProductID, selectedProductId) {
8+
var self = this;
9+
console.log({maintProductID})
10+
console.log({selectedProductId})
11+
12+
13+
if (!window.mfLabelProcessed) {
14+
window.mfLabelProcessed = {};
15+
}
16+
17+
if (!window.mfLabelProcessed[maintProductID]) {
18+
let url = BASE_URL + 'mfpl/label/get?product_ids=' + maintProductID + '&get_children=1&product_page=' + (this.inProductList ? '0' : '1');
19+
20+
MagefanJs.ajax({'url':url, 'type': 'GET',
21+
success: function(response) {
22+
response = JSON.parse(response)
23+
console.log(response);
24+
window.mfLabelProcessed[maintProductID] = response.labels;
25+
self.replaceLabel(lableEl, maintProductID, selectedProductId)
26+
}
27+
});
28+
} else {
29+
self.replaceLabel(lableEl, maintProductID, selectedProductId)
30+
}
31+
},
32+
33+
replaceLabel: function (lableEl, maintProductID, selectedProductId) {
34+
35+
let labelHtml = window.mfLabelProcessed[maintProductID] && window.mfLabelProcessed[maintProductID][selectedProductId]
36+
? window.mfLabelProcessed[maintProductID][selectedProductId] : '';
37+
console.log(labelHtml);
38+
39+
if (labelHtml) {
40+
const tempDiv = document.createElement('div');
41+
tempDiv.innerHTML = labelHtml;
42+
const newLable = tempDiv.firstElementChild;
43+
44+
if (newLable) {
45+
lableEl.replaceWith(newLable);
46+
}
47+
}
48+
}
49+
};

view/frontend/web/js/swatch-renderer-mixin.js

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,42 @@
55

66
define([
77
'jquery',
8-
'jquery-ui-modules/widget'
8+
'jquery-ui-modules/widget',
9+
'Magefan_ProductLabel/js/label'
910
], function ($) {
1011
'use strict';
1112

1213
return function (SwatchRenderer) {
1314
$.widget('mage.SwatchRenderer', SwatchRenderer, {
1415
_OnClick: function ($this, $widget) {
1516
this._super($this, $widget);
16-
this.dispatchItemSelect();
17+
this.processLable();
1718
},
1819

1920
_OnChange: function ($this, $widget) {
2021
this._super($this, $widget);
21-
this.dispatchItemSelect();
22+
this.processLable();
2223
},
2324

24-
dispatchItemSelect: function () {
25+
processLable: function () {
2526
if (this.options.jsonConfig.productId) {
26-
this.processProductLabel(this.options.jsonConfig.productId, this.getProductId());
27-
}
28-
29-
$(document).trigger('mfChildItemSelected', {
30-
mainProductId: this.options.jsonConfig.productId,
31-
selectedProductId: this.getProductId()});
32-
},
33-
34-
processProductLabel: function (maintProductID, selectedProductId) {
35-
var self = this;
36-
console.log({maintProductID})
37-
console.log({selectedProductId})
38-
39-
40-
if (!window.mfLabelProcessed) {
41-
window.mfLabelProcessed = {};
42-
}
43-
44-
if (!window.mfLabelProcessed[maintProductID]) {
45-
let url = BASE_URL + 'mfpl/label/get?product_ids=' + maintProductID + '&get_children=1&product_page=' + (this.inProductList ? '0' : '1');
27+
let lableEl = null;
28+
29+
if (this.inProductList) {
30+
const listItem = this.element.closest('li.item').get(0); // Convert jQuery object to a native DOM element
4631

47-
MagefanJs.ajax({'url':url, 'type': 'GET',
48-
success: function(response) {
49-
response = JSON.parse(response)
50-
console.log(response);
51-
window.mfLabelProcessed[maintProductID] = response.labels;
52-
self.replaceLabel(maintProductID, selectedProductId)
32+
if (listItem) {
33+
lableEl = listItem.querySelector('.mf-label-container');
5334
}
54-
});
55-
} else {
56-
self.replaceLabel(maintProductID, selectedProductId)
57-
}
58-
},
59-
60-
replaceLabel: function (maintProductID, selectedProductId) {
61-
let mainLableEl = null;
62-
63-
if (this.inProductList) {
64-
mainLableEl = this.element.closest('li.item')
65-
.find('.mf-label-container');
66-
} else {
67-
mainLableEl = $('.mfpl-product-page');
68-
}
69-
70-
console.log({mainLableEl});
71-
let labelHtml = window.mfLabelProcessed[maintProductID] && window.mfLabelProcessed[maintProductID][selectedProductId]
72-
? window.mfLabelProcessed[maintProductID][selectedProductId] : '';
73-
console.log(labelHtml);
74-
75-
if (labelHtml) {
76-
mainLableEl.replaceWith(labelHtml);
35+
} else {
36+
lableEl = document.querySelector('.mfpl-product-page');
37+
}
38+
39+
console.log({lableEl});
40+
41+
if (lableEl) {
42+
MagefanPL.processConfigurableProductLabel(lableEl, this.options.jsonConfig.productId, this.getProductId());
43+
}
7744
}
7845
}
7946
});

0 commit comments

Comments
 (0)