summaryrefslogtreecommitdiff
path: root/client/js/views/login_view.js
blob: 64d49f97de9bea65d37965a3a8161855dfb2bd96 (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
"use strict";

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

const template = views.getTemplate("login");

class LoginView extends events.EventTarget {
    constructor() {
        super();
        this._hostNode = document.getElementById("content-holder");

        views.replaceContent(
            this._hostNode,
            template({
                userNamePattern: api.getUserNameRegex(),
                passwordPattern: api.getPasswordRegex(),
                canSendMails: api.canSendMails(),
            })
        );
        views.syncScrollPosition();

        views.decorateValidator(this._formNode);
        this._userNameInputNode.setAttribute(
            "pattern",
            api.getUserNameRegex()
        );
        this._passwordInputNode.setAttribute(
            "pattern",
            api.getPasswordRegex()
        );
        this._formNode.addEventListener("submit", (e) => {
            e.preventDefault();
            this.dispatchEvent(
                new CustomEvent("submit", {
                    detail: {
                        name: this._userNameInputNode.value,
                        password: this._passwordInputNode.value,
                        remember: this._rememberInputNode.checked,
                    },
                })
            );
        });
    }

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

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

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

    get _rememberInputNode() {
        return this._formNode.querySelector("[name=remember-user]");
    }

    disableForm() {
        views.disableForm(this._formNode);
    }

    enableForm() {
        views.enableForm(this._formNode);
    }

    clearMessages() {
        views.clearMessages(this._hostNode);
    }

    showError(message) {
        views.showError(this._hostNode, message);
    }
}

module.exports = LoginView;