let dragButton = document.querySelector('div.drag-see-more-container'); let article, widgetwrapper, counter = 0, all_counter = 0, flagged = false, url_flag = true, offset, nextPostId, nextPostUrl, nextPostTitle; article = document.querySelector('article.post'); widgetwrapper = document.querySelector('.widget-wrapper'); if (dragButton) { //article = document.querySelector('article.post'); offset = widgetwrapper.offsetTop; nextPostId = article.dataset.next; nextPostUrl = article.dataset.url; nextPostTitle = article.dataset.title; nextPostPermalink = article.dataset.permalink; window.addEventListener('scroll', () => { offset = widgetwrapper.offsetTop + article.parentNode.offsetTop; if (window.pageYOffset + 300 > offset && flagged === false && counter < 5) { flagged = true; jQuery.ajax({ url: nx_loadmore_params.ajaxurl, //url: article.dataset.permalink.replace(/\/$/, "") + '/nxinfinite', type: 'POST', data: { 'nextPost': nextPostId, 'action': 'single_post_pagination' }, success: function (data) { if (data) { //max 2 times counter++; //update the ui updateUi(data); //set flag to false the for next fetch } else { dragButton.remove(); // if no data, remove the button as well } } }); } else if (counter >= 5) { dragButton.remove(); } if (jQuery('article.active-post').length > 0) { if (window.pageYOffset + 300 > jQuery('article.active-post').offset().top && url_flag === true) { url_flag = false; all_counter++; var bm = $('.nav-socials .bookmark-add'); bm.attr("data-id",nextPostId); bm.attr("data-permalink",nextPostPermalink); bm.attr("data-title",nextPostTitle); history.pushState({}, '', '../' + nextPostUrl + '/'); document.querySelector('.top-header-title').textContent = nextPostTitle; document.title = nextPostTitle; if (typeof ga === 'function') { ga('set', { page: nextPostPermalink.replace("https://kathimerini.nxcode.gr", ""), title: nextPostTitle }); ga('send', 'pageview'); } } console.log(all_counter); } }); } const updateUi = data => { //render next post // dragButton.insertAdjacentHTML('beforeBegin', data); $(data).insertBefore(dragButton); nextPostPermalink = article.dataset.permalink; nextPostUrl = article.dataset.url; nextPostTitle = article.dataset.title; article = document.querySelector('article#post-' + nextPostId); widgetwrapper = article.parentNode.parentNode.parentNode.querySelector('.widget-wrapper'); nextPostId = article.dataset.next; jQuery('article').removeClass('active-post'); article.classList.add('active-post'); offset = widgetwrapper.offsetTop + article.parentNode.parentNode.offsetTop; console.log(offset); url_flag = true; flagged = false; //target next article //change url and title for the next post //history.pushState({}, '', `../${nextPostUrl}/`); //document.title = nextPostTitle; }; $('.nx_loadmore').click(function(){ var button = $(this), data = { 'action': 'loadposts', 'query': nx_loadmore_params.posts, // that's how we get params from wp_localize_script() function 'page' : nx_loadmore_params.current_page, 'advs' : nx_loadmore_params.adv_search }; console.log(nx_loadmore_params.adv_search); $.ajax({ // you can also use $.post here url : nx_loadmore_params.ajaxurl, // AJAX handler data : data, type : 'POST', beforeSend : function ( xhr ) { button.text('Loading'); // change the button text, you can also add a preloader image }, success : function( data ){ if( data ) { //button.text( 'ΔΕΙΤΕ ΠΕΡΙΣΣΟΤΕΡΑ' ).before(data); // insert new posts button.text( 'More' ).parent().parent().before(data); // insert new posts console.log(button.text( 'More' ).parent().parent()); nx_loadmore_params.current_page++; if ( nx_loadmore_params.current_page == nx_loadmore_params.max_page ) button.remove(); // if last page, remove the button // you can also fire the "post-load" event here if you use a plugin that requires it // $( document.body ).trigger( 'post-load' ); } else { button.remove(); // if no data, remove the button as well } } }); });