| 1 |
import { useEffect, useRef, useState, Fragment } from "@wordpress/element"; |
| 2 |
import { Swiper, SwiperSlide } from "swiper/react"; |
| 3 |
import { |
| 4 |
Navigation, |
| 5 |
Pagination, |
| 6 |
Grid, |
| 7 |
Autoplay, |
| 8 |
Scrollbar, |
| 9 |
Keyboard, |
| 10 |
Mousewheel, |
| 11 |
EffectFade, |
| 12 |
EffectCoverflow, |
| 13 |
EffectFlip, |
| 14 |
EffectCube, |
| 15 |
FreeMode, |
| 16 |
} from "swiper/modules"; |
| 17 |
import Marquee from "react-fast-marquee"; |
| 18 |
import TemplateOne from "../shared/templates/templateCards/templateOne"; |
| 19 |
import { arrayChunk, toggleEqualHeight } from "../shared/helpFn"; |
| 20 |
import { SwiperNavButton, SwiperPagination } from "../shared/templates/templates-parts/templates-parts"; |
| 21 |
import { breakpoint, buildFontClasses, inArray, paginationDotType } from "../../controls/controls"; |
| 22 |
import classNames from "classnames"; |
| 23 |
|
| 24 |
const Render = ({ attributes, posts, swiperKey = "", pageType = "frontend" }) => { |
| 25 |
const { |
| 26 |
uniqueId, |
| 27 |
carouselArrowStyle, |
| 28 |
carouselPaginationStyle, |
| 29 |
dynamicClassNames, |
| 30 |
carouselStyle, |
| 31 |
carouselColumn, |
| 32 |
carouselHeight, |
| 33 |
carouselGap, |
| 34 |
carouselAutoPlay, |
| 35 |
carouselSpeed, |
| 36 |
carouselAutoPlayDelay, |
| 37 |
carouselDirection, |
| 38 |
slideToScroll, |
| 39 |
carouselPauseOnHover, |
| 40 |
carouselAnimationEffect, |
| 41 |
carouselTabKeyNav, |
| 42 |
carouselMouseWheelControl, |
| 43 |
carouselNavArrow, |
| 44 |
carouselPaginationDot, |
| 45 |
carouselAdaptiveHeight, |
| 46 |
carouselFreeScrollMode, |
| 47 |
equalHeightEnable, |
| 48 |
contentVerticalPosition, |
| 49 |
infiniteLoop, |
| 50 |
imagePosition, |
| 51 |
carouselTickerSpeed, |
| 52 |
contentOrientation, |
| 53 |
currentScreen, |
| 54 |
navArrowVisibilityOnHover, |
| 55 |
catTabCategoryPosition, |
| 56 |
partialViewSlide, |
| 57 |
contentOnHover, |
| 58 |
} = attributes; |
| 59 |
|
| 60 |
const swiperDotsRef = useRef(null); |
| 61 |
const swiperNavNextRef = useRef(null); |
| 62 |
const swiperNavPrevRef = useRef(null); |
| 63 |
const spTickerRef = useRef(null); |
| 64 |
const [tickerWidth, setTickerWidth] = useState(spTickerRef && spTickerRef.current?.clientWidth); |
| 65 |
|
| 66 |
const isPartialView = carouselStyle === "standard" && partialViewSlide === true; |
| 67 |
|
| 68 |
const deviceType = breakpoint(); |
| 69 |
|
| 70 |
const paginationDotStyle = paginationDotType(swiperDotsRef, carouselPaginationStyle); |
| 71 |
|
| 72 |
let AllPosts = posts; |
| 73 |
if (inArray(["fade", "cube", "flip"], carouselAnimationEffect)) { |
| 74 |
let columnWithDevice = carouselColumn?.device[currentScreen] ? carouselColumn?.device[currentScreen] : 1; |
| 75 |
AllPosts = arrayChunk(posts, columnWithDevice); |
| 76 |
} |
| 77 |
|
| 78 |
const cardHeightClass = classNames(buildFontClasses("height", carouselHeight)); |
| 79 |
|
| 80 |
// Equalizing the post cards height. |
| 81 |
useEffect(() => { |
| 82 |
setTimeout(() => { |
| 83 |
toggleEqualHeight(uniqueId, equalHeightEnable); |
| 84 |
}, 200); |
| 85 |
}, [equalHeightEnable, uniqueId, AllPosts, carouselStyle, contentOrientation]); |
| 86 |
|
| 87 |
const { Desktop: desktopScroll, Tablet: tabletScroll, Mobile: mobileScroll } = slideToScroll.device; |
| 88 |
|
| 89 |
const contentHeight = |
| 90 |
carouselHeight.device?.[deviceType] !== "" |
| 91 |
? carouselHeight.device?.[deviceType] + carouselHeight.unit?.[deviceType] |
| 92 |
: "auto"; |
| 93 |
|
| 94 |
const [selectedPostId, setSelectedPostId] = useState(null); |
| 95 |
|
| 96 |
useEffect(() => { |
| 97 |
const handleClickOutside = (event) => { |
| 98 |
if (spTickerRef.current && !spTickerRef.current.contains(event.target)) { |
| 99 |
setSelectedPostId(null); |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
document.addEventListener("mousedown", handleClickOutside); |
| 104 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 105 |
}, []); |
| 106 |
|
| 107 |
useEffect(() => { |
| 108 |
if (!spTickerRef.current) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
const element = spTickerRef.current; |
| 113 |
const resizeObserver = new ResizeObserver((entries) => { |
| 114 |
for (const entry of entries) { |
| 115 |
const newWidth = entry.contentRect.width; |
| 116 |
setTickerWidth(newWidth / carouselColumn.device?.[deviceType] - 20); |
| 117 |
} |
| 118 |
}); |
| 119 |
|
| 120 |
resizeObserver.observe(element); |
| 121 |
|
| 122 |
return () => resizeObserver.disconnect(); |
| 123 |
}, [deviceType, carouselColumn]); |
| 124 |
|
| 125 |
const templateOneAndTwo = (post) => { |
| 126 |
return ( |
| 127 |
<TemplateOne |
| 128 |
key={post?.post_id} |
| 129 |
spTickerRef={spTickerRef} |
| 130 |
data={post} |
| 131 |
posts={posts} |
| 132 |
attributes={attributes} |
| 133 |
contentPosition={contentVerticalPosition} |
| 134 |
contentHeight={contentHeight} |
| 135 |
HeightClass={cardHeightClass} |
| 136 |
isSelected={selectedPostId === post?.post_id} |
| 137 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 138 |
tickerWidth={tickerWidth} |
| 139 |
/> |
| 140 |
); |
| 141 |
}; |
| 142 |
|
| 143 |
const loopContent = (slideType, allPosts) => { |
| 144 |
return allPosts?.map((post, i) => ( |
| 145 |
<SwiperSlide key={i} className={`sp-slide-item`}> |
| 146 |
{"fade" === slideType ? post?.map((data, j) => templateOneAndTwo(data, j)) : templateOneAndTwo(post, i)} |
| 147 |
</SwiperSlide> |
| 148 |
)); |
| 149 |
}; |
| 150 |
|
| 151 |
return ( |
| 152 |
<div |
| 153 |
className={`sp-smart-post-carousel-two sp-smart-post-block-wrapper sp-smart-post-carousel-${carouselStyle} ${contentOrientation} img-position-${imagePosition} sp-cat-position-${catTabCategoryPosition} ${ |
| 154 |
"background" === imagePosition ? "sp-smart-post-background-layout" : "" |
| 155 |
} ${contentOnHover ? "sp-content-on-hover" : ""}`} |
| 156 |
> |
| 157 |
<div className="sp-smart-post-swiper sp-relative" ref={spTickerRef}> |
| 158 |
{"ticker" !== carouselStyle && AllPosts.length > 0 && ( |
| 159 |
<> |
| 160 |
<Fragment key={swiperKey}> |
| 161 |
<Swiper |
| 162 |
slidesPerView={Number(carouselColumn.device?.[deviceType]) + (isPartialView ? 0.3 : 0)} |
| 163 |
centeredSlides={"center" === carouselStyle || isPartialView ? true : false} |
| 164 |
watchSlidesProgress={true} |
| 165 |
simulateTouch={"editor" === pageType ? false : true} |
| 166 |
grid={carouselStyle === "multi_row" ? { rows: 2, fill: "row" } : false} |
| 167 |
navigation={{ |
| 168 |
nextEl: swiperNavNextRef.current, |
| 169 |
prevEl: swiperNavPrevRef.current, |
| 170 |
enabled: true, |
| 171 |
}} |
| 172 |
grabCursor={false} |
| 173 |
// Pagination apply if not scrollbar. |
| 174 |
pagination={paginationDotStyle} |
| 175 |
scrollbar={carouselPaginationStyle === "scrollbar" ? { draggable: true } : false} |
| 176 |
loop={ |
| 177 |
!infiniteLoop || |
| 178 |
carouselStyle === "multi_row" || |
| 179 |
desktopScroll > 1 || |
| 180 |
tabletScroll > 1 || |
| 181 |
mobileScroll > 1 |
| 182 |
? false |
| 183 |
: true |
| 184 |
} |
| 185 |
// Slide per group |
| 186 |
slidesPerGroup={slideToScroll.device?.[deviceType] || 1} |
| 187 |
autoplay={ |
| 188 |
carouselAutoPlay && { |
| 189 |
delay: carouselAutoPlayDelay?.value, |
| 190 |
disableOnInteraction: false, |
| 191 |
pauseOnMouseEnter: carouselPauseOnHover, |
| 192 |
reverseDirection: carouselDirection === "left_to_right" ? true : false, |
| 193 |
} |
| 194 |
} |
| 195 |
speed={carouselSpeed?.value} |
| 196 |
spaceBetween={ |
| 197 |
carouselAnimationEffect === "cube" |
| 198 |
? 0 |
| 199 |
: carouselGap.device?.[deviceType] || carouselGap.device?.Desktop |
| 200 |
} |
| 201 |
modules={[ |
| 202 |
Navigation, |
| 203 |
Pagination, |
| 204 |
Scrollbar, |
| 205 |
Grid, |
| 206 |
Autoplay, |
| 207 |
Keyboard, |
| 208 |
Mousewheel, |
| 209 |
EffectFade, |
| 210 |
EffectCoverflow, |
| 211 |
EffectFlip, |
| 212 |
EffectCube, |
| 213 |
FreeMode, |
| 214 |
]} // Include Autoplay in modules |
| 215 |
className={`swiper-${carouselAnimationEffect} sp-swiper-slide pagination-${carouselPaginationStyle} layout-${carouselStyle}`} |
| 216 |
effect={carouselAnimationEffect} |
| 217 |
cubeEffect={ |
| 218 |
carouselAnimationEffect === "cube" |
| 219 |
? { |
| 220 |
shadow: true, |
| 221 |
slideShadows: true, |
| 222 |
shadowOffset: 20, |
| 223 |
shadowScale: 0.94, |
| 224 |
} |
| 225 |
: false |
| 226 |
} |
| 227 |
coverflowEffect={ |
| 228 |
carouselAnimationEffect === "coverflow" |
| 229 |
? { |
| 230 |
rotate: 50, |
| 231 |
stretch: 0, |
| 232 |
depth: 100, |
| 233 |
modifier: 1, |
| 234 |
slideShadows: true, |
| 235 |
} |
| 236 |
: false |
| 237 |
} |
| 238 |
keyboard={ |
| 239 |
carouselTabKeyNav |
| 240 |
? { |
| 241 |
enabled: true, |
| 242 |
onlyInViewport: true, |
| 243 |
} |
| 244 |
: false |
| 245 |
} |
| 246 |
mousewheel={carouselMouseWheelControl} |
| 247 |
autoHeight={carouselAdaptiveHeight ? true : false} |
| 248 |
freeMode={carouselFreeScrollMode || false} |
| 249 |
direction="horizontal" |
| 250 |
> |
| 251 |
{inArray(["fade", "cube", "flip"], carouselAnimationEffect) && |
| 252 |
loopContent("fade", AllPosts)} |
| 253 |
{!inArray(["fade", "cube", "flip"], carouselAnimationEffect) && |
| 254 |
loopContent("slide", posts)} |
| 255 |
</Swiper> |
| 256 |
</Fragment> |
| 257 |
{carouselNavArrow && ( |
| 258 |
<SwiperNavButton |
| 259 |
style={carouselArrowStyle} |
| 260 |
navArrow={carouselNavArrow} |
| 261 |
props={{ |
| 262 |
dynamicClassNames, |
| 263 |
navArrowVisibilityOnHover, |
| 264 |
}} |
| 265 |
swiperNavNextRef={swiperNavNextRef} |
| 266 |
swiperNavPrevRef={swiperNavPrevRef} |
| 267 |
/> |
| 268 |
)} |
| 269 |
|
| 270 |
{carouselPaginationDot && carouselPaginationStyle !== "scrollbar" && ( |
| 271 |
<SwiperPagination |
| 272 |
uniqueId={uniqueId} |
| 273 |
bulletStyle={carouselPaginationStyle} |
| 274 |
swiperDotsRef={swiperDotsRef} |
| 275 |
/> |
| 276 |
)} |
| 277 |
</> |
| 278 |
)} |
| 279 |
{"ticker" === carouselStyle && ( |
| 280 |
<Marquee |
| 281 |
pauseOnHover={carouselPauseOnHover} |
| 282 |
direction={"left_to_right" === carouselDirection ? "right" : "left"} |
| 283 |
className="sp-smart-post-ticker" |
| 284 |
gradientWidth={300} |
| 285 |
speed={(1000 * 250) / carouselTickerSpeed.value} |
| 286 |
> |
| 287 |
{posts?.map((data, i) => templateOneAndTwo(data, i))} |
| 288 |
</Marquee> |
| 289 |
)} |
| 290 |
</div> |
| 291 |
</div> |
| 292 |
); |
| 293 |
}; |
| 294 |
|
| 295 |
export default Render; |
| 296 |
|