szurubooru/client/js/controllers/pool_controller.js

177 lines
5.8 KiB
JavaScript
Raw Normal View History

"use strict";
2020-05-04 04:53:28 +02:00
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");
const Post = require("../models/post.js");
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");
2020-05-04 04:53:28 +02:00
class PoolController {
constructor(ctx, section) {
if (!api.hasPrivilege("pools:view")) {
2020-05-04 04:53:28 +02:00
this._view = new EmptyView();
this._view.showError("You don't have privileges to view pools.");
2020-05-04 04:53:28 +02:00
return;
}
Promise.all([
2020-06-05 03:01:28 +02:00
PoolCategoryList.get(),
Pool.get(ctx.parameters.id),
]).then(
(responses) => {
const [poolCategoriesResponse, pool] = responses;
2020-05-04 04:53:28 +02:00
topNavigation.activate("pools");
topNavigation.setTitle("Pool #" + pool.names[0]);
2020-05-04 04:53:28 +02:00
this._name = ctx.parameters.name;
pool.addEventListener("change", (e) =>
this._evtSaved(e, section)
);
2020-05-04 04:53:28 +02:00
const categories = {};
for (let category of poolCategoriesResponse.results) {
categories[category.name] = category.name;
}
2020-05-04 04:53:28 +02:00
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"
),
canEditPosts: api.hasPrivilege("pools:edit:posts"),
canMerge: api.hasPrivilege("pools:merge"),
canDelete: api.hasPrivilege("pools:delete"),
categories: categories,
escapeColons: uri.escapeColons,
});
2020-05-04 04:53:28 +02:00
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)
);
},
(error) => {
this._view = new EmptyView();
this._view.showError(error.message);
}
);
2020-05-04 04:53:28 +02:00
}
_evtChange(e) {
misc.enableExitConfirmation();
}
_evtSaved(e, section) {
misc.disableExitConfirmation();
if (this._name !== e.detail.pool.names[0]) {
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
}
}
e.detail.pool.save().then(
() => {
this._view.showSuccess("Pool saved.");
this._view.enableForm();
},
(error) => {
this._view.showError(error.message);
this._view.enableForm();
}
);
2020-05-04 04:53:28 +02:00
}
_evtMerge(e) {
this._view.clearMessages();
this._view.disableForm();
e.detail.pool.merge(e.detail.targetPoolId, e.detail.addAlias).then(
() => {
this._view.showSuccess("Pool merged.");
2020-05-04 04:53:28 +02:00
this._view.enableForm();
router.replace(
uri.formatClientLink(
"pool",
e.detail.targetPoolId,
"merge"
),
2020-06-05 03:01:28 +02:00
null,
false
);
},
(error) => {
2020-05-04 04:53:28 +02:00
this._view.showError(error.message);
this._view.enableForm();
}
);
2020-05-04 04:53:28 +02:00
}
_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) => {
2020-05-04 04:53:28 +02:00
this._view.showError(error.message);
this._view.enableForm();
}
);
2020-05-04 04:53:28 +02:00
}
}
module.exports = (router) => {
router.enter(["pool", ":id", "edit"], (ctx, next) => {
ctx.controller = new PoolController(ctx, "edit");
2020-05-04 04:53:28 +02:00
});
router.enter(["pool", ":id", "merge"], (ctx, next) => {
ctx.controller = new PoolController(ctx, "merge");
2020-05-04 04:53:28 +02:00
});
router.enter(["pool", ":id", "delete"], (ctx, next) => {
ctx.controller = new PoolController(ctx, "delete");
2020-05-04 04:53:28 +02:00
});
router.enter(["pool", ":id"], (ctx, next) => {
ctx.controller = new PoolController(ctx, "summary");
2020-05-04 04:53:28 +02:00
});
};