1 |
- define(["knockout","jquery","ccResourceLoader!global/OCS_BFService","navigation","ccRestClient","pubsub"],function(e,t,n,r,i,s){"use strict";var o,u,a=i;return{WIDGET_ID:"x_LastProductsViewed",CONSTANTS:{TYPE_AU:"automovil-usado",TYPE_AN:"automovil-nuevo",OPT_RECOMENDADOS_USADOS:"RECOMENDADOS_USADOS",OPT_DESTACADOS_USADOS:"DESTACADOS_USADOS",FIELDS:"id,route,primaryFullImageURL,brand,displayName,x_version,x_agno,x_kilometraje,x_cilindrada,type,x_combustible,x_transmision,x_mixtoKmL,listPrices,salePrices,type,x_marca,x_modelo,listPrice,x_tipo,x_views,childSKUs,creationDate",PRODUCTS_VIEWED:"PRODUCTS_VIEWED",OPTIONS_VIEWS:[{icon:"history",label:"Últimos vistos",key:"last_viewed"},{icon:"swap_horiz",label:"Similares",key:"similar"},{icon:"star",label:"Destacados",key:"featured"}]},products:e.observableArray([]),productsSimilar:e.observableArray([]),productsFeatured:e.observableArray([]),productsMostViewed:e.observableArray([]),viewedProducts:e.observableArray([]),showBanner:e.observable(!1),loading:e.observable(!0),optionSelected:e.observable("last_viewed"),noProductsToShow:e.observable(!1),onLoad:function(e){o=e,o.optionSelected.subscribe(function(){o.loading(!0),o.products([]),u&&u.destroy();switch(o.optionSelected()){case"last_viewed":o.getViewedProducts();break;case"similar":o.recomendacionesUsados();break;case"most_viewed":o.getMostViewed();break;case"featured":o.getFeaturedProducts();break;default:}})},setOption:function(e){o.loading()||o.optionSelected(e)},renderSwiper:function(){var e={slidesPerView:"auto",spaceBetween:0,slidesOffsetAfter:0,centerInsufficientSlides:!0,pagination:{el:".swiper-pagination-lastViewed",dynamicBullets:!0},navigation:{nextEl:".swiper-button-next-lastViewed",prevEl:".swiper-button-prev-lastViewed"},breakpoints:{320:{slidesOffsetAfter:10,slidesOffsetBefore:10,slidesPerView:"auto"},768:{slidesOffsetAfter:0,slidesOffsetBefore:0,slidesPerView:"auto"},1201:{slidesOffsetAfter:0,slidesOffsetBefore:0,slidesPerView:4}}};u=new Swiper(".swiper-reserva-lastViewed",e)},beforeAppear:function(){var e=this;e.products([]),e.viewedProducts([]),e.productsMostViewed([]),e.productsSimilar([]),e.noProductsToShow(!1),e.optionSelected("last_viewed"),e.showBanner(!1),e.loading(!0),u&&u.destroy(),window.localStorage.getItem("PRODUCTS_VIEWED")?e.pageContext().page.name=="product"?e.getViewedProducts(function(){e.optionSelected("similar")}):e.getViewedProducts():(e.noProductsToShow(!0),e.optionSelected("featured"))},showHighlyViewed:function(e){return n.showHighlyViewed(e.creationDate,e.x_views)},getMostViewed:function(){if(o.productsMostViewed().length===0){var e=function(e){var t=e.filter(function(e,t){return e.childSKUs[0].quantity!==888&&o.showHighlyViewed(e)}).filter(function(e,t){return t<13});o.productsMostViewed(t),o.products(t),o.loading(!1)},t={Ns:"product.x_views|1",suppressResults:!1,searchType:"guided",type:"search",ccvp:"lg"};t.Nrpp=30,t.Nf="product.x_views|BTWN+100+999",o.searchProducts(t,function(n){t.Nf="product.x_views|BTWN+1000+9999",o.searchProducts(t,function(t){var r=n.concat(t).map(function(e){return e.records[0].attributes["product.repositoryId"][0]});o.getProducts(r,e)})})}else o.products(o.productsMostViewed()),o.loading(!1)},resetLocalStorage:function(){window.localStorage.removeItem(o.CONSTANTS.PRODUCTS_VIEWED),o.noProductsToShow(!0),o.products([]),o.optionSelected("featured")},getViewedProducts:function(e){if(window.localStorage.getItem(o.CONSTANTS.PRODUCTS_VIEWED)&&o.viewedProducts().length===0){var t=window.localStorage.getItem(o.CONSTANTS.PRODUCTS_VIEWED).split(",");t.length>0?n.utils.product.getProducts({fields:o.CONSTANTS.FIELDS,ids:t,success:function(n){var r=t.reverse().map(function(e){for(var t=0;t<n.length;t++)if(n[t].id===e)return n[t]}).filter(function(e){return e});r.length>0?(o.viewedProducts(r),o.products(r),e&&e()):o.resetLocalStorage(),o.loading(!1)},fail:function(e){console.error("Error obteniendo productos",e)}}):(localStorage.removeItem(o.CONSTANTS.PRODUCTS_VIEWED),o.loading(!1))}else o.products(o.viewedProducts()),o.loading(!1),e&&e()},textSequence:function(){var e=["últimos vistos","más vistos"],n=0,r=function(){t("#title-loop").fadeOut(800,function(){n=(n+1)%e.length,this.textContent=e[n]}).fadeIn(800,r).delay(1e3)};r()},redirect:function(){location.href="/comprar-seminuevo-precio-promocion-chile/autos-usados"},getFeaturedProducts:function(){o.productsFeatured().length===0?n.utils.product.getProducts({fields:o.CONSTANTS.FIELDS,q:'type eq "automovil-usado" and x_bestSeller eq true and active eq true',additionalParams:{limit:12},success:function(e){o.productsFeatured(e.items),o.products(e.items),o.loading(!1)},fail:function(e){console.error("Error obteniendo productos x_LastProductsViewed",e)}}):(o.products(o.productsFeatured()),o.loading(!1))},recomendacionesUsados:function(t){var n=this,r=t?t:0,i=null;n.pageContext().page.name=="product"&&r==0&&(i=n.product());if(o.viewedProducts().length>0&&n.productsSimilar().length===0){var s=i?e.toJS(i):e.toJS(o.viewedProducts()[r]),u=function(e){var t=e.filter(function(e){return e.id!=s.id}).filter(function(e){return e});t.length>0?(o.productsSimilar(t),o.products(t)):o.viewedProducts().length>r+1?o.recomendacionesUsados(r+1):o.resetLocalStorage(),o.loading(!1)},a={No:0,Ns:"sku.activePrice|1",language:"es",searchType:"simple",suppressResults:!1};a.Ntt=s.x_tipo+" "+s.x_transmision,a.Nrpp=12,a.Nf="product.listPrice|BTWN+"+(s.listPrice-2e6)+"+"+(s.listPrice+2e6),a.visitId=window.localStorage.getItem("visitId")||"",a.visitorId=window.localStorage.getItem("visitorId")||"",o.searchProducts(a,function(e){if(!e.length||e.length<4)a.Ntt=s.x_tipo,a.Nf="product.listPrice|BTWN+"+(s.listPrice-3e6)+"+"+(s.listPrice+3e6),o.searchProducts(a,function(e){if(e.length>0){var t=e.map(function(e){return e.records[0].attributes["product.repositoryId"][0]}).filter(function(e){return e!=s.id&&e.length==7});o.getProducts(t,u)}else o.viewedProducts().length>r+1?o.recomendacionesUsados(r+1):o.resetLocalStorage()});else{var t=e.map(function(e){return e.records[0].attributes["product.repositoryId"][0]}).filter(function(e){return e!=s.id&&e.length==7});o.getProducts(t,u)}})}else n.products(n.productsSimilar()),n.loading(!1)},getProducts:function(e,t){n.utils.product.getProducts({fields:o.CONSTANTS.FIELDS,ids:e,success:function(e){t(e)},fail:function(e){console.error("Error obteniendo productos",e)}})},searchProducts:function(e,t){var n="search";a.request(n,e,function(e){t(e.resultsList.records)},function(e){console.error("ERR",e)},null)}}})
|