82 lines
3.0 KiB
JavaScript
82 lines
3.0 KiB
JavaScript
/* This script implements HTML5 History states for clients that support it. It parses all links for those beginning in "?"(local), replaces with History-browsing functionality, and onclick updates the "content" div (and reparses accordingly). */
|
|
var hist = {
|
|
content: null,
|
|
crumbs: null,
|
|
title: 'kettek',
|
|
Go: function() {
|
|
if (window.history.replaceState === undefined) return;
|
|
this.content = document.getElementById("main_content");
|
|
//document.title = this.title + ' ' + this.getFirstHeaderText(this.content);
|
|
this.crumbs = document.getElementById('crumbs');
|
|
window.history.replaceState({content: this.content.innerHTML, url: window.location.href, title: document.title}, document.title, window.location.href);
|
|
this.doParse();
|
|
window.onpopstate = this.popState;
|
|
},
|
|
doParse: function(ele) {
|
|
hist.buildCrumbs();
|
|
var l = (typeof ele === 'undefined' ? document.getElementsByTagName('A') : ele.getElementsByTagName('A'));
|
|
for (var i = 0; i < l.length; i++) {
|
|
if (!l[i].href) continue;
|
|
if (l[i].host !== window.location.host) continue;
|
|
var last = l[i].href.lastIndexOf('/');
|
|
var area = l[i].href.substr(last+1);
|
|
if (area.indexOf('.') > -1) continue; // ignore files with extensions
|
|
if (l[i].hash) {
|
|
if (window.location.host+window.location.pathname == l[i].host+l[i].pathname) continue;
|
|
}
|
|
l[i].onclick = function() {
|
|
hist.loadContent(this.getAttribute("href"));
|
|
return false;
|
|
};
|
|
}
|
|
img2popup.doParse(hist.content);
|
|
},
|
|
loadContent: function(target) {
|
|
if (target[target.length-1] === '/') {
|
|
window.location.href = target;
|
|
return;
|
|
}
|
|
var r = new XMLHttpRequest();
|
|
r.open("GET", target+'?QRULE=@@CONTENT@@', true);
|
|
r.onreadystatechange = (function(r, target) { return function() {
|
|
switch (r.readyState) {
|
|
case 4:
|
|
document.title = hist.title+' '+target;
|
|
window.history.pushState({content: r.responseText, url: target, title: hist.title+' '+target}, target, target);
|
|
hist.content.innerHTML = r.responseText;
|
|
hist.doParse(hist.content);
|
|
break;
|
|
}
|
|
}})(r, target);
|
|
r.send();
|
|
},
|
|
popState: function(e) {
|
|
if (e.state == null) return;
|
|
document.title = e.state.title;
|
|
hist.content.innerHTML = e.state.content;
|
|
hist.doParse(hist.content);
|
|
},
|
|
buildCrumbs: function() {
|
|
hist.crumbs.innerHTML = '';
|
|
var parts = window.location.pathname.split('/');
|
|
var path = '';
|
|
parts[0] = '>';
|
|
for (var i = 0; i < parts.length; i++) {
|
|
if (parts[i] == '') continue;
|
|
path += (i == 0 ? '/' : parts[i]);
|
|
var li = document.createElement('li');
|
|
var a = document.createElement('a');
|
|
a.href = path;
|
|
a.innerHTML = parts[i];
|
|
li.appendChild(a);
|
|
hist.crumbs.appendChild(li);
|
|
path += (i == 0 ? '' : '/');
|
|
}
|
|
},
|
|
getFirstHeaderText: function(el) {
|
|
var els = el.getElementsByTagName('H1');
|
|
if (els.length >= 1) return els[0].innerHTML;
|
|
return '';
|
|
}
|
|
};
|