jquery.infinite-scroller.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. define([
  2. 'jquery'
  3. ], function ($) {
  4. "use strict";
  5. $.widget('smartwave.infiniteScroll', {
  6. options: {
  7. productsListSelector: '#layer-product-list',
  8. next_page: '',
  9. loading: false,
  10. infinite_loaded_count: 0,
  11. active: false
  12. },
  13. _create: function () {
  14. this.inFinite();
  15. },
  16. inFinite: function(){
  17. var self = this;
  18. self.options.next_page = "";
  19. if($(self.options.productsListSelector +' .infinite-loader').length > 0){
  20. self.options.active = true;
  21. }
  22. $(".pages-items li > a.next").each(function(){
  23. self.options.next_page = $(this).attr("href");
  24. });
  25. $(window).scroll(function(){
  26. if(!self.options.loading && self.options.active && self.options.next_page && $(window).scrollTop() >= $(".infinite-loader").offset().top-$(window).height()+100){
  27. if(self.options.infinite_loaded_count < 2){
  28. self.options.loading = true;
  29. $(".pages-items li > a.next").each(function(){
  30. self.options.next_page = $(this).attr("href");
  31. });
  32. self.ajaxInfinite(self.options.next_page);
  33. }
  34. }
  35. });
  36. $(".infinite-loader .btn-load-more").click(function(){
  37. if(!self.options.loading && self.options.next_page && self.options.infinite_loaded_count >= 2){
  38. self.options.loading = true;
  39. self.ajaxInfinite(self.options.next_page);
  40. }
  41. });
  42. },
  43. ajaxInfinite: function (submitUrl) {
  44. var self = this;
  45. self.options.infinite_loaded_count++;
  46. $('.infinite-loader .btn-load-more').hide();
  47. $('.infinite-loader .loading').fadeIn(300);
  48. $.ajax({
  49. type: 'GET',
  50. url: submitUrl,
  51. dataType: "html",
  52. beforeSend: function () {
  53. $('.infinite-loader .btn-load-more').hide();
  54. $('.infinite-loader .loading').fadeIn(300);
  55. },
  56. success: function (res) {
  57. self.options.loading = false;
  58. var items = $(res).find(self.options.productsListSelector +' .product-items .item');
  59. var b = $('<div/>').html($(res).find(self.options.productsListSelector));
  60. if (items.length > 0) {
  61. if($(b).find('div.products-grid')){
  62. $(self.options.productsListSelector + ' .products.wrapper').last()
  63. .after($(res).find(self.options.productsListSelector +' div.products-grid.wrapper').detach())
  64. .trigger('contentUpdated');
  65. }
  66. if($(b).find('div.products-list')){
  67. $(self.options.productsListSelector + ' .products.wrapper').last()
  68. .after($(res).find(self.options.productsListSelector +' div.products-list.wrapper').detach())
  69. .trigger('contentUpdated');
  70. }
  71. if($(b).find('.pages a.next').length>0){
  72. $(self.options.productsListSelector + ' .pages a.next').attr('href', $(res).find('.pages a.next').attr('href'));
  73. }else{
  74. $(self.options.productsListSelector + ' .pages a.next').remove();
  75. }
  76. $(b).remove();
  77. $("form[data-role=tocart-form]").catalogAddToCart();
  78. $('.main .products.grid .product-items li.product-item:nth-child(2n)').addClass('nth-child-2n');
  79. $('.main .products.grid .product-items li.product-item:nth-child(2n+1)').addClass('nth-child-2np1');
  80. $('.main .products.grid .product-items li.product-item:nth-child(3n)').addClass('nth-child-3n');
  81. $('.main .products.grid .product-items li.product-item:nth-child(3n+1)').addClass('nth-child-3np1');
  82. $('.main .products.grid .product-items li.product-item:nth-child(4n)').addClass('nth-child-4n');
  83. $('.main .products.grid .product-items li.product-item:nth-child(4n+1)').addClass('nth-child-4np1');
  84. $('.main .products.grid .product-items li.product-item:nth-child(5n)').addClass('nth-child-5n');
  85. $('.main .products.grid .product-items li.product-item:nth-child(5n+1)').addClass('nth-child-5np1');
  86. $('.main .products.grid .product-items li.product-item:nth-child(6n)').addClass('nth-child-6n');
  87. $('.main .products.grid .product-items li.product-item:nth-child(6n+1)').addClass('nth-child-6np1');
  88. $('.main .products.grid .product-items li.product-item:nth-child(7n)').addClass('nth-child-7n');
  89. $('.main .products.grid .product-items li.product-item:nth-child(7n+1)').addClass('nth-child-7np1');
  90. $('.main .products.grid .product-items li.product-item:nth-child(8n)').addClass('nth-child-8n');
  91. $('.main .products.grid .product-items li.product-item:nth-child(8n+1)').addClass('nth-child-8np1');
  92. var hist = submitUrl;
  93. if(submitUrl.indexOf("p=") > -1){
  94. var len = submitUrl.length-submitUrl.indexOf("p=");
  95. var str_temp = submitUrl.substr(submitUrl.indexOf("p="),len);
  96. var page_param = "";
  97. if(str_temp.indexOf("&") == -1){
  98. page_param = str_temp;
  99. } else {
  100. page_param = str_temp.substr(0,str_temp.indexOf("&"));
  101. }
  102. hist = submitUrl.replace(page_param, "");
  103. }
  104. if (typeof window.history.pushState === 'function') {
  105. window.history.pushState({url: hist}, '', hist);
  106. }
  107. if(typeof enable_quickview != 'undefined' && enable_quickview == true) {
  108. requirejs(['jquery', 'weltpixel_quickview' ],
  109. function($, quickview) {
  110. $('.weltpixel-quickview').off('click').on('click', function() {
  111. var prodUrl = $(this).attr('data-quickview-url');
  112. if (prodUrl.length) {
  113. quickview.displayContent(prodUrl);
  114. }
  115. });
  116. });
  117. }
  118. $(".products-grid .weltpixel-quickview").each(function(){
  119. $(this).appendTo($(this).parent().parent().children(".product-item-photo"));
  120. });
  121. self.options.next_page = "";
  122. $(".pages-items li > a.next").each(function(){
  123. self.options.next_page = $(this).attr("href");
  124. });
  125. if(self.options.infinite_loaded_count >= 2){
  126. $('.infinite-loader .loading').hide();
  127. if(self.options.next_page){
  128. $('.infinite-loader .btn-load-more').show();
  129. $(".infinite-loader .btn-load-more").unbind("click").click(function(){
  130. if(!self.options.loading && self.options.next_page && self.options.infinite_loaded_count >= 2){
  131. self.options.loading = true;
  132. self.ajaxInfinite(self.options.next_page);
  133. }
  134. });
  135. }
  136. } else {
  137. $('.infinite-loader .loading').fadeOut(300);
  138. }
  139. }
  140. },
  141. error: function () {
  142. window.location.reload();
  143. }
  144. });
  145. }
  146. });
  147. return $.smartwave.infiniteScroll;
  148. });