szurubooru/client/js/views/user_edit_view.js

126 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-04-06 22:34:21 +02:00
'use strict';
const config = require('../config.js');
const events = require('../events.js');
const views = require('../util/views.js');
const FileDropperControl = require('../controls/file_dropper_control.js');
2016-04-06 22:34:21 +02:00
const template = views.getTemplate('user-edit');
class UserEditView extends events.EventTarget {
constructor(ctx) {
super();
2016-04-06 22:34:21 +02:00
ctx.userNamePattern = config.userNameRegex + /|^$/.source;
ctx.passwordPattern = config.passwordRegex + /|^$/.source;
this._user = ctx.user;
this._hostNode = ctx.hostNode;
views.replaceContent(this._hostNode, template(ctx));
views.decorateValidator(this._formNode);
this._avatarContent = null;
2016-08-05 20:09:11 +02:00
if (this._avatarContentInputNode) {
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];
});
}
for (let node of this._formNode.querySelectorAll('input, select')) {
node.addEventListener(
'change', e => {
if (!e.target.classList.contains('anticomplete')) {
this.dispatchEvent(new CustomEvent('change'));
}
});
}
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: {
user: this._user,
2016-08-05 20:09:11 +02:00
name: this._userNameInputNode ?
this._userNameInputNode.value :
undefined,
2016-08-05 20:09:11 +02:00
email: this._emailInputNode ?
this._emailInputNode.value :
undefined,
2016-08-05 20:09:11 +02:00
rank: this._rankInputNode ?
this._rankInputNode.value :
undefined,
2016-08-05 20:09:11 +02:00
avatarStyle: this._avatarStyleInputNode ?
this._avatarStyleInputNode.value :
undefined,
2016-08-05 20:09:11 +02:00
password: this._passwordInputNode ?
this._passwordInputNode.value :
undefined,
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-08-05 20:09:11 +02:00
get _emailInputNode() {
return this._formNode.querySelector('[name=email]');
}
2016-08-05 20:09:11 +02:00
get _userNameInputNode() {
return this._formNode.querySelector('[name=name]');
}
2016-08-05 20:09:11 +02:00
get _passwordInputNode() {
return this._formNode.querySelector('[name=password]');
}
2016-08-05 20:09:11 +02:00
get _avatarContentInputNode() {
return this._formNode.querySelector('#avatar-content');
}
2016-08-05 20:09:11 +02:00
get _avatarStyleInputNode() {
return this._formNode.querySelector('[name=avatar-style]:checked');
2016-04-06 22:34:21 +02:00
}
}
module.exports = UserEditView;