summaryrefslogtreecommitdiff
path: root/client/js/views/user_edit_view.js
blob: 4886726a2a9b2ed31e82bed7fad6b2a973302b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"use strict";

const events = require("../events.js");
const api = require("../api.js");
const views = require("../util/views.js");
const FileDropperControl = require("../controls/file_dropper_control.js");

const template = views.getTemplate("user-edit");

class UserEditView extends events.EventTarget {
    constructor(ctx) {
        super();

        ctx.userNamePattern = api.getUserNameRegex() + /|^$/.source;
        ctx.passwordPattern = api.getPasswordRegex() + /|^$/.source;

        this._user = ctx.user;
        this._hostNode = ctx.hostNode;
        views.replaceContent(this._hostNode, template(ctx));
        views.decorateValidator(this._formNode);

        this._avatarContent = null;
        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,

                    name: this._userNameInputNode
                        ? this._userNameInputNode.value
                        : undefined,

                    email: this._emailInputNode
                        ? this._emailInputNode.value
                        : undefined,

                    rank: this._rankInputNode
                        ? this._rankInputNode.value
                        : undefined,

                    avatarStyle: this._avatarStyleInputNode
                        ? this._avatarStyleInputNode.value
                        : undefined,

                    password: this._passwordInputNode
                        ? this._passwordInputNode.value
                        : undefined,

                    avatarContent: this._avatarContent,
                },
            })
        );
    }

    get _formNode() {
        return this._hostNode.querySelector("form");
    }

    get _rankInputNode() {
        return this._formNode.querySelector("[name=rank]");
    }

    get _emailInputNode() {
        return this._formNode.querySelector("[name=email]");
    }

    get _userNameInputNode() {
        return this._formNode.querySelector("[name=name]");
    }

    get _passwordInputNode() {
        return this._formNode.querySelector("[name=password]");
    }

    get _avatarContentInputNode() {
        return this._formNode.querySelector("#avatar-content");
    }

    get _avatarStyleInputNode() {
        return this._formNode.querySelector("[name=avatar-style]:checked");
    }
}

module.exports = UserEditView;