UNPKG

@salla.sa/twilight-components

Version:
5 lines 13.9 kB
var __awaiter=this&&this.__awaiter||function(e,r,t,i){function n(e){return e instanceof t?e:new t((function(r){r(e)}))}return new(t||(t=Promise))((function(t,a){function s(e){try{u(i.next(e))}catch(e){a(e)}}function o(e){try{u(i["throw"](e))}catch(e){a(e)}}function u(e){e.done?t(e.value):n(e.value).then(s,o)}u((i=i.apply(e,r||[])).next())}))};var __generator=this&&this.__generator||function(e,r){var t={label:0,sent:function(){if(a[0]&1)throw a[1];return a[1]},trys:[],ops:[]},i,n,a,s;return s={next:o(0),throw:o(1),return:o(2)},typeof Symbol==="function"&&(s[Symbol.iterator]=function(){return this}),s;function o(e){return function(r){return u([e,r])}}function u(o){if(i)throw new TypeError("Generator is already executing.");while(s&&(s=0,o[0]&&(t=0)),t)try{if(i=1,n&&(a=o[0]&2?n["return"]:o[0]?n["throw"]||((a=n["return"])&&a.call(n),0):n.next)&&!(a=a.call(n,o[1])).done)return a;if(n=0,a)o=[o[0]&2,a.value];switch(o[0]){case 0:case 1:a=o;break;case 4:t.label++;return{value:o[1],done:false};case 5:t.label++;n=o[1];o=[0];continue;case 7:o=t.ops.pop();t.trys.pop();continue;default:if(!(a=t.trys,a=a.length>0&&a[a.length-1])&&(o[0]===6||o[0]===2)){t=0;continue}if(o[0]===3&&(!a||o[1]>a[0]&&o[1]<a[3])){t.label=o[1];break}if(o[0]===6&&t.label<a[1]){t.label=a[1];a=o;break}if(a&&t.label<a[2]){t.label=a[2];t.ops.push(o);break}if(a[2])t.ops.pop();t.trys.pop();continue}o=r.call(e,t)}catch(e){o=[6,e];n=0}finally{i=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(e,r,t){if(t||arguments.length===2)for(var i=0,n=r.length,a;i<n;i++){if(a||!(i in r)){if(!a)a=Array.prototype.slice.call(r,0,i);a[i]=r[i]}}return e.concat(a||Array.prototype.slice.call(r))}; /*! * Crafted with ❤ by Salla */import{a as require_root,d as requireIsObject,e as requireIsSymbol,f as getDefaultExportFromCjs,r as registerInstance,h,g as getElement}from"./index-DWStDVKB.js";import{S as ShoppingBag}from"./shopping-bag-DiKTtDW5.js";import{H as Helper}from"./Helper-CzEjEM5j.js";import"./anime.es-CgtvEd63.js";var now_1;var hasRequiredNow;function requireNow(){if(hasRequiredNow)return now_1;hasRequiredNow=1;var e=require_root();var r=function(){return e.Date.now()};now_1=r;return now_1}var _trimmedEndIndex;var hasRequired_trimmedEndIndex;function require_trimmedEndIndex(){if(hasRequired_trimmedEndIndex)return _trimmedEndIndex;hasRequired_trimmedEndIndex=1;var e=/\s/;function r(r){var t=r.length;while(t--&&e.test(r.charAt(t))){}return t}_trimmedEndIndex=r;return _trimmedEndIndex}var _baseTrim;var hasRequired_baseTrim;function require_baseTrim(){if(hasRequired_baseTrim)return _baseTrim;hasRequired_baseTrim=1;var e=require_trimmedEndIndex();var r=/^\s+/;function t(t){return t?t.slice(0,e(t)+1).replace(r,""):t}_baseTrim=t;return _baseTrim}var toNumber_1;var hasRequiredToNumber;function requireToNumber(){if(hasRequiredToNumber)return toNumber_1;hasRequiredToNumber=1;var e=require_baseTrim(),r=requireIsObject(),t=requireIsSymbol();var i=0/0;var n=/^[-+]0x[0-9a-f]+$/i;var a=/^0b[01]+$/i;var s=/^0o[0-7]+$/i;var o=parseInt;function u(u){if(typeof u=="number"){return u}if(t(u)){return i}if(r(u)){var l=typeof u.valueOf=="function"?u.valueOf():u;u=r(l)?l+"":l}if(typeof u!="string"){return u===0?u:+u}u=e(u);var f=a.test(u);return f||s.test(u)?o(u.slice(2),f?2:8):n.test(u)?i:+u}toNumber_1=u;return toNumber_1}var debounce_1;var hasRequiredDebounce;function requireDebounce(){if(hasRequiredDebounce)return debounce_1;hasRequiredDebounce=1;var e=requireIsObject(),r=requireNow(),t=requireToNumber();var i="Expected a function";var n=Math.max,a=Math.min;function s(s,o,u){var l,f,c,h,d,v,m=0,p=false,b=false,g=true;if(typeof s!="function"){throw new TypeError(i)}o=t(o)||0;if(e(u)){p=!!u.leading;b="maxWait"in u;c=b?n(t(u.maxWait)||0,o):c;g="trailing"in u?!!u.trailing:g}function _(e){var r=l,t=f;l=f=undefined;m=e;h=s.apply(t,r);return h}function w(e){m=e;d=setTimeout(T,o);return p?_(e):h}function y(e){var r=e-v,t=e-m,i=o-r;return b?a(i,c-t):i}function q(e){var r=e-v,t=e-m;return v===undefined||r>=o||r<0||b&&t>=c}function T(){var e=r();if(q(e)){return E(e)}d=setTimeout(T,y(e))}function E(e){d=undefined;if(g&&l){return _(e)}l=f=undefined;return h}function R(){if(d!==undefined){clearTimeout(d)}m=0;l=v=f=d=undefined}function x(){return d===undefined?h:E(r())}function I(){var e=r(),t=q(e);l=arguments;f=this;v=e;if(t){if(d===undefined){return w(v)}if(b){clearTimeout(d);d=setTimeout(T,o);return _(v)}}if(d===undefined){d=setTimeout(T,o)}return h}I.cancel=R;I.flush=x;return I}debounce_1=s;return debounce_1}var throttle_1;var hasRequiredThrottle;function requireThrottle(){if(hasRequiredThrottle)return throttle_1;hasRequiredThrottle=1;var e=requireDebounce(),r=requireIsObject();var t="Expected a function";function i(i,n,a){var s=true,o=true;if(typeof i!="function"){throw new TypeError(t)}if(r(a)){s="leading"in a?!!a.leading:s;o="trailing"in a?!!a.trailing:o}return e(i,n,{leading:s,maxWait:n,trailing:o})}throttle_1=i;return throttle_1}var throttleExports=requireThrottle();var throttle=getDefaultExportFromCjs(throttleExports);var MasonryLayout=function(){function e(e){var r=this;this.grid=e;this.create=function(){r.mutationObserver.observe(r.grid,{childList:true});for(var e=0,t=Array.from(r.grid.children);e<t.length;e++){var i=t[e];r.resizeObserver.observe(i)}};this.onContainerMutation=function(e){var t=e.flatMap((function(e){return Array.from(e.removedNodes)}));var i=e.flatMap((function(e){return Array.from(e.addedNodes)}));for(var n=0,a=t;n<a.length;n++){var s=a[n];if(s instanceof Element){r.resizeObserver.unobserve(s)}}for(var o=0,u=i;o<u.length;o++){var s=u[o];if(s instanceof Element){r.resizeObserver.observe(s)}}if(t.length>0&&i.length===0){r.update()}};this.onChildrenResize=function(e){var t=e.filter((function(e){return e.target.parentElement!==null}));if(t.length>0){r.update()}};this.update=throttle((function(){var e=window.getComputedStyle(r.grid);if(e.getPropertyValue("display").includes("grid")===false){r.clean();return}var t=parseGridTemplateColumns(r.grid);if(t.length<=1){r.clean();return}var i=Number.parseFloat(e.getPropertyValue("row-gap").trim())||0;var n=Array.from(r.grid.children);for(var a=0;a<t.length;a++){var s=n[a];s===null||s===void 0?void 0:s.style.removeProperty("margin-top")}for(var o=0;o<n.length;o++){var u=n[o-t.length];var l=n[o];if(u!==undefined&&l!==undefined){var f=u.getBoundingClientRect().bottom;l.style.removeProperty("margin-top");var c=l.getBoundingClientRect().top;if(c-i!==f){var h=Math.round((f-(c-i)+Number.EPSILON)*100)/100;l.style.setProperty("margin-top","".concat(h,"px"))}}}}),32);this.destroy=function(){r.resizeObserver.disconnect();r.mutationObserver.disconnect();r.clean()};this.clean=function(){for(var e=0,t=Array.from(r.grid.children);e<t.length;e++){var i=t[e];i.style.removeProperty("margin-top")}};this.mutationObserver=new MutationObserver(this.onContainerMutation);this.resizeObserver=new ResizeObserver(this.onChildrenResize);if(CSS.supports("grid-template-rows","masonry")===false){this.create()}}e.prototype[Symbol.dispose]=function(){this.destroy()};return e}();function parseGridTemplateColumns(e){var r=window.getComputedStyle(e);var t=r.getPropertyValue("grid-template-columns");return t.trim().split(/\s+(?=(?:[^()]*\([^()]*\))*[^()]*$)/)}var sallaReviewsPageCss=":host{display:block}";var SallaReviewsPage=function(){function e(e){registerInstance(this,e);this.reviews=[];this.isLoading=false;this.pagination=null;this.sort="latest"}e.prototype.getUrlParams=function(){var e=new URLSearchParams(window.location.search);return{sort:e.get("sort")||null,page:Number.parseInt(e.get("page"))||1}};e.prototype.updateUrlParams=function(e){var r=new URL(window.location.href);for(var t=0,i=Object.entries(e);t<i.length;t++){var n=i[t],a=n[0],s=n[1];if(s){r.searchParams.set(a,s.toString())}else{r.searchParams.delete(a)}}window.history.replaceState({},"",r.toString())};e.prototype.fetchReviews=function(e,r){var t=this.getUrlParams();return salla.api.request("reviews",{params:{type:"products",format:"lite",per_page:8,page:r||t.page||1,sort:e||t.sort||null}})};e.prototype.initializeMasonry=function(){return __awaiter(this,void 0,void 0,(function(){var e;return __generator(this,(function(r){e=this.el.querySelector(".s-reviews-page-grid");if(!e)return[2];try{new MasonryLayout(e);salla.logger.info("Masonry initialized successfully")}catch(e){salla.logger.error("Masonry initialization failed:",e)}return[2]}))}))};e.prototype.animateReviewCards=function(){var e=this.wrapper.querySelectorAll("salla-review-card:not(.animated)");Helper.animateItems(e)};e.prototype.initiateInfiniteScroll=function(){var e=this;var r,t,i;if(!this.wrapper){salla.logger.error("Wrapper is undefined. Cannot initiate infinite scroll.");return}this.infiniteScroll=salla.infiniteScroll.initiate(this.wrapper,this.wrapper,{path:function(){var r,t;return((t=(r=e.pagination)===null||r===void 0?void 0:r.links)===null||t===void 0?void 0:t.next)||null},history:false,scrollThreshold:false},true);(r=this.infiniteScroll)===null||r===void 0?void 0:r.on("request",(function(){e.isLoading=true}));(t=this.infiniteScroll)===null||t===void 0?void 0:t.on("load",(function(r){e.pagination=r.pagination;e.reviews=__spreadArray(__spreadArray([],e.reviews,true),r.data,true);e.isLoading=false;e.updateUrlParams({page:r.pagination.current_page})}));(i=this.infiniteScroll)===null||i===void 0?void 0:i.on("error",(function(r){salla.logger.error("Error loading more reviews:",r);e.isLoading=false}))};e.prototype.componentDidRender=function(){var e=this;setTimeout((function(){requestAnimationFrame(e.animateReviewCards.bind(e))}),176)};e.prototype.componentWillLoad=function(){return __awaiter(this,void 0,void 0,(function(){var e,r,t,i;return __generator(this,(function(n){switch(n.label){case 0:n.trys.push([0,3,,4]);return[4,salla.onReady()];case 1:n.sent();this.langTitlesReviews=salla.lang.get("common.titles.reviews");this.langSorting=salla.lang.get("pages.categories.sorting");this.placeholderText=salla.lang.choice("pages.rating.reviews",0);this.langLoadMore=salla.lang.get("common.elements.load_more");this.langSortByTopRating=salla.lang.get("pages.testimonials.sort_by_rating_desc");this.langSortByMostRecent=salla.lang.get("pages.testimonials.sort_by_date_desc");this.langSortByLeastRated=salla.lang.get("pages.testimonials.sort_by_rating_asc");this.langSortByLeastRecent=salla.lang.get("pages.testimonials.sort_by_date_asc");r=this.getUrlParams();return[4,this.fetchReviews(r.sort)];case 2:t=n.sent();this.sort=r.sort;this.reviews=t.data;this.pagination=t.pagination;this.langRatingReviews=salla.lang.choice("pages.rating.reviews",(e=this.pagination)===null||e===void 0?void 0:e.total);return[3,4];case 3:i=n.sent();salla.logger.error("Error loading reviews:",i);return[3,4];case 4:return[2]}}))}))};e.prototype.handleSorting=function(e){return __awaiter(this,void 0,void 0,(function(){var r,t;return __generator(this,(function(i){switch(i.label){case 0:r=e.target.value;this.sort=r;this.updateUrlParams({sort:r,page:1});return[4,this.fetchReviews(r,1)];case 1:t=i.sent();this.reviews=t.data;this.pagination=t.pagination;return[2]}}))}))};e.prototype.componentDidLoad=function(){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(e){switch(e.label){case 0:return[4,this.initializeMasonry()];case 1:e.sent();this.initiateInfiniteScroll();this.sort=this.getUrlParams().sort||"latest";this.updateUrlParams({sort:this.sort,page:1});return[2]}}))}))};e.prototype.disconnectedCallback=function(){if(this.infiniteScroll){this.infiniteScroll.destroy()}};e.prototype.renderSortingOptions=function(){var e=this;var r=[{value:"latest",label:this.langSortByMostRecent},{value:"oldest",label:this.langSortByLeastRecent},{value:"top_rating",label:this.langSortByTopRating},{value:"bottom_rating",label:this.langSortByLeastRated}];return r.map((function(r){return h("option",{key:r.value,value:r.value,selected:r.value===e.sort},r.label)}))};e.prototype.render=function(){var e=this;var r,t;return h("host",{key:"399a4d822a8442fb0f9a906f0db52fddec92ffc4"},h("div",{key:"fa4fa15c19b774d7eabf56f67f5bcae6b71572ab",class:"s-reviews-page-header-wrapper"},h("h2",{key:"169d557070590248e506718c5b9aa116a84e5634",class:"s-reviews-page-title"},this.langTitlesReviews,h("span",{key:"2549bf0697263d25aa3f2666164705e20dc51527",class:"s-reviews-page-count"},"(",this.langRatingReviews,")")),h("div",{key:"bfc9cef261a830871a9832e0f137e2f181051c95",class:"s-reviews-page-filter-wrapper"},h("label",{key:"e4e245499b86254383ed7fb3290d0a09ee9bc604",class:"s-reviews-page-filter-label",htmlFor:"testimonials-filter"},this.langSorting),h("select",{key:"6fcf56fa0a74026fb568d884218c55a96cd820f1",onChange:function(r){return e.handleSorting(r)},disabled:!this.reviews.length,class:"s-reviews-page-filter"},this.renderSortingOptions()))),this.reviews.length?h("main",{class:"s-reviews-page-grid",ref:function(r){e.wrapper=r}},this.reviews.map((function(e){return h("salla-review-card",{key:e.id,review:e})}))):h("div",{class:"s-products-list-placeholder"},h("span",{innerHTML:ShoppingBag}),h("p",null,this.placeholderText)),((t=(r=this.pagination)===null||r===void 0?void 0:r.links)===null||t===void 0?void 0:t.next)&&this.reviews.length?h("div",{class:"s-reviews-page-load-more-container"},h("salla-button",{class:"s-reviews-page-load-more-btn",loading:this.isLoading,onClick:function(){var r;return(r=e.infiniteScroll)===null||r===void 0?void 0:r.loadNextPage()},onKeyUp:function(){var r;return(r=e.infiniteScroll)===null||r===void 0?void 0:r.loadNextPage()}},this.langLoadMore)):null)};Object.defineProperty(e.prototype,"el",{get:function(){return getElement(this)},enumerable:false,configurable:true});return e}();SallaReviewsPage.style=sallaReviewsPageCss;export{SallaReviewsPage as salla_reviews_page}; //# sourceMappingURL=salla-reviews-page.entry.js.map