{"version":3,"file":"CMSRandomize.CaBUbHRT.mjs","names":["useState","useMemo"],"sources":["https:/framerusercontent.com/modules/M4PVdeA5WjttkTYxo9ar/Q74xGqdx32rp5KZxAhax/CMSRandomize.js"],"sourcesContent":["import{jsx as _jsx,jsxs as _jsxs}from\"react/jsx-runtime\";import{useState,useEffect,useMemo,cloneElement}from\"react\";import{addPropertyControls,ControlType,RenderTarget,// @ts-ignore\nuseQueryData,// @ts-ignore\nuseLoadMorePaginatedQuery}from\"framer\";import{getCollectionData}from\"https://framer.com/m/CMSLibrary-vH7O.js\";import{Message}from\"https://framerusercontent.com/modules/bWuIUhIQLpjdL0ydQqRy/3nMJlk7029Qui8lmEzG8/Message.js\";/**\n * @framerSupportedLayoutWidth any-prefer-fixed\n * @framerSupportedLayoutHeight auto\n */export default function CMSRandomize(props){const{addLoadingPlaceholder,loadingPlaceholderComponent,columns,gap,padding,randomize,saveRandomizationOrder,collectionName,limit,hasLoadMore}=props;/* Checks */const currentTarget=RenderTarget.current();const isCanvas=currentTarget===RenderTarget.canvas||currentTarget===RenderTarget.export;const collectionList=props.collectionList?.[0];const pageSize=collectionList?.props?.children?.props?.children?.props?.pageSize;const queryRaw=collectionList?.props?.children?.props?.children?.props?.query;if(queryRaw){collectionList.props.children.props.children.props.query.limit={type:\"LiteralValue\",value:limit};}const placeholderItems=[];if(addLoadingPlaceholder){for(let i=0;i<limit;i++){if(loadingPlaceholderComponent?.[0]){const clonedElement=/*#__PURE__*/cloneElement(loadingPlaceholderComponent[0],{key:`placeholder-${i}`,width:\"100%\",style:{width:\"100%\",...loadingPlaceholderComponent[0]?.props?.style??{}}});placeholderItems.push(clonedElement);}}}const[items,setItems]=useState(placeholderItems);const[allItems,setAllItems]=useState([]);const[loadCount,setLoadCount]=useState(1);const[loadMoreBtn,setLoadMoreBtn]=useState(null);const[moreItemsMessage,setMoreItemsMessage]=useState(null);// Get collection data\nconst{query,collections,childrenFunction}=useMemo(()=>getCollectionData(collectionList),[collectionList]);const collectionAlias=collections[0]?.alias;const{paginatedQuery,paginationInfo,loadMore}=!isCanvas?useLoadMorePaginatedQuery(query,pageSize,collectionAlias):\"\";const collectionData=!isCanvas?useQueryData(paginatedQuery):[];const children=!isCanvas?childrenFunction(collectionData,paginationInfo,loadMore):[];const loadMoreClick=()=>{const newCount=loadCount+1;setLoadCount(newCount);const newItems=allItems.slice(0,limit*newCount);setItems(newItems);};// Process CMS Data\nuseEffect(()=>{if(isCanvas)return;// Immediately show loading placeholders\nsetItems(placeholderItems);if(!collectionData||!Array.isArray(collectionData))return;let rawItems=[];if(hasLoadMore){rawItems=children.props.children[0];setLoadMoreBtn(children.props.children[1]);setMoreItemsMessage(children.props.children[2]);}else{rawItems=children.props.children;}if(randomize){if(saveRandomizationOrder){const storageKey=`CMSRandomize:${collectionName}`;const stored=sessionStorage.getItem(storageKey);if(stored){const keys=JSON.parse(stored);rawItems.sort((a,b)=>{const ia=keys.indexOf(a.key);const ib=keys.indexOf(b.key);if(ia===-1&&ib===-1)return 0;if(ia===-1)return 1;if(ib===-1)return-1;return ia-ib;});}else{rawItems=shuffleArray(rawItems);const keys=rawItems.map(item=>item.key);sessionStorage.setItem(storageKey,JSON.stringify(keys));}}else{rawItems=shuffleArray(rawItems);}}setAllItems(rawItems);setItems(rawItems.slice(0,limit));},[randomize,saveRandomizationOrder,collectionName,hasLoadMore]);const allShown=items.length>=allItems.length;return /*#__PURE__*/_jsx(\"div\",{style:{width:\"100%\",display:\"flex\",justifyContent:\"center\"},children:/*#__PURE__*/_jsxs(\"div\",{style:{width:\"100%\",display:hasLoadMore?\"flex\":\"\",flexDirection:hasLoadMore?\"column\":\"\",gap:hasLoadMore?gap:\"gap\",padding:padding},children:[/*#__PURE__*/_jsx(\"div\",{className:isCanvas?\"\":collectionList?.props?.className,style:{width:\"100%\",gridTemplateColumns:`repeat(${columns},minmax(50px,1fr))`,gap:gap,padding:\"0px\"},children:isCanvas?/*#__PURE__*/_jsx(Message,{title:\"Preview site to see results\",subtitle:\"The randomized CMS collection is not available in the editor. Preview or publish your site to see the results.\"}):items}),hasLoadMore&&!allShown?/*#__PURE__*/_jsx(\"div\",{style:{display:\"flex\",justifyContent:\"start\",width:\"100%\"},children:/*#__PURE__*/_jsx(\"div\",{style:{width:\"fit-content\"},onClick:loadMoreClick,children:loadMoreBtn})}):loadCount>1?/*#__PURE__*/_jsx(\"div\",{style:{display:\"flex\",justifyContent:\"center\",width:\"100%\"},children:/*#__PURE__*/_jsx(\"div\",{children:moreItemsMessage})}):\"\"]})});}function shuffleArray(array){return array.map(value=>({value,sort:Math.random()})).sort((a,b)=>a.sort-b.sort).map(({value})=>value);}addPropertyControls(CMSRandomize,{collectionList:{type:ControlType.ComponentInstance,title:\"CMS Collection\"},addLoadingPlaceholder:{type:ControlType.Boolean,title:\"Loading Placeholder\",defaultValue:false,description:\"Link a component that will appear briefly as a placeholder while the real items are still loading.\"},loadingPlaceholderComponent:{type:ControlType.ComponentInstance,title:\"Loading Placeholder\",hidden(props){return!props.addLoadingPlaceholder;}},columns:{type:ControlType.Number,title:\"Grid Columns\",displayStepper:true,defaultValue:1},gap:{type:ControlType.Gap,title:\"Gap\",defaultValue:\"24px\"},padding:{type:ControlType.Padding,title:\"Padding\",defaultValue:\"0px\"},randomize:{type:ControlType.Boolean,title:\"Randomize\",defaultValue:false},saveRandomizationOrder:{type:ControlType.Boolean,title:\"Save Randomization Order\",defaultValue:false,description:\"When enabled, the randomized order of items is remembered during the current page session (using session storage). This ensures that switching tabs or re-rendering the component will keep the same randomized order. When disabled, items are reshuffled each time the component is rendered.\",hidden(props){return!props.randomize;}},collectionName:{type:ControlType.String,title:\"Collection Name\",defaultValue:\"collection\",description:\"A unique name used to store the randomized order in session storage. If multiple randomized collections exist on the same page, each should have a different name so their saved orders don’t overwrite each other.\",hidden(props){return!props.saveRandomizationOrder;}},limit:{type:ControlType.Number,title:\"Limit to\",defaultValue:20,min:0},hasLoadMore:{type:ControlType.Boolean,title:\"Paginate Load More\",defaultValue:false,description:\"Enables load more functionality. Make sure to disable pagination on the Collection. Include a Load More button inside the Collection (used internally), followed by a message element that will be shown when all items have been loaded.\"}});\nexport const __FramerMetadata__ = {\"exports\":{\"default\":{\"type\":\"reactComponent\",\"name\":\"CMSRandomize\",\"slots\":[],\"annotations\":{\"framerContractVersion\":\"1\",\"framerSupportedLayoutWidth\":\"any-prefer-fixed\",\"framerSupportedLayoutHeight\":\"auto\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./CMSRandomize.map"],"mappings":"yUAKG,SAAwB,EAAa,EAAM,CAAC,GAAK,CAAC,wBAAsB,8BAA4B,UAAQ,MAAI,UAAQ,YAAU,yBAAuB,iBAAe,QAAM,eAAa,EAAwB,EAAc,EAAa,QAAQ,EAAQ,EAAS,IAAgB,EAAa,QAAQ,IAAgB,EAAa,OAAa,EAAe,EAAM,iBAAiB,GAAS,EAAS,GAAgB,OAAO,UAAU,OAAO,UAAU,OAAO,SAAwB,GAAgB,OAAO,UAAU,OAAO,UAAU,OAAO,QAAmB,EAAe,MAAM,SAAS,MAAM,SAAS,MAAM,MAAM,MAAM,CAAC,KAAK,eAAe,MAAM,CAAK,GAAG,IAAM,EAAiB,CAAC,EAAE,GAAG,OAA2B,IAAI,EAAE,EAAE,EAAE,EAAM,IAAK,GAAG,IAA8B,GAAG,CAAC,IAAM,EAA2B,EAAa,EAA4B,GAAG,CAAC,IAAI,eAAe,IAAI,MAAM,OAAO,MAAM,CAAC,MAAM,OAAO,GAAG,EAA4B,EAAE,EAAE,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,EAAiB,KAAK,CAAa,CAAE,EAAG,GAAK,CAAC,EAAM,GAAUA,EAAS,CAAgB,EAAO,CAAC,EAAS,GAAaA,EAAS,CAAC,CAAC,EAAO,CAAC,EAAU,GAAcA,EAAS,CAAC,EAAO,CAAC,EAAY,GAAgBA,EAAS,IAAI,EAAO,CAAC,EAAiB,GAAqBA,EAAS,IAAI,EAChtC,CAAC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