| 1 |
import { useRef } from "@wordpress/element"; |
| 2 |
// eslint-disable-next-line import/no-unresolved |
| 3 |
import { Swiper, SwiperSlide } from "swiper/react"; |
| 4 |
import { Navigation, Autoplay, EffectFade, FreeMode } from "swiper/modules"; |
| 5 |
import Marquee from "react-fast-marquee"; |
| 6 |
import Template from "./template"; |
| 7 |
import TemplateOne from "./templateOne"; |
| 8 |
|
| 9 |
import { arrayChunk } from "../shared/helpFn"; |
| 10 |
import { NewsIcon, NewsIcon2, NewsIcon3, NewsIcon4, NewsIcon5 } from "../../icons/icons"; |
| 11 |
import { breakpoint, inArray } from "../../controls/controls"; |
| 12 |
// eslint-disable-next-line no-duplicate-imports |
| 13 |
import { Fragment } from "@wordpress/element"; |
| 14 |
import { SlideButton } from "./SlideButton"; |
| 15 |
import NewsTickerTypewriter from "./cutomTypewriter"; |
| 16 |
import classNames from "classnames"; |
| 17 |
|
| 18 |
const Render = ({ attributes, posts, swiperKey = "", renderKey = null, pageType = "frontend" }) => { |
| 19 |
const { |
| 20 |
// uniqueId, |
| 21 |
carouselPaginationStyle, |
| 22 |
displayStyle, |
| 23 |
tickerTitleGap, |
| 24 |
carouselAutoPlay, |
| 25 |
carouselAutoPlayDelay, |
| 26 |
carouselDirection, |
| 27 |
carouselPauseOnHover, |
| 28 |
carouselAnimationEffect, |
| 29 |
carouselTickerSpeed, |
| 30 |
HeadingLabel, |
| 31 |
tickerIconColor, |
| 32 |
tickerIconSource, |
| 33 |
tickerIconEnabled, |
| 34 |
tickerSeparatorEnable, |
| 35 |
tickerListStyle, |
| 36 |
tickerListStyleColor, |
| 37 |
tickerDate, |
| 38 |
tickerDateType, |
| 39 |
metaDateFormat, |
| 40 |
tickerImg, |
| 41 |
tickerImagePosition, |
| 42 |
carouselNavArrow, |
| 43 |
carouselArrowStyle, |
| 44 |
dynamicClassNames, |
| 45 |
navArrowVisibilityOnHover, |
| 46 |
tickerNavigation, |
| 47 |
tickerPause, |
| 48 |
tickerDivider, |
| 49 |
tickerTitleListStyleEnble, |
| 50 |
HeadingPosition, |
| 51 |
headingStyle, |
| 52 |
titleLength, |
| 53 |
newsTickerCarouselArrowColor, |
| 54 |
tickerImgShape, |
| 55 |
newsTickerItemToDisplay, |
| 56 |
headingGlobalTypography, |
| 57 |
tickerTitleGlobalTypography, |
| 58 |
} = attributes; |
| 59 |
// const swiperDotsRef = useRef(null); |
| 60 |
const spTickerRef = useRef(null); |
| 61 |
|
| 62 |
const swiperNevNextRef = useRef(null); |
| 63 |
const swiperNevPrevRef = useRef(null); |
| 64 |
|
| 65 |
const deviceType = breakpoint(); |
| 66 |
|
| 67 |
// const paginationDotStyle = paginationDotType(swiperDotsRef, carouselPaginationStyle); |
| 68 |
let AllPosts = posts; |
| 69 |
if (inArray(["fade"], carouselAnimationEffect)) { |
| 70 |
const columnWithDevice = newsTickerItemToDisplay.device?.[deviceType]; |
| 71 |
AllPosts = arrayChunk(posts, columnWithDevice); |
| 72 |
} |
| 73 |
|
| 74 |
if (inArray(["fade"], carouselAnimationEffect)) { |
| 75 |
const columnWithDevice = newsTickerItemToDisplay.device?.[deviceType]; |
| 76 |
AllPosts = arrayChunk(posts, columnWithDevice); |
| 77 |
} |
| 78 |
const itemToDisplay = newsTickerItemToDisplay.device?.[deviceType]; |
| 79 |
const templateOneAndTwo = (post) => { |
| 80 |
return <TemplateOne key={post?.post_id} data={post} attributes={attributes} />; |
| 81 |
}; |
| 82 |
|
| 83 |
const Icon = ({ iconSource }) => { |
| 84 |
switch (iconSource) { |
| 85 |
case "newsIcon": |
| 86 |
return <NewsIcon fillColor={tickerIconColor} />; |
| 87 |
case "newsIcon2": |
| 88 |
return <NewsIcon2 fillColor={tickerIconColor} />; |
| 89 |
case "newsIcon3": |
| 90 |
return <NewsIcon3 fillColor={tickerIconColor} />; |
| 91 |
case "newsIcon4": |
| 92 |
return <NewsIcon4 fillColor={tickerIconColor} />; |
| 93 |
case "newsIcon5": |
| 94 |
return <NewsIcon5 fillColor={tickerIconColor} />; |
| 95 |
default: |
| 96 |
return null; |
| 97 |
} |
| 98 |
}; |
| 99 |
const Heading = () => { |
| 100 |
return ( |
| 101 |
<div className={`ticker-heading sp-ticker-heading-${headingStyle}-${HeadingPosition}`}> |
| 102 |
<span className="ticker-heading-content"> |
| 103 |
{tickerIconEnabled && <Icon iconSource={tickerIconSource} />} |
| 104 |
|
| 105 |
{deviceType !== "Mobile" && ( |
| 106 |
<span |
| 107 |
className={classNames( |
| 108 |
"ticker-heading-label", |
| 109 |
headingGlobalTypography?.class ? headingGlobalTypography?.class : "" |
| 110 |
)} |
| 111 |
> |
| 112 |
{HeadingLabel} |
| 113 |
</span> |
| 114 |
)} |
| 115 |
</span> |
| 116 |
</div> |
| 117 |
); |
| 118 |
}; |
| 119 |
|
| 120 |
const loopContent = (slideType, allPosts) => { |
| 121 |
return allPosts?.map((post, i) => ( |
| 122 |
<SwiperSlide key={i} className={`sp-slide-item`}> |
| 123 |
{"fade" === slideType ? post?.map((data, j) => templateOneAndTwo(data, j)) : templateOneAndTwo(post, i)} |
| 124 |
</SwiperSlide> |
| 125 |
)); |
| 126 |
}; |
| 127 |
|
| 128 |
return ( |
| 129 |
<div className={`sp-smart-post-news-ticker sp-smart-post-block-wrapper sp-smart-post-carousel-${displayStyle}`}> |
| 130 |
<div className="sp-smart-post-swiper sp-relative" ref={spTickerRef}> |
| 131 |
{"slide" === displayStyle && AllPosts?.length > 0 && ( |
| 132 |
<> |
| 133 |
<Fragment key={renderKey ? renderKey : swiperKey}> |
| 134 |
<Heading /> |
| 135 |
<Swiper |
| 136 |
slidesPerView={itemToDisplay} |
| 137 |
simulateTouch={false} |
| 138 |
navigation={{ |
| 139 |
nextEl: swiperNevNextRef.current, |
| 140 |
prevEl: swiperNevPrevRef.current, |
| 141 |
}} |
| 142 |
grabCursor={false} |
| 143 |
loop={true} |
| 144 |
autoplay={ |
| 145 |
carouselAutoPlay && { |
| 146 |
delay: carouselAutoPlayDelay?.value, |
| 147 |
disableOnInteraction: false, |
| 148 |
pauseOnMouseEnter: carouselPauseOnHover, |
| 149 |
reverseDirection: ["left_to_right", "top_to_bottom"].includes(carouselDirection) |
| 150 |
? true |
| 151 |
: false, |
| 152 |
} |
| 153 |
} |
| 154 |
speed={carouselTickerSpeed?.value} |
| 155 |
spaceBetween={ |
| 156 |
carouselAnimationEffect === "cube" ? 0 : tickerTitleGap.device?.[deviceType] || 24 |
| 157 |
} |
| 158 |
modules={[Navigation, Autoplay, EffectFade, FreeMode]} // Include Autoplay in modules |
| 159 |
className={`slider-class swiper-${carouselAnimationEffect} sp-swiper-slide pagination-${carouselPaginationStyle} layout-${displayStyle}`} |
| 160 |
effect={carouselAnimationEffect} |
| 161 |
direction={ |
| 162 |
["top_to_bottom", "bottom_to_top"].includes(carouselDirection) |
| 163 |
? "vertical" |
| 164 |
: "horizontal" |
| 165 |
} |
| 166 |
> |
| 167 |
{inArray(["fade"], carouselAnimationEffect) && loopContent("fade", AllPosts)} |
| 168 |
{!inArray(["fade"], carouselAnimationEffect) && loopContent("slide", posts)} |
| 169 |
</Swiper> |
| 170 |
</Fragment> |
| 171 |
|
| 172 |
{tickerNavigation && deviceType !== "Mobile" && ( |
| 173 |
<SlideButton |
| 174 |
style={carouselArrowStyle} |
| 175 |
tickerPause={tickerPause} |
| 176 |
swiperNavNextRef={swiperNevNextRef} |
| 177 |
swiperNavPrevRef={swiperNevPrevRef} |
| 178 |
navArrow={carouselNavArrow} |
| 179 |
tickerDivider={tickerDivider} |
| 180 |
HeadingPosition={HeadingPosition} |
| 181 |
props={{ |
| 182 |
dynamicClassNames, |
| 183 |
|
| 184 |
navArrowVisibilityOnHover, |
| 185 |
}} |
| 186 |
newsTickerCarouselArrowColor={newsTickerCarouselArrowColor} |
| 187 |
// displayStyle={ displayStyle } |
| 188 |
// carouselAnimationEffect={ carouselAnimationEffect } |
| 189 |
/> |
| 190 |
)} |
| 191 |
</> |
| 192 |
)} |
| 193 |
|
| 194 |
{"typewriter" === displayStyle && ( |
| 195 |
<> |
| 196 |
<Heading /> |
| 197 |
{/* <img src={ postsThumbs?.[1] } /> */} |
| 198 |
<div className="sp-smart-post-card-content"> |
| 199 |
<div |
| 200 |
className={classNames( |
| 201 |
"sp-smart-post-ticker-title", |
| 202 |
tickerTitleGlobalTypography?.class ? tickerTitleGlobalTypography?.class : "" |
| 203 |
)} |
| 204 |
> |
| 205 |
<NewsTickerTypewriter |
| 206 |
posts={posts} |
| 207 |
limit={titleLength} |
| 208 |
className={"sp-smart-post-ticker-title-img-wrapper sp-d-flex sp-img-left"} |
| 209 |
showImage={tickerImg} |
| 210 |
/> |
| 211 |
</div> |
| 212 |
</div> |
| 213 |
</> |
| 214 |
)} |
| 215 |
|
| 216 |
{"ticker" === displayStyle && ( |
| 217 |
<> |
| 218 |
<Heading /> |
| 219 |
|
| 220 |
<Marquee |
| 221 |
pauseOnHover={carouselPauseOnHover} |
| 222 |
direction={carouselDirection === "left_to_right" ? "right" : "left"} |
| 223 |
className={`sp-smart-post-ticker${ |
| 224 |
tickerSeparatorEnable ? " sp-smart-ticker-separator" : "" |
| 225 |
}`} |
| 226 |
gradientWidth={300} |
| 227 |
speed={(1000 * 250) / carouselTickerSpeed.value} |
| 228 |
> |
| 229 |
{posts?.map((post, index) => ( |
| 230 |
<span |
| 231 |
key={post?.post_id} |
| 232 |
style={{ |
| 233 |
display: "inline-flex", |
| 234 |
alignItems: "center", |
| 235 |
}} |
| 236 |
className="sp-ticker-item" |
| 237 |
> |
| 238 |
<Template |
| 239 |
data={post} |
| 240 |
tickerListStyle={tickerListStyle} |
| 241 |
tickerListStyleColor={tickerListStyleColor} |
| 242 |
tickerDate={tickerDate} |
| 243 |
tickerDateType={tickerDateType} |
| 244 |
metaDateFormat={metaDateFormat} |
| 245 |
tickerImg={tickerImg} |
| 246 |
tickerImagePosition={tickerImagePosition} |
| 247 |
tickerTitleListStyleEnble={tickerTitleListStyleEnble} |
| 248 |
titleLength={titleLength} |
| 249 |
tickerImgShape={tickerImgShape} |
| 250 |
tickerTitleGlobalTypography={tickerTitleGlobalTypography} |
| 251 |
/> |
| 252 |
{tickerSeparatorEnable && ( |
| 253 |
// index !== posts?.length - 1 && |
| 254 |
<span className="ticker-separator">|</span> |
| 255 |
)} |
| 256 |
</span> |
| 257 |
))} |
| 258 |
</Marquee> |
| 259 |
</> |
| 260 |
)} |
| 261 |
</div> |
| 262 |
</div> |
| 263 |
); |
| 264 |
}; |
| 265 |
|
| 266 |
export default Render; |
| 267 |
|