Debian Codegraph @

addons/website_sale/static/src/interactions/product/product_variant_preview.js

raw
1 import { Interaction } from '@web/public/interaction';
2 import { registry } from '@web/core/registry';
3
4 export class ProductVariantPreview extends Interaction {
5 static selector = "#o_wsale_products_grid";
6
7 dynamicContent = {
8 _window: {
9 "t-on-resize": this.debounced(this.updateVariantPreview, 250),
10 },
11 };
12
13 setup() {
14 // Class `gap-1` on parent adds 4px margin for each ptav.
15 this.margin = 4;
16 this.updateVariantPreview();
17 }
18
19 /**
20 * Hide all attribute values from view to be able to recompute correctly how many elements are
21 * to be shown.
22 *
23 * @private
24 *
25 * @returns {void}
26 */
27 _resetDisplay(attributePreviewer) {
28 for (const child of attributePreviewer.children) {
29 child.classList.add('d-none');
30 }
31 }
32
33 /**
34 * Update the count of hidden PTAVs with the correct number and make it visible.
35 *
36 * @private
37 * @param {Element} currentPTAV
38 * @param {Number} remainingSpace
39 *
40 * @returns {void}
41 */
42 _showHiddenPTAVsElement(
43 attributePreviewerValues, currentPTAV, remainingSpace, displayedPTAVCount
44 ) {
45 const {
46 ptavCount,
47 offsetWidthPTAVS,
48 hiddenCountSpan,
49 hiddenCountSpanWidth,
50 } = attributePreviewerValues;
51 while (currentPTAV && hiddenCountSpanWidth >= remainingSpace) {
52 currentPTAV.classList.add("d-none");
53 displayedPTAVCount--;
54 remainingSpace += offsetWidthPTAVS.get(currentPTAV);
55 currentPTAV = currentPTAV.previousElementSibling;
56 }
57 const hiddenPTAVCount = ptavCount - displayedPTAVCount;
58 hiddenCountSpan.firstElementChild.textContent = `+${hiddenPTAVCount}`;
59 hiddenCountSpan.classList.remove("d-none");
60 }
61
62 /**
63 * For each ptav check if there is enough space to add on the parent element and update the
64 * hidden PTAVs count accordingly, with the truncated elements from the backend.
65 *
66 * @private
67 *
68 * @returns {void}
69 */
70 _updateVariantPreview(attributePreviewer, attributePreviewerValues) {
71 const { containerWidth, ptavs, ptavCount, offsetWidthPTAVS } = attributePreviewerValues;
72 this._resetDisplay(attributePreviewer);
73 let usedWidth = 0;
74 let displayedPTAVCount = 0;
75 for (const ptav of ptavs) {
76 ptav.classList.remove('d-none');
77 usedWidth += offsetWidthPTAVS.get(ptav) + this.margin;
78 displayedPTAVCount++;
79 const remainingSpace = containerWidth - usedWidth;
80 const isLastPTAV = ptav === ptavs[ptavs.length - 1];
81 const hasHiddenPtavs = isLastPTAV && ptavCount > displayedPTAVCount;
82 if (usedWidth >= containerWidth || hasHiddenPtavs) {
83 this._showHiddenPTAVsElement(
84 attributePreviewerValues, ptav, remainingSpace, displayedPTAVCount,
85 );
86 break;
87 }
88 }
89 }
90
91 /**
92 * Triggered on the parent element of the '.o_wsale_attribute_previewer' elements to run the
93 * interaction once instead of multiple times depending on how many elements exist on the page.
94 *
95 * Schedules and batches updates for all active '.o_wsale_attribute_previewer' elements
96 * to refresh their variant previews efficiently.
97 *
98 * Uses `requestAnimationFrame` to ensure that updates occur in sync with the browser’s
99 * rendering cycle, preventing redundant or frequent recalculations (trigger by offsetWidth).
100 */
101 updateVariantPreview() {
102 const attributePreviewers = this.el.querySelectorAll(".o_wsale_attribute_previewer");
103 const updateAllVariantPreview = this.protectSyncAfterAsync(() => {
104 const attributePreviewerValues = new Map();
105
106 // Initiate the values needed for each attribute previewer.
107 for (const attributePreviewer of attributePreviewers) {
108 this._resetDisplay(attributePreviewer);
109 const ptavs = attributePreviewer.querySelectorAll(".o_product_variant_preview");
110 // Set the hiddenCountSpan to the maximum number of ptavs there is to assume
111 // the worst case space it needs.
112 const hiddenCountSpan = attributePreviewer.querySelector(
113 "span[name='hidden_ptavs_count']");
114 const ptavCount = ptavs.length + Number(
115 attributePreviewer.dataset.hiddenPtavCount ?? 0);
116 hiddenCountSpan.firstElementChild.textContent = `+${ptavCount}`;
117 hiddenCountSpan.classList.remove("d-none");
118 attributePreviewerValues.set(
119 attributePreviewer,
120 {
121 containerWidth: attributePreviewer.offsetWidth,
122 ptavs,
123 hiddenCountSpan,
124 ptavCount,
125 offsetWidthPTAVS: new Map(),
126 hiddenCountSpanWidth: 0,
127 },
128 );
129 }
130
131 // Display all hidden elements to get the correct width.
132 for (const attributePreviewer of attributePreviewers) {
133 const currentValues = attributePreviewerValues.get(attributePreviewer);
134 for (const ptav of currentValues.ptavs) {
135 ptav.classList.remove("d-none");
136 }
137 }
138
139 // A recalculation of the styles is triggered every time offsetWidth is called.
140 // Get all offsetWidths in one step to avoid recalculation for each element separately.
141 for (const attributePreviewer of attributePreviewers) {
142 const currentValues = attributePreviewerValues.get(attributePreviewer);
143 for (const ptav of currentValues.ptavs) {
144 currentValues.offsetWidthPTAVS.set(ptav, ptav.offsetWidth);
145 }
146 currentValues.hiddenCountSpanWidth = (
147 currentValues.hiddenCountSpan.offsetWidth + this.margin * 2
148 );
149 }
150
151 for (const attributePreviewer of attributePreviewers) {
152 this._updateVariantPreview(
153 attributePreviewer, attributePreviewerValues.get(attributePreviewer)
154 );
155 }
156 });
157 requestAnimationFrame(updateAllVariantPreview);
158 }
159 }
160
161 registry
162 .category('public.interactions')
163 .add('website_sale.product_variant_preview', ProductVariantPreview);
164
165 registry
166 .category("public.interactions.edit")
167 .add("website.product_variant_preview", { Interaction: ProductVariantPreview });