summaryrefslogtreecommitdiff
path: root/day9/task5/index.html
blob: 7d65bd90c79b8e51944c51ca24e5a6c6b22e4723 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<html lang="ru">

<head>
    <meta charset="utf-8">
    <title>"table_task1" table view</title>
    <style type="text/css">
        table {
            border-spacing: 0;
            padding: 0;
            border: 1px solid black;
        }

        thead > tr > th {
            padding: 10px;
            font-size: 20px;
            background-color: #6f6f6f;
        }

        td {
            padding: 5px;
            font-size: 17px;
        }

        #table_operations {
            margin: 30px;
            font-size: 20px;
        }

        .odd {
            background-color: #dedede;
        }

        .even {
            background-color: #c5c5c5;
        }

        .keyword {
            color: blue;
        }

    </style>
</head>

<body onload="loadDefaultOperation()">
    <table>
        %%heading%%

        %%body%%
    </table>

    <div id="table_operations">
        <select onchange="typeChanged()" id="operation">
            <option>UPDATE</option>
            <option>DELETE FROM</option>
            <option>INSERT INTO</option>
        </select> `table_task1`<br>

        <form method="post" action="/update" id="operation_form">
            <div id="query"></div>
            <button type="submit">Выполнить запрос</button>
        </form>
    </div>

    <script>
        let queryHTMLElements = {
            'UPDATE': `
                <input hidden name="operation" value="UPDATE">

                <span class="keyword">SET</span><br>

                <div id="update_set"></div>
                <button type="button" onclick="addUpdateSet()">Добавить условие</button><br>

                <span class="keyword">WHERE</span><br>

                <div id="conditions"></div>
                <button type="button" onclick="addCondition()">Добавить условие</button>
            `,

            'DELETE FROM': `
                <input hidden name="operation" value="DELETE FROM">

                <span class="keyword">WHERE</span><br>

                <div id="conditions"></div>
                <button type="button" onclick="addCondition()">Добавить условие</button>
            `,

            'INSERT INTO': `
                <input hidden name="operation" value="INSERT INTO">

                <span class="keyword">VALUES</span><br>
            `
        };

        const DEFAULT_OPERATION = 'UPDATE';

        let expressionsCount = 0;
        let conditionsCount = 0;

        function loadDefaultOperation() {
            let query = document.getElementById('query');
            query.innerHTML = queryHTMLElements[DEFAULT_OPERATION];
        }

        function addUpdateSet() {
            let element = document.createElement('div');

            const keyName = `expression${expressionsCount}_key`;
            const valueName = `expression${expressionsCount}_value`;
            element.innerHTML = `<input type="text" name="${keyName}"> = <input type="text" name="${valueName}">`;

            document.getElementById('update_set').appendChild(element);

            expressionsCount++;
        }

        function addCondition() {
            let element = document.createElement('div');

            const keyName = `condition${conditionsCount}_key`;
            const valueName = `condition${conditionsCount}_value`;
            element.innerHTML = `<input type="text" name="${keyName}"> = <input type="text" name="${valueName}">`;

            document.getElementById('conditions').appendChild(element);

            conditionsCount++;
        }

        function typeChanged() {
            let query = document.getElementById('query');
            let text = document.getElementById('operation').value;
            query.innerHTML = queryHTMLElements[text];

            let operationForm = document.getElementById('operation_form');
            switch (text) {
                case 'UPDATE':
                    operationForm.method = 'POST';
                    operationForm.action = '/update';
                    break;
                case 'DELETE FROM':
                    operationForm.method = 'POST';
                    operationForm.action = '/delete';
                    break;
                case 'INSERT INTO':
                    operationForm.method = 'POST';
                    operationForm.action = 'add';
                    break;
            }

            expressionsCount = 0;
            conditionsCount = 0;
        }
    </script>
</body>

</html>