2016-04-06 22:34:21 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-07 22:54:45 +02:00
|
|
|
const config = require('../config.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-04-09 18:54:23 +02:00
|
|
|
class UserEditView {
|
2016-04-06 22:34:21 +02:00
|
|
|
constructor() {
|
2016-05-20 21:35:12 +02:00
|
|
|
this._template = views.getTemplate('user-edit');
|
2016-04-06 22:34:21 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 10:35:38 +02:00
|
|
|
render(ctx) {
|
2016-04-10 10:23:27 +02:00
|
|
|
ctx.userNamePattern = config.userNameRegex + /|^$/.source;
|
|
|
|
ctx.passwordPattern = config.passwordRegex + /|^$/.source;
|
|
|
|
|
2016-04-08 10:35:38 +02:00
|
|
|
const target = ctx.target;
|
2016-05-20 21:35:12 +02:00
|
|
|
const source = this._template(ctx);
|
2016-04-07 22:54:45 +02:00
|
|
|
|
2016-04-08 10:35:38 +02:00
|
|
|
const form = source.querySelector('form');
|
2016-04-10 15:55:56 +02:00
|
|
|
const avatarContentField = source.querySelector('#avatar-content');
|
2016-04-07 22:54:45 +02:00
|
|
|
|
2016-04-09 18:54:23 +02:00
|
|
|
views.decorateValidator(form);
|
2016-04-07 22:54:45 +02:00
|
|
|
|
2016-04-10 15:55:56 +02:00
|
|
|
let avatarContent = null;
|
2016-05-20 21:35:12 +02:00
|
|
|
if (avatarContentField) {
|
|
|
|
new FileDropperControl(
|
|
|
|
avatarContentField,
|
|
|
|
{
|
|
|
|
lock: true,
|
|
|
|
resolve: files => {
|
|
|
|
source.querySelector(
|
|
|
|
'[name=avatar-style][value=manual]').checked = true;
|
|
|
|
avatarContent = files[0];
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2016-04-07 22:54:45 +02:00
|
|
|
|
|
|
|
form.addEventListener('submit', e => {
|
2016-04-10 15:55:56 +02:00
|
|
|
const rankField = source.querySelector('#user-rank');
|
|
|
|
const emailField = source.querySelector('#user-email');
|
|
|
|
const userNameField = source.querySelector('#user-name');
|
|
|
|
const passwordField = source.querySelector('#user-password');
|
|
|
|
const avatarStyleField = source.querySelector(
|
|
|
|
'[name=avatar-style]:checked');
|
|
|
|
|
2016-04-07 22:54:45 +02:00
|
|
|
e.preventDefault();
|
2016-04-09 18:54:23 +02:00
|
|
|
views.clearMessages(target);
|
|
|
|
views.disableForm(form);
|
2016-04-10 15:55:56 +02:00
|
|
|
ctx.edit({
|
|
|
|
name: userNameField.value,
|
|
|
|
password: passwordField.value,
|
|
|
|
email: emailField.value,
|
|
|
|
rank: rankField.value,
|
|
|
|
avatarStyle: avatarStyleField.value,
|
|
|
|
avatarContent: avatarContent})
|
2016-04-09 18:54:23 +02:00
|
|
|
.always(() => { views.enableForm(form); });
|
2016-04-07 22:54:45 +02:00
|
|
|
});
|
2016-04-08 10:35:38 +02:00
|
|
|
|
2016-05-11 21:29:57 +02:00
|
|
|
views.listenToMessages(source);
|
2016-04-09 18:54:23 +02:00
|
|
|
views.showView(target, source);
|
2016-04-06 22:34:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = UserEditView;
|