-
Notifications
You must be signed in to change notification settings - Fork 3
/
ldf.js
137 lines (136 loc) · 3.73 KB
/
ldf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var ldf = {
mainselector: "#ldf",
notfound: "<div>Page not Found</div>",
waitselector: "link[rel='stylesheet'],:not(script):not(iframe)[src]",
pagedir: "pages",
baseurl: "",
begin: undefined,
end: undefined,
hash: true,
nav: function (location) {
if (ldf.hash && !location.startsWith("#") && location != "/") {
location = "#" + location;
}
if (ldf.baseurl != "" && location == "/") location = "";
history.pushState(null, "", ldf.baseurl + location);
ldf.locchange();
},
locchange: function () {
if (ldf.begin) ldf.begin();
var query, loc;
if (ldf.hash) {
query = document.location.hash.split("\?")[1] || "";
query = query ? "?" + query : "";
loc = document.location.hash;
} else {
query = document.location.search || "";
loc = document.location.pathname;
}
var matches = loc.match(/(?<=#|\/)[^?|\/]*/);
if (matches != null && matches.length > 0) loc = matches.pop();
if (loc == "") loc = "index";
ldf.load(ldf.mainselector, loc, query);
},
load: function (selector, loc, query) {
if (!loc.startsWith("/")) {
loc = "/" + loc;
}
loc = ldf.pagedir + loc + loc + ".html";
loc += query;
ldf.helpers.request(loc, function (succ, content) {
if (succ || ldf.notfound == undefined) {
ldf.change(selector, content);
} else {
ldf.change(selector, ldf.notfound);
}
});
},
change: function (selector, content) {
var ldfnode = document.querySelector(selector);
var ldfclone = ldfnode.cloneNode();
var ended = false;
ldfclone.innerHTML = content;
ldfclone.style.display = "none";
ldfnode.parentElement.insertBefore(ldfclone, ldfnode);
ldf.helpers.waitForLoad(ldfclone.querySelectorAll(ldf.waitselector),
function () {
if (!ended) {
ended = true;
ldfnode.remove();
ldfclone.style.display = "";
if (ldf.end) ldf.end();
}
}
);
ldf.updatePageLinks();
ldf.loadScripts(ldfclone);
},
updatePageLinks: function () {
document.querySelectorAll("a").forEach(function (a) {
var className = a.className;
var href = a.href;
if (className.includes("ldfignore")) return;
if (href.startsWith("javascript:")) return;
if (className.includes("ldfinclude") || (href && (href.includes(location.href.split("#")[0]) || a.getAttribute("href") == "/") && !href.split("/").pop().match(/\.(?!html)/))) {
a.removeEventListener("click", ldf.helpers.listener);
a.addEventListener("click", ldf.helpers.listener);
}
});
},
loadScripts: function (node) {
node.querySelectorAll("script").forEach(function (old) {
var src = old.getAttribute("src");
var parent = old.parentNode;
parent.removeChild(old);
var script = document.createElement("script");
script.setAttribute("src", src);
parent.appendChild(script);
});
},
helpers: {
waitForLoad: function (loadings, cb) {
if (loadings.length > 0) {
var loadedcounter = 0;
var onloadOrError = function () {
loadedcounter++;
if (loadedcounter == loadings.length) {
cb();
}
}
loadings.forEach(function (loading) {
loading.onload = onloadOrError;
loading.onerror = onloadOrError;
});
} else {
cb();
}
},
listener: function (e) {
e.preventDefault();
var loc = "";
if (e.target.tagName === "A") {
loc = e.target.getAttribute("href");
} else {
for (var i = 1; i < e.path.length; i++) {
if (e.path[i].tagName === "A") {
loc = e.path[i].getAttribute("href");
break;
}
}
}
ldf.nav(loc);
},
request: function (loc, cb) {
var req = new XMLHttpRequest();
req.open("GET", loc);
req.send();
req.onreadystatechange = function () {
if (req.readyState === 4) {
cb(req.status.toString().startsWith("2"), req.response);
}
};
}
}
};
onpopstate = ldf.locchange;
onload = ldf.locchange;