@salla.sa/twilight-components
Version:
Salla Web Component
5 lines • 10.7 kB
JavaScript
var __awaiter=this&&this.__awaiter||function(t,e,r,n){function i(t){return t instanceof r?t:new r((function(e){e(t)}))}return new(r||(r=Promise))((function(r,a){function u(t){try{c(n.next(t))}catch(t){a(t)}}function o(t){try{c(n["throw"](t))}catch(t){a(t)}}function c(t){t.done?r(t.value):i(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(a[0]&1)throw a[1];return a[1]},trys:[],ops:[]},n,i,a,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,i&&(a=o[0]&2?i["return"]:o[0]?i["throw"]||((a=i["return"])&&a.call(i),0):i.next)&&!(a=a.call(i,o[1])).done)return a;if(i=0,a)o=[o[0]&2,a.value];switch(o[0]){case 0:case 1:a=o;break;case 4:r.label++;return{value:o[1],done:false};case 5:r.label++;i=o[1];o=[0];continue;case 7:o=r.ops.pop();r.trys.pop();continue;default:if(!(a=r.trys,a=a.length>0&&a[a.length-1])&&(o[0]===6||o[0]===2)){r=0;continue}if(o[0]===3&&(!a||o[1]>a[0]&&o[1]<a[3])){r.label=o[1];break}if(o[0]===6&&r.label<a[1]){r.label=a[1];a=o;break}if(a&&r.label<a[2]){r.label=a[2];r.ops.push(o);break}if(a[2])r.ops.pop();r.trys.pop();continue}o=e.call(t,r)}catch(t){o=[6,t];i=0}finally{n=a=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,i=e.length,a;n<i;n++){if(a||!(n in e)){if(!a)a=Array.prototype.slice.call(e,0,n);a[n]=e[n]}}return t.concat(a||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 i,a;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:(i=t.classList).remove.apply(i,u?r:e);t===null||t===void 0?void 0:(a=t.classList).add.apply(a,u?e:r);return this};t.prototype.toggleClassIf=function(t,e,r,n){var i=this;document.querySelectorAll(t).forEach((function(t){return i.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 i=n[1];var a=n[3];if(a){e[i]=e[i]||{};e[i][a]=t}else{e[i]=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,i;return __generator(this,(function(a){switch(a.label){case 0:if(!t||!this.includes)return[2,t];e=t.data.map((function(t){return t.id}));a.label=1;case 1:a.trys.push([1,4,,5]);return[4,this.fetchImagesAndOptions(e)];case 2:r=a.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=a.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:i=a.sent();console.error("Error in injectExtraFieldsToResponse:",i);throw i;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 i={"@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(i);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 i={"@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(i);document.head.appendChild(n)};t.prototype.formatDateFromString=function(t,e,r){if(r===void 0){r="en"}var n=new Date(t);var i={en:"Invalid Date",ar:"تاريخ غير صالح"};if(isNaN(n.getTime())){return i[r]}var a={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=a[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:t.name,datePublished:t.date*1e3,description:t.content,reviewRating:{"@type":"Rating",bestRating:"5",ratingValue:"5",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":"CollectionPage",name:"Category Name",description:"Description of the category",review:t.map((function(t){return e.createReviewObject(t)}))};r.textContent=JSON.stringify(n);document.head.appendChild(r)};return t}();var Helper$1=new Helper;export{Helper$1 as H};
//# sourceMappingURL=Helper-BrCHOtZG.js.map