{"version":3,"file":"CMSRandomize.CHGoWSzz.mjs","names":[],"sources":["https:/framerusercontent.com/modules/M4PVdeA5WjttkTYxo9ar/oUikngOg7PbyfbdCWfyc/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,gapObj,padding,randomize,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){rawItems=shuffleArray(rawItems);}setAllItems(rawItems);setItems(rawItems.slice(0,limit));},[randomize,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\":\"\",rowGap:hasLoadMore?gapObj.y:\"\",padding:padding},children:[/*#__PURE__*/_jsx(\"div\",{className:isCanvas?\"\":collectionList?.props?.className,style:{width:\"100%\",gridTemplateColumns:`repeat(${columns},minmax(50px,1fr))`,columnGap:gapObj.x,rowGap:gapObj.y,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},gapObj:{type:ControlType.Object,title:\"Gap\",controls:{x:{type:ControlType.Number,title:\"X\",displayStepper:true,defaultValue:24},y:{type:ControlType.Number,title:\"Y\",displayStepper:true,defaultValue:24}}},padding:{type:ControlType.Padding,title:\"Padding\",defaultValue:\"0px\"},randomize:{type:ControlType.Boolean,title:\"Randomize\",defaultValue:false},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\":{\"framerSupportedLayoutHeight\":\"auto\",\"framerSupportedLayoutWidth\":\"any-prefer-fixed\",\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./CMSRandomize.map"],"mappings":"mgBAKG,SAAwB,EAAa,EAAM,CAAC,GAAK,CAAC,wBAAsB,8BAA4B,UAAQ,SAAO,UAAQ,YAAU,QAAM,cAAY,CAAC,EAAwB,EAAc,EAAa,SAAS,CAAO,EAAS,IAAgB,EAAa,QAAQ,IAAgB,EAAa,OAAa,EAAe,EAAM,iBAAiB,GAAS,EAAS,GAA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