|
2 | 2 | /**
|
3 | 3 | * @var \Magento\Catalog\Block\Product\View\Gallery $block
|
4 | 4 | */
|
| 5 | +$cloudinaryGalleryHelper = $this->helper('Cloudinary\Cloudinary\Helper\ProductGalleryHelper'); |
5 | 6 | ?>
|
6 |
| -<div id="<?= /* @escapeNotVerified */ $this->getCldPGid() ?>" |
7 |
| - class="cloudinary-product-gallery" |
8 |
| - data-mage-init='{"cloudinaryProductGallery": <?= $block->getCloudinaryPGOptions() ?>}' |
9 |
| - style="max-width:100%;margin:auto"></div> |
| 7 | + |
| 8 | +<?php if ($cloudinaryGalleryHelper->isHyvaThemeEnabled()): ?> |
| 9 | + <script> |
| 10 | + document.addEventListener("alpine:init", () => { |
| 11 | + Alpine.data("cloudinaryGallery", () => ({ |
| 12 | + cld_gallery: null, |
| 13 | + cld_mediaAssets: [], |
| 14 | + |
| 15 | + init() { |
| 16 | + this.loadCloudinary(() => { |
| 17 | + this.$nextTick(() => { |
| 18 | + this.setupGallery(); |
| 19 | + this.registerSwatchListener(); |
| 20 | + }); |
| 21 | + }); |
| 22 | + }, |
| 23 | + |
| 24 | + loadCloudinary(callback) { |
| 25 | + if (window.cloudinary && typeof cloudinary.galleryWidget === "function") { |
| 26 | + callback(); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + let script = document.createElement("script"); |
| 31 | + script.src = "https://product-gallery.cloudinary.com/latest/all.js"; |
| 32 | + script.async = true; |
| 33 | + script.onload = callback; |
| 34 | + document.head.appendChild(script); |
| 35 | + }, |
| 36 | + |
| 37 | + setupGallery() { |
| 38 | + let galleryElement = document.querySelector(`[data-selector="cloudinary-product-gallery"]`); |
| 39 | + if (!galleryElement) { |
| 40 | + setTimeout(() => this.setupGallery(), 500); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + let cldOptions = galleryElement.getAttribute("data-cloudinary-options"); |
| 45 | + if (!cldOptions) return; |
| 46 | + |
| 47 | + try { |
| 48 | + cldOptions = JSON.parse(cldOptions); |
| 49 | + } catch (e) { |
| 50 | + return; |
| 51 | + } |
| 52 | + this.cld_mediaAssets = cldOptions.cloudinaryPGoptions.mediaAssets || []; |
| 53 | + |
| 54 | + let options = { |
| 55 | + cloudinaryPGoptions: cldOptions.cloudinaryPGoptions || {}, |
| 56 | + cldPGid: cldOptions.cldPGid || galleryElement.id, |
| 57 | + mediaAssets: this.cld_mediaAssets, |
| 58 | + }; |
| 59 | + |
| 60 | + window.cloudinary_pg = window.cloudinary_pg || {}; |
| 61 | + let cldpgid = options.cldPGid; |
| 62 | + |
| 63 | + if (!window.cloudinary_pg[cldpgid]) { |
| 64 | + this.cld_gallery = window.cloudinary_pg[cldpgid] = cloudinary.galleryWidget(options.cloudinaryPGoptions); |
| 65 | + this.cld_gallery.render(); |
| 66 | + } else { |
| 67 | + this.cld_gallery = window.cloudinary_pg[cldpgid]; |
| 68 | + } |
| 69 | + }, |
| 70 | + |
| 71 | + registerSwatchListener() { |
| 72 | + document.querySelectorAll("label.swatch-option").forEach((label) => { |
| 73 | + if (!label.dataset.listenerAdded) { |
| 74 | + label.dataset.listenerAdded = "true"; |
| 75 | + label.addEventListener("click", () => { |
| 76 | + let input = label.querySelector("input.product-option-value-input"); |
| 77 | + if (input) { |
| 78 | + let selectedSwatch = input.value; |
| 79 | + let newImages = this.getProductImages(selectedSwatch); |
| 80 | + if (newImages.length > 0) { |
| 81 | + this.updateCloudinaryGallery(newImages); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + }); |
| 87 | + }, |
| 88 | + |
| 89 | + getProductImages(selectedSwatch) { |
| 90 | + const productIdElement = document.querySelector('input[name="product"]'); |
| 91 | + if (!productIdElement) return []; |
| 92 | + |
| 93 | + const productId = productIdElement.value; |
| 94 | + const functionName = `initConfigurableSwatchOptions_${productId}`; |
| 95 | + if (typeof window[functionName] !== "function") { |
| 96 | + setTimeout(() => this.getProductImages(selectedSwatch), 1000); |
| 97 | + return []; |
| 98 | + } |
| 99 | + |
| 100 | + let configData = window[functionName](); |
| 101 | + if (!configData?.optionConfig?.images) return []; |
| 102 | + |
| 103 | + let matchingProductId = Object.keys(configData.optionConfig.index).find( |
| 104 | + (id) => configData.optionConfig.index[id]["93"] == selectedSwatch |
| 105 | + ); |
| 106 | + |
| 107 | + return matchingProductId ? configData.optionConfig.images[matchingProductId] : []; |
| 108 | + }, |
| 109 | + |
| 110 | + extractImageName(url) { |
| 111 | + let fileNameWithExtension = url.substring(url.lastIndexOf("/") + 1); |
| 112 | + let firstChar = fileNameWithExtension.charAt(0); |
| 113 | + let secondChar = fileNameWithExtension.charAt(1); |
| 114 | + let publicId = `media/catalog/product/${firstChar}/${secondChar}/${fileNameWithExtension}`; |
| 115 | + return { publicId: publicId.replace("?_i=AB", ""), mediaType: "image" }; |
| 116 | + }, |
| 117 | + |
| 118 | + mergeMediaAssets(existingAssets, newAssets) { |
| 119 | + const existingPublicIds = existingAssets.map(asset => asset.publicId); |
| 120 | + return [ |
| 121 | + ...existingAssets, |
| 122 | + ...newAssets.filter(newAsset => !existingPublicIds.includes(newAsset.publicId)), |
| 123 | + ]; |
| 124 | + }, |
| 125 | + |
| 126 | + updateCloudinaryGallery(newImages) { |
| 127 | + if (!this.cld_gallery) { |
| 128 | + setTimeout(() => this.updateCloudinaryGallery(newImages), 500); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if (typeof this.cld_gallery.update !== "function") return; |
| 133 | + |
| 134 | + let newMediaAssets = newImages.map(image => this.extractImageName(image.full)); |
| 135 | + let updatedAssets = this.mergeMediaAssets(newMediaAssets, this.cld_mediaAssets); |
| 136 | + |
| 137 | + |
| 138 | + this.$nextTick(() => { |
| 139 | + try { |
| 140 | + this.cld_gallery.update({ mediaAssets: updatedAssets }); |
| 141 | + this.cld_gallery.setItem(0); |
| 142 | + } catch (error) {} |
| 143 | + }); |
| 144 | + } |
| 145 | + })); |
| 146 | + }); |
| 147 | + </script> |
| 148 | + |
| 149 | + <div x-data="cloudinaryGallery" |
| 150 | + data-cloudinary-options='<?= $block->getCloudinaryPGOptions() ?>' |
| 151 | + id="<?= /* @escapeNotVerified */ $this->getCldPGid() ?>" |
| 152 | + data-selector="cloudinary-product-gallery" |
| 153 | + class="w-full pt-6 md:pt-0 md:h-auto md:row-start-1 md:row-span-2 md:col-start-1" |
| 154 | + ></div> |
| 155 | + |
| 156 | +<?php else: ?> |
| 157 | + <div id="<?= /* @escapeNotVerified */ $this->getCldPGid() ?>" |
| 158 | + class="cloudinary-product-gallery" |
| 159 | + data-mage-init='{"cloudinaryProductGallery": <?= $block->getCloudinaryPGOptions() ?>}' |
| 160 | + style="max-width:100%;margin:auto"></div> |
| 161 | +<?php endif; ?> |
0 commit comments