Advertisement
Guest User

Untitled

a guest
Jun 18th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Velo API Reference: https://www.wix.com/velo/reference/api-overview/introduction
  2.  
  3. import wixData from 'wix-data';
  4. import wixLocationFrontend from "wix-location-frontend";
  5.  
  6.  
  7. const searchItems = async () => {
  8.     try {
  9.         console.log("Running the script")
  10.        
  11.         const info = await $w("#productPage1").getProduct()
  12.         const firstImage = info.mainMedia
  13.         const files = await getAllFilesInSameFolder(firstImage)
  14.  
  15.         // Map the results to the gallery items format
  16.         const items = files.map(item => ({
  17.             src: item.fileUrl || item.iconImage, // Assuming 'image' is the field key for image URLs in your collection
  18.             type: "image", // REQUIRED field
  19.             title: item.originalFileName.split('.')[0] || item.originalFileName || "", // Optional, if you have a title
  20.             description: "" // Assuming 'description' is the field key for descriptions in your collection
  21.         }));
  22.  
  23.         if (items.length > 15)
  24.         {
  25.             $w("#section2").show()
  26.             $w("#gallery1").items = items;
  27.         }
  28.         else
  29.             $w("#section2").hide();
  30.         // $w("#productPage1").selec
  31.     } catch (error) {
  32.         console.error("Error loading gallery items:", error);
  33.     }
  34. }
  35.  
  36. async function getAllFilesInSameFolder(mediaUrl) {
  37.     try {
  38.         // Extract the media ID from the URL
  39.         const regex = /wix:image:\/\/v1\/([^\/]+)\//;
  40.         const match = mediaUrl.match(regex);
  41.  
  42.         if (!match || !match[1]) {
  43.             console.error("Could not extract media ID");
  44.             return [];
  45.         }
  46.  
  47.         const mediaId = match[1]; // e.g., b8a7f4_d1fa164b0bdc4af6a521a0c2eefb335b~mv2.png
  48.  
  49.         // console.log("Media ID found:",mediaId)
  50.  
  51.         // Step 1: Find the file with that media ID
  52.         const fileQuery = await wixData
  53.             .query("Media/Files")
  54.             // .limit(100000)
  55.             .eq("_id", mediaId)
  56.             .find();
  57.  
  58.         if (fileQuery.items[0] === undefined) {
  59.             console.error("No media file found for given ID");
  60.             return [];
  61.         }
  62.  
  63.         const file = fileQuery.items[0];
  64.         const folderId = file.parentFolderId;
  65.         // console.log("File found:", file)
  66.  
  67.         // Step 2: Get all files in the same folder
  68.         const siblingFilesQuery = await wixData
  69.             .query("Media/Files")
  70.             .eq("parentFolderId", folderId)
  71.             .find();
  72.  
  73.         // console.log(`Found ${siblingFilesQuery.totalCount} files in folder`);
  74.  
  75.         const filteredItems = siblingFilesQuery.items.filter(item => item.originalFileName && item.originalFileName.length < 20)
  76.         // console.log("original items", siblingFilesQuery.items)
  77.         return filteredItems;
  78.  
  79.     } catch (error) {
  80.         console.error("Error retrieving sibling files:", error);
  81.         return [];
  82.     }
  83. }
  84.  
  85.  
  86. $w.onReady(async () => {
  87.     await searchItems();
  88.     await wixLocationFrontend.onChange(searchItems);
  89. });
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement