UNPKG

@salla.sa/twilight-components

Version:
5 lines 10.8 kB
var __awaiter=this&&this.__awaiter||function(t,e,r,n){function a(t){return t instanceof r?t:new r((function(e){e(t)}))}return new(r||(r=Promise))((function(r,i){function u(t){try{c(n.next(t))}catch(t){i(t)}}function o(t){try{c(n["throw"](t))}catch(t){i(t)}}function c(t){t.done?r(t.value):a(t.value).then(u,o)}c((n=n.apply(t,e||[])).next())}))};var __generator=this&&this.__generator||function(t,e){var r={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]},n,a,i,u;return u={next:o(0),throw:o(1),return:o(2)},typeof Symbol==="function"&&(u[Symbol.iterator]=function(){return this}),u;function o(t){return function(e){return c([t,e])}}function c(o){if(n)throw new TypeError("Generator is already executing.");while(u&&(u=0,o[0]&&(r=0)),r)try{if(n=1,a&&(i=o[0]&2?a["return"]:o[0]?a["throw"]||((i=a["return"])&&i.call(a),0):a.next)&&!(i=i.call(a,o[1])).done)return i;if(a=0,i)o=[o[0]&2,i.value];switch(o[0]){case 0:case 1:i=o;break;case 4:r.label++;return{value:o[1],done:false};case 5:r.label++;a=o[1];o=[0];continue;case 7:o=r.ops.pop();r.trys.pop();continue;default:if(!(i=r.trys,i=i.length>0&&i[i.length-1])&&(o[0]===6||o[0]===2)){r=0;continue}if(o[0]===3&&(!i||o[1]>i[0]&&o[1]<i[3])){r.label=o[1];break}if(o[0]===6&&r.label<i[1]){r.label=i[1];i=o;break}if(i&&r.label<i[2]){r.label=i[2];r.ops.push(o);break}if(i[2])r.ops.pop();r.trys.pop();continue}o=e.call(t,r)}catch(t){o=[6,t];a=0}finally{n=i=0}if(o[0]&5)throw o[1];return{value:o[0]?o[1]:void 0,done:true}}};var __spreadArray=this&&this.__spreadArray||function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,i;n<a;n++){if(i||!(n in e)){if(!i)i=Array.prototype.slice.call(e,0,n);i[n]=e[n]}}return t.concat(i||Array.prototype.slice.call(e))}; /*! * Crafted with ❤ by Salla */import{a as anime}from"./anime.es-CgtvEd63.js";salla.event.setMaxListeners(100);var Helper=function(){function t(){}t.prototype.setIncludes=function(t){this.includes=t;return this};t.prototype.toggleElementClassIf=function(t,e,r,n){var a,i;e=Array.isArray(e)?e:e.split(" ");r=Array.isArray(r)?r:r.split(" ");var u=n(t);t===null||t===void 0?void 0:(a=t.classList).remove.apply(a,u?r:e);t===null||t===void 0?void 0:(i=t.classList).add.apply(i,u?e:r);return this};t.prototype.toggleClassIf=function(t,e,r,n){var a=this;document.querySelectorAll(t).forEach((function(t){return a.toggleElementClassIf(t,e,r,n)}));return this};t.prototype.isValidEmail=function(t){var e=/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;return e.test(String(t).toLowerCase())};t.prototype.filterEmojies=function(t){var e=/(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g;return t.replace(e,"")};t.prototype.debounce=function(t){var e=[];for(var r=1;r<arguments.length;r++){e[r-1]=arguments[r]}if(!this.debounce_){this.debounce_=salla.helpers.debounce((function(t){var e=[];for(var r=1;r<arguments.length;r++){e[r-1]=arguments[r]}return t.apply(void 0,e)}),500)}return this.debounce_.apply(this,__spreadArray([t],e,false))};t.prototype.getProductsSource=function(t){return{"brands.single":"brands","product.index":"categories","product.index.latest":"latest","product.index.offers":"offers","product.index.search":"search","customer.wishlist":"wishlist","landing-page":"landing-page","product.index.tag":"tags","product.index.sales":"sales","components.most_sales_products":"sales"}[t||salla.config.get("page.slug")]||t||"latest"};t.prototype.getPageTitleForSource=function(t){t={brands:"common.titles.brands",latest:"blocks.home.latest_products",offers:"common.titles.discounts",sales:"common.titles.most_sales"}[t];return t?salla.lang.get(t):""};t.prototype.getProductsSourceValue=function(t,e){var r=this.getProductsSource(t);var n=null;if(e){try{n=typeof e==="string"?JSON.parse(e):e}catch(t){console.error("Failed to parse JSON string in sourceValue:",t)}}if(!["search","json","offers","latest","sales","related"].includes(r)){if(Array.isArray(n)&&n.length){return n}if(Array.isArray(n)&&!n.length){return""}if(typeof n==="number"){return[n]}if(!e&&["categories","tags","brands"].includes(t)){return[salla.config.get("page.id")]}}if(n||e){return n||e}if(r==="search"){return new URLSearchParams(window.location.search).get("q")||""}return salla.config.get("page.id")};t.prototype.extractFiltersFromUrl=function(t){var e={};t.forEach((function(t,r){var n=r.match(/^filters\[(.*?)\](\[(.*?)\])?$/u);if(n){var a=n[1];var i=n[3];if(i){e[a]=e[a]||{};e[a][i]=t}else{e[a]=t}}else{var u=r.match(/^(.*?)$/u);if(u){e[u[1]]=t}}}));return e};t.prototype.injectExtraFieldsToResponse=function(t){return __awaiter(this,void 0,void 0,(function(){var e,r,n,a;return __generator(this,(function(i){switch(i.label){case 0:if(!t||!this.includes)return[2,t];e=t.data.map((function(t){return t.id}));i.label=1;case 1:i.trys.push([1,4,,5]);return[4,this.fetchImagesAndOptions(e)];case 2:r=i.sent().data;this.injectOptionsAndImages(t.data,r);if(!this.includes.includes("metadata")){return[2,t]}return[4,salla.api.metadata.fetchValues("product",e)];case 3:n=i.sent();t.data.forEach((function(t){t["metadata"]=n.data.find((function(e){return parseInt(e.entity_id)===parseInt(t.id)}))}));return[2,t];case 4:a=i.sent();console.error("Error in injectExtraFieldsToResponse:",a);throw a;case 5:return[2]}}))}))};t.prototype.fetchImagesAndOptions=function(t){return this.productsIncludes().length?salla.api.product.fetchOptions(t,{with:this.productsIncludes()}):Promise.resolve({data:[]})};t.prototype.injectOptionsAndImages=function(t,e){var r=this;return e.length?t.map((function(t){r.includes.filter((function(t){return t!=="metadata"})).forEach((function(r){var n=e.find((function(e){return parseInt(e.id)===parseInt(t.id)}));t[r]=n?n[r]:null}));return t})):null};t.prototype.productsIncludes=function(){var t;return(t=this.includes)===null||t===void 0?void 0:t.filter((function(t){return t!="metadata"}))};t.prototype.parseJson=function(t){if(typeof t!=="string"){return t}try{return JSON.parse(t)}catch(t){salla.logger.error("Failed to parse includes as JSON:",t)}return null};t.prototype.getProductSchemaMarkupScript=function(t){var e,r;var n=document.createElement("script");n.type="application/ld+json";var a={"@context":"http://schema.org","@type":"Product",name:t===null||t===void 0?void 0:t.name,image:((e=t===null||t===void 0?void 0:t.image)===null||e===void 0?void 0:e.url)||(t===null||t===void 0?void 0:t.thumbnail),description:t===null||t===void 0?void 0:t.description,brand:{"@type":"Brand",logo:(r=t===null||t===void 0?void 0:t.brand)===null||r===void 0?void 0:r.logo},offers:{"@type":"Offer",price:t===null||t===void 0?void 0:t.price}};n.text=JSON.stringify(a);return n};t.prototype.createProductSchema=function(t,e){var r;var n={productID:"".concat(t.id),"@type":"Product",name:t.name,url:t.url,image:t.image.url||t.image,description:t.description||t.subtitle||t.name,offers:{"@type":"Offer",url:t.url,priceCurrency:t.currency,price:t.price,itemCondition:"NewCondition",availability:t.is_available?"InStock":"OutOfStock",priceValidUntil:t.discount_ends||new Date((new Date).setDate((new Date).getDate()+7)).toISOString().split("T")[0],priceSpecification:{"@type":"PriceSpecification",price:t.regular_price,priceCurrency:t.currency,valueAddedTaxIncluded:t.is_taxable},seller:{"@type":"Organization",name:salla.config.get("store.name")}}};if((r=t.rating)===null||r===void 0?void 0:r.stars){n.aggregateRating={"@type":"AggregateRating",ratingValue:t.rating.stars,reviewCount:t.rating.count}}return{"@type":"ListItem",position:e,item:n}};t.prototype.generateProductSchema=function(t){var e=this;var r="salla-product-schema-script";if(document.getElementById(r)){salla.logger.warn("already added schema-script with id (".concat(r,")!"));return}var n=document.createElement("script");n.type="application/ld+json";n.id=r;var a={"@context":"https://schema.org","@type":"ItemList",name:salla.config.get("page.title"),itemListElement:t.map((function(t,r){return e.createProductSchema(t,r+1)}))};n.textContent=JSON.stringify(a);document.head.appendChild(n)};t.prototype.formatDateFromString=function(t,e,r){if(r===void 0){r="en"}var n=new Date(t);var a={en:"Invalid Date",ar:"تاريخ غير صالح"};if(isNaN(n.getTime())){return a[r]}var i={en:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],ar:["الأحد","الاثنين","الثلاثاء","الأربعاء","الخميس","الجمعة","السبت"]};var u={en:["January","February","March","April","May","June","July","August","September","October","November","December"],ar:["يناير","فبراير","مارس","أبريل","مايو","يونيو","يوليو","أغسطس","سبتمبر","أكتوبر","نوفمبر","ديسمبر"]};var o=i[r][n.getDay()];var c=e(n.getDate(),r==="en");var s=u[r][n.getMonth()];var d=e(n.getFullYear(),r==="en");return"".concat(o," ").concat(c," ").concat(s," ").concat(d)};t.prototype.copyToClipboard=function(t){var e=t.target.innerText;navigator.clipboard.writeText(e).then((function(){console.log("Text copied to clipboard: "+e)})).catch((function(t){console.error("Unable to copy text to clipboard",t)}))};t.prototype.animateItems=function(t){if(!t)return;anime({targets:t,opacity:[0,1],duration:1200,translateY:[20,0],delay:function(t,e){return e*100},easing:"easeOutExpo",complete:function(e){t.forEach((function(t){t.classList.add("animated")}))}})};t.prototype.createReviewObject=function(t){return{"@type":"Review",author:{"@type":"Person",name:t.name},reviewBody:t.content,reviewRating:{"@type":"Rating",bestRating:"5",ratingValue:t.stars,worstRating:"1"}}};t.prototype.generateReviewSchema=function(t){var e=this;var r=document.createElement("script");r.type="application/ld+json";var n={"@context":"http://schema.org/","@type":"Store",name:salla.config.get("store.name"),url:salla.config.get("store.url"),address:"الرياض",review:t.map((function(t){return e.createReviewObject(t)})),aggregateRating:{"@type":"AggregateRating",ratingValue:t.reduce((function(t,e){return t+e.stars}),0)/t.length,reviewCount:t.length}};r.textContent=JSON.stringify(n);document.head.appendChild(r)};return t}();var Helper$1=new Helper;export{Helper$1 as H}; //# sourceMappingURL=Helper-CzEjEM5j.js.map