@salla.sa/twilight-components
Version:
Salla Web Component
4 lines • 14.7 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import{r as t,h as s,H as i,a}from"./p-Dbv0I4re.js";import{S as e}from"./p-D4oPi1Ov.js";const o=class{constructor(s){t(this,s),this.size="md",this.color="dark",this.digits="auto",this.daysLabel=salla.lang.getWithDefault("pages.checkout.day","يوم"),this.hoursLabel=salla.lang.getWithDefault("pages.checkout.hour","ساعة"),this.minutesLabel=salla.lang.getWithDefault("pages.checkout.minute","دقيقة"),this.secondsLabel=salla.lang.getWithDefault("pages.checkout.second","ثانية"),this.endLabel=salla.lang.getWithDefault("pages.checkout.offer_ended","انتهت مدة العرض"),this.invalidDate=salla.lang.getWithDefault("blocks.buy_as_gift.incorrect_date","الرجاء إدخال الموعد بشكل صحيح"),this.offerEnded=!1,this.days=this.number(0),this.hours=this.number(0),this.minutes=this.number(0),this.seconds=this.number(0),salla.lang.onLoaded((()=>{this.daysLabel=salla.lang.getWithDefault("pages.checkout.day","يوم"),this.hoursLabel=salla.lang.getWithDefault("pages.checkout.hour","ساعة"),this.minutesLabel=salla.lang.getWithDefault("pages.checkout.minute","دقيقة"),this.invalidDate=salla.lang.getWithDefault("blocks.buy_as_gift.incorrect_date","الرجاء إدخال الموعد بشكل صحيح"),this.secondsLabel=salla.lang.getWithDefault("pages.checkout.second","ثانية"),this.endLabel=salla.lang.getWithDefault("pages.checkout.offer_ended","انتهت مدة العرض")})),this.date&&this.isValidDate(this.date)&&this.startCountDown()}async endCountDown(){clearInterval(this.countInterval),this.offerEnded=!0,this.days=this.number(0),this.hours=this.number(0),this.minutes=this.number(0),this.seconds=this.number(0)}componentWillLoad(){if("string"==typeof this.preOrder)try{this.normalizedPreOrder=JSON.parse(this.preOrder)}catch{this.normalizedPreOrder=void 0}else this.normalizedPreOrder=this.preOrder;this.normalizedPreOrder?.end_date&&(this.date=this.normalizedPreOrder.end_date,this.date&&this.isValidDate(this.date)&&this.startCountDown())}isValidDate(t){let s,i=t.includes("-"),a=t.split(" ");return s=i?a[0].replace(/-/g,"/"):a[0],!isNaN(Date.parse(s))}number(t){return salla.helpers.number(t,"en"===this.digits)}startCountDown(){let t=new Date(this.date.replace(/-/g,"/"));(this.endOfDay||1===this.date.split(" ").length)&&t.setHours(23,59,59,999);let s=t.getTime();this.countInterval=setInterval((()=>{let t=(new Date).getTime(),i=s-t;this.days=this.number(Math.floor(i/864e5)),this.hours=this.number(Math.floor(i%864e5/36e5)),this.minutes=this.number(Math.floor(i%36e5/6e4)),this.seconds=this.number(Math.floor(i%6e4/1e3)),i<0&&this.endCountDown()}),1e3)}renderCountDown(){return s("ul",{class:`s-count-down-list ${this.boxed?"s-count-down-boxed":""} ${this.offerEnded?"s-count-down-ended":""} s-count-down-${this.size} s-count-down-${this.color}`},s("li",{class:"s-count-down-item"},s("div",{class:"s-count-down-item-value"},this.seconds),this.labeled&&s("div",{class:"s-count-down-item-label"},this.secondsLabel)),s("li",{class:"s-count-down-item"},s("div",{class:"s-count-down-item-value"},this.minutes),this.labeled&&s("div",{class:"s-count-down-item-label"},this.minutesLabel)),s("li",{class:"s-count-down-item"},s("div",{class:"s-count-down-item-value"},this.hours),this.labeled&&s("div",{class:"s-count-down-item-label"},this.hoursLabel)),s("li",{class:"s-count-down-item"},s("div",{class:"s-count-down-item-value"},this.days),this.labeled&&s("div",{class:"s-count-down-item-label"},this.daysLabel)))}renderInvalidDate(){return s("div",{class:"s-count-down-text-center"},this.invalidDate)}renderOfferEnded(){return s("div",{class:"s-count-down-end-text"},this.endText?this.endText:this.endLabel)}renderPreOrderToBeAvailableOn(){return this.normalizedPreOrder?.availability_date&&this.isValidDate(this.normalizedPreOrder?.availability_date)?s("div",{class:"s-count-down-info-message"},s("i",{class:"sicon-info"}),s("span",null,salla.lang.getWithDefault("pages.products.expected_to_be","متوقَّع توفُّره بتاريخ:")),s("span",null,new Date(this.normalizedPreOrder?.availability_date).toLocaleDateString(salla.lang.locale,{weekday:"long",year:"numeric",month:"long",day:"numeric"}))):null}renderPreOrderCountDown(){return this.normalizedPreOrder?.activate_countdown?s("div",{class:"s-count-down-pre-order-container"},s("p",{class:"text-sm "},salla.lang.getWithDefault("pages.products.pre_order_ends_in","ينتهي الطلب المسبق خلال:")),this.renderCountDown()):null}renderPreOrder(){return s("div",null,"object"==typeof this.normalizedPreOrder&&this.normalizedPreOrder?.availability_date&&this.renderPreOrderToBeAvailableOn(),this.renderPreOrderCountDown())}renderPrefixText(){return this.prefixText?s("span",{class:"s-count-down-prefix-text"},this.prefixText):null}renderButton(){return this.withButton&&this.buttonText?s("salla-button",{color:"primary",size:"medium",href:this.buttonHref},this.buttonIcon?s("i",{class:this.buttonIcon}):null,s("span",null,this.buttonText)):null}renderContent(){return this.date?this.isValidDate(this.date)?this.preOrder?this.renderPreOrder():this.renderCountDown():this.renderInvalidDate():null}render(){return s(i,{key:"0cfdbb009d256be108123cabf3cb4311866bdc17",class:`s-count-down-wrapper ${this.preOrder&&this.isValidDate(this.date)?"s-count-down-pre-order":""} ${this.horizontal?"s-count-down-horizontal":""} ${this.withButton?"s-count-down-with-button":""}`},this.renderPrefixText(),this.renderContent(),this.offerEnded&&this.renderOfferEnded(),this.renderButton())}};o.style="";var r='\x3c!-- Generated by IcoMoon.io --\x3e\n<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">\n<title>heart</title>\n<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>\n</svg>\n';const c=class{constructor(s){if(t(this,s),salla.onReady((()=>{this.fitImageHeight=salla.config.get("store.settings.product.fit_type"),salla.wishlist.event.onAdded(((t,s)=>this.toggleFavoriteIcon(!0,s))),salla.wishlist.event.onRemoved(((t,s)=>this.toggleFavoriteIcon(!1,s))),this.placeholder=salla.url.asset(salla.config.get("theme.settings.placeholder"))})),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")})),this.product)try{this.productData="object"==typeof this.product?this.product:JSON.parse(this.product)}catch(t){salla.log("Bad json passed via product prop")}}initCircleBar(){let t=this.productData.quantity/(this.productData.quantity>100?2*this.productData.quantity:100)*100;this.pie.querySelector(".s-product-card-content-pie-svg-bar").style.strokeDashoffset=100-t}toggleFavoriteIcon(t=!0,s=null){s&&s!==this.productData.id||this.wishlistBtn?.classList.toggle("s-product-card-wishlist-added",t)}isInWishlist(){return salla.storage.get("salla::wishlist",[]).includes(this.productData?.id)}async handleWishlistClick(){const t=this.isInWishlist();this.toggleFavoriteIcon(!t);try{await salla.wishlist.toggle(this.productData.id)}catch{this.toggleFavoriteIcon(t)}}formatDate(t){let s=new Date(t);return`${s.getFullYear()}-${s.getMonth()+1}-${s.getDate()}`}getProductBadge(){return this.productData.promotion_title?s("div",{class:"s-product-card-promotion-title"},this.productData.promotion_title):this.showQuantity&&this.productData?.quantity?s("div",{class:"s-product-card-quantity"},this.remained," ",salla.helpers.number(this.productData?.quantity)):this.showQuantity&&this.productData?.is_out_of_stock?s("div",{class:"s-product-card-out-badge"},this.outOfStock):""}getPriceFormat(t){return t&&0!=t?salla.money(t):salla.config.get("store.settings.product.show_price_as_dash")?"-":""}getProductPrice(){return this.productData.is_on_sale?s("div",{class:"s-product-card-sale-price"},s("h4",{innerHTML:this.getPriceFormat(this.productData.sale_price)}),s("span",{innerHTML:this.getPriceFormat(this.productData?.regular_price)})):this.productData.starting_price?s("div",{class:"s-product-card-starting-price"},s("p",null,this.startingPrice),s("h4",{innerHTML:this.getPriceFormat(this.productData?.starting_price)})):s("h4",{class:"s-product-card-price",innerHTML:this.getPriceFormat(this.productData?.price)})}render(){const t={"s-product-card-entry":!0,"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},a=this.productData?.url?{href:this.productData.url,title:`Learn more about ${this.productData?.name}`}:{};return s(i,{key:"6c9b39e50f045c2511188597128546e440431b24",id:`product-${this.productData?.id}`,class:t},s("div",{key:"69813dd46d8df8298835d6de6faca959f2645c6b",class:this.fullImage?"s-product-card-image-full":"s-product-card-image"},s("a",{key:"67080662eb9a5ff4af903b6e57f029925565138b",...a},s("img",{key:"8e7a60fff16a8e4f7b27f19aae1c39a16ce8cadd",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&&s("a",{key:"648cf477ab6ded2d2c02dbf6957d68365524c847",...a,class:"s-product-card-overlay"}),this.horizontal||this.fullImage?"":s("salla-button",{shape:"icon",fill:"none",color:"light",loading:!1,"aria-label":"Add or remove to wishlist",ref:t=>this.wishlistBtn=t,class:"s-product-card-wishlist-btn animated",onClick:()=>this.handleWishlistClick()},s("span",{innerHTML:r}))),s("div",{key:"a2ed73abfe91993ded6e84f6f493ea50ee0899a9",class:"s-product-card-content"},this.isSpecial&&this.productData?.quantity?s("div",{class:"s-product-card-content-pie",ref:t=>this.pie=t},s("span",null,s("b",null,salla.helpers.number(this.productData?.quantity)),this.remained),s("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"-2 -1 36 34",class:"s-product-card-content-pie-svg"},s("circle",{cx:"16",cy:"16",r:"15.9155",class:"s-product-card-content-pie-svg-base"}),s("circle",{cx:"16",cy:"16",r:"15.9155",class:"s-product-card-content-pie-svg-bar"}))):"",s("div",{key:"09fd5336d5c9d454620a91be8271399b938bb1f4",class:{"s-product-card-content-main":!0,"s-product-card-content-extra-padding":this.isSpecial}},s("h3",{key:"c915604dafa02d6ba50e6e9933edbd57149e7a56",class:"s-product-card-content-title"},s("a",{key:"2b80635accb51545b72baf7ff57a115a62457613",...a},this.productData?.name)),this.productData?.subtitle&&!this.minimal?s("p",{class:"s-product-card-content-subtitle"},this.productData?.subtitle):""),!this.productData?.donation||this.minimal||this.fullImage?"":[s("salla-progress-bar",{donation:this.productData?.donation}),s("div",{class:"s-product-card-donation-input"},this.productData?.donation?.can_donate?[s("label",{htmlFor:"donation-amount"},this.donationAmount," ",s("span",null,"*")),s("input",{type:"text",onInput:t=>{salla.helpers.inputDigitsOnly(t.target),this.addBtn.donatingAmount=t.target.value},id:"donation-amount",name:"donating_amount",class:"s-form-control",placeholder:this.donationAmount})]:"")],s("div",{key:"12613a96e7d914ea156470305c28a7449a3dcb9b",class:{"s-product-card-content-sub":!0,"s-product-card-content-extra-padding":this.isSpecial}},this.getProductPrice(),this.productData?.rating?.stars&&!this.minimal?s("div",{class:"s-product-card-rating"},s("span",{innerHTML:e}),s("span",null,this.productData.rating.stars)):""),this.isSpecial&&this.productData.discount_ends?s("salla-count-down",{date:this.formatDate(this.productData.discount_ends),"end-of-day":!0,boxed:!0,labeled:!0}):"",this.hideAddBtn||this.compact?"":s("div",{class:"s-product-card-content-footer"},s("salla-add-product-button",{fill:"outline",width:"wide",ref:t=>this.addBtn=t,"product-id":this.productData.id,"product-status":this.productData.status,"product-type":this.productData.type},s("slot",{name:"add-to-cart-label"},this.productData.add_to_cart_label)),this.horizontal||this.fullImage?s("salla-button",{shape:"icon",fill:"none",color:"light",loading:!1,ref:t=>this.wishlistBtn=t,"aria-label":"Add or remove to wishlist",class:"s-product-card-wishlist-btn animated",onClick:()=>this.handleWishlistClick(),"data-id":"{{ product.id }}"},s("span",{class:"text-xl",innerHTML:r})):"")),!this.hideAddBtn&&this.compact?s("div",{class:"s-product-card-content-footer"},s("salla-add-product-button",{ref:t=>this.addBtn=t,"product-id":this.productData.id,"product-status":this.productData.status,"product-type":this.productData.type,class:"s-add-product-button-compact"},s("slot",{name:"add-to-cart-label"},this.productData.add_to_cart_label))):"")}componentDidLoad(){document.lazyLoadInstance?.update(this.host.querySelectorAll(".lazy")),this.productData?.quantity&&this.isSpecial&&this.initCircleBar(),!salla.config.isGuest()&&salla.storage.get("salla::wishlist",[]).includes(this.productData?.id)&&this.toggleFavoriteIcon()}static get assetsDirs(){return["assets"]}get host(){return a(this)}};c.style='.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}';export{o as salla_count_down,c as salla_product_card}