2019-02-15 14:13:43 +01:00
|
|
|
function Navigation(modules) {
|
2019-02-15 18:07:59 +01:00
|
|
|
var articles = modules.cache.make(function(key) {
|
|
|
|
var url = ["", blog.path.articlesPath, key + '.md'].join('/');
|
2019-02-15 14:13:43 +01:00
|
|
|
return modules.async.bind(
|
|
|
|
modules.async.http({method: 'GET', url: url}),
|
|
|
|
function(queryResult) {
|
|
|
|
if(queryResult.status == 200) {
|
|
|
|
return modules.async.wrap(queryResult.responseText);
|
|
|
|
} else {
|
|
|
|
return modules.async.fail(
|
|
|
|
"Could not load article " + url + " (" + queryResult.status + " " + queryResult.statusText + ")"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
window.addEventListener('popstate', function(e) {navigate(e.state.url);});
|
|
|
|
history.replaceState({url: window.location.pathname}, 'Blog - title', window.location.pathname);
|
|
|
|
return {
|
|
|
|
hijackLinks: hijackLinks
|
|
|
|
};
|
|
|
|
|
|
|
|
function hijackLinks(domElem) {
|
|
|
|
domElem = domElem || document;
|
|
|
|
var links = domElem.getElementsByTagName('a');
|
|
|
|
for(var i = 0; i < links.length; i++) {
|
|
|
|
var a = links[i];
|
|
|
|
if(a.classList.contains("navigation")) {
|
|
|
|
a.addEventListener('click', visit(a.getAttribute("href")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function visit(url) {
|
|
|
|
return function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
history.pushState({url: url}, 'Blog - title', url);
|
|
|
|
navigate(url);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function navigate(url) {
|
|
|
|
var path = url.split("/").slice(1);
|
|
|
|
if(blog.tags[path[0]] != undefined) {
|
|
|
|
show(getArticlesList(path[0], path[1] == "all.html"));
|
2019-02-15 18:07:59 +01:00
|
|
|
} else if(path[0] == blog.path.articlesPath) {
|
|
|
|
show(getArticle(path[1].replace(/\.html$/, '')));
|
2019-02-15 14:13:43 +01:00
|
|
|
} else {
|
|
|
|
show(getArticlesList(null, path[0] == "all.html"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 18:07:59 +01:00
|
|
|
function getArticle(key) {
|
2019-02-15 14:13:43 +01:00
|
|
|
return modules.async.bind(
|
2019-02-15 18:07:59 +01:00
|
|
|
articles.get(key),
|
2019-02-15 14:13:43 +01:00
|
|
|
modules.async.map(
|
2019-02-15 18:07:59 +01:00
|
|
|
function(contents) {return [modules.domRenderer.article(key, contents)];}
|
2019-02-15 14:13:43 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-15 18:07:59 +01:00
|
|
|
function preview(key) {
|
2019-02-15 14:13:43 +01:00
|
|
|
return modules.async.bind(
|
2019-02-15 18:07:59 +01:00
|
|
|
articles.get(key),
|
2019-02-15 14:13:43 +01:00
|
|
|
function(contents) {
|
|
|
|
return modules.async.wrap(
|
|
|
|
modules.domRenderer.article(
|
2019-02-15 18:07:59 +01:00
|
|
|
key,
|
2019-02-15 14:13:43 +01:00
|
|
|
contents,
|
|
|
|
blog.skin.previewLinesCount
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function articleIds(tag, all) {
|
|
|
|
var ids = tag != undefined ? blog.tags[tag] : Object.keys(blog.articles);
|
|
|
|
ids.sort(function(idA, idB) {return blog.articles[idB].date - blog.articles[idA].date;});
|
|
|
|
return ids.slice(0, all ? undefined : blog.skin.previewArticlesCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArticlesList(tag, all) {
|
|
|
|
return modules.async.bind(
|
|
|
|
modules.async.parallel.apply(null, articleIds(tag, all).map(preview)),
|
|
|
|
modules.async.map(modules.domRenderer.articlesList(tag, all))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function show(contents) {
|
|
|
|
modules.async.run(
|
|
|
|
modules.async.bind(
|
|
|
|
contents,
|
|
|
|
modules.async.map(function (domElems) {
|
|
|
|
domElems = domElems.filter(modules.fun.defined);
|
|
|
|
var div = document.getElementById('contents');
|
|
|
|
modules.dom.clear(div);
|
|
|
|
for(var i = 0; i < domElems.length; i++) {
|
|
|
|
div.appendChild(domElems[i]);
|
|
|
|
}
|
|
|
|
hijackLinks(div);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|