| 1 |
/** |
| 2 |
* Merchant Pagination. |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
'use strict'; |
| 7 |
|
| 8 |
var merchant = merchant || {}; |
| 9 |
merchant.pagination = { |
| 10 |
// Defaults are set to woocommerce pagination, but we can change |
| 11 |
// these defaults to make blog pagination for example |
| 12 |
defaults: { |
| 13 |
button: document.querySelector('.merchant-pagination-button'), |
| 14 |
pagination: document.querySelector('.woocommerce-pagination'), |
| 15 |
next: document.querySelector('.woocommerce-pagination .next'), |
| 16 |
itemsWrapper: document.querySelector('.site-main .products'), |
| 17 |
itemsTag: 'li', |
| 18 |
items: '.site-main .products .product', |
| 19 |
currentPage: 1, |
| 20 |
totalPages: document.querySelector('.merchant-pagination-button') !== null ? parseInt(document.querySelector('.merchant-pagination-button').getAttribute('data-total-pages')) : 0, |
| 21 |
infiniteScroll: document.querySelector('.merchant-pagination-button') !== null && document.querySelector('.merchant-pagination-button').getAttribute('data-pagination-type') === 'infinite-scroll' ? true : false, |
| 22 |
eventPrefix: 'merchant.shop', |
| 23 |
triggerOffset: 200 |
| 24 |
}, |
| 25 |
// To ensure better compatibility with plugins like WP Rocket that has |
| 26 |
// options to defer/lazy-load JS files, each JS script should have your own |
| 27 |
// 'domReady' function. This way the script has no dependecies and can be loaded standalone. |
| 28 |
domReady: function domReady(fn) { |
| 29 |
if (typeof fn !== 'function') { |
| 30 |
return; |
| 31 |
} |
| 32 |
if (document.readyState === 'interactive' || document.readyState === 'complete') { |
| 33 |
return fn(); |
| 34 |
} |
| 35 |
document.addEventListener('DOMContentLoaded', fn, false); |
| 36 |
}, |
| 37 |
init: function init() { |
| 38 |
if (typeof this.defaults.button === 'undefined') { |
| 39 |
return false; |
| 40 |
} |
| 41 |
if (null === this.defaults.pagination) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
var _this = this, |
| 45 |
is_mobile = matchMedia('screen and (max-width: 767px)').matches ? true : false; |
| 46 |
this.ename = is_mobile ? 'touchend' : 'click'; |
| 47 |
this.defaults.button.addEventListener('click', this.loadMoreButtonEventHandler.bind(this)); |
| 48 |
this.defaults.button.addEventListener('touchend', this.loadMoreButtonEventHandler.bind(this)); |
| 49 |
|
| 50 |
// Infinite Scroll |
| 51 |
if (this.defaults.infiniteScroll) { |
| 52 |
this.infiniteScroll(); |
| 53 |
|
| 54 |
// Check if the button it's on view in the first load of the page |
| 55 |
if (this.isAlmostInViewport(this.defaults.button)) { |
| 56 |
this.defaults.button.dispatchEvent(new Event(this.ename)); |
| 57 |
} |
| 58 |
} |
| 59 |
window.dispatchEvent(new Event(_this.defaults.eventPrefix + '.pagination.initialized')); |
| 60 |
}, |
| 61 |
loadMoreButtonEventHandler: function loadMoreButtonEventHandler(e) { |
| 62 |
e.preventDefault(); |
| 63 |
if (!this.defaults.button.classList.contains('loading')) { |
| 64 |
this.loadMorePosts(this.defaults); |
| 65 |
} |
| 66 |
this.defaults.button.classList.add('loading'); |
| 67 |
}, |
| 68 |
loadMorePosts: function loadMorePosts() { |
| 69 |
if (this.defaults.currentPage >= this.defaults.totalPages) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
var _this = this, |
| 73 |
ajax = new XMLHttpRequest(), |
| 74 |
nextURL = this.defaults.next.getAttribute('href'); |
| 75 |
ajax.open('GET', nextURL, true); |
| 76 |
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); |
| 77 |
ajax.onload = function () { |
| 78 |
if (this.status >= 200 && this.status < 400) { |
| 79 |
var html = document.createElement('html'); |
| 80 |
html.innerHTML = this.responseText; |
| 81 |
|
| 82 |
// Masonry |
| 83 |
var is_masonry_layout = typeof Masonry === 'function' && _this.defaults.itemsWrapper.classList.contains('masonry-brick'), |
| 84 |
msnry = is_masonry_layout ? Masonry.data(_this.defaults.itemsWrapper.parentElement) : ''; |
| 85 |
var is_shop_masonry_layout = typeof Masonry === 'function' && _this.defaults.itemsWrapper.classList.contains('masonry'), |
| 86 |
shop_masonry_first_item = is_shop_masonry_layout ? _this.defaults.itemsWrapper.querySelector(_this.defaults.items) : '', |
| 87 |
shop_msnry = is_shop_masonry_layout ? Masonry.data(_this.defaults.itemsWrapper) : ''; |
| 88 |
var items = html.querySelectorAll(_this.defaults.items); |
| 89 |
for (var i = 0; i < items.length; i++) { |
| 90 |
var list_item = document.createElement(_this.defaults.itemsTag); |
| 91 |
list_item.setAttribute('class', items[i].classList.value); |
| 92 |
if (!is_masonry_layout && !is_shop_masonry_layout) { |
| 93 |
list_item.classList.add('mrc-animated'); |
| 94 |
list_item.classList.add('mrcPagFadeInShort'); |
| 95 |
list_item.classList.add('mrc-anim-duration-300ms'); |
| 96 |
list_item.classList.add('mrc-anim-fowards'); |
| 97 |
list_item.setAttribute('style', 'animation-delay: ' + i * 200 + 'ms;'); |
| 98 |
} |
| 99 |
list_item.innerHTML = items[i].innerHTML; |
| 100 |
_this.defaults.itemsWrapper.append(list_item); |
| 101 |
if (is_masonry_layout) { |
| 102 |
msnry.appended(list_item); |
| 103 |
msnry.layout(); |
| 104 |
} else if (is_shop_masonry_layout) { |
| 105 |
list_item.style.width = shop_masonry_first_item.style.width; |
| 106 |
list_item.style.marginBottom = shop_masonry_first_item.style.marginBottom; |
| 107 |
shop_msnry.appended(list_item); |
| 108 |
shop_msnry.layout(); |
| 109 |
} |
| 110 |
} |
| 111 |
_this.defaults.button.classList.remove('loading'); |
| 112 |
_this.maybeInitExtraFeatures(); |
| 113 |
_this.updateNextURL(); |
| 114 |
|
| 115 |
// Infinite Scroll |
| 116 |
if (_this.defaults.infiniteScroll && _this.isAlmostInViewport(_this.defaults.button)) { |
| 117 |
_this.defaults.button.dispatchEvent(new Event('click')); |
| 118 |
} |
| 119 |
_this.hideButton(); |
| 120 |
window.dispatchEvent(new Event(_this.defaults.eventPrefix + '.pagination.items.added')); |
| 121 |
} |
| 122 |
}; |
| 123 |
ajax.send(); |
| 124 |
}, |
| 125 |
updateNextURL: function updateNextURL() { |
| 126 |
var nextPage = this.defaults.currentPage < this.defaults.totalPages ? this.defaults.currentPage + 1 : this.defaults.currentPage, |
| 127 |
nextURL = this.defaults.next.getAttribute('href').replace('/page/' + nextPage, '/page/' + (nextPage + 1)); |
| 128 |
if (this.defaults.next.getAttribute('href').indexOf('paged=') > 0) { |
| 129 |
nextURL = this.defaults.next.getAttribute('href').replace('paged=' + nextPage, 'paged=' + (nextPage + 1)); |
| 130 |
} else if (this.defaults.next.getAttribute('href').indexOf('/comment-page-') > 0) { |
| 131 |
nextURL = this.defaults.next.getAttribute('href').replace('/comment-page-' + nextPage, '/comment-page-' + (nextPage + 1)); |
| 132 |
} else if (this.defaults.next.getAttribute('href').indexOf('cpage=') > 0) { |
| 133 |
nextURL = this.defaults.next.getAttribute('href').replace('cpage=' + nextPage, 'cpage=' + (nextPage + 1)); |
| 134 |
} |
| 135 |
this.defaults.currentPage++; |
| 136 |
this.defaults.next.setAttribute('href', nextURL); |
| 137 |
}, |
| 138 |
hideButton: function hideButton() { |
| 139 |
if (this.defaults.currentPage >= this.defaults.totalPages) { |
| 140 |
this.defaults.button.remove(); |
| 141 |
} |
| 142 |
}, |
| 143 |
infiniteScroll: function infiniteScroll() { |
| 144 |
var _this = this; |
| 145 |
window.addEventListener('scroll', function () { |
| 146 |
if (_this.isAlmostInViewport(_this.defaults.button)) { |
| 147 |
_this.defaults.button.dispatchEvent(new Event(_this.ename)); |
| 148 |
} |
| 149 |
}); |
| 150 |
}, |
| 151 |
maybeInitExtraFeatures: function maybeInitExtraFeatures() { |
| 152 |
// Quick View |
| 153 |
if (document.querySelector('.merchant-quick-view-popup') !== null) { |
| 154 |
if (typeof merchant.quickView !== 'undefined') { |
| 155 |
merchant.quickView.init(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// Wishlist |
| 160 |
if (document.querySelector('.merchant-wishlist-button') !== null) { |
| 161 |
if (typeof merchant.wishList !== 'undefined') { |
| 162 |
merchant.wishList.init(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// AddToAny Share Buttons |
| 167 |
if (typeof a2a !== 'undefined') { |
| 168 |
a2a.init_all(); |
| 169 |
} |
| 170 |
}, |
| 171 |
isAlmostInViewport: function isAlmostInViewport(el) { |
| 172 |
var rect = el.getBoundingClientRect(); |
| 173 |
return rect.bottom - this.defaults.triggerOffset <= (window.innerHeight || document.documentElement.clientHeight); |
| 174 |
} |
| 175 |
}; |
| 176 |
|
| 177 |
// Initialize. |
| 178 |
merchant.pagination.domReady(setTimeout(function () { |
| 179 |
merchant.pagination.init(); |
| 180 |
}, 100)); |