| 1 |
/** @odoo-module **/ |
| 2 |
|
| 3 |
import { Interaction } from "@web/public/interaction"; |
| 4 |
import { registry } from "@web/core/registry"; |
| 5 |
|
| 6 |
/** |
| 7 |
* Interaction that sets the height of images as a CSS custom property |
| 8 |
* on the product grid element. Used for responsive product grid layouts on mobile devices. |
| 9 |
*/ |
| 10 |
export class ProductGridLayout extends Interaction { |
| 11 |
static selector = "#o-grid-product"; |
| 12 |
|
| 13 |
dynamicContent = { |
| 14 |
_window: { |
| 15 |
"t-on-resize": this.debounced(this.onResize, 100), |
| 16 |
}, |
| 17 |
_root: { |
| 18 |
"t-att-class": () => ({ |
| 19 |
"o_grid_product_ready": this.isGridReady, |
| 20 |
}), |
| 21 |
"t-att-style": () => ({ |
| 22 |
"--o-wsale-js-grid-product-height": this.gridHeight || "auto", |
| 23 |
}), |
| 24 |
}, |
| 25 |
}; |
| 26 |
|
| 27 |
setup() { |
| 28 |
this.gridHeight = null; |
| 29 |
this.maxHeight = 0; |
| 30 |
this.isGridReady = false; |
| 31 |
this.loadedImages = new Set(); |
| 32 |
|
| 33 |
this.imagesEls = this.el.querySelectorAll('.product_detail_img'); |
| 34 |
this.isAutoRatioMode = this.el.classList.contains('o_grid_uses_ratio_auto') && |
| 35 |
this.el.classList.contains('o_grid_uses_ratio_mobile_auto'); |
| 36 |
} |
| 37 |
|
| 38 |
start() { |
| 39 |
if (this.imagesEls.length === 0) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
if (this.imagesEls.length === 1 || !this.isAutoRatioMode) { |
| 44 |
this.handleStandardMode(); |
| 45 |
} else { |
| 46 |
// Multiple images in auto ratio mode: use tallest |
| 47 |
this.handleAutoRatioMode(); |
| 48 |
} |
| 49 |
|
| 50 |
this.updateContent(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Handle standard mode - use first image height |
| 55 |
*/ |
| 56 |
handleStandardMode() { |
| 57 |
const firstImage = this.imagesEls[0]; |
| 58 |
|
| 59 |
if (firstImage.complete && firstImage.naturalHeight !== 0) { |
| 60 |
this.calculateImageHeight(); |
| 61 |
} else { |
| 62 |
this.addListener(firstImage, 'load', this.calculateImageHeight); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Calculate and store the image height (standard mode) |
| 68 |
*/ |
| 69 |
calculateImageHeight() { |
| 70 |
const firstImage = this.imagesEls[0]; |
| 71 |
if (!firstImage) return; |
| 72 |
|
| 73 |
const height = firstImage.offsetHeight; |
| 74 |
this.isGridReady = Boolean(height); |
| 75 |
this.gridHeight = height ? `${height}px` : null; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Handle auto ratio mode - wait for all images and use tallest |
| 80 |
*/ |
| 81 |
handleAutoRatioMode() { |
| 82 |
// Set 5-second timeout |
| 83 |
const timeoutId = this.waitForTimeout(() => { |
| 84 |
this.finalizeAutoRatioCalculation(); |
| 85 |
}, 5000); |
| 86 |
|
| 87 |
this.imagesEls.forEach(imgEl => { |
| 88 |
if (imgEl.complete && imgEl.naturalHeight !== 0) { |
| 89 |
this.processLoadedImage(imgEl); |
| 90 |
} else { |
| 91 |
this.addListener(imgEl, 'load', () => { |
| 92 |
this.processLoadedImage(imgEl); |
| 93 |
|
| 94 |
// If all images are loaded, finalize early |
| 95 |
if (this.loadedImages.size === this.imagesEls.length) { |
| 96 |
clearTimeout(timeoutId); |
| 97 |
this.finalizeAutoRatioCalculation(); |
| 98 |
} |
| 99 |
}); |
| 100 |
} |
| 101 |
}); |
| 102 |
|
| 103 |
// If all images were already loaded, finalize immediately |
| 104 |
if (this.loadedImages.size === this.imagesEls.length) { |
| 105 |
clearTimeout(timeoutId); |
| 106 |
this.finalizeAutoRatioCalculation(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Process a loaded image and track its height |
| 112 |
*/ |
| 113 |
processLoadedImage(imgEl) { |
| 114 |
this.loadedImages.add(imgEl); |
| 115 |
const height = imgEl.offsetHeight; |
| 116 |
if (height > this.maxHeight) { |
| 117 |
this.maxHeight = height; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Finalize calculation for auto ratio mode |
| 123 |
*/ |
| 124 |
finalizeAutoRatioCalculation() { |
| 125 |
this.isGridReady = true; |
| 126 |
this.gridHeight = this.maxHeight ? `${this.maxHeight}px` : null; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* On page resize, recalculate the image height (mobile only) |
| 131 |
*/ |
| 132 |
onResize() { |
| 133 |
if (!this.env.isSmall) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
if (this.isAutoRatioMode) { |
| 138 |
// Recalculate max height from all loaded images |
| 139 |
this.maxHeight = 0; |
| 140 |
this.loadedImages.forEach(imgEl => { |
| 141 |
const height = imgEl.offsetHeight; |
| 142 |
if (height > this.maxHeight) { |
| 143 |
this.maxHeight = height; |
| 144 |
} |
| 145 |
}); |
| 146 |
this.gridHeight = this.maxHeight ? `${this.maxHeight}px` : null; |
| 147 |
} else { |
| 148 |
this.calculateImageHeight(); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
registry |
| 154 |
.category("public.interactions") |
| 155 |
.add("website.website_sale_product_grid_layout", ProductGridLayout); |
| 156 |
|
| 157 |
registry |
| 158 |
.category("public.interactions.edit") |
| 159 |
.add("website.website_sale_product_grid_layout", { Interaction: ProductGridLayout }); |