@salla.sa/twilight-components
Version:
Salla Web Component
5 lines • 10.7 kB
JavaScript
var __awaiter=this&&this.__awaiter||function(e,t,r,n){function a(e){return e instanceof r?e:new r((function(t){t(e)}))}return new(r||(r=Promise))((function(r,i){function o(e){try{c(n.next(e))}catch(e){i(e)}}function u(e){try{c(n["throw"](e))}catch(e){i(e)}}function c(e){e.done?r(e.value):a(e.value).then(o,u)}c((n=n.apply(e,t||[])).next())}))};var __generator=this&&this.__generator||function(e,t){var r={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]},n,a,i,o;return o={next:u(0),throw:u(1),return:u(2)},typeof Symbol==="function"&&(o[Symbol.iterator]=function(){return this}),o;function u(e){return function(t){return c([e,t])}}function c(u){if(n)throw new TypeError("Generator is already executing.");while(o&&(o=0,u[0]&&(r=0)),r)try{if(n=1,a&&(i=u[0]&2?a["return"]:u[0]?a["throw"]||((i=a["return"])&&i.call(a),0):a.next)&&!(i=i.call(a,u[1])).done)return i;if(a=0,i)u=[u[0]&2,i.value];switch(u[0]){case 0:case 1:i=u;break;case 4:r.label++;return{value:u[1],done:false};case 5:r.label++;a=u[1];u=[0];continue;case 7:u=r.ops.pop();r.trys.pop();continue;default:if(!(i=r.trys,i=i.length>0&&i[i.length-1])&&(u[0]===6||u[0]===2)){r=0;continue}if(u[0]===3&&(!i||u[1]>i[0]&&u[1]<i[3])){r.label=u[1];break}if(u[0]===6&&r.label<i[1]){r.label=i[1];i=u;break}if(i&&r.label<i[2]){r.label=i[2];r.ops.push(u);break}if(i[2])r.ops.pop();r.trys.pop();continue}u=t.call(e,r)}catch(e){u=[6,e];a=0}finally{n=i=0}if(u[0]&5)throw u[1];return{value:u[0]?u[1]:void 0,done:true}}};var __spreadArray=this&&this.__spreadArray||function(e,t,r){if(r||arguments.length===2)for(var n=0,a=t.length,i;n<a;n++){if(i||!(n in t)){if(!i)i=Array.prototype.slice.call(t,0,n);i[n]=t[n]}}return e.concat(i||Array.prototype.slice.call(t))};
/*!
* Crafted with ❤ by Salla
*/System.register(["./p-8f7b9e36.system.js"],(function(e){"use strict";var t;return{setters:[function(e){t=e.a}],execute:function(){salla.event.setMaxListeners(100);var r=function(){function e(){}e.prototype.setIncludes=function(e){this.includes=e;return this};e.prototype.toggleElementClassIf=function(e,t,r,n){var a,i;t=Array.isArray(t)?t:t.split(" ");r=Array.isArray(r)?r:r.split(" ");var o=n(e);e===null||e===void 0?void 0:(a=e.classList).remove.apply(a,o?r:t);e===null||e===void 0?void 0:(i=e.classList).add.apply(i,o?t:r);return this};e.prototype.toggleClassIf=function(e,t,r,n){var a=this;document.querySelectorAll(e).forEach((function(e){return a.toggleElementClassIf(e,t,r,n)}));return this};e.prototype.isValidEmail=function(e){var t=/^(([^<>()[\]\\.,;:\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 t.test(String(e).toLowerCase())};e.prototype.filterEmojies=function(e){var t=/(?:[\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 e.replace(t,"")};e.prototype.debounce=function(e){var t=[];for(var r=1;r<arguments.length;r++){t[r-1]=arguments[r]}if(!this.debounce_){this.debounce_=salla.helpers.debounce((function(e){var t=[];for(var r=1;r<arguments.length;r++){t[r-1]=arguments[r]}return e.apply(void 0,t)}),500)}return this.debounce_.apply(this,__spreadArray([e],t,false))};e.prototype.getProductsSource=function(e){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"}[e||salla.config.get("page.slug")]||e||"latest"};e.prototype.getPageTitleForSource=function(e){e={brands:"common.titles.brands",latest:"blocks.home.latest_products",offers:"common.titles.discounts",sales:"common.titles.most_sales"}[e];return e?salla.lang.get(e):""};e.prototype.getProductsSourceValue=function(e,t){var r=this.getProductsSource(e);var n=null;if(t){try{n=JSON.parse(t)}catch(e){console.error("Invalid JSON string in sourceValue:",e)}}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(!t&&["categories","tags","brands"].includes(e)){return[salla.config.get("page.id")]}}if(n||t){return n||t}if(r==="search"){return new URLSearchParams(window.location.search).get("q")||""}return salla.config.get("page.id")};e.prototype.extractFiltersFromUrl=function(e){var t={};e.forEach((function(e,r){var n=r.match(/^filters\[(.*?)\](\[(.*?)\])?$/u);if(n){var a=n[1];var i=n[3];if(i){t[a]=t[a]||{};t[a][i]=e}else{t[a]=e}}else{var o=r.match(/^(.*?)$/u);if(o){t[o[1]]=e}}}));return t};e.prototype.injectExtraFieldsToResponse=function(e){return __awaiter(this,void 0,void 0,(function(){var t,r,n,a;return __generator(this,(function(i){switch(i.label){case 0:if(!e||!this.includes)return[2,e];t=e.data.map((function(e){return e.id}));i.label=1;case 1:i.trys.push([1,4,,5]);return[4,this.fetchImagesAndOptions(t)];case 2:r=i.sent().data;this.injectOptionsAndImages(e.data,r);if(!this.includes.includes("metadata")){return[2,e]}return[4,salla.api.metadata.fetchValues("product",t)];case 3:n=i.sent();e.data.forEach((function(e){e["metadata"]=n.data.find((function(t){return parseInt(t.entity_id)===parseInt(e.id)}))}));return[2,e];case 4:a=i.sent();console.error("Error in injectExtraFieldsToResponse:",a);throw a;case 5:return[2]}}))}))};e.prototype.fetchImagesAndOptions=function(e){return this.productsIncludes().length?salla.api.product.fetchOptions(e,{with:this.productsIncludes()}):Promise.resolve({data:[]})};e.prototype.injectOptionsAndImages=function(e,t){var r=this;return t.length?e.map((function(e){r.includes.filter((function(e){return e!=="metadata"})).forEach((function(r){var n=t.find((function(t){return parseInt(t.id)===parseInt(e.id)}));e[r]=n?n[r]:null}));return e})):null};e.prototype.productsIncludes=function(){var e;return(e=this.includes)===null||e===void 0?void 0:e.filter((function(e){return e!="metadata"}))};e.prototype.parseJson=function(e){if(typeof e!=="string"){return e}try{return JSON.parse(e)}catch(e){salla.logger.error("Failed to parse includes as JSON:",e)}return null};e.prototype.getProductSchemaMarkupScript=function(e){var t,r;var n=document.createElement("script");n.type="application/ld+json";var a={"@context":"http://schema.org","@type":"Product",name:e===null||e===void 0?void 0:e.name,image:((t=e===null||e===void 0?void 0:e.image)===null||t===void 0?void 0:t.url)||(e===null||e===void 0?void 0:e.thumbnail),description:e===null||e===void 0?void 0:e.description,brand:{"@type":"Brand",logo:(r=e===null||e===void 0?void 0:e.brand)===null||r===void 0?void 0:r.logo},offers:{"@type":"Offer",price:e===null||e===void 0?void 0:e.price}};n.text=JSON.stringify(a);return n};e.prototype.createProductSchema=function(e,t){var r;var n={productID:"".concat(e.id),"@type":"Product",name:e.name,url:e.url,image:e.image.url||e.image,description:e.description||e.subtitle||e.name,offers:{"@type":"Offer",url:e.url,priceCurrency:e.currency,price:e.price,itemCondition:"NewCondition",availability:e.is_available?"InStock":"OutOfStock",priceValidUntil:e.discount_ends||new Date((new Date).setDate((new Date).getDate()+7)).toISOString().split("T")[0],priceSpecification:{"@type":"PriceSpecification",price:e.regular_price,priceCurrency:e.currency,valueAddedTaxIncluded:e.is_taxable},seller:{"@type":"Organization",name:salla.config.get("store.name")}}};if((r=e.rating)===null||r===void 0?void 0:r.stars){n.aggregateRating={"@type":"AggregateRating",ratingValue:e.rating.stars,reviewCount:e.rating.count}}return{"@type":"ListItem",position:t,item:n}};e.prototype.generateProductSchema=function(e){var t=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:e.map((function(e,r){return t.createProductSchema(e,r+1)}))};n.textContent=JSON.stringify(a);document.head.appendChild(n)};e.prototype.formatDateFromString=function(e,t,r){if(r===void 0){r="en"}var n=new Date(e);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 o={en:["January","February","March","April","May","June","July","August","September","October","November","December"],ar:["يناير","فبراير","مارس","أبريل","مايو","يونيو","يوليو","أغسطس","سبتمبر","أكتوبر","نوفمبر","ديسمبر"]};var u=i[r][n.getDay()];var c=t(n.getDate(),r==="en");var s=o[r][n.getMonth()];var l=t(n.getFullYear(),r==="en");return"".concat(u," ").concat(c," ").concat(s," ").concat(l)};e.prototype.copyToClipboard=function(e){var t=e.target.innerText;navigator.clipboard.writeText(t).then((function(){console.log("Text copied to clipboard: "+t)})).catch((function(e){console.error("Unable to copy text to clipboard",e)}))};e.prototype.animateItems=function(e){if(!e)return;t({targets:e,opacity:[0,1],duration:1200,translateY:[20,0],delay:function(e,t){return t*100},easing:"easeOutExpo",complete:function(t){e.forEach((function(e){e.classList.add("animated")}))}})};e.prototype.createReviewObject=function(e){return{"@type":"Review",author:e.name,datePublished:e.date*1e3,description:e.content,reviewRating:{"@type":"Rating",bestRating:"5",ratingValue:"5",worstRating:"1"}}};e.prototype.generateReviewSchema=function(e){var t=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:e.map((function(e){return t.createReviewObject(e)}))};r.textContent=JSON.stringify(n);document.head.appendChild(r)};return e}();var n=e("H",new r)}}}));
//# sourceMappingURL=p-8c9d72f2.system.js.map