szurubooru/client/js/controllers/post_list_controller.js

121 lines
4 KiB
JavaScript
Raw Normal View History

'use strict';
const api = require('../api.js');
const settings = require('../models/settings.js');
2016-06-03 19:12:10 +02:00
const misc = require('../util/misc.js');
const PostList = require('../models/post_list.js');
const topNavigation = require('../models/top_navigation.js');
const PageController = require('../controllers/page_controller.js');
const PostsHeaderView = require('../views/posts_header_view.js');
const PostsPageView = require('../views/posts_page_view.js');
const EmptyView = require('../views/empty_view.js');
const fields = [
'id', 'thumbnailUrl', 'type',
'score', 'favoriteCount', 'commentCount', 'tags', 'version'];
2016-06-13 23:10:53 +02:00
class PostListController {
constructor(ctx) {
if (!api.hasPrivilege('posts:list')) {
this._view = new EmptyView();
this._view.showError('You don\'t have privileges to view posts.');
return;
}
topNavigation.activate('posts');
topNavigation.setTitle('Listing posts');
2016-07-05 21:20:28 +02:00
this._ctx = ctx;
this._pageController = new PageController();
this._headerView = new PostsHeaderView({
hostNode: this._pageController.view.pageHeaderHolderNode,
parameters: ctx.parameters,
canMassTag: api.hasPrivilege('tags:masstag'),
massTagTags: this._massTagTags,
});
this._headerView.addEventListener(
'navigate', e => this._evtNavigate(e));
this._syncPageController();
}
2016-08-02 11:05:40 +02:00
showSuccess(message) {
this._pageController.showSuccess(message);
}
2016-07-05 21:20:28 +02:00
get _massTagTags() {
return (this._ctx.parameters.tag || '').split(/\s+/).filter(s => s);
2016-07-05 21:20:28 +02:00
}
_evtNavigate(e) {
history.pushState(
null,
window.title,
'/posts/' + misc.formatUrlParameters(e.detail.parameters));
Object.assign(this._ctx.parameters, e.detail.parameters);
this._syncPageController();
}
2016-07-05 21:20:28 +02:00
_evtTag(e) {
for (let tag of this._massTagTags) {
e.detail.post.addTag(tag);
}
2017-01-08 02:12:38 +01:00
e.detail.post.save().catch(error => window.alert(error.message));
2016-07-05 21:20:28 +02:00
}
_evtUntag(e) {
for (let tag of this._massTagTags) {
e.detail.post.removeTag(tag);
}
2017-01-08 02:12:38 +01:00
e.detail.post.save().catch(error => window.alert(error.message));
2016-07-05 21:20:28 +02:00
}
_decorateSearchQuery(text) {
const browsingSettings = settings.get();
let disabledSafety = [];
for (let key of Object.keys(browsingSettings.listPosts)) {
if (browsingSettings.listPosts[key] === false) {
disabledSafety.push(key);
}
}
if (disabledSafety.length) {
text = `-rating:${disabledSafety.join(',')} ${text}`;
}
return text.trim();
}
_syncPageController() {
this._pageController.run({
parameters: this._ctx.parameters,
getClientUrlForPage: page => {
return '/posts/' + misc.formatUrlParameters(
Object.assign({}, this._ctx.parameters, {page: page}));
},
requestPage: page => {
return PostList.search(
this._decorateSearchQuery(this._ctx.parameters.query),
page, settings.get().postsPerPage, fields);
},
pageRenderer: pageCtx => {
Object.assign(pageCtx, {
canViewPosts: api.hasPrivilege('posts:view'),
canMassTag: api.hasPrivilege('tags:masstag'),
massTagTags: this._massTagTags,
});
const view = new PostsPageView(pageCtx);
view.addEventListener('tag', e => this._evtTag(e));
view.addEventListener('untag', e => this._evtUntag(e));
return view;
},
});
}
}
module.exports = router => {
router.enter(
'/posts/:parameters(.*)?',
(ctx, next) => { misc.parseUrlParametersRoute(ctx, next); },
(ctx, next) => { ctx.controller = new PostListController(ctx); });
};