szurubooru/client/js/controllers/pool_list_controller.js

120 lines
3.4 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 uri = require("../util/uri.js");
const PoolList = require("../models/pool_list.js");
const topNavigation = require("../models/top_navigation.js");
const PageController = require("../controllers/page_controller.js");
const PoolsHeaderView = require("../views/pools_header_view.js");
const PoolsPageView = require("../views/pools_page_view.js");
const EmptyView = require("../views/empty_view.js");
2020-05-04 04:53:28 +02:00
const fields = [
"id",
"names",
"posts",
"creationTime",
"postCount",
"category",
2020-06-05 03:01:28 +02:00
];
2020-05-04 04:53:28 +02:00
class PoolListController {
constructor(ctx) {
2020-05-04 09:09:33 +02:00
this._pageController = new PageController();
if (!api.hasPrivilege("pools:list")) {
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;
}
2020-05-04 09:09:33 +02:00
this._ctx = ctx;
topNavigation.activate("pools");
topNavigation.setTitle("Listing pools");
2020-05-04 04:53:28 +02:00
this._headerView = new PoolsHeaderView({
hostNode: this._pageController.view.pageHeaderHolderNode,
parameters: ctx.parameters,
canCreate: api.hasPrivilege("pools:create"),
canEditPoolCategories: api.hasPrivilege("poolCategories:edit"),
2020-05-04 04:53:28 +02:00
});
this._headerView.addEventListener(
"submit",
(e) => this._evtSubmit(e),
"navigate",
(e) => this._evtNavigate(e)
);
2020-05-04 04:53:28 +02:00
this._syncPageController();
}
showSuccess(message) {
this._pageController.showSuccess(message);
}
showError(message) {
this._pageController.showError(message);
}
_evtSubmit(e) {
this._view.clearMessages();
this._view.disableForm();
e.detail.pool.save().then(
() => {
this._installView(e.detail.pool, "edit");
this._view.showSuccess("Pool created.");
2020-05-04 04:53:28 +02:00
router.replace(
uri.formatClientLink("pool", e.detail.pool.id, "edit"),
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
}
_evtNavigate(e) {
router.showNoDispatch(
uri.formatClientLink("pools", e.detail.parameters)
);
2020-05-04 04:53:28 +02:00
Object.assign(this._ctx.parameters, e.detail.parameters);
this._syncPageController();
}
_syncPageController() {
this._pageController.run({
parameters: this._ctx.parameters,
defaultLimit: 50,
getClientUrlForPage: (offset, limit) => {
const parameters = Object.assign({}, this._ctx.parameters, {
offset: offset,
limit: limit,
});
return uri.formatClientLink("pools", parameters);
2020-05-04 04:53:28 +02:00
},
requestPage: (offset, limit) => {
return PoolList.search(
this._ctx.parameters.query,
offset,
limit,
fields
);
2020-05-04 04:53:28 +02:00
},
pageRenderer: (pageCtx) => {
2020-05-04 04:53:28 +02:00
return new PoolsPageView(pageCtx);
},
});
}
}
module.exports = (router) => {
router.enter(["pools"], (ctx, next) => {
ctx.controller = new PoolListController(ctx);
});
2020-05-04 04:53:28 +02:00
};