2013-05-11 11:44:25 -05:00
|
|
|
/**
|
|
|
|
* Main controller for Ghost frontend
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*global require, module */
|
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
var Ghost = require('../../ghost'),
|
2013-07-11 14:02:18 -05:00
|
|
|
api = require('../api'),
|
2013-05-11 11:44:25 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
ghost = new Ghost(),
|
|
|
|
frontendControllers;
|
2013-05-11 11:44:25 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
frontendControllers = {
|
|
|
|
'homepage': function (req, res) {
|
2013-08-18 12:41:55 -05:00
|
|
|
// Parse the page number
|
|
|
|
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1;
|
2013-08-19 21:18:39 -05:00
|
|
|
|
|
|
|
// No negative pages
|
|
|
|
if (pageParam < 1) {
|
|
|
|
return res.redirect("/page/1/");
|
|
|
|
}
|
|
|
|
|
2013-08-18 12:41:55 -05:00
|
|
|
api.posts.browse({page: pageParam}).then(function (page) {
|
2013-08-21 09:05:17 -05:00
|
|
|
var maxPage = page.pages;
|
|
|
|
|
|
|
|
// A bit of a hack for situations with no content.
|
|
|
|
if (maxPage === 0) {
|
|
|
|
maxPage = 1;
|
|
|
|
page.pages = 1;
|
|
|
|
}
|
|
|
|
|
2013-08-18 12:41:55 -05:00
|
|
|
// If page is greater than number of pages we have, redirect to last page
|
2013-08-21 09:05:17 -05:00
|
|
|
if (pageParam > maxPage) {
|
|
|
|
return res.redirect("/page/" + maxPage + "/");
|
2013-08-18 12:41:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Render the page of posts
|
2013-06-25 06:43:15 -05:00
|
|
|
ghost.doFilter('prePostsRender', page.posts, function (posts) {
|
2013-06-25 10:13:19 -05:00
|
|
|
res.render('index', {posts: posts, pagination: {page: page.page, prev: page.prev, next: page.next, limit: page.limit, total: page.total, pages: page.pages}});
|
2013-05-11 11:44:25 -05:00
|
|
|
});
|
2013-06-25 06:43:15 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
'single': function (req, res) {
|
|
|
|
api.posts.read({'slug': req.params.slug}).then(function (post) {
|
|
|
|
ghost.doFilter('prePostsRender', post.toJSON(), function (post) {
|
2013-07-11 11:57:12 -05:00
|
|
|
res.render('post', {post: post});
|
2013-05-11 11:44:25 -05:00
|
|
|
});
|
2013-06-25 06:43:15 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2013-05-11 11:44:25 -05:00
|
|
|
|
2013-06-25 06:43:15 -05:00
|
|
|
module.exports = frontendControllers;
|