2021-01-20 13:18:45 +01:00
|
|
|
import {articlesList, getResource, render} from DomRenderer;
|
2020-01-11 09:05:32 +01:00
|
|
|
import blog from Hablo.Config;
|
2020-01-10 18:56:59 +01:00
|
|
|
import * as Async from UnitJS.Async;
|
|
|
|
import * as Cache from UnitJS.Cache;
|
|
|
|
import * as Dom from UnitJS.Dom;
|
|
|
|
import * as Fun from UnitJS.Fun;
|
|
|
|
|
2020-12-13 20:09:23 +01:00
|
|
|
var cache = {};
|
|
|
|
['article', 'page'].forEach(function(contentType) {
|
|
|
|
cache[contentType] = Cache.make(function(key) {
|
|
|
|
var url = ["", blog.path[contentType + 'sPath'], key + '.md'].join('/');
|
|
|
|
return Async.bind(
|
|
|
|
Async.http({method: 'GET', url: url}),
|
|
|
|
function(queryResult) {
|
|
|
|
if(queryResult.status == 200) {
|
|
|
|
return Async.wrap(queryResult.responseText);
|
|
|
|
} else {
|
|
|
|
return Async.fail(
|
|
|
|
"Could not load " + contentType + " " + url + " (" + queryResult.status + " " + queryResult.statusText + ")"
|
|
|
|
);
|
|
|
|
}
|
2020-01-10 18:56:59 +01:00
|
|
|
}
|
2020-12-13 20:09:23 +01:00
|
|
|
);
|
|
|
|
});
|
2020-01-10 18:56:59 +01:00
|
|
|
});
|
2020-12-13 20:09:23 +01:00
|
|
|
|
2020-01-10 18:56:59 +01:00
|
|
|
window.addEventListener('popstate', function(e) {
|
|
|
|
if(e.state != undefined) {
|
|
|
|
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];
|
|
|
|
var href = a.getAttribute("href");
|
|
|
|
if((href[0] == "/" && href.slice(-3) != ".md") || href[0] == "#") {
|
|
|
|
a.addEventListener('click', visit(a.getAttribute("href")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function visit(url) {
|
|
|
|
return function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if(url[0] == '#') {
|
|
|
|
window.location = url;
|
|
|
|
history.replaceState({url: window.location.pathname}, 'Blog - title', url);
|
|
|
|
} else {
|
|
|
|
navigate(url);
|
|
|
|
history.pushState({url: url}, 'Blog - title', url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function navigate(url) {
|
2021-01-20 13:18:45 +01:00
|
|
|
var resource = getResource(url);
|
|
|
|
switch(resource.type) {
|
|
|
|
case 'list': show(getArticlesList(resource)); break;
|
|
|
|
case 'article':
|
|
|
|
case 'page': show(getCached(resource)); break;
|
|
|
|
default: console.log("No idea how to navigate to " + url);
|
2020-01-10 18:56:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:18:45 +01:00
|
|
|
function getCached(resource) {
|
2020-01-10 18:56:59 +01:00
|
|
|
return Async.bind(
|
2021-01-20 13:18:45 +01:00
|
|
|
cache[resource.type].get(resource.key),
|
2020-01-10 18:56:59 +01:00
|
|
|
Async.map(
|
2021-01-20 13:18:45 +01:00
|
|
|
function(contents) {return [render(resource, contents)];}
|
2020-01-10 18:56:59 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function preview(key) {
|
|
|
|
return Async.bind(
|
2020-12-13 20:09:23 +01:00
|
|
|
cache.article.get(key),
|
2020-01-10 18:56:59 +01:00
|
|
|
function(contents) {
|
|
|
|
return Async.wrap(
|
2021-01-20 13:18:45 +01:00
|
|
|
render({type: 'article', key: key}, contents, blog.skin.previewLinesCount)
|
2020-01-10 18:56:59 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:18:45 +01:00
|
|
|
function articleIds(resource) {
|
|
|
|
var ids = resource.tag != undefined ? blog.tags[resource.tag] : Object.keys(blog.articles);
|
2020-01-10 18:56:59 +01:00
|
|
|
var reverseDate = function (id) {return -blog.articles[id].metadata.date;};
|
|
|
|
ids.sort(Fun.compare(reverseDate));
|
2021-01-20 13:18:45 +01:00
|
|
|
return ids.slice(0, resource.all ? undefined : blog.skin.previewArticlesCount);
|
2020-01-10 18:56:59 +01:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:18:45 +01:00
|
|
|
function getArticlesList(resource) {
|
2020-01-10 18:56:59 +01:00
|
|
|
return Async.bind(
|
2021-01-20 13:18:45 +01:00
|
|
|
Async.parallel.apply(null, articleIds(resource).map(preview)),
|
|
|
|
Async.map(articlesList(resource))
|
2020-01-10 18:56:59 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function show(contents) {
|
|
|
|
Async.run(
|
|
|
|
Async.bind(
|
|
|
|
contents,
|
|
|
|
Async.map(function (domElems) {
|
|
|
|
domElems = domElems.filter(Fun.defined);
|
|
|
|
var div = document.getElementById('contents');
|
|
|
|
Dom.clear(div);
|
|
|
|
for(var i = 0; i < domElems.length; i++) {
|
|
|
|
div.appendChild(domElems[i]);
|
|
|
|
}
|
|
|
|
hijackLinks(div);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|