2014-10-05 10:09:02 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Services = App.Services || {};
|
|
|
|
|
|
|
|
App.Services.PostsAroundCalculator = function(_, promise, util, pager) {
|
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
pager.init({url: '/posts'});
|
2014-10-05 10:09:02 +02:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
function resetCache() {
|
|
|
|
pager.resetCache();
|
|
|
|
}
|
2014-10-05 10:09:02 +02:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
function getLinksToPostsAround(query, postId) {
|
|
|
|
return promise.make(function(resolve, reject) {
|
|
|
|
pager.setSearchParams(query);
|
|
|
|
pager.setPage(query.page);
|
|
|
|
promise.wait(pager.retrieveCached())
|
|
|
|
.then(function(response) {
|
|
|
|
var postIds = _.pluck(response.entities, 'id');
|
|
|
|
var position = _.indexOf(postIds, postId);
|
2014-10-05 10:09:02 +02:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
if (position === -1) {
|
|
|
|
resolve(null, null);
|
|
|
|
}
|
2014-10-05 10:09:02 +02:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
promise.wait(
|
|
|
|
getLinkToPostAround(postIds, position, query.page, -1),
|
|
|
|
getLinkToPostAround(postIds, position, query.page, 1))
|
|
|
|
.then(function(nextPostUrl, prevPostUrl) {
|
|
|
|
resolve(nextPostUrl, prevPostUrl);
|
|
|
|
}).fail(function() {
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
}).fail(function() {
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-10-05 10:09:02 +02:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
function getLinkToPostAround(postIds, position, page, direction) {
|
|
|
|
return promise.make(function(resolve, reject) {
|
|
|
|
if (position + direction >= 0 && position + direction < postIds.length) {
|
|
|
|
var url = util.appendComplexRouteParam(
|
|
|
|
'#/post/' + postIds[position + direction],
|
|
|
|
util.simplifySearchQuery(
|
|
|
|
_.extend(
|
|
|
|
{page: page},
|
|
|
|
pager.getSearchParams())));
|
2014-12-20 10:36:29 +01:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
resolve(url);
|
|
|
|
} else if (page + direction >= 1) {
|
|
|
|
pager.setPage(page + direction);
|
|
|
|
promise.wait(pager.retrieveCached())
|
|
|
|
.then(function(response) {
|
|
|
|
if (response.entities.length) {
|
|
|
|
var post = direction === - 1 ?
|
|
|
|
_.last(response.entities) :
|
|
|
|
_.first(response.entities);
|
2014-10-05 10:09:02 +02:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
var url = util.appendComplexRouteParam(
|
|
|
|
'#/post/' + post.id,
|
|
|
|
util.simplifySearchQuery(
|
|
|
|
_.extend(
|
|
|
|
{page: page + direction},
|
|
|
|
pager.getSearchParams())));
|
2014-12-20 10:36:29 +01:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
resolve(url);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
}).fail(function() {
|
|
|
|
reject();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-10-05 10:09:02 +02:00
|
|
|
|
2015-06-28 10:07:11 +02:00
|
|
|
return {
|
|
|
|
resetCache: resetCache,
|
|
|
|
getLinksToPostsAround: getLinksToPostsAround,
|
|
|
|
};
|
2014-10-05 10:09:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
App.DI.register('postsAroundCalculator', ['_', 'promise', 'util', 'pager'], App.Services.PostsAroundCalculator);
|