UNPKG

@salla.sa/twilight-components

Version:
5 lines 10.7 kB
var __awaiter=this&&this.__awaiter||function(t,e,n,r){function i(t){return t instanceof n?t:new n((function(e){e(t)}))}return new(n||(n=Promise))((function(n,u){function a(t){try{c(r.next(t))}catch(t){u(t)}}function o(t){try{c(r["throw"](t))}catch(t){u(t)}}function c(t){t.done?n(t.value):i(t.value).then(a,o)}c((r=r.apply(t,e||[])).next())}))};var __generator=this&&this.__generator||function(t,e){var n={label:0,sent:function(){if(u[0]&1)throw u[1];return u[1]},trys:[],ops:[]},r,i,u,a;return a={next:o(0),throw:o(1),return:o(2)},typeof Symbol==="function"&&(a[Symbol.iterator]=function(){return this}),a;function o(t){return function(e){return c([t,e])}}function c(o){if(r)throw new TypeError("Generator is already executing.");while(a&&(a=0,o[0]&&(n=0)),n)try{if(r=1,i&&(u=o[0]&2?i["return"]:o[0]?i["throw"]||((u=i["return"])&&u.call(i),0):i.next)&&!(u=u.call(i,o[1])).done)return u;if(i=0,u)o=[o[0]&2,u.value];switch(o[0]){case 0:case 1:u=o;break;case 4:n.label++;return{value:o[1],done:false};case 5:n.label++;i=o[1];o=[0];continue;case 7:o=n.ops.pop();n.trys.pop();continue;default:if(!(u=n.trys,u=u.length>0&&u[u.length-1])&&(o[0]===6||o[0]===2)){n=0;continue}if(o[0]===3&&(!u||o[1]>u[0]&&o[1]<u[3])){n.label=o[1];break}if(o[0]===6&&n.label<u[1]){n.label=u[1];u=o;break}if(u&&n.label<u[2]){n.label=u[2];n.ops.push(o);break}if(u[2])n.ops.pop();n.trys.pop();continue}o=e.call(t,n)}catch(t){o=[6,t];i=0}finally{r=u=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,n){if(n||arguments.length===2)for(var r=0,i=e.length,u;r<i;r++){if(u||!(r in e)){if(!u)u=Array.prototype.slice.call(e,0,r);u[r]=e[r]}}return t.concat(u||Array.prototype.slice.call(e))}; /*! * Crafted with ❤ by Salla */System.register(["./p-CLqFzsVe.system.js"],(function(t){"use strict";var e;return{setters:[function(t){e=t.a}],execute:function(){salla.event.setMaxListeners(100);var n=function(){function t(){}t.prototype.setIncludes=function(t){this.includes=t;return this};t.prototype.toggleElementClassIf=function(t,e,n,r){var i,u;e=Array.isArray(e)?e:e.split(" ");n=Array.isArray(n)?n:n.split(" ");var a=r(t);t===null||t===void 0?void 0:(i=t.classList).remove.apply(i,a?n:e);t===null||t===void 0?void 0:(u=t.classList).add.apply(u,a?e:n);return this};t.prototype.toggleClassIf=function(t,e,n,r){var i=this;document.querySelectorAll(t).forEach((function(t){return i.toggleElementClassIf(t,e,n,r)}));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 n=1;n<arguments.length;n++){e[n-1]=arguments[n]}if(!this.debounce_){this.debounce_=salla.helpers.debounce((function(t){var e=[];for(var n=1;n<arguments.length;n++){e[n-1]=arguments[n]}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 n=this.getProductsSource(t);var r=null;if(e){try{r=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(n)){if(Array.isArray(r)&&r.length){return r}if(Array.isArray(r)&&!r.length){return""}if(typeof r==="number"){return[r]}if(!e&&["categories","tags","brands"].includes(t)){return[salla.config.get("page.id")]}}if(r||e){return r||e}if(n==="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,n){var r=n.match(/^filters\[(.*?)\](\[(.*?)\])?$/u);if(r){var i=r[1];var u=r[3];if(u){e[i]=e[i]||{};e[i][u]=t}else{e[i]=t}}else{var a=n.match(/^(.*?)$/u);if(a){e[a[1]]=t}}}));return e};t.prototype.injectExtraFieldsToResponse=function(t){return __awaiter(this,void 0,void 0,(function(){var e,n,r,i;return __generator(this,(function(u){switch(u.label){case 0:if(!t||!this.includes)return[2,t];e=t.data.map((function(t){return t.id}));u.label=1;case 1:u.trys.push([1,4,,5]);return[4,this.fetchImagesAndOptions(e)];case 2:n=u.sent().data;this.injectOptionsAndImages(t.data,n);if(!this.includes.includes("metadata")){return[2,t]}return[4,salla.api.metadata.fetchValues("product",e)];case 3:r=u.sent();t.data.forEach((function(t){t["metadata"]=r.data.find((function(e){return parseInt(e.entity_id)===parseInt(t.id)}))}));return[2,t];case 4:i=u.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 n=this;return e.length?t.map((function(t){n.includes.filter((function(t){return t!=="metadata"})).forEach((function(n){var r=e.find((function(e){return parseInt(e.id)===parseInt(t.id)}));t[n]=r?r[n]: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,n;var r=document.createElement("script");r.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:(n=t===null||t===void 0?void 0:t.brand)===null||n===void 0?void 0:n.logo},offers:{"@type":"Offer",price:t===null||t===void 0?void 0:t.price}};r.text=JSON.stringify(i);return r};t.prototype.createProductSchema=function(t,e){var n;var r={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((n=t.rating)===null||n===void 0?void 0:n.stars){r.aggregateRating={"@type":"AggregateRating",ratingValue:t.rating.stars,reviewCount:t.rating.count}}return{"@type":"ListItem",position:e,item:r}};t.prototype.generateProductSchema=function(t){var e=this;var n="salla-product-schema-script";if(document.getElementById(n)){salla.logger.warn("already added schema-script with id (".concat(n,")!"));return}var r=document.createElement("script");r.type="application/ld+json";r.id=n;var i={"@context":"https://schema.org","@type":"ItemList",name:salla.config.get("page.title"),itemListElement:t.map((function(t,n){return e.createProductSchema(t,n+1)}))};r.textContent=JSON.stringify(i);document.head.appendChild(r)};t.prototype.formatDateFromString=function(t,e,n){if(n===void 0){n="en"}var r=new Date(t);var i={en:"Invalid Date",ar:"تاريخ غير صالح"};if(isNaN(r.getTime())){return i[n]}var u={en:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],ar:["الأحد","الاثنين","الثلاثاء","الأربعاء","الخميس","الجمعة","السبت"]};var a={en:["January","February","March","April","May","June","July","August","September","October","November","December"],ar:["يناير","فبراير","مارس","أبريل","مايو","يونيو","يوليو","أغسطس","سبتمبر","أكتوبر","نوفمبر","ديسمبر"]};var o=u[n][r.getDay()];var c=e(r.getDate(),n==="en");var s=a[n][r.getMonth()];var d=e(r.getFullYear(),n==="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;e({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 n=document.createElement("script");n.type="application/ld+json";var r={"@context":"http://schema.org/","@type":"CollectionPage",name:"Category Name",description:"Description of the category",review:t.map((function(t){return e.createReviewObject(t)}))};n.textContent=JSON.stringify(r);document.head.appendChild(n)};return t}();var r=t("H",new n)}}})); //# sourceMappingURL=p-CorPzHaH.system.js.map