UNPKG

@salla.sa/twilight-components

Version:
5 lines 9.51 kB
/*! * Crafted with ❤ by Salla */ import{r as registerInstance,h,g as getElement}from"./index-BrMYHkA5.js";var PageType;(function(e){e["ProductDetail"]="product.single";e["Cart"]="cart"})(PageType||(PageType={}));var OfferType;(function(e){e["Conditional"]="conditional";e["PercentageOrFixed"]="fixed";e["DiscountsTable"]="discounts_table";e["Bank"]="bank";e["BuyXGetY"]="buy_x_get_y"})(OfferType||(OfferType={}));var sallaOfferCss='.s-offer-wrapper .s-slider-block__title h2{font-size:1.125rem;line-height:1.75rem;color:#f87171}.s-offer-wrapper .s-slider-block__title h2::before{font-family:"sallaicons";content:"\\ee30" !important;position:absolute;top:1rem;font-size:3rem;font-weight:400;line-height:1;color:#fef2f2}.s-offer-bank-wrapper-sinlge-item{display:-ms-flexbox;display:flex;-ms-flex-align:center !important;align-items:center !important;gap:14px}.s-offer-bank-wrapper{display:-ms-flexbox !important;display:flex !important;width:100% !important}';var SallaOffer=function(){function e(e){var s=this;registerInstance(this,e);var r;this.productCardComponent="custom-salla-product-card";this.offersList=[];this.isMultipleBank=false;this.isBankOffer=false;this.canRender=false;this.showOffer=salla.config.get("store.settings.product.show_special_offers");this.offer_with_price_text=salla.lang.get("pages.offer.with_price",{price:""});this.with_discount_text=salla.lang.get("pages.products.with_a_discount");this.product_discount_text=salla.lang.get("pages.products.discount");this.special_offer_text=salla.lang.get("pages.products.special_offer");this.multipleBankOfferTitleText=salla.lang.get("pages.offer.multiple_bank_offers_title");this.multipleBankOfferTitleDescription=salla.lang.get("pages.offer.multiple_bank_offers_message");this.buy_quantity_text=function(e){return salla.lang.get("pages.offer.buy_quantity",{quantity:e})};salla.lang.onLoaded((function(){s.offer_with_price_text=salla.lang.get("pages.offer.with_price");s.with_discount_text=salla.lang.get("pages.products.with_a_discount");s.product_discount_text=salla.lang.get("pages.products.discount");s.special_offer_text=salla.lang.get("pages.products.special_offer");s.multipleBankOfferTitleText=salla.lang.get("pages.offer.multiple_bank_offers_title");s.multipleBankOfferTitleDescription=salla.lang.get("pages.offer.multiple_bank_offers_message");s.buy_quantity_text(0)}));salla.onReady((function(){s.currentPage=salla.config.get("page.slug");s.userCurrency=salla.config.get("currencies")[salla.config.get("user.currency_code")].symbol}));this.categorySlot=((r=this.host.querySelector('[slot="category"]'))===null||r===void 0?void 0:r.innerHTML)||'<a href={url} class="s-offer-slide-cat-entry"><i class={icon}></i><h4>{name}</h4></a>'}e.prototype.getEndpointByPageName=function(){var e;return(e={},e[PageType.Cart]="offers/cart/".concat(salla.storage.get("cart.id")),e[PageType.ProductDetail]="offers/product/".concat(salla.config.get("page.id")),e)[this.currentPage]||"offers"};e.prototype.componentWillLoad=function(){var e=this;this.hasCustomComponent=!!customElements.get(this.productCardComponent);return new Promise((function(e){return salla.onReady(e)})).then((function(){e.showOffer=!salla.url.is_page("product.single")||salla.config.get("store.settings.product.show_special_offers");if(e.showOffer){return}throw new Error("Merchant disabled showing the offers on product page")})).then((function(){return salla.api.request(e.getEndpointByPageName())})).then((function(s){if(!(e.offersList=s.data).length){throw new Error("salla-offers:: There is no offers!")}var r=e.offersList.find((function(e){return[OfferType.Bank,OfferType.BuyXGetY,OfferType.DiscountsTable].includes(e.type)}));if(!r){throw new Error("salla-offers:: Offer type not supported yet!")}if(r.type===OfferType.DiscountsTable){return e.offersList=[r]}if(e.isBankOffer=r.type===OfferType.Bank){e.offersList=e.offersList.filter((function(e){return e.type===OfferType.Bank}));e.isMultipleBank=e.offersList.length>1;return e.offersList}var t=r.details.get;return t.source==="products"?salla.product.api.fetch({source:"selected",source_value:t.source_value}).then((function(s){t.products=s.data;r.details.get=t;return e.offersList=[r]})):salla.product.api.categories().then((function(s){t.categories=e.findCategories(s.data,t.source_value);r.details.get=t;return e.offersList=[r]}))})).then((function(s){salla.storage.set(e.getStorageKey(),{offers:s,stored_at:(new Date).getTime()});e.canRender=true})).catch((function(e){salla.logger.warn(e)}))};e.prototype.componentDidLoad=function(){var e=this.host.querySelector(".s-slider-block__title-nav");e===null||e===void 0?void 0:e.classList.add("s-offer-bank-payment-nav")};e.prototype.findCategories=function(e,s){var r;var t=[];for(var a=0,n=e;a<n.length;a++){var i=n[a];if(s.includes(i.id_||i.id)){t.push(i)}if(((r=i.sub_categories)===null||r===void 0?void 0:r.length)>0){t=t.concat(this.findCategories(i.sub_categories,s))}}return t};e.prototype.getStorageKey=function(){try{var e=salla.config.get("page.slug").replace(".","_");var s=salla.lang.getLocale();var r=salla.config.currency().code;if(!e||!s||!r){throw new Error("Unable to get the storage key.")}return"s-offers-".concat(e,"-").concat(salla.config.get("page.id"),"-").concat(s,"-").concat(r)}catch(e){return""}};e.prototype.getOffersFromStorage=function(){var e=salla.storage.get(this.getStorageKey());if(!e||e.stored_at<(new Date).getTime()-10*60*1e3){salla.storage.remove(this.getStorageKey());return null}this.canRender=true;return Promise.resolve(e.offers)};e.prototype.render=function(){if(!this.offersList.length||!this.canRender||!this.showOffer)return null;var e=this.offersList[0];var s=this.isBankOffer?this.isMultipleBank?this.multipleBankOfferTitleText:null:e.title;var r=this.isBankOffer?this.isMultipleBank?this.multipleBankOfferTitleDescription:null:e.description;var t={"block-title":s,"block-subTitle":r,"show-controls":this.isMultipleBank};return h("div",{class:"s-offer-wrapper"},h("p",{class:"s-offer-corner-badge"},this.special_offer_text),h("salla-slider",Object.assign({type:"carousel",id:"offer-slider"},t),h("div",{slot:"items"},this.renderSectionForOfferType(e.type))))};e.prototype.renderSectionForOfferType=function(e){if(this.isBankOffer){return this.renderBankSection()}if(e==OfferType.BuyXGetY){return this.renderBuyXGetYSection()}return this.renderDiscountTableSection()};e.prototype.getCategoriesSection=function(e){return h("div",{class:"s-offer-slide-one-sixth swiper-slide",innerHTML:this.categorySlot.replace(/\{url\}/g,e.url).replace(/\{icon\}/g,e.icon||"sicon-store").replace(/\{name\}/g,e.name)})};e.prototype.renderBuyXGetYSection=function(){var e=this;var s,r;var t=this.offersList[0].details;return[(s=t.get.products)===null||s===void 0?void 0:s.map((function(s){return h("div",{class:"s-offer-slide-one-fourth"},e.hasCustomComponent?h(e.productCardComponent,{product:s}):h("salla-product-card",{"shadow-on-hover":true,product:s}))})),(r=t.get.categories)===null||r===void 0?void 0:r.map((function(s){return e.getCategoriesSection(s)}))]};e.prototype.renderBankSection=function(){var e=this;return this.offersList.map((function(s){return h("div",{class:{"s-offer-slide-one-sixth":e.isMultipleBank,"s-offer-bank-wrapper-sinlge-item":!e.isMultipleBank}},h("div",{class:{"s-offer-bank-wrapper":true,"s-offer-slide-one-sixth":!e.isMultipleBank,"s-offer-bank-wrapper-multi-spacer":e.isMultipleBank}},h("div",{class:"s-offer-bank-logo"},h("img",{src:s.details.logo||salla.url.cdn("images/s-empty.png"),"data-src":s.details.logo,alt:s.title+" offer"})),h("ul",{class:"s-offer-bank-payment-wrapper"},s.details.payments.map((function(e){return h("li",{class:"s-offer-bank-payment-single"},h("img",{src:salla.url.cdn("images/payment/"+e+".png")||salla.url.cdn("images/s-empty.png"),"data-src":salla.url.cdn("images/payment/"+e+".png"),alt:"payment"}))}))),e.isMultipleBank?h("p",{class:"s-offer-bank-payment-discount-percent"},"".concat(e.product_discount_text," ").concat(s.details.discount_value).concat(s.details.discount_type==="percentage"&&"%")):""),!e.isMultipleBank?h("div",{class:"s-offer-bank-message s-offer-slide-one-fourth"},h("h2",null,e.offersList[0].title),h("p",{innerHTML:e.generateBankDescription(e.offersList[0].description,s.details.discount_value)})):"")}))};e.prototype.generateBankDescription=function(e,s){return e.replace(new RegExp("".concat(s," %")),'<span class="s-offer-bank-message-amount">'.concat(s," %</span>"))};e.prototype.renderDiscountTableSection=function(){var e=this;var s;var r=this.offersList[0];var t=r.details.show_price_after_discount;return(s=this.offersList[0].details.discounts)===null||s===void 0?void 0:s.map((function(s){return h("div",{class:"s-offer-slide-one-fourth"},h("div",{class:"s-offer-slide-offer-entry"},h("div",{class:"s-offer-slide-offer-entry-price-quantity-container"},h("p",{class:"s-offer-slide-offer-entry-quantity"},e.buy_quantity_text(s.quantity)),t?h("div",{class:"s-offer-slide-offer-entry-price"},h("span",null,e.offer_with_price_text),h("span",{class:"s-offer-slide-offer-entry-price-amount"},s.discounted_amount)," ",h("span",null,e.userCurrency)):""),h("p",{class:"s-offer-slide-offer-entry-price-amount-percent"},e.with_discount_text,"(",h("span",null,s.percentage,!!s.percentage&&"%"),")")))}))};Object.defineProperty(e.prototype,"host",{get:function(){return getElement(this)},enumerable:false,configurable:true});return e}();SallaOffer.style=sallaOfferCss;export{SallaOffer as salla_offer}; //# sourceMappingURL=salla-offer.entry.js.map