summaryrefslogtreecommitdiffstats
path: root/nfsw/static/nfsw.js
blob: 6ad58383bc6c4f6b3a43f9f1ebe838bbfa35412d (plain) (blame)
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
window.nfsw = {
    fErr: function(msg) {
        $('.msg-block .content p').empty().text(msg);
        $('.msg-block .content').addClass('error');
        $('.msg-block').show();
    },
    fInfo: function(msg) {
        $('.msg-block .content p').empty().text(msg);
        $('.msg-block .content').addClass('info');
        $('.msg-block').show();
    },
    fFormInit: function() {
        var root = this;

        $('form.auth').submit(function(ev) {
            ev.preventDefault();

            root.fFormPost($(this).serialize());
        });
    },
    fFormPost: function(post) {
        var root = this;

        $.post('/auth/start', post)
         .done(function(data, status, xhr) {
             if (!'status' in data) {
                 root.fErr('Unable to log you in!');
             }
             var status = data['status'];

             if (status == 'pass') {
                 return root.fFormAskPass(data['msg']);
             }
             if (status == 'error') {
                 return root.fFormError(data);
             }
             if (status == 'ok') {
                 return root.fFormPostOk(data);
             }

             root.fErr('Unknow error. Unable to log you in!');
         })
         .fail(function(xhr, status, err) {
             console.log([xhr,status, err])
         });
    },
    fFormAskPass: function(msg) {
        var root = this;

        root.fInfo(msg);

        $('input[type=password]').prop('disabled', false);
        //$('input[type=password]').prop('required', true);
        $('.auth-pass').show();
    },
    fFormError: function(data) {
        var root = this;

        root.fErr(data['msg']);
        if (!('fields' in data)) {
            return;
        }

        $.each(data['fields'], function(i, f) {
            $('form .' + f).addClass('error');
        });
    },
    fFormPostOk: function(data) {
        if (!('url' in data)) {
            root.fErr('Unable to redirect!')
        }

        window.location.href = data.url;
    },

    fInit: function(options) {
        var root = this;

        console.log('Initing nfsw...');

        root.fFormInit();
    },
}

$(document).ready(function() {
    window.nfsw.fInit({});
});