gists/2015.js.responsive-loader.js

113 lines
2.9 KiB
JavaScript

/*jslint browser: true, indent: 4 */
/*global updateCanvas: true */
(function (g) {
"use strict";
var lookForNextPage,
getNextPage,
appendContent,
loadManually,
manual,
loader,
working;
if (g.ScrollGuard === undefined) {
throw new Error("scrollguard.js is required!");
}
appendContent = function (content) {
var body, rows, i, max, newContent, table;
body = document.createElement("div");
body.innerHTML = content;
newContent = document.createDocumentFragment();
rows = body.querySelectorAll(".main-c table tr");
for (i = 1, max = rows.length; i < max; i += 1) {
newContent.appendChild(rows[i]);
}
table = document.querySelector(".main-c table").tBodies[0];
table.replaceChild(newContent, table.querySelector("tr.pagination"));
if (typeof(updateCanvas) === "function") {
updateCanvas();
}
loadManually();
};
loadManually = function () {
var button;
button = document.getElementById("load-manually");
if (button) {
if (!document.getElementById("pager-next")) {
button.parentNode.removeChild(button);
}
button.onclick = function () {
var nextPage = document.getElementById("pager-next");
if (nextPage) {
getNextPage(nextPage.href);
button.parentNode.replaceChild(loader.cloneNode(), button);
}
};
}
};
manual = function () {
var style, elm;
elm = document.getElementById("load-manually");
if (!elm) { return false; }
style = window.getComputedStyle(elm, null);
return style.getPropertyValue("display") !== "none";
};
lookForNextPage = function () {
if (!manual()) {
var nextPage = document.getElementById("pager-next");
if (nextPage) {
getNextPage(nextPage.href);
}
}
};
getNextPage = function (href) {
var request = new XMLHttpRequest();
if (working === true) {
return;
}
working = true;
request.onreadystatechange = function () {
if (request.readyState === 4) {
working = false;
if (request.status === 200 || request.status === 304) {
appendContent(request.responseText);
}
}
};
request.open("GET", href, true);
request.send(null);
};
loadManually();
loader = document.createElement("img");
loader.style.paddingTop = "14px";
loader.src = "/templates/wide/css/images/loader.gif";
if (document.getElementById("pager-next")) {
g.ScrollGuard.add(lookForNextPage, -1);
g.ScrollGuard.start();
}
}(this));