| 1 |
/* eslint-disable import/no-unresolved */ |
| 2 |
import { useEffect, useRef, useState } from "@wordpress/element"; |
| 3 |
import { Swiper, SwiperSlide } from "swiper/react"; |
| 4 |
import { breakpoint, buildFontClasses, inArray, paginationDotType, 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 |
} from "swiper/modules"; |
| 20 |
import TemplateOne from "../shared/templates/templateCards/templateOne"; |
| 21 |
import { SwiperNavButton, SwiperPagination } from "../shared/templates/templates-parts/templates-parts"; |
| 22 |
import "swiper/css"; |
| 23 |
import "swiper/css/navigation"; |
| 24 |
import "swiper/css/pagination"; |
| 25 |
import "swiper/css/free-mode"; |
| 26 |
import "swiper/css/autoplay"; |
| 27 |
import "swiper/css/effect-flip"; |
| 28 |
import "swiper/css/thumbs"; |
| 29 |
import classNames from "classnames"; |
| 30 |
import ThumbsTemplate from "../shared/templates/templateCards/thumbsTemplate"; |
| 31 |
|
| 32 |
const Render = ({ attributes, posts, pageType = "frontend" }) => { |
| 33 |
const { |
| 34 |
dynamicClassNames, |
| 35 |
carouselPauseOnHover, |
| 36 |
carouselArrowStyle, |
| 37 |
// postThumbnailNavigationArrow, |
| 38 |
carouselPaginationStyle, |
| 39 |
carouselAutoPlay, |
| 40 |
carouselAutoPlayDelay, |
| 41 |
carouselDirection, |
| 42 |
carouselSpeed, |
| 43 |
postThumbnailAnimationEffect, |
| 44 |
postThumbnailTabKeyNavigation, |
| 45 |
postThumbnailMouseWheelControl, |
| 46 |
postThumbnailAdaptiveHeight, |
| 47 |
postThumbnailFreeScrollMode, |
| 48 |
postThumbnailHeight, |
| 49 |
// postThumbnailPaginationDot, |
| 50 |
thumbnailSliderLayout, |
| 51 |
postThumbnailSlideToScroll, |
| 52 |
contentVerticalPosition, |
| 53 |
postThumbnailGap, |
| 54 |
postThumbnailItemsPerSlide, |
| 55 |
postThumbnailPosition, |
| 56 |
currentScreen, |
| 57 |
postThumbnailLoop, |
| 58 |
navArrowVisibilityOnHover, |
| 59 |
catTabCategoryPosition, |
| 60 |
uniqueId, |
| 61 |
carouselNavArrow, |
| 62 |
carouselPaginationDot, |
| 63 |
} = attributes; |
| 64 |
|
| 65 |
const [thumbsSwiper, setThumbsSwiper] = useState(null); |
| 66 |
const swiperDotsRef = useRef(null); |
| 67 |
const swiperNevNextRef = useRef(null); |
| 68 |
const swiperNevPrevRef = useRef(null); |
| 69 |
const containerRef = useRef(null); |
| 70 |
|
| 71 |
const deviceType = useDeviceType(); |
| 72 |
|
| 73 |
const thumbnailsSlidePerGroups = postThumbnailSlideToScroll?.device?.[deviceType]; |
| 74 |
|
| 75 |
const paginationDotStyle = paginationDotType(swiperDotsRef, carouselPaginationStyle, uniqueId); |
| 76 |
|
| 77 |
const bgPosition = "center"; |
| 78 |
const backgroundSize = "cover"; |
| 79 |
|
| 80 |
const { Desktop: desktopGap } = postThumbnailGap.device; |
| 81 |
|
| 82 |
// const { Desktop: desktopScroll, Tablet: tabletScroll, Mobile: mobileScroll } = postThumbnailSlideToScroll.device; |
| 83 |
const cardHeightClass = classNames(buildFontClasses("height", postThumbnailHeight)); |
| 84 |
|
| 85 |
const thumbnailItemsPerSlide = postThumbnailItemsPerSlide.device[breakpoint()]; |
| 86 |
const thumbnailsContentItem = ["thumbnail-slider-layout-three", "thumbnail-slider-layout-four"].includes( |
| 87 |
thumbnailSliderLayout |
| 88 |
) |
| 89 |
? ["image", "taxonomy", "title"] |
| 90 |
: ["image"]; |
| 91 |
const [selectedPostId, setSelectedPostId] = useState(null); |
| 92 |
|
| 93 |
useEffect(() => { |
| 94 |
const handleClickOutside = (event) => { |
| 95 |
if (containerRef.current && !containerRef.current.contains(event.target)) { |
| 96 |
setSelectedPostId(null); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
document.addEventListener("mousedown", handleClickOutside); |
| 101 |
return () => document.removeEventListener("mousedown", handleClickOutside); |
| 102 |
}, []); |
| 103 |
|
| 104 |
return ( |
| 105 |
<div |
| 106 |
className={`sp-smart-post-block-wrapper sp-smart-post-thumbnail-slider sp-smart-md-10px ${thumbnailSliderLayout} sp-thumbnail-${postThumbnailPosition} sp-cat-position-${catTabCategoryPosition}`} |
| 107 |
ref={containerRef} |
| 108 |
> |
| 109 |
<div className={"sp-smart-post-thumbnail-slide"}> |
| 110 |
{posts?.length > 0 && ( |
| 111 |
<Swiper |
| 112 |
slidesPerView={1} |
| 113 |
thumbs={{ swiper: thumbsSwiper }} |
| 114 |
simulateTouch={"editor" === pageType ? false : true} |
| 115 |
style={{ |
| 116 |
"--swiper-navigation-color": "#fff", |
| 117 |
"--swiper-pagination-color": "#fff", |
| 118 |
}} |
| 119 |
initialSlide={0} |
| 120 |
navigation={{ |
| 121 |
nextEl: swiperNevNextRef.current, |
| 122 |
prevEl: swiperNevPrevRef.current, |
| 123 |
}} |
| 124 |
// Pagination apply if not scrollbar. |
| 125 |
pagination={paginationDotStyle} |
| 126 |
scrollbar={carouselPaginationStyle === "scrollbar" ? { draggable: true } : false} |
| 127 |
loop={ |
| 128 |
postThumbnailLoop && thumbnailSliderLayout !== "thumbnail-slider-layout-five" |
| 129 |
? true |
| 130 |
: false && posts.length > 1 |
| 131 |
} |
| 132 |
slidesPerGroup={Math.min(thumbnailsSlidePerGroups, 10)} |
| 133 |
autoplay={ |
| 134 |
carouselAutoPlay |
| 135 |
? { |
| 136 |
delay: carouselAutoPlayDelay?.value, |
| 137 |
disableOnInteraction: false, |
| 138 |
pauseOnMouseEnter: carouselPauseOnHover, |
| 139 |
reverseDirection: carouselDirection === "left_to_right" ? true : false, |
| 140 |
} |
| 141 |
: false |
| 142 |
} |
| 143 |
speed={carouselSpeed?.value} |
| 144 |
// touchStartPreventDefault={ false } |
| 145 |
modules={[ |
| 146 |
Navigation, |
| 147 |
Pagination, |
| 148 |
Scrollbar, |
| 149 |
Autoplay, |
| 150 |
Keyboard, |
| 151 |
Mousewheel, |
| 152 |
EffectFade, |
| 153 |
EffectCoverflow, |
| 154 |
EffectFlip, |
| 155 |
EffectCube, |
| 156 |
FreeMode, |
| 157 |
Thumbs, |
| 158 |
]} |
| 159 |
className={`sp-smart-post-background-layout sp-swiper-${postThumbnailAnimationEffect} sp-smart-post-swiper ${thumbnailSliderLayout}`} |
| 160 |
effect={postThumbnailAnimationEffect} |
| 161 |
cubeEffect={ |
| 162 |
postThumbnailAnimationEffect === "cube" |
| 163 |
? { |
| 164 |
shadow: true, |
| 165 |
slideShadows: true, |
| 166 |
shadowOffset: 20, |
| 167 |
shadowScale: 0.94, |
| 168 |
} |
| 169 |
: false |
| 170 |
} |
| 171 |
coverflowEffect={ |
| 172 |
postThumbnailAnimationEffect === "coverflow" |
| 173 |
? { |
| 174 |
rotate: 50, |
| 175 |
stretch: 0, |
| 176 |
depth: 100, |
| 177 |
modifier: 1, |
| 178 |
slideShadows: true, |
| 179 |
} |
| 180 |
: false |
| 181 |
} |
| 182 |
keyboard={ |
| 183 |
postThumbnailTabKeyNavigation |
| 184 |
? { |
| 185 |
enabled: true, |
| 186 |
onlyInViewport: true, |
| 187 |
} |
| 188 |
: false |
| 189 |
} |
| 190 |
mousewheel={postThumbnailMouseWheelControl} |
| 191 |
autoHeight={postThumbnailAdaptiveHeight ? true : false} |
| 192 |
freeMode={postThumbnailFreeScrollMode || false} |
| 193 |
> |
| 194 |
{posts?.map((post, i) => { |
| 195 |
return ( |
| 196 |
<SwiperSlide key={i} className={`sp-slide-item`}> |
| 197 |
<TemplateOne |
| 198 |
key={post?.post_id} |
| 199 |
data={post} |
| 200 |
posts={posts} |
| 201 |
attributes={attributes} |
| 202 |
bgPosition={bgPosition} |
| 203 |
backgroundSize={backgroundSize} |
| 204 |
contentPosition={contentVerticalPosition} |
| 205 |
HeightClass={cardHeightClass} |
| 206 |
isSelected={selectedPostId === post?.post_id} |
| 207 |
onSelect={() => setSelectedPostId(post?.post_id)} |
| 208 |
/> |
| 209 |
</SwiperSlide> |
| 210 |
); |
| 211 |
})} |
| 212 |
{carouselNavArrow && |
| 213 |
inArray( |
| 214 |
[ |
| 215 |
"thumbnail-slider-layout-one", |
| 216 |
"thumbnail-slider-layout-four", |
| 217 |
"thumbnail-slider-layout-six", |
| 218 |
], |
| 219 |
thumbnailSliderLayout |
| 220 |
) && ( |
| 221 |
<SwiperNavButton |
| 222 |
style={carouselArrowStyle} |
| 223 |
navArrow={carouselNavArrow} |
| 224 |
swiperNavNextRef={swiperNevNextRef} |
| 225 |
swiperNavPrevRef={swiperNevPrevRef} |
| 226 |
props={{ |
| 227 |
dynamicClassNames, |
| 228 |
navArrowVisibilityOnHover, |
| 229 |
}} |
| 230 |
/> |
| 231 |
)} |
| 232 |
</Swiper> |
| 233 |
)} |
| 234 |
{carouselNavArrow && |
| 235 |
inArray( |
| 236 |
[ |
| 237 |
"thumbnail-slider-layout-two", |
| 238 |
"thumbnail-slider-layout-three", |
| 239 |
"thumbnail-slider-layout-five", |
| 240 |
], |
| 241 |
thumbnailSliderLayout |
| 242 |
) && ( |
| 243 |
<SwiperNavButton |
| 244 |
style={carouselArrowStyle} |
| 245 |
navArrow={carouselNavArrow} |
| 246 |
swiperNavNextRef={swiperNevNextRef} |
| 247 |
swiperNavPrevRef={swiperNevPrevRef} |
| 248 |
props={{ |
| 249 |
dynamicClassNames, |
| 250 |
navArrowVisibilityOnHover, |
| 251 |
}} |
| 252 |
/> |
| 253 |
)} |
| 254 |
</div> |
| 255 |
<div className={"sp-smart-post-thumbnail-thumb"}> |
| 256 |
{posts?.length > 0 && ( |
| 257 |
<Swiper |
| 258 |
onSwiper={setThumbsSwiper} |
| 259 |
loop={ |
| 260 |
postThumbnailLoop && |
| 261 |
thumbnailSliderLayout !== "thumbnail-slider-layout-five" && |
| 262 |
posts.length > 1 |
| 263 |
} |
| 264 |
spaceBetween={postThumbnailGap?.device?.[currentScreen] || desktopGap} |
| 265 |
slidesPerView={thumbnailItemsPerSlide} |
| 266 |
grid={ |
| 267 |
thumbnailSliderLayout === "thumbnail-slider-layout-five" ? { rows: 2, fill: "row" } : false |
| 268 |
} |
| 269 |
freeMode={true} |
| 270 |
watchSlidesProgress={true} |
| 271 |
touchStartPreventDefault={false} |
| 272 |
modules={[FreeMode, Navigation, Pagination, Thumbs, Grid]} |
| 273 |
className={`sp-smart-post-swiper2 ${ |
| 274 |
!["thumbnail-slider-layout-three", "thumbnail-slider-layout-four"].includes( |
| 275 |
thumbnailSliderLayout |
| 276 |
) |
| 277 |
? "sp-smart-post-background-layout" |
| 278 |
: "" |
| 279 |
}`} |
| 280 |
direction={ |
| 281 |
deviceType === "Desktop" && ["left", "right"].includes(postThumbnailPosition) |
| 282 |
? "vertical" |
| 283 |
: "horizontal" |
| 284 |
} |
| 285 |
slideToClickedSlide={true} |
| 286 |
> |
| 287 |
{posts?.map((post, index) => { |
| 288 |
return ( |
| 289 |
<SwiperSlide key={index} className={`sp-slide-item-thumb`}> |
| 290 |
<ThumbsTemplate |
| 291 |
key={post?.post_id} |
| 292 |
data={post} |
| 293 |
posts={posts} |
| 294 |
layout={thumbnailSliderLayout} |
| 295 |
attributes={attributes} |
| 296 |
thumbIndex={""} |
| 297 |
contentArray={thumbnailsContentItem} |
| 298 |
/> |
| 299 |
</SwiperSlide> |
| 300 |
); |
| 301 |
})} |
| 302 |
</Swiper> |
| 303 |
)} |
| 304 |
</div> |
| 305 |
{carouselPaginationDot && carouselPaginationStyle !== "scrollbar" && ( |
| 306 |
<SwiperPagination swiperDotsRef={swiperDotsRef} bulletStyle={carouselPaginationStyle} /> |
| 307 |
)} |
| 308 |
</div> |
| 309 |
); |
| 310 |
}; |
| 311 |
|
| 312 |
export default Render; |
| 313 |
|