@salla.sa/twilight-components
Version:
Salla Web Component
274 lines (266 loc) • 18 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
'use strict';
var index = require('./index-uoA36zqH.js');
var star2 = require('./star2-R146a27p.js');
var Helper = require('./Helper-CU4Xuiki.js');
require('./anime.es-BqW8JHZi.js');
var Heart = `<!-- Generated by IcoMoon.io -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
<title>heart</title>
<path d="M23.333 2.267c-3.547 0-5.779 1.605-7.333 3.061-1.555-1.456-3.787-3.061-7.333-3.061-5.955 0-8.667 5.045-8.667 9.733 0 8.503 10.147 14.735 15.513 16.841 0.156 0.061 0.321 0.092 0.487 0.092s0.331-0.031 0.487-0.092c5.367-2.107 15.513-8.339 15.513-16.841 0-4.688-2.712-9.733-8.667-9.733zM16 26.161c-5.537-2.309-13.333-7.799-13.333-14.161 0-3.517 1.856-7.067 6-7.067 2.983 0 4.656 1.451 6.384 3.203 0.5 0.508 1.399 0.508 1.899 0 1.728-1.752 3.401-3.203 6.384-3.203 4.144 0 6 3.549 6 7.067 0 6.363-7.796 11.852-13.333 14.161z"></path>
</svg>
`;
const sallaProductCardCss = ".s-product-card-image::before{font-family:\"sallaicons\";content:\"\\ec1f\" !important}.s-product-card-content-pie-svg circle{transition:stroke-dashoffset 1s linear;-webkit-transition:stroke-dashoffset 1s linear;-moz-transition:stroke-dashoffset 1s linear;-ms-transition:stroke-dashoffset 1s linear;-o-transition:stroke-dashoffset 1s linear;stroke:#E8EDF2;stroke-width:2px;stroke-linecap:round;fill:none}.s-product-card-content-pie-svg-bar{stroke:var(--color-primary) !important;stroke-dasharray:100 100;stroke-dashoffset:100}";
const SallaProductCard = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
// Store configs
salla.onReady(() => {
this.fitImageHeight = salla.config.get('store.settings.product.fit_type');
salla.wishlist.event.onAdded((_res, id) => this.toggleFavoriteIcon(true, id));
salla.wishlist.event.onRemoved((_res, id) => this.toggleFavoriteIcon(false, id));
this.placeholder = salla.url.asset(salla.config.get('theme.settings.placeholder'));
});
// Language
salla.lang.onLoaded(() => {
this.remained = salla.lang.get('pages.products.remained');
this.donationAmount = salla.lang.get('pages.products.donation_amount');
this.startingPrice = salla.lang.get('pages.products.starting_price');
this.addToCart = salla.lang.get('pages.cart.add_to_cart');
this.outOfStock = salla.lang.get('pages.products.out_of_stock');
});
// Parse product data
if (!this.product) {
return;
}
try {
this.productData = typeof this.product == 'object' ? this.product : JSON.parse(this.product);
}
catch (e) {
salla.log('Bad json passed via product prop');
}
}
// Private Methods
initCircleBar() {
let qty = this.productData.quantity, total = this.productData.quantity > 100 ? this.productData.quantity * 2 : 100, roundPercent = (qty / total) * 100, bar = this.pie.querySelector('.s-product-card-content-pie-svg-bar'), strokeDashOffsetValue = 100 - roundPercent;
bar.style.strokeDashoffset = strokeDashOffsetValue;
}
toggleFavoriteIcon(isAdded = true, id = null) {
if (id && id !== this.productData.id) {
return;
}
this.wishlistBtn?.classList.toggle('s-product-card-wishlist-added', isAdded);
}
isInWishlist() {
return salla.storage.get('salla::wishlist', []).includes(this.productData?.id);
}
async handleWishlistClick() {
const wasInWishlist = this.isInWishlist();
this.toggleFavoriteIcon(!wasInWishlist);
try {
await salla.wishlist.toggle(this.productData.id);
}
catch {
this.toggleFavoriteIcon(wasInWishlist);
}
}
formatDate(date) {
let d = new Date(date);
return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`;
}
getProductBadge() {
if (this.productData.promotion_title) {
return index.h("div", { class: "s-product-card-promotion-title" }, this.productData.promotion_title);
}
if (this.showQuantity && this.productData?.quantity) {
return index.h("div", { class: "s-product-card-quantity" }, this.remained, " ", salla.helpers.number(this.productData?.quantity));
}
if (this.showQuantity && this.productData?.is_out_of_stock) {
return index.h("div", { class: "s-product-card-out-badge" }, this.outOfStock);
}
return '';
}
getPriceFormat(price) {
if (!price || price == 0) {
return salla.config.get('store.settings.product.show_price_as_dash') ? '-' : '';
}
return salla.money(price);
}
getProductPrice() {
if (this.productData.is_on_sale) {
return index.h("div", { class: "s-product-card-sale-price" }, index.h("h4", { innerHTML: this.getPriceFormat(this.productData.sale_price) }), index.h("span", { innerHTML: this.getPriceFormat(this.productData?.regular_price) }));
}
if (this.productData.starting_price) {
return index.h("div", { class: "s-product-card-starting-price" }, index.h("p", null, this.startingPrice), index.h("h4", { innerHTML: this.getPriceFormat(this.productData?.starting_price) }));
}
return index.h("h4", { class: "s-product-card-price", innerHTML: this.getPriceFormat(this.productData?.price) });
}
render() {
const classes = {
's-product-card-entry': true,
's-product-card-vertical': !this.horizontal && !this.fullImage && !this.minimal,
's-product-card-horizontal': this.horizontal && !this.fullImage && !this.minimal,
's-product-card-fit-height': this.fitImageHeight && !this.isSpecial && !this.fullImage && !this.minimal,
's-product-card-special': this.isSpecial,
's-product-card-full-image': this.fullImage,
's-product-card-minimal': this.minimal,
's-product-card-compact': this.compact,
's-product-card-donation': this.productData?.donation,
's-product-card-shadow': this.shadowOnHover,
's-product-card-out-of-stock': this.productData?.is_out_of_stock,
};
const hrefProp = this.productData?.url ? { href: this.productData.url, title: `Learn more about ${this.productData?.name}` } : {};
return (index.h(index.Host, { key: '07e7e2bfac3f3e744e288088af63451aaaccb9dd', id: `product-${this.productData?.id}`, class: classes }, index.h("div", { key: '950d7c19363a90dfd44ba12469b7e59c7726f73a', class: !this.fullImage ? 's-product-card-image' : 's-product-card-image-full' }, index.h("a", { key: '5df4385b43eebb43b54561f3cdca3d21ad21e110', ...hrefProp }, index.h("img", { key: 'be40dfaabea59a468537fa13438d0df11d7d4d32', class: `s-product-card-image-${salla.url.is_placeholder(this.productData?.image?.url)
? 'contain'
: this.fitImageHeight
? this.fitImageHeight
: 'cover'} lazy`, src: this.placeholder, alt: this.productData?.image?.alt || this.productData?.name, "data-src": this.productData?.image?.url || this.productData?.thumbnail }), !this.fullImage && !this.minimal ? this.getProductBadge() : ''), this.fullImage && index.h("a", { key: '405858af6e56aa685fdb5d869bdf3ce521d4dd0e', ...hrefProp, class: "s-product-card-overlay" }), !this.horizontal && !this.fullImage ?
index.h("salla-button", { shape: "icon", fill: "none", color: "light", loading: false, "aria-label": "Add or remove to wishlist", ref: el => this.wishlistBtn = el, class: "s-product-card-wishlist-btn animated", onClick: () => this.handleWishlistClick() }, index.h("span", { innerHTML: Heart })) : ''), index.h("div", { key: '82198e012d297a558557780355ffd16ce7241fa4', class: "s-product-card-content" }, this.isSpecial && this.productData?.quantity ?
index.h("div", { class: "s-product-card-content-pie", ref: pie => this.pie = pie }, index.h("span", null, index.h("b", null, salla.helpers.number(this.productData?.quantity)), this.remained), index.h("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "-2 -1 36 34", class: "s-product-card-content-pie-svg" }, index.h("circle", { cx: "16", cy: "16", r: "15.9155", class: "s-product-card-content-pie-svg-base" }), index.h("circle", { cx: "16", cy: "16", r: "15.9155", class: "s-product-card-content-pie-svg-bar" })))
: '', index.h("div", { key: 'cfc073356d10f50e94f8047241188625952e3157', class: { 's-product-card-content-main': true, 's-product-card-content-extra-padding': this.isSpecial } }, index.h("h3", { key: '8a49535a746e2608bb6d54224765ee876c9b43a6', class: "s-product-card-content-title" }, index.h("a", { key: '29567b47dccb5a41a916538a60d7caef1d60f59b', ...hrefProp }, this.productData?.name)), this.productData?.subtitle && !this.minimal ?
index.h("p", { class: "s-product-card-content-subtitle" }, this.productData?.subtitle)
: ''), this.productData?.donation && !this.minimal && !this.fullImage ?
[index.h("salla-progress-bar", { donation: this.productData?.donation }), index.h("div", { class: "s-product-card-donation-input" }, this.productData?.donation?.can_donate ?
[index.h("label", { htmlFor: "donation-amount" }, this.donationAmount, " ", index.h("span", null, "*")), index.h("input", { type: "text", onInput: e => {
salla.helpers.inputDigitsOnly(e.target);
this.addBtn.donatingAmount = e.target.value;
}, id: "donation-amount", name: "donating_amount", class: "s-form-control", placeholder: this.donationAmount })]
: '')]
: '', index.h("div", { key: 'c530bac8ba02e48672deb29819e487c3eaf1c2b6', class: { 's-product-card-content-sub': true, 's-product-card-content-extra-padding': this.isSpecial } }, this.getProductPrice(), this.productData?.rating?.stars && !this.minimal ?
index.h("div", { class: "s-product-card-rating" }, index.h("span", { innerHTML: star2.Star }), index.h("span", null, this.productData.rating.stars))
: ''), this.isSpecial && this.productData.discount_ends
? index.h("salla-count-down", { date: this.formatDate(this.productData.discount_ends), "end-of-day": true, boxed: true, labeled: true })
: '', !this.hideAddBtn && !this.compact ?
index.h("div", { class: "s-product-card-content-footer" }, index.h("salla-add-product-button", { fill: "outline", width: "wide", ref: el => this.addBtn = el, "product-id": this.productData.id, "product-status": this.productData.status, "product-type": this.productData.type }, index.h("slot", { name: "add-to-cart-label" }, this.productData.add_to_cart_label)), this.horizontal || this.fullImage ?
index.h("salla-button", { shape: "icon", fill: "none", color: "light", loading: false, ref: el => this.wishlistBtn = el, "aria-label": "Add or remove to wishlist", class: "s-product-card-wishlist-btn animated", onClick: () => this.handleWishlistClick(), "data-id": "{{ product.id }}" }, index.h("span", { class: "text-xl", innerHTML: Heart }))
: '')
: ''), !this.hideAddBtn && this.compact ?
index.h("div", { class: "s-product-card-content-footer" }, index.h("salla-add-product-button", { ref: el => this.addBtn = el, "product-id": this.productData.id, "product-status": this.productData.status, "product-type": this.productData.type, class: "s-add-product-button-compact" }, index.h("slot", { name: "add-to-cart-label" }, this.productData.add_to_cart_label)))
: ''));
}
componentDidLoad() {
document.lazyLoadInstance?.update(this.host.querySelectorAll('.lazy'));
if (this.productData?.quantity && this.isSpecial) {
this.initCircleBar();
}
if (!salla.config.isGuest() && salla.storage.get('salla::wishlist', []).includes(this.productData?.id)) {
this.toggleFavoriteIcon();
}
}
static get assetsDirs() { return ["assets"]; }
get host() { return index.getElement(this); }
};
SallaProductCard.style = sallaProductCardCss;
const sallaProductsSliderCss = "";
const SallaProductsSlider = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
/**
* Custom Card Component for the Salla Products Slider.
*
* This component allows you to customize the appearance of individual product cards within a Salla Products Slider.
*
* @example
* <salla-products-slider product-card-component="my-custom-card-style1" ...
* <salla-products-slider product-card-component="my-custom-card-style2" ...
*/
this.productCardComponent = 'custom-salla-product-card';
this.apiUrl = '';
}
componentWillLoad() {
return salla.onReady()
.then(() => {
//TODO:: check why `this.includes` not working!!
this.includes = Helper.Helper.parseJson(this.includes || this.host.getAttribute('includes'));
if (!Array.isArray(this.includes)) {
this.includes = null;
}
Helper.Helper.setIncludes(this.includes);
this.sourceValueIsValid = !!(this.getSourceValue() || this.isSourceWithoutValue());
if (!this.sourceValueIsValid) {
salla.logger.warn(`source-value prop is required for source [${this.getSource()}]`);
return;
}
this.hasCustomComponent = !!customElements.get(this.productCardComponent);
if (this.source === 'json') {
this.productsData = this.getSourceValue();
this.isReady = true;
return;
}
if ((this.getSource() == 'related' && !salla.config.get('store.settings.product.related_products_enabled'))) {
this.isReady = false;
return;
}
return salla.api.withoutNotifier(() => salla.product.api.fetch({
source: Helper.Helper.getApiSource(this.getSource()),
source_value: this.getSourceValue(),
limit: this.limit
}))
.then(response => Helper.Helper.injectExtraFieldsToResponse(response))
.then(response => {
this.productsData = response.data;
this.isReady = true;
response.source = this.getSource();
response.sourceValue = this.getSourceValue();
salla.event.emit('salla-products-slider::products.fetched', response.data);
});
});
}
componentDidRender() {
let processedCount = 0;
const intervalId = setInterval(() => {
this.host.querySelectorAll('[loading="lazy"]')?.forEach(img => img.removeAttribute('loading'));
processedCount++;
if (processedCount >= 10) {
clearInterval(intervalId);
}
}, 1000);
}
async componentDidLoad() {
await Salla.hooks.registerComponent('salla-products-slider', this);
}
isSourceWithoutValue() {
return ['offers', 'latest', 'sales', 'top-rated'].includes(this.getSource());
}
getItemHTML(product) {
//as a request they don't want to let the user to open the product details
//todo:: find a better way to handle this request
const isLandingPage = this.getSource() === 'landing-page';
isLandingPage && (product.url = '');
const handleClick = isLandingPage ? undefined : () => {
Helper.Helper.saveProductSource(this.getSource());
};
if (this.hasCustomComponent && this.productCardComponent.toLowerCase() == 'custom-salla-product-card') {
return index.h("div", { class: "s-products-slider-card", onClick: handleClick }, index.h("custom-salla-product-card", { product: product, source: this.getSource(), "source-value": this.getSourceValue() }));
}
if (this.hasCustomComponent) {
const customElem = document.createElement(this.productCardComponent);
customElem.setAttribute('product', JSON.stringify(product));
customElem.setAttribute('source', this.getSource());
customElem.setAttribute('source-value', this.getSourceValue());
return index.h("div", { class: "s-products-slider-card", onClick: handleClick, innerHTML: customElem.outerHTML });
}
return index.h("div", { class: "s-products-slider-card", onClick: handleClick }, index.h("salla-product-card", { "show-quantity": isLandingPage, "hide-add-btn": isLandingPage, "shadow-on-hover": true, product: product }));
}
canRender() {
return this.sourceValueIsValid && this.isReady;
}
getSource() {
return Helper.Helper.getProductsSource(this.source);
}
getSourceValue() {
return Helper.Helper.getProductsSourceValue(this.source, this.sourceValue);
}
render() {
if (!this.canRender()) {
return;
}
return (index.h(index.Host, { class: "s-products-slider-wrapper" }, index.h("salla-slider", { class: "s-products-slider-slider", id: this.sliderId || `s-products-slider-${Math.random().toString(36).substr(2, 9)}`, ...(this.sliderProps || {}), "auto-play": this.autoplay, type: "carousel", "block-title": this.blockTitle, "block-subTitle": this.subTitle, "display-all-url": this.displayAllUrl, sliderConfig: this.sliderConfig ? this.sliderConfig : null }, index.h("div", { slot: "items" }, this.productsData?.map(product => this.getItemHTML(product))))));
}
get host() { return index.getElement(this); }
};
SallaProductsSlider.style = sallaProductsSliderCss;
exports.salla_product_card = SallaProductCard;
exports.salla_products_slider = SallaProductsSlider;