mirror of
https://github.com/rr-/szurubooru.git
synced 2025-07-17 08:26:24 +00:00
Switched to spaces
This commit is contained in:
@ -3,83 +3,83 @@ App.Services = App.Services || {};
|
||||
|
||||
App.Services.PostsAroundCalculator = function(_, promise, util, pager) {
|
||||
|
||||
pager.init({url: '/posts'});
|
||||
pager.init({url: '/posts'});
|
||||
|
||||
function resetCache() {
|
||||
pager.resetCache();
|
||||
}
|
||||
function resetCache() {
|
||||
pager.resetCache();
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
if (position === -1) {
|
||||
resolve(null, null);
|
||||
}
|
||||
if (position === -1) {
|
||||
resolve(null, null);
|
||||
}
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
}
|
||||
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();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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())));
|
||||
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())));
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
var url = util.appendComplexRouteParam(
|
||||
'#/post/' + post.id,
|
||||
util.simplifySearchQuery(
|
||||
_.extend(
|
||||
{page: page + direction},
|
||||
pager.getSearchParams())));
|
||||
var url = util.appendComplexRouteParam(
|
||||
'#/post/' + post.id,
|
||||
util.simplifySearchQuery(
|
||||
_.extend(
|
||||
{page: page + direction},
|
||||
pager.getSearchParams())));
|
||||
|
||||
resolve(url);
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
}).fail(function() {
|
||||
reject();
|
||||
});
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
resolve(url);
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
}).fail(function() {
|
||||
reject();
|
||||
});
|
||||
} else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
resetCache: resetCache,
|
||||
getLinksToPostsAround: getLinksToPostsAround,
|
||||
};
|
||||
return {
|
||||
resetCache: resetCache,
|
||||
getLinksToPostsAround: getLinksToPostsAround,
|
||||
};
|
||||
};
|
||||
|
||||
App.DI.register('postsAroundCalculator', ['_', 'promise', 'util', 'pager'], App.Services.PostsAroundCalculator);
|
||||
|
Reference in New Issue
Block a user