summaryrefslogtreecommitdiffstats
path: root/nfsw/static/io.js
blob: b3c3b843225b6173f84615bf5e379042e591adae (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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
document.addEventListener('DOMContentLoaded', () => {
    /**
     * Handling for sending commands to server.
     */
    window.qip = false;  // query in prograss flag.
    function query(q) {
        if (window.qip) {
            return window.setTimeout(query, 10, q)
        }
        window.qip = true;

        var qipoff = function() {
            window.qip = false;
        }
        var spit = function(response) {
            var gdg = 'Oops! getting gobbledygook from server';

            var r;
            try {
                r = JSON.parse(response)
            } catch (e) {
                return barfslow(gdg, 'error', qipoff);
            }

            if (!(typeof r === 'object')) {
                return barf(gdg);
            }

            if (!('ans' in r)) {
                return barfslow(
                    'No answer from server!',
                    'error',
                    qipoff
                )
            }

            return barfslow(r.ans, 'concierge', qipoff)
        }

        var xhr = new XMLHttpRequest()
        xhr.open('POST', '/io/query', true)

        xhr.onreadystatechange = function() {
            if (this.readyState != 4) {
                return;
            }

            if (this.status == 200) {
                return spit(this.responseText)
            } else {
                return barfslow(
                    'Unable send command to server!', 'error', qipoff
                )
            }
        }
        xhr.send(q)
    }

    /**
     * Handling for console.
     */
    function barf(txt, type, p) {
        if (txt.length < 1) {
            return ''
        }
        barfblank()

        p = document.createElement('p')
        p.className = type

        p.append(txt)

        ioconsole.appendChild(p)

        return ''
    }
    function barfblank() {
        p = document.createElement('p')
        p.className = 'blank'

        p.innerHTML = '<br />'

        ioconsole.appendChild(p)
    }
    function barfslow(txt, type, cb, p) {
        if (txt.length < 1) {
            if (cb)
                cb()

            return ''
        }

        nl = false
        if (txt.charAt(0) == '\n')
            nl = true

        if (!p)
            barfblank()
        if (!p || nl) {
            p = document.createElement('p')
            p.className = type

            ioconsole.appendChild(p)
        }
        p.append(txt.substring(0, 1))
        p.scrollIntoView();

        window.setTimeout(
            barfslow, 0,
            txt.substring(1, txt.length),
            type,
            cb,
            p
        )

        return ''
    }
    var ioconsole = document.getElementsByClassName('console')[0]


    /**
     * Handling for the prompt.
     */
    function submit(e) {
        e.preventDefault()

        var input = e.target.querySelector('input')

        var cmd = input.value
        if (cmd.length < 1)
            return

        /**
         * Reset prompt.
         */
        input.value = ''

        /**
         * Send command to server.
         */
        query(cmd)
    }
    var form = document.querySelector('form')
    form.onsubmit = submit

});