2016-04-06 22:34:21 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const events = require('../events.js');
|
2016-04-09 18:54:23 +02:00
|
|
|
const views = require('../util/views.js');
|
2016-05-11 12:17:40 +02:00
|
|
|
const UserDeleteView = require('./user_delete_view.js');
|
2018-02-25 11:44:02 +01:00
|
|
|
const UserTokensView = require('./user_tokens_view.js');
|
2016-04-06 22:34:21 +02:00
|
|
|
const UserSummaryView = require('./user_summary_view.js');
|
|
|
|
const UserEditView = require('./user_edit_view.js');
|
2016-08-23 22:06:30 +02:00
|
|
|
const EmptyView = require('../views/empty_view.js');
|
2016-04-06 22:34:21 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const template = views.getTemplate('user');
|
2016-04-06 22:34:21 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
class UserView extends events.EventTarget {
|
|
|
|
constructor(ctx) {
|
|
|
|
super();
|
2016-04-08 10:35:38 +02:00
|
|
|
|
2016-06-19 19:16:40 +02:00
|
|
|
this._ctx = ctx;
|
|
|
|
ctx.user.addEventListener('change', e => this._evtChange(e));
|
2016-04-08 10:35:38 +02:00
|
|
|
ctx.section = ctx.section || 'summary';
|
|
|
|
|
2016-06-19 19:16:40 +02:00
|
|
|
this._hostNode = document.getElementById('content-holder');
|
|
|
|
this._install();
|
|
|
|
}
|
|
|
|
|
|
|
|
_install() {
|
|
|
|
const ctx = this._ctx;
|
|
|
|
views.replaceContent(this._hostNode, template(ctx));
|
2017-12-18 19:41:53 +01:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
for (let item of this._hostNode.querySelectorAll('[data-name]')) {
|
2017-12-18 19:41:53 +01:00
|
|
|
item.classList.toggle(
|
|
|
|
'active', item.getAttribute('data-name') === ctx.section);
|
2016-04-08 10:35:38 +02:00
|
|
|
if (item.getAttribute('data-name') === ctx.section) {
|
2017-12-18 19:41:53 +01:00
|
|
|
item.parentNode.scrollLeft =
|
|
|
|
item.getBoundingClientRect().left -
|
|
|
|
item.parentNode.getBoundingClientRect().left
|
2016-04-08 10:35:38 +02:00
|
|
|
}
|
2016-04-06 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
ctx.hostNode = this._hostNode.querySelector('#user-content-holder');
|
2020-06-04 20:09:35 +02:00
|
|
|
if (ctx.section === 'edit') {
|
2016-08-23 22:06:30 +02:00
|
|
|
if (!this._ctx.canEditAnything) {
|
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError(
|
|
|
|
'You don\'t have privileges to edit users.');
|
|
|
|
} else {
|
|
|
|
this._view = new UserEditView(ctx);
|
|
|
|
events.proxyEvent(this._view, this, 'submit');
|
|
|
|
}
|
2020-06-04 20:09:35 +02:00
|
|
|
} else if (ctx.section === 'list-tokens') {
|
2018-02-25 11:44:02 +01:00
|
|
|
if (!this._ctx.canListTokens) {
|
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError(
|
|
|
|
'You don\'t have privileges to view user tokens.');
|
|
|
|
} else {
|
|
|
|
this._view = new UserTokensView(ctx);
|
|
|
|
events.proxyEvent(this._view, this, 'delete', 'delete-token');
|
|
|
|
events.proxyEvent(this._view, this, 'submit', 'create-token');
|
|
|
|
events.proxyEvent(this._view, this, 'update', 'update-token');
|
|
|
|
}
|
2020-06-04 20:09:35 +02:00
|
|
|
} else if (ctx.section === 'delete') {
|
2016-08-23 22:06:30 +02:00
|
|
|
if (!this._ctx.canDelete) {
|
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError(
|
|
|
|
'You don\'t have privileges to delete users.');
|
|
|
|
} else {
|
|
|
|
this._view = new UserDeleteView(ctx);
|
|
|
|
events.proxyEvent(this._view, this, 'submit', 'delete');
|
|
|
|
}
|
|
|
|
|
2016-04-06 22:34:21 +02:00
|
|
|
} else {
|
2016-06-14 10:31:48 +02:00
|
|
|
this._view = new UserSummaryView(ctx);
|
2016-04-06 22:34:21 +02:00
|
|
|
}
|
2016-07-13 17:18:28 +02:00
|
|
|
|
2016-08-22 21:38:21 +02:00
|
|
|
events.proxyEvent(this._view, this, 'change');
|
2016-07-13 17:18:28 +02:00
|
|
|
views.syncScrollPosition();
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
clearMessages() {
|
|
|
|
this._view.clearMessages();
|
|
|
|
}
|
|
|
|
|
|
|
|
showSuccess(message) {
|
|
|
|
this._view.showSuccess(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
showError(message) {
|
|
|
|
this._view.showError(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
enableForm() {
|
|
|
|
this._view.enableForm();
|
|
|
|
}
|
2016-04-06 22:34:21 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
disableForm() {
|
|
|
|
this._view.disableForm();
|
2016-04-06 22:34:21 +02:00
|
|
|
}
|
2016-06-19 19:16:40 +02:00
|
|
|
|
|
|
|
_evtChange(e) {
|
|
|
|
this._ctx.user = e.detail.user;
|
|
|
|
this._install(this._ctx);
|
|
|
|
}
|
2016-04-06 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UserView;
|