From d40858f14c575d85ac7fbacfb9cd8c00093fb56d Mon Sep 17 00:00:00 2001 From: Oscar Cortez Date: Sun, 19 Jul 2015 11:56:00 -0600 Subject: [PATCH] Native javascript pagination --- js/site.js | 65 ++++++++++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/js/site.js b/js/site.js index 878ff5a..6fa0e51 100644 --- a/js/site.js +++ b/js/site.js @@ -1,66 +1,49 @@ -//$(".loadMore").click(loadMorePosts); var _this = document.getElementById("js-load-more"); _this.onclick = loadMorePosts; var HttpClient = function() { + 'use strict'; this.get = function(Url, Callback) { var HttpRequest = new XMLHttpRequest(); HttpRequest.onreadystatechange = function() { if (HttpRequest.readyState == 4 && HttpRequest.status == 200) Callback(HttpRequest.responseText); - } + }; - HttpRequest.open( "GET", Url, true ); - HttpRequest.send( null ); - } -} + HttpRequest.open("GET", Url, true); + HttpRequest.send(null); + }; +}; Element.prototype.remove = function() { this.parentElement.removeChild(this); -} +}; NodeList.prototype.remove = HTMLCollection.prototype.remove = function() { - for(var i = this.length - 1; i >= 0; i--) { - if(this[i] && this[i].parentElement) { + for (var i = this.length - 1; i >= 0; i--) { + if (this[i] && this[i].parentElement) { this[i].parentElement.removeChild(this[i]); } } -} +}; function loadMorePosts() { - //var _this = document.getElementById("js-load-more"); - var blogContainer = document.getElementById("js-post-container"); //$("#js-post-container"); - var nextPage = parseInt(blogContainer.dataset.page) + 1; //parseInt($blogContainer.attr("data-page")) + 1; - var totalPages = parseInt(blogContainer.dataset.totalPages); //parseInt($blogContainer.attr("data-totalPages")); + var blogContainer = document.getElementById("js-post-container"); + var nextPage = parseInt(blogContainer.dataset.page) + 1; - _this.className += " loading"; + _this.className += " loading"; - Client = new HttpClient(); - Client.get('/page' + nextPage, function(data) { - parser = new DOMParser(); - var htmlData = parser.parseFromString(data, "text/html");; - var articles = htmlData.getElementsByTagName("article"); - console.log(articles); - blogContainer.innerHTML += data; + Client = new HttpClient(); + Client.get('/page' + nextPage, function(data) { + parser = new DOMParser(); + var htmlData = parser.parseFromString(data, "text/html"); + var articles = htmlData.getElementsByTagName("article"); + blogContainer.parentNode.insertBefore(articles[0], blogContainer.nextSibling); - if (blogContainer.dataset.totalPages == nextPage) { - document.getElementById("js-load-more").remove(); - } - _this.className = ""; + if (blogContainer.dataset.totalPages == nextPage) { + document.getElementById("js-load-more").remove(); + } + _this.className = ""; - }); - // $.get("/page" + nextPage, function (data) { - // var htmlData = $.parseHTML(data); - // console.log(htmlData); - // var $articles = $(htmlData).find("article"); - - // blogContainer.attr("data-page", nextPage).append($articles); - - // if (blogContainer.attr("data-totalPages") == nextPage) { - // $(".loadMore").remove(); - // } - - // $(_this).removeClass("loading"); - // }); + }); } -//---- native js----