| 1 |
import { colorControls, jsonParse, randomSolidColor, stringTrim } from "../shared/helpFn"; |
| 2 |
import { useQueryTerms } from "./query"; |
| 3 |
import { hoverAnimate, useOverlay } from "./helpFunc"; |
| 4 |
import { useEffect, useState } from "@wordpress/element"; |
| 5 |
import { breakpoint } from "../../controls/controls"; |
| 6 |
import { usePanelBodyContext } from "../../context"; |
| 7 |
import classNames from "classnames"; |
| 8 |
|
| 9 |
export default function Render({ attributes }) { |
| 10 |
const { |
| 11 |
icon, |
| 12 |
taxonomyIconStyle, |
| 13 |
layout, |
| 14 |
titleEnable, |
| 15 |
noResultFoundText, |
| 16 |
countEnable, |
| 17 |
displayOverlyThum, |
| 18 |
displayOverlyHoverThum, |
| 19 |
hoverAnimation, |
| 20 |
excerptShow, |
| 21 |
excerptLength, |
| 22 |
imageEnable, |
| 23 |
imageOverlay, |
| 24 |
hoverEffect, |
| 25 |
afterCount, |
| 26 |
beforeCount, |
| 27 |
counterMultiColorBg, |
| 28 |
limit = 6, |
| 29 |
contentMultiColorBg, |
| 30 |
postCardBg, |
| 31 |
showHideDivider, |
| 32 |
postType, |
| 33 |
SelectTerms, |
| 34 |
taxonomyType, |
| 35 |
emptyCategory, |
| 36 |
allTaxonomyTerm, |
| 37 |
excludeTerms, |
| 38 |
titleGlobalTypography, |
| 39 |
excerptGlobalTypography, |
| 40 |
counterGlobalTypography, |
| 41 |
} = attributes; |
| 42 |
|
| 43 |
const { overlayColor, overlayHoverColor } = useOverlay(attributes); |
| 44 |
const [randomColor, setRandomColor] = useState(randomSolidColor(imageOverlay)); |
| 45 |
|
| 46 |
const { togglePanelBody } = usePanelBodyContext(); |
| 47 |
|
| 48 |
useEffect(() => { |
| 49 |
setRandomColor(randomSolidColor(imageOverlay)); |
| 50 |
}, [imageOverlay]); |
| 51 |
|
| 52 |
const terms = useQueryTerms( |
| 53 |
(attributes = { |
| 54 |
postType, |
| 55 |
SelectTerms, |
| 56 |
taxonomyType, |
| 57 |
emptyCategory, |
| 58 |
allTaxonomyTerm, |
| 59 |
excludeTerms, |
| 60 |
limit, |
| 61 |
}) |
| 62 |
); |
| 63 |
|
| 64 |
const { transformValue, initialValue } = hoverAnimate(hoverAnimation, layout); |
| 65 |
const isLayoutFiveSix = ["taxonomy-layout-five", "taxonomy-layout-six"].includes(layout); |
| 66 |
const placeholderUrl = `${sp_smart_post_block_localize.placeholderImg}public/assets/img/placeholder.png`; |
| 67 |
const deviceType = breakpoint(); |
| 68 |
|
| 69 |
const getThumbnail = (term) => term?.category_thumbnail || placeholderUrl; |
| 70 |
const getStyle = (term) => ({ |
| 71 |
"--bg-url": displayOverlyThum ? `url(${getThumbnail(term)})` : "none", |
| 72 |
"--hover-bg-url": displayOverlyHoverThum |
| 73 |
? `url(${getThumbnail(term)})` |
| 74 |
: colorControls(postCardBg.hover.style, postCardBg.hover.solidColor, postCardBg.hover.gradient), |
| 75 |
"--overlay-color": displayOverlyThum && overlayColor, |
| 76 |
"--overlay-hover-color": overlayHoverColor, |
| 77 |
}); |
| 78 |
|
| 79 |
const getTaxonomyClass = () => |
| 80 |
[ |
| 81 |
"sp-smart-post-taxonomy-info", |
| 82 |
layout !== "taxonomy-layout-one" && "sp-smart-post-taxonomy-info-overlay", |
| 83 |
displayOverlyThum && "sp-taxonomy-overlay", |
| 84 |
|
| 85 |
displayOverlyHoverThum && "sp-taxonomy-hover-overlay ", |
| 86 |
layout === "taxonomy-layout-one" && showHideDivider && "sp-smart-post-taxonomy-info-layout-one", |
| 87 |
] |
| 88 |
.filter(Boolean) |
| 89 |
.join(" "); |
| 90 |
|
| 91 |
const shouldShowExcerpt = |
| 92 |
excerptShow && |
| 93 |
[ |
| 94 |
"taxonomy-layout-one", |
| 95 |
"taxonomy-layout-two", |
| 96 |
"taxonomy-layout-three", |
| 97 |
"taxonomy-layout-five", |
| 98 |
"taxonomy-layout-six", |
| 99 |
].includes(layout); |
| 100 |
|
| 101 |
const shouldShowCountInName = countEnable && ["taxonomy-layout-four", "taxonomy-layout-eight"].includes(layout); |
| 102 |
|
| 103 |
const shouldShowBeforeAfterCount = ["taxonomy-layout-seven", "taxonomy-layout-eight"].includes(layout); |
| 104 |
|
| 105 |
const renderExcerpt = (term) => |
| 106 |
shouldShowExcerpt && ( |
| 107 |
<span |
| 108 |
className={classNames( |
| 109 |
"taxonomy-excerpt", |
| 110 |
excerptGlobalTypography?.class ? excerptGlobalTypography.class : "" |
| 111 |
)} |
| 112 |
onClick={(e) => togglePanelBody("excerpt", e)} |
| 113 |
> |
| 114 |
{stringTrim(term?.description, excerptLength)} |
| 115 |
</span> |
| 116 |
); |
| 117 |
|
| 118 |
const renderCount = (term) => |
| 119 |
countEnable && |
| 120 |
!shouldShowCountInName && ( |
| 121 |
<div |
| 122 |
className="sp-smart-post-taxonomy-count" |
| 123 |
style={{ |
| 124 |
background: counterMultiColorBg && term?.category_color, |
| 125 |
}} |
| 126 |
onClick={(e) => togglePanelBody("counter", e)} |
| 127 |
> |
| 128 |
<span className={counterGlobalTypography?.class ? counterGlobalTypography.class : ""}> |
| 129 |
{shouldShowBeforeAfterCount && beforeCount} |
| 130 |
{term?.count} |
| 131 |
{shouldShowBeforeAfterCount && afterCount}{" "} |
| 132 |
{layout === "taxonomy-layout-three" && |
| 133 |
deviceType !== "Mobile" && |
| 134 |
(term?.count <= 1 ? "Post" : "Posts")} |
| 135 |
</span> |
| 136 |
</div> |
| 137 |
); |
| 138 |
|
| 139 |
const renderTitle = (term) => |
| 140 |
titleEnable && ( |
| 141 |
<div className="sps-taxonomy-title-wrapper" onClick={(e) => togglePanelBody("title", e)}> |
| 142 |
{icon && <i style={{ zIndex: 2 }} className={`sp-icon-right-${taxonomyIconStyle}`} />} |
| 143 |
<span |
| 144 |
className={classNames( |
| 145 |
"sps-taxonomy-title-name", |
| 146 |
titleGlobalTypography?.class ? titleGlobalTypography.class : "" |
| 147 |
)} |
| 148 |
> |
| 149 |
{term?.name} |
| 150 |
</span> |
| 151 |
</div> |
| 152 |
); |
| 153 |
|
| 154 |
const renderFiveSixLayout = (term) => ( |
| 155 |
<div className="sp-smart-post-taxonomy-info taxonomy-layout-five-six sp-smart-post-card" key={term?.id}> |
| 156 |
<div |
| 157 |
className={`taxonomy-layout-five-six-img-div sp-smart-post-card-image img-${hoverEffect}`} |
| 158 |
onClick={(e) => togglePanelBody("general", e)} |
| 159 |
style={{ |
| 160 |
"--bg-url": `url(${getThumbnail(term)})`, |
| 161 |
"--hover-bg-url": `url(${getThumbnail(term)})`, |
| 162 |
}} |
| 163 |
> |
| 164 |
{imageEnable && ( |
| 165 |
<> |
| 166 |
<img |
| 167 |
className="taxonomy-layout-five-six-img" |
| 168 |
src={getThumbnail(term)} |
| 169 |
alt={getThumbnail(term)} |
| 170 |
/> |
| 171 |
{imageOverlay !== "noOverlay" && ( |
| 172 |
<div |
| 173 |
className={`taxonomy-image-overlay overlay-${imageOverlay} pointer-none`} |
| 174 |
style={{ background: randomColor }} |
| 175 |
/> |
| 176 |
)} |
| 177 |
</> |
| 178 |
)} |
| 179 |
</div> |
| 180 |
<div className="sp-smart-post-taxonomy-name"> |
| 181 |
<div onClick={(e) => togglePanelBody("title", e)}> |
| 182 |
<span className="sp-smart-post-taxonomy-icon" /> |
| 183 |
{titleEnable && ( |
| 184 |
<span |
| 185 |
className={classNames( |
| 186 |
"sps-taxonomy-title-name", |
| 187 |
titleGlobalTypography?.class ? titleGlobalTypography.class : "" |
| 188 |
)} |
| 189 |
> |
| 190 |
{term?.name} |
| 191 |
</span> |
| 192 |
)} |
| 193 |
{countEnable && ( |
| 194 |
<span className="sp-smart-post-taxonomy-count"> |
| 195 |
<span> |
| 196 |
{beforeCount} |
| 197 |
{term?.count} |
| 198 |
{afterCount} |
| 199 |
</span> |
| 200 |
</span> |
| 201 |
)} |
| 202 |
</div> |
| 203 |
{term?.description && renderExcerpt(term)} |
| 204 |
</div> |
| 205 |
</div> |
| 206 |
); |
| 207 |
|
| 208 |
const renderDefaultLayout = (term) => ( |
| 209 |
<div |
| 210 |
className={getTaxonomyClass()} |
| 211 |
style={{ |
| 212 |
...(layout !== "taxonomy-layout-three" ? getStyle(term) : {}), |
| 213 |
"--transformValue": transformValue, |
| 214 |
"--initialValue": initialValue, |
| 215 |
}} |
| 216 |
key={term?.id} |
| 217 |
> |
| 218 |
<div |
| 219 |
className={[ |
| 220 |
"sp-smart-post-taxonomy-name", |
| 221 |
layout === "taxonomy-layout-three" && "sp-smart-post-taxonomy-name-layout-three", |
| 222 |
displayOverlyThum && layout === "taxonomy-layout-three" && "sp-taxonomy-overlay-three", |
| 223 |
displayOverlyHoverThum && layout === "taxonomy-layout-three" && "sp-taxonomy-hover-overlay-three", |
| 224 |
] |
| 225 |
.filter(Boolean) |
| 226 |
|
| 227 |
.join(" ")} |
| 228 |
style={{ |
| 229 |
...(["taxonomy-layout-eight", "taxonomy-layout-four"].includes(layout) |
| 230 |
? // contentMultiColorBg |
| 231 |
{ background: term?.category_color || "#FFF" } |
| 232 |
: {}), |
| 233 |
...(layout === "taxonomy-layout-three" ? getStyle(term) : {}), |
| 234 |
}} |
| 235 |
> |
| 236 |
<span style={{ zIndex: 2 }} className="sp-smart-post-taxonomy-icon"></span> |
| 237 |
{renderTitle(term)} |
| 238 |
{shouldShowCountInName && ( |
| 239 |
<div |
| 240 |
className="sp-smart-post-taxonomy-count" |
| 241 |
style={{ |
| 242 |
background: counterMultiColorBg && term?.category_color, |
| 243 |
}} |
| 244 |
> |
| 245 |
<span> |
| 246 |
{shouldShowBeforeAfterCount && beforeCount} |
| 247 |
{term?.count} |
| 248 |
{layout === "taxonomy-layout-eight" && afterCount} |
| 249 |
</span> |
| 250 |
</div> |
| 251 |
)} |
| 252 |
{term?.description && renderExcerpt(term)} |
| 253 |
</div> |
| 254 |
{renderCount(term)} |
| 255 |
</div> |
| 256 |
); |
| 257 |
|
| 258 |
return ( |
| 259 |
<div className="sp-smart-post-taxonomy-render"> |
| 260 |
{terms.length > 0 ? ( |
| 261 |
terms.map((term) => (isLayoutFiveSix ? renderFiveSixLayout(term) : renderDefaultLayout(term))) |
| 262 |
) : ( |
| 263 |
<p className="sp-taxonomy-no-results">{noResultFoundText || "No result found."}</p> |
| 264 |
)} |
| 265 |
</div> |
| 266 |
); |
| 267 |
} |
| 268 |
|