2016-04-06 22:34:21 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const events = require('../events.js');
|
2018-06-25 16:47:20 +02:00
|
|
|
const api = require('../api.js');
|
2016-04-09 18:54:23 +02:00
|
|
|
const views = require('../util/views.js');
|
2016-05-20 21:35:12 +02:00
|
|
|
const FileDropperControl = require('../controls/file_dropper_control.js');
|
2016-04-06 22:34:21 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
const template = views.getTemplate('user-edit');
|
|
|
|
|
|
|
|
class UserEditView extends events.EventTarget {
|
|
|
|
constructor(ctx) {
|
|
|
|
super();
|
2016-04-06 22:34:21 +02:00
|
|
|
|
2020-06-04 20:09:35 +02:00
|
|
|
ctx.userNamePattern = api.getUserNameRegex() + (/|^$/).source;
|
|
|
|
ctx.passwordPattern = api.getPasswordRegex() + (/|^$/).source;
|
2016-04-10 10:23:27 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
this._user = ctx.user;
|
|
|
|
this._hostNode = ctx.hostNode;
|
|
|
|
views.replaceContent(this._hostNode, template(ctx));
|
|
|
|
views.decorateValidator(this._formNode);
|
2016-04-07 22:54:45 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
this._avatarContent = null;
|
2016-08-05 20:09:11 +02:00
|
|
|
if (this._avatarContentInputNode) {
|
2016-08-20 22:30:08 +02:00
|
|
|
this._avatarFileDropper = new FileDropperControl(
|
|
|
|
this._avatarContentInputNode, {lock: true});
|
|
|
|
this._avatarFileDropper.addEventListener('fileadd', e => {
|
|
|
|
this._hostNode.querySelector(
|
|
|
|
'[name=avatar-style][value=manual]').checked = true;
|
|
|
|
this._avatarContent = e.detail.files[0];
|
|
|
|
});
|
2016-05-20 21:35:12 +02:00
|
|
|
}
|
2016-04-07 22:54:45 +02:00
|
|
|
|
2016-08-22 21:38:21 +02:00
|
|
|
for (let node of this._formNode.querySelectorAll('input, select')) {
|
|
|
|
node.addEventListener(
|
|
|
|
'change', e => {
|
2016-08-22 21:43:59 +02:00
|
|
|
if (!e.target.classList.contains('anticomplete')) {
|
|
|
|
this.dispatchEvent(new CustomEvent('change'));
|
|
|
|
}
|
2016-08-22 21:38:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
this._formNode.addEventListener('submit', e => this._evtSubmit(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
clearMessages() {
|
|
|
|
views.clearMessages(this._hostNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
showSuccess(message) {
|
|
|
|
views.showSuccess(this._hostNode, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
showError(message) {
|
|
|
|
views.showError(this._hostNode, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
enableForm() {
|
|
|
|
views.enableForm(this._formNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
disableForm() {
|
|
|
|
views.disableForm(this._formNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtSubmit(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.dispatchEvent(new CustomEvent('submit', {
|
|
|
|
detail: {
|
2016-07-26 21:02:21 +02:00
|
|
|
user: this._user,
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
name: this._userNameInputNode ?
|
|
|
|
this._userNameInputNode.value :
|
2016-07-26 21:02:21 +02:00
|
|
|
undefined,
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
email: this._emailInputNode ?
|
|
|
|
this._emailInputNode.value :
|
2016-07-26 21:02:21 +02:00
|
|
|
undefined,
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
rank: this._rankInputNode ?
|
|
|
|
this._rankInputNode.value :
|
2016-07-26 21:02:21 +02:00
|
|
|
undefined,
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
avatarStyle: this._avatarStyleInputNode ?
|
|
|
|
this._avatarStyleInputNode.value :
|
2016-07-26 21:02:21 +02:00
|
|
|
undefined,
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
password: this._passwordInputNode ?
|
|
|
|
this._passwordInputNode.value :
|
2016-07-26 21:02:21 +02:00
|
|
|
undefined,
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
avatarContent: this._avatarContent,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
get _formNode() {
|
|
|
|
return this._hostNode.querySelector('form');
|
|
|
|
}
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
get _rankInputNode() {
|
|
|
|
return this._formNode.querySelector('[name=rank]');
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
get _emailInputNode() {
|
|
|
|
return this._formNode.querySelector('[name=email]');
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
get _userNameInputNode() {
|
|
|
|
return this._formNode.querySelector('[name=name]');
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
get _passwordInputNode() {
|
|
|
|
return this._formNode.querySelector('[name=password]');
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
get _avatarContentInputNode() {
|
2016-06-14 10:31:48 +02:00
|
|
|
return this._formNode.querySelector('#avatar-content');
|
|
|
|
}
|
|
|
|
|
2016-08-05 20:09:11 +02:00
|
|
|
get _avatarStyleInputNode() {
|
2016-06-14 10:31:48 +02:00
|
|
|
return this._formNode.querySelector('[name=avatar-style]:checked');
|
2016-04-06 22:34:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UserEditView;
|