| 1 |
/* eslint-disable import/no-unresolved */ |
| 2 |
import { useEffect, useRef, useState } from "@wordpress/element"; |
| 3 |
import { Swiper, SwiperSlide } from "swiper/react"; |
| 4 |
import { buildFontClasses, useDeviceType } from "../../controls/controls"; |
| 5 |
import { |
| 6 |
Autoplay, |
| 7 |
EffectCoverflow, |
| 8 |
EffectCube, |
| 9 |
EffectFade, |
| 10 |
EffectFlip, |
| 11 |
FreeMode, |
| 12 |
Grid, |
| 13 |
Keyboard, |
| 14 |
Mousewheel, |
| 15 |
Navigation, |
| 16 |
Pagination, |
| 17 |
Scrollbar, |
| 18 |
Thumbs, |
| 19 |
Controller, |
| 20 |
} from "swiper/modules"; |
| 21 |
import TemplateOne from "../shared/templates/templateCards/templateOne"; |
| 22 |
import { SwiperNavButton } from "../shared/templates/templates-parts/templates-parts"; |
| 23 |
import "swiper/css"; |
| 24 |
import "swiper/css/navigation"; |
| 25 |
import "swiper/css/pagination"; |
| 26 |
import "swiper/css/free-mode"; |
| 27 |
import "swiper/css/autoplay"; |
| 28 |
import "swiper/css/effect-flip"; |
| 29 |
import "swiper/css/thumbs"; |
| 30 |
import "swiper/css/controller"; |
| 31 |
import classNames from "classnames"; |
| 32 |
import ThumbsTemplate from "../shared/templates/templateCards/thumbsTemplate"; |
| 33 |
import { ProTopBar } from "../shared/helpFn"; |
| 34 |
|
| 35 |
const Render = ({ posts, attributes, pageType = "frontend" }) => { |
| 36 |
const { |
| 37 |
dynamicClassNames, |
| 38 |
carouselPauseOnHover, |
| 39 |
carouselArrowStyle, |
| 40 |
// thumbnailTwoNavArrow, |
| 41 |
carouselAutoPlay, |
| 42 |
carouselAutoPlayDelay, |
| 43 |
carouselDirection, |
| 44 |
carouselSpeed, |
| 45 |
thumbnailTwoSlideToScroll, |
| 46 |
thumbnailTwoAnimationEffect, |
| 47 |
thumbnailTwoTabKeyNavigation, |
| 48 |
thumbnailTwoMouseWheelControl, |
| 49 |
thumbnailTwoAdaptiveHeight, |
| 50 |
thumbnailTwoFreeScrollMode, |
| 51 |
thumbnailSliderTwoLayout, |
| 52 |
contentVerticalPosition, |
| 53 |
thumbnailTwoPosition, |
| 54 |
currentScreen, |
| 55 |
infiniteLoop, |
| 56 |
navArrowVisibilityOnHover, |
| 57 |
thumbnailItemsToShow, |
| 58 |
thumbnailSliderTwoHeight, |
| 59 |
thumbnailTwoItemsHeight, |
| 60 |
thumbnailProgressPosition, |
| 61 |
blockLayoutName, |
| 62 |
thumbnailTwoAlignment, |
| 63 |
catTabCategoryPosition, |
| 64 |
carouselNavArrow, |
| 65 |
blockName, |
| 66 |
} = attributes; |
| 67 |
|
| 68 |
const [thumbsSwiper, setThumbsSwiper] = useState(null); |
| 69 |
const [mainSwiper, setMainSwiper] = useState(null); |
| 70 |
const isInitialize = useRef(false); |
| 71 |
const swiperNevNextRef = useRef(null); |
| 72 |
const swiperNevPrevRef = useRef(null); |
| 73 |
const containerRef = useRef(null); |
| 74 |
const [selectedPostId, setSelectedPostId] = useState(null); |
| 75 |
|
| 76 |
const deviceType = useDeviceType(); |
| 77 |
const thumbnailsSlidePerGroups = thumbnailTwoSlideToScroll?.device?.[deviceType]; |
| 78 |
|
| 79 |
const bgPosition = "center"; |
| 80 |
const backgroundSize = "cover"; |
| 81 |
|
| 82 |
// const { Desktop: desktopScroll, Tablet: tabletScroll, Mobile: mobileScroll } = thumbnailTwoSlideToScroll.device; |
| 83 |
|
| 84 |
const cardHeightClass = classNames(buildFontClasses("height", thumbnailSliderTwoHeight)); |
| 85 |
// const thumbsHeight = ["thumbnail-slider-two-layout-four", "thumbnail-slider-two-layout-five"].includes( |
| 86 |
// blockLayoutName |
| 87 |
// ) |
| 88 |
// ? "auto" |
| 89 |
// : thumbnailTwoItemsHeight.device[currentScreen] + thumbnailTwoItemsHeight.unit[currentScreen]; |
| 90 |
|
| 91 |
const thumbnailItemsPerSlide = thumbnailItemsToShow.device?.[deviceType]; |
| 92 |
const thumbPosts = [...posts, ...posts.slice(0, thumbnailItemsPerSlide)]; |
| 93 |
|
| 94 |
const thumbsGap = (layout) => { |
| 95 |
const thumbsConfig = { |
| 96 |
"thumbnail-slider-two-layout-one": 0, |
| 97 |
"thumbnail-slider-two-layout-two": 48, |
| 98 |
"thumbnail-slider-two-layout-three": 20, |
| 99 |
"thumbnail-slider-two-layout-four": 12, |
| 100 |
"thumbnail-slider-two-layout-five": 24, |
| 101 |
}; |
| 102 |
return thumbsConfig[layout]; |
| 103 |
}; |
| 104 |
|
| 105 |
const handleMainSlideChange = (swiper) => { |
| 106 |
if (thumbnailSliderTwoLayout !== "thumbnail-slider-two-layout-three") { |
| 107 |
return; |
| 108 |
} |
| 109 |
if (!thumbsSwiper || thumbsSwiper.destroyed) { |
| 110 |
return; |
| 111 |
} |
| 112 |
const realIndex = swiper.realIndex; |
| 113 |
let thumbIndex; |
| 114 |
|
| 115 |
// if ( realIndex > posts?.length - (thumbnailItemsPerSlide + 1) ) { |
| 116 |
if (realIndex > posts?.length - 1) { |
| 117 |
thumbIndex = 1; |
| 118 |
} else { |
| 119 |
thumbIndex = realIndex + 1; |
| 120 |
} |
| 121 |
|
| 122 |
if (thumbsSwiper.activeIndex !== thumbIndex) { |
| 123 |
thumbsSwiper.slideTo(thumbIndex); |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
const thumbnailsContentItem = ["thumbnail-slider-two-layout-one", "thumbnail-slider-two-layout-two"].includes( |
| 128 |
thumbnailSliderTwoLayout |
| 129 |
) |
| 130 |
? ["title"] |
| 131 |
: ["image", "taxonomy", "title"]; |
| 132 |
|
| 133 |
useEffect(() => { |
| 134 |
if (thumbnailSliderTwoLayout === "thumbnail-slider-two-layout-three") { |
| 135 |
if (thumbsSwiper && !isInitialize.current) { |
| 136 |
thumbsSwiper.slideTo(1, 0); |
| 137 |
isInitialize.current = true; |
| 138 |
} |
| 139 |
} |
| 140 |
}, [thumbsSwiper, thumbnailSliderTwoLayout, deviceType]); |
| 141 |
|
| 142 |
useEffect(() => { |
| 143 |
if (thumbnailSliderTwoLayout === "thumbnail-slider-two-layout-three") { |
| 144 |
if (mainSwiper?.controller && thumbsSwiper) { |
| 145 |
mainSwiper.controller.control = thumbsSwiper; |
| 146 |
} |
| 147 |
} |
| 148 |
}, [mainSwiper?.controller, thumbsSwiper, thumbnailSliderTwoLayout, deviceType]); |
| 149 |
|
| 150 |
useEffect(() => { |
| 151 |
if (mainSwiper && !mainSwiper.destroyed) { |
| 152 |
handleMainSlideChange(mainSwiper); |
| 153 |
} |
| 154 |
}, [mainSwiper]); |
| 155 |
|
| 156 |
useEffect(() => { |
| 157 |
const handleClickOutside = (event) => { |
| 158 |
if (containerRef.current && !containerRef.current.contains(event.target)) { |
| 159 |
setSelectedPostId(null); |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
document.addEventListener("mousedown", handleClickOutside); |
| 164 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 165 |
}, []); |
| 166 |
|
| 167 |
return ( |
| 168 |
<> |
| 169 |
<ProTopBar |
| 170 |
blockName={blockName} |
| 171 |
title="Thumbnails Slider 02" |
| 172 |
/> |
| 173 |
<div |
| 174 |
className={`sp-smart-post-block-wrapper sp-smart-post-thumbnail-slider-two sp-smart-thumbnail-slider-two sp-smart-md-10px ${thumbnailSliderTwoLayout} sp-thumbnail-${thumbnailTwoPosition} sp-cat-position-${catTabCategoryPosition}`} |
| 175 |
ref={containerRef} |
| 176 |
> |
| 177 |
<div className={"sp-smart-post-thumbnail-slide-two"}> |
| 178 |
{posts?.length > 0 && ( |
| 179 |
<Swiper |
| 180 |
slidesPerView={1} |
| 181 |
thumbs={{ swiper: thumbsSwiper }} |
| 182 |
onSwiper={setMainSwiper} |
| 183 |
onSlideChange={handleMainSlideChange} |
| 184 |
simulateTouch={"editor" === pageType ? false : true} |
| 185 |
style={{ |
| 186 |
"--swiper-navigation-color": "#fff", |
| 187 |
"--swiper-pagination-color": "#fff", |
| 188 |
}} |
| 189 |
initialSlide={0} |
| 190 |
navigation={{ |
| 191 |
nextEl: swiperNevNextRef.current, |
| 192 |
prevEl: swiperNevPrevRef.current, |
| 193 |
}} |
| 194 |
loop={ |
| 195 |
infiniteLoop |
| 196 |
// && |
| 197 |
// thumbnailSliderTwoLayout !== |
| 198 |
// 'thumbnail-slider-layout-five' |
| 199 |
// ? true |
| 200 |
// : false |
| 201 |
} |
| 202 |
slidesPerGroup={Math.min(thumbnailsSlidePerGroups, 10)} |
| 203 |
autoplay={ |
| 204 |
carouselAutoPlay |
| 205 |
? { |
| 206 |
delay: carouselAutoPlayDelay?.value, |
| 207 |
disableOnInteraction: false, |
| 208 |
pauseOnMouseEnter: carouselPauseOnHover, |
| 209 |
reverseDirection: carouselDirection === "left_to_right" ? true : false, |
| 210 |
} |
| 211 |
: false |
| 212 |
} |
| 213 |
speed={carouselSpeed?.value} |
| 214 |
// touchStartPreventDefault={ false } |
| 215 |
modules={[ |
| 216 |
Navigation, |
| 217 |
Pagination, |
| 218 |
Scrollbar, |
| 219 |
Autoplay, |
| 220 |
Keyboard, |
| 221 |
Mousewheel, |
| 222 |
EffectFade, |
| 223 |
EffectCoverflow, |
| 224 |
EffectFlip, |
| 225 |
EffectCube, |
| 226 |
FreeMode, |
| 227 |
Thumbs, |
| 228 |
Controller, |
| 229 |
]} |
| 230 |
className={`sp-swiper-${thumbnailTwoAnimationEffect} sp-smart-post-swiper ${thumbnailSliderTwoLayout} sp-smart-post-background-layout`} |
| 231 |
effect={thumbnailTwoAnimationEffect} |
| 232 |
cubeEffect={ |
| 233 |
thumbnailTwoAnimationEffect === "cube" |
| 234 |
? { |
| 235 |
shadow: true, |
| 236 |
slideShadows: true, |
| 237 |
shadowOffset: 20, |
| 238 |
shadowScale: 0.94, |
| 239 |
} |
| 240 |
: false |
| 241 |
} |
| 242 |
coverflowEffect={ |
| 243 |
thumbnailTwoAnimationEffect === "coverflow" |
| 244 |
? { |
| 245 |
rotate: 50, |
| 246 |
stretch: 0, |
| 247 |
depth: 100, |
| 248 |
modifier: 1, |
| 249 |
slideShadows: true, |
| 250 |
} |
| 251 |
: false |
| 252 |
} |
| 253 |
keyboard={ |
| 254 |
thumbnailTwoTabKeyNavigation |
| 255 |
? { |
| 256 |
enabled: true, |
| 257 |
onlyInViewport: true, |
| 258 |
} |
| 259 |
: false |
| 260 |
} |
| 261 |
mousewheel={thumbnailTwoMouseWheelControl} |
| 262 |
autoHeight={thumbnailTwoAdaptiveHeight ? true : false} |
| 263 |
freeMode={thumbnailTwoFreeScrollMode || false} |
| 264 |
> |
| 265 |
{posts?.map((post, i) => { |
| 266 |
return ( |
| 267 |
<SwiperSlide key={i} className={`sp-slide-item`}> |
| 268 |
<TemplateOne |
| 269 |
key={post?.post_id} |
| 270 |
data={post} |
| 271 |
posts={posts} |
| 272 |
attributes={attributes} |
| 273 |
bgPosition={bgPosition} |
| 274 |
backgroundSize={backgroundSize} |
| 275 |
contentPosition={contentVerticalPosition} |
| 276 |
HeightClass={cardHeightClass} |
| 277 |
isSelected={selectedPostId === post?.post_id} |
| 278 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 279 |
/> |
| 280 |
</SwiperSlide> |
| 281 |
); |
| 282 |
})} |
| 283 |
</Swiper> |
| 284 |
)} |
| 285 |
{carouselNavArrow && ( |
| 286 |
<SwiperNavButton |
| 287 |
style={carouselArrowStyle} |
| 288 |
navArrow={carouselNavArrow} |
| 289 |
swiperNavNextRef={swiperNevNextRef} |
| 290 |
swiperNavPrevRef={swiperNevPrevRef} |
| 291 |
props={{ |
| 292 |
dynamicClassNames, |
| 293 |
navArrowVisibilityOnHover, |
| 294 |
}} |
| 295 |
/> |
| 296 |
)} |
| 297 |
</div> |
| 298 |
<div |
| 299 |
className={`sp-smart-post-thumbnail-two-thumb ${ |
| 300 |
!["thumbnail-slider-two-layout-one", "thumbnail-slider-two-layout-five"].includes( |
| 301 |
thumbnailSliderTwoLayout |
| 302 |
) |
| 303 |
? "thumbnails-" + thumbnailTwoPosition |
| 304 |
: "" |
| 305 |
}${ |
| 306 |
thumbnailSliderTwoLayout === "thumbnail-slider-two-layout-two" |
| 307 |
? " progress-bar-" + thumbnailProgressPosition |
| 308 |
: "" |
| 309 |
}${ |
| 310 |
thumbnailSliderTwoLayout === "thumbnail-slider-two-layout-one" |
| 311 |
? "thumbs-align-" + thumbnailTwoAlignment |
| 312 |
: "" |
| 313 |
}`} |
| 314 |
> |
| 315 |
<Swiper |
| 316 |
onSwiper={setThumbsSwiper} |
| 317 |
loop={!!infiniteLoop && thumbPosts?.length > 1} |
| 318 |
// loop={ false } |
| 319 |
spaceBetween={thumbsGap(thumbnailSliderTwoLayout)} |
| 320 |
slidesPerView={thumbnailItemsPerSlide} |
| 321 |
freeMode={true} |
| 322 |
watchSlidesProgress={true} |
| 323 |
touchStartPreventDefault={false} |
| 324 |
modules={[FreeMode, Navigation, Pagination, Thumbs, Grid, Controller]} |
| 325 |
className="sp-smart-post-swiper2 sp-smart-post-background-layout" |
| 326 |
direction={ |
| 327 |
!["thumbnail-slider-two-layout-one", "thumbnail-slider-two-layout-three"].includes( |
| 328 |
thumbnailSliderTwoLayout |
| 329 |
) && ["left", "right"].includes(thumbnailTwoPosition) |
| 330 |
? "vertical" |
| 331 |
: "horizontal" |
| 332 |
} |
| 333 |
slideToClickedSlide={true} |
| 334 |
> |
| 335 |
{/* Thumbnails Thumbs */} |
| 336 |
{thumbPosts?.map((post, index) => { |
| 337 |
return ( |
| 338 |
<SwiperSlide key={index} className={`sp-slide-item-thumb`}> |
| 339 |
<ThumbsTemplate |
| 340 |
key={post?.post_id} |
| 341 |
data={post} |
| 342 |
posts={posts} |
| 343 |
layout={thumbnailSliderTwoLayout} |
| 344 |
attributes={attributes} |
| 345 |
thumbIndex={ |
| 346 |
thumbnailSliderTwoLayout === "thumbnail-slider-two-layout-one" ? index : "" |
| 347 |
} |
| 348 |
contentArray={thumbnailsContentItem} |
| 349 |
/> |
| 350 |
</SwiperSlide> |
| 351 |
); |
| 352 |
})} |
| 353 |
</Swiper> |
| 354 |
</div> |
| 355 |
</div> |
| 356 |
</> |
| 357 |
); |
| 358 |
}; |
| 359 |
|
| 360 |
export default Render; |
| 361 |
|