Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Velo API Reference: https://www.wix.com/velo/reference/api-overview/introduction
- import wixData from 'wix-data';
- import wixLocationFrontend from "wix-location-frontend";
- const searchItems = async () => {
- try {
- console.log("Running the script")
- const info = await $w("#productPage1").getProduct()
- const firstImage = info.mainMedia
- const files = await getAllFilesInSameFolder(firstImage)
- // Map the results to the gallery items format
- const items = files.map(item => ({
- src: item.fileUrl || item.iconImage, // Assuming 'image' is the field key for image URLs in your collection
- type: "image", // REQUIRED field
- title: item.originalFileName.split('.')[0] || item.originalFileName || "", // Optional, if you have a title
- description: "" // Assuming 'description' is the field key for descriptions in your collection
- }));
- if (items.length > 15)
- {
- $w("#section2").show()
- $w("#gallery1").items = items;
- }
- else
- $w("#section2").hide();
- // $w("#productPage1").selec
- } catch (error) {
- console.error("Error loading gallery items:", error);
- }
- }
- async function getAllFilesInSameFolder(mediaUrl) {
- try {
- // Extract the media ID from the URL
- const regex = /wix:image:\/\/v1\/([^\/]+)\//;
- const match = mediaUrl.match(regex);
- if (!match || !match[1]) {
- console.error("Could not extract media ID");
- return [];
- }
- const mediaId = match[1]; // e.g., b8a7f4_d1fa164b0bdc4af6a521a0c2eefb335b~mv2.png
- // console.log("Media ID found:",mediaId)
- // Step 1: Find the file with that media ID
- const fileQuery = await wixData
- .query("Media/Files")
- // .limit(100000)
- .eq("_id", mediaId)
- .find();
- if (fileQuery.items[0] === undefined) {
- console.error("No media file found for given ID");
- return [];
- }
- const file = fileQuery.items[0];
- const folderId = file.parentFolderId;
- // console.log("File found:", file)
- // Step 2: Get all files in the same folder
- const siblingFilesQuery = await wixData
- .query("Media/Files")
- .eq("parentFolderId", folderId)
- .find();
- // console.log(`Found ${siblingFilesQuery.totalCount} files in folder`);
- const filteredItems = siblingFilesQuery.items.filter(item => item.originalFileName && item.originalFileName.length < 20)
- // console.log("original items", siblingFilesQuery.items)
- return filteredItems;
- } catch (error) {
- console.error("Error retrieving sibling files:", error);
- return [];
- }
- }
- $w.onReady(async () => {
- await searchItems();
- await wixLocationFrontend.onChange(searchItems);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement