2020-05-04 04:53:28 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const router = require('../router.js');
|
|
|
|
const api = require('../api.js');
|
|
|
|
const misc = require('../util/misc.js');
|
|
|
|
const uri = require('../util/uri.js');
|
|
|
|
const Pool = require('../models/pool.js');
|
2020-05-04 09:09:33 +02:00
|
|
|
const Post = require('../models/post.js');
|
2020-05-04 04:53:28 +02:00
|
|
|
const PoolCategoryList = require('../models/pool_category_list.js');
|
|
|
|
const topNavigation = require('../models/top_navigation.js');
|
|
|
|
const PoolView = require('../views/pool_view.js');
|
|
|
|
const EmptyView = require('../views/empty_view.js');
|
|
|
|
|
|
|
|
class PoolController {
|
|
|
|
constructor(ctx, section) {
|
|
|
|
if (!api.hasPrivilege('pools:view')) {
|
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError('You don\'t have privileges to view pools.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise.all([
|
2020-06-05 03:01:28 +02:00
|
|
|
PoolCategoryList.get(),
|
|
|
|
Pool.get(ctx.parameters.id)
|
2020-05-04 04:53:28 +02:00
|
|
|
]).then(responses => {
|
|
|
|
const [poolCategoriesResponse, pool] = responses;
|
|
|
|
|
|
|
|
topNavigation.activate('pools');
|
|
|
|
topNavigation.setTitle('Pool #' + pool.names[0]);
|
|
|
|
|
|
|
|
this._name = ctx.parameters.name;
|
|
|
|
pool.addEventListener('change', e => this._evtSaved(e, section));
|
|
|
|
|
|
|
|
const categories = {};
|
|
|
|
for (let category of poolCategoriesResponse.results) {
|
|
|
|
categories[category.name] = category.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._view = new PoolView({
|
|
|
|
pool: pool,
|
|
|
|
section: section,
|
|
|
|
canEditAnything: api.hasPrivilege('pools:edit'),
|
|
|
|
canEditNames: api.hasPrivilege('pools:edit:names'),
|
|
|
|
canEditCategory: api.hasPrivilege('pools:edit:category'),
|
|
|
|
canEditDescription: api.hasPrivilege('pools:edit:description'),
|
2020-05-04 09:09:33 +02:00
|
|
|
canEditPosts: api.hasPrivilege('pools:edit:posts'),
|
2020-05-04 04:53:28 +02:00
|
|
|
canMerge: api.hasPrivilege('pools:merge'),
|
|
|
|
canDelete: api.hasPrivilege('pools:delete'),
|
|
|
|
categories: categories,
|
|
|
|
escapeColons: uri.escapeColons,
|
|
|
|
});
|
|
|
|
|
|
|
|
this._view.addEventListener('change', e => this._evtChange(e));
|
|
|
|
this._view.addEventListener('submit', e => this._evtUpdate(e));
|
|
|
|
this._view.addEventListener('merge', e => this._evtMerge(e));
|
|
|
|
this._view.addEventListener('delete', e => this._evtDelete(e));
|
2020-06-03 02:43:18 +02:00
|
|
|
}, error => {
|
2020-05-04 04:53:28 +02:00
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError(error.message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtChange(e) {
|
|
|
|
misc.enableExitConfirmation();
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtSaved(e, section) {
|
|
|
|
misc.disableExitConfirmation();
|
|
|
|
if (this._name !== e.detail.pool.names[0]) {
|
2020-06-03 02:43:18 +02:00
|
|
|
router.replace(uri.formatClientLink('pool', e.detail.pool.id, section), null, false);
|
2020-05-04 04:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtUpdate(e) {
|
|
|
|
this._view.clearMessages();
|
|
|
|
this._view.disableForm();
|
|
|
|
if (e.detail.names !== undefined) {
|
|
|
|
e.detail.pool.names = e.detail.names;
|
|
|
|
}
|
|
|
|
if (e.detail.category !== undefined) {
|
|
|
|
e.detail.pool.category = e.detail.category;
|
|
|
|
}
|
|
|
|
if (e.detail.description !== undefined) {
|
|
|
|
e.detail.pool.description = e.detail.description;
|
|
|
|
}
|
2020-05-04 09:09:33 +02:00
|
|
|
if (e.detail.posts !== undefined) {
|
2020-06-03 02:43:18 +02:00
|
|
|
e.detail.pool.posts.clear();
|
2020-06-05 03:01:28 +02:00
|
|
|
for (let postId of e.detail.posts) {
|
|
|
|
e.detail.pool.posts.add(Post.fromResponse({id: parseInt(postId)}));
|
2020-05-04 09:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 04:53:28 +02:00
|
|
|
e.detail.pool.save().then(() => {
|
|
|
|
this._view.showSuccess('Pool saved.');
|
|
|
|
this._view.enableForm();
|
|
|
|
}, error => {
|
|
|
|
this._view.showError(error.message);
|
|
|
|
this._view.enableForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtMerge(e) {
|
|
|
|
this._view.clearMessages();
|
|
|
|
this._view.disableForm();
|
|
|
|
e.detail.pool
|
2020-05-05 00:15:30 +02:00
|
|
|
.merge(e.detail.targetPoolId, e.detail.addAlias)
|
2020-05-04 04:53:28 +02:00
|
|
|
.then(() => {
|
|
|
|
this._view.showSuccess('Pool merged.');
|
|
|
|
this._view.enableForm();
|
|
|
|
router.replace(
|
|
|
|
uri.formatClientLink(
|
2020-05-05 00:15:30 +02:00
|
|
|
'pool', e.detail.targetPoolId, 'merge'),
|
2020-06-05 03:01:28 +02:00
|
|
|
null,
|
|
|
|
false);
|
2020-05-04 04:53:28 +02:00
|
|
|
}, error => {
|
|
|
|
this._view.showError(error.message);
|
|
|
|
this._view.enableForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtDelete(e) {
|
|
|
|
this._view.clearMessages();
|
|
|
|
this._view.disableForm();
|
|
|
|
e.detail.pool.delete()
|
|
|
|
.then(() => {
|
|
|
|
const ctx = router.show(uri.formatClientLink('pools'));
|
|
|
|
ctx.controller.showSuccess('Pool deleted.');
|
|
|
|
}, error => {
|
|
|
|
this._view.showError(error.message);
|
|
|
|
this._view.enableForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = router => {
|
|
|
|
router.enter(['pool', ':id', 'edit'], (ctx, next) => {
|
|
|
|
ctx.controller = new PoolController(ctx, 'edit');
|
|
|
|
});
|
|
|
|
router.enter(['pool', ':id', 'merge'], (ctx, next) => {
|
|
|
|
ctx.controller = new PoolController(ctx, 'merge');
|
|
|
|
});
|
|
|
|
router.enter(['pool', ':id', 'delete'], (ctx, next) => {
|
|
|
|
ctx.controller = new PoolController(ctx, 'delete');
|
|
|
|
});
|
|
|
|
router.enter(['pool', ':id'], (ctx, next) => {
|
2020-06-05 03:01:28 +02:00
|
|
|
ctx.controller = new PoolController(ctx, 'summary');
|
2020-05-04 04:53:28 +02:00
|
|
|
});
|
|
|
|
};
|