summaryrefslogtreecommitdiff
path: root/js/pgconfig.js
blob: d71f2a9eade9dfcc2529b9a0225d4047343b3709 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
$(window).load(function()
{
    $('input[type="radio"], select').each(function()
    {
        toggleTbody($(this));
    });
    $('input[type="radio"], select').change(function()
    {
        toggleTbody($(this));
    });

    /* ========================================================================= */
    /* If no health check config of this node,                                   */
    /* don't show the per node health check form.                                */
    /* ========================================================================= */

    $('[id^=tr_hc_node_num_]').each(function()
    {
        var tt = null;
        tt = $(this).nextAll('tr').find('input[value!=""]').val();
        if ( tt === undefined) {
            $(this).nextAll('tr').css('display', 'none');
        }
    });

    /* ========================================================================= */
    /* In per node health check form,                                            */
    /* input "name" is like "health_check_period0", "health_check_period1"       */
    /* ========================================================================= */
    $('[id^=tb_per_node_healthcheck_]').find('input[name^=health_check], input[name=connect_timeout]').each(function()
    {
        var node_num = $(this).parents('tbody').attr('id').split('_')[4];
        var name = $(this).attr('name');
        $(this).attr('name', name + node_num)
    });

    /* ========================================================================= */
    /* Click "add" button                                                        */
    /* ========================================================================= */

    /* Click "add" button to add backends form and add per node health_check row */
    $('#add_backends_node').click(function()
    {
        var next_node_num = $('[id^=tb_backends_node_]').length;
        var pgpool_ver = $('#pgpool_ver').val();

        addBackendForm(next_node_num);

        /* Only show per node health check form when pgpool version >= 3.7.*/
        if (pgpool_ver >= 3.7) {
            addPerNodeHealthCheck(next_node_num);
        }
    });

    /* Click "add" button to add per node health_check. */
    $('[id^=add_per_node_health_check_]').click(function()
    {
        addPerNodeHealthCheckForm($(this));
    });

    /* Click "add" button to add watchdog form */
    $('#add_watchdog_node').click(function()
    {
        var next_node_num = $('[id^=tr_wd_node_num_]').length;
        addWatchdogNodeForm(next_node_num);
    });

    $('#add_watchdog_heartbeat_node').click(function()
    {
        var next_node_num = $('[id^=tr_wd_hb_num_]').length;
        addWdHbForm(next_node_num);
    });

    /* ========================================================================= */
    /* Click "delete" button                                                     */
    /* ========================================================================= */

    /* Delete backends form */
    $('[id^=delete_backends_node_]').on({'click': deleteBackendForm});

    /* Delete per node health_check form. */
    $('[id^=delete_per_node_health_check_]').on({'click': deletePerNodeHealthCheckForm});

    /* Delete watchdog form */
    $('[id^=delete_watchdog_node_]').on({'click': deleteWatchdogForm});

    /* Delete watchdog heartbeat destination form */
    $('[id^=delete_watchdog_heartbeat_node_]').on({'click': deleteWdHbForm});

});

function sendForm(action, num)
{
    $('#pgconfig_action').val(action);
    if (num != null) {
        $('#pgconfig_num').val(num);
    }
    $('#form_pgconfig').submit();
}

function toggleTbody(item)
{
    var param_name = $(item).attr('name');
    var tagname = item.prop('tagName');
    var val = null;

    if (tagname == 'INPUT' && $(item).attr('type') == 'radio') {
        val = $(item).filter(':checked').val();
        if (val === undefined) {
            return;
        }

    } else if (tagname == 'SELECT') {
        val = $(item).children('option').filter(':selected').val();

    }

    var tbody_id_to_enable = param_name + '_' + val;

    $('tbody[id^="tb_"][id*="' + param_name + '_"]').each(function()
    {
        var tbody_id = $(this).attr('id');
        var enabled = ($('[name="' + param_name + '"]').prop('disabled') == false &&
                       tbody_id.indexOf(tbody_id_to_enable) != -1);

        $('#' + tbody_id + ' input').prop('disabled', ! enabled);
        $('#' + tbody_id + ' select').prop('disabled', ! enabled);

        $('#' + tbody_id + ' select').each(function()
        {
            toggleTbody($(this));
        });
    });
}

/*
 * Add backends form by using "add" button
 */
function addBackendForm(next_node_num)
{

    var html = '<tbody id="tb_backends_node_' + next_node_num + '">' +
               '<tr id="tr_ba_node_num_' + next_node_num +
               '" name="tr_ba_node_num"><th colspan="2">' +
               '<span class="param_group">Backend node ' + next_node_num + '</span>' +
               '</th></tr>';

    $('tr[name="tr_ba_node_num"]').last().nextAll('tr').each(function()
    {
        html += '<tr>' + $(this).html() + '</tr>';
    });
    html += '</tbody>';

    $('#t_backends').append(html);
    $('#tr_ba_node_num_' + next_node_num).nextAll('tr').find('input').val('');

}

/*
 * clicking backends "add" button to add per node health_check row.
 */
function addPerNodeHealthCheck(next_node_num)
{
    var html = '<tbody id="tb_per_node_healthcheck_' + next_node_num + '">' +
               '<tr id="tr_hc_node_num_' + next_node_num +
               '" name="tr_hc_node_num"><th colspan="2">' +
               '<span class="param_group">Backend node ' + next_node_num + '</span>' +
               '<input id="add_per_node_health_check_' + next_node_num +
               '" type="button" name="add" value="Add" onclick="addPerNodeHealthCheckForm($(this))" />' +
               '</th></tr>';

    $('#tb_global_healthcheck').find('tr').each(function()
    {
        html += '<tr style="display: none">' + $(this).html() + '</tr>';
    });

    html += '</tbody>';

    $('#t_healthcheck').append(html);
    $('#tr_hc_node_num_' + next_node_num).nextAll('tr').find('input').val('');

    $('#tb_per_node_healthcheck_' + next_node_num).find('input[name^=health_check], input[name=connect_timeout]').each(function()
    {
        var name = $(this).attr('name');
        $(this).attr('name', name + next_node_num)
    });
}

/*
 * Add per node health check form by using "add" button of health check table.
 */
function addPerNodeHealthCheckForm(obj)
{
    var node_num = obj.parents('tbody').attr('id').split('_')[4];

    obj.parents('tbody').find('tr').show();
    $('#tr_hc_node_num_' + node_num).nextAll('tr').find('input').val('');
}

/*
 * Add watchdog form by using "add" button
 */
function addWatchdogNodeForm(next_node_num)
{
    var html = '<tbody id="tb_watchdog_use_watchdog_on_node_' + next_node_num + '">' +
               '<tr id="tr_wd_node_num_' + next_node_num +
               '" name="tr_wd_node_num"><th colspan="2">' +
               '<span class="param_group">Watchdog node ' + next_node_num + '</span>' +
               '</th></tr>';
    $('tr[name="tr_wd_node_num"]').last().nextAll('tr').each(function()
    {
        html += '<tr>' + $(this).html() + '</tr>';
    });
    html += '</tbody>';

    $('#tb_watchdog_use_watchdog_on_node_' + (next_node_num - 1) ).after(html);
    $('#tr_wd_node_num_' + next_node_num).nextAll('tr').find('input').val('');
}

/*
 * Add watchdog form by using "add" button
 */
function addWdHbForm(next_node_num)
{
    var html = '<tbody id="tb_watchdog_use_watchdog_on_wd_heartbeat_' + next_node_num + '">' +
               '<tr id="tr_wd_hb_num_' + next_node_num +
               '" name="tr_wd_hb_num"><th colspan="2">' +
               '<span class="param_group">Heartbeat destination ' + next_node_num + '</span>' +
               '</th></tr>';

    $('tr[name="tr_wd_hb_num"]').last().nextAll('tr').each(function()
    {
        html += '<tr>' + $(this).html() + '</tr>';
    });

    html += '</tbody>';

    $('#tb_watchdog_use_watchdog_on_wd_heartbeat_' + (next_node_num - 1) ).after(html);
    $('#tr_wd_hb_num_' + next_node_num).nextAll('tr').find('input').val('');
}

/*
 * Delete backend node form
 */
var deleteBackendForm = function()
{
    var deleted_node_num = $(this).attr('id').split('_')[3];
    $('#tb_backends_node_' + deleted_node_num).remove();
}

/*
 * Delete per node health check form
 */
var deletePerNodeHealthCheckForm = function()
{
    var node_num = $(this).parents('tbody').attr('id').split('_')[4];

    $('#tr_hc_node_num_' + node_num).nextAll('tr').find('input').val('');
    $(this).parents('tr').nextAll('tr').css('display', 'none');
}

/*
 * Delete watchdog form
 */
var deleteWatchdogForm = function()
{
    var node_num = $(this).attr('id').split('_')[3];
    $('#tb_watchdog_use_watchdog_on_node_' + node_num).remove();
}

/*
 * Delete watchdog heartbeat form
 */
var deleteWdHbForm = function()
{
    var node_num = $(this).attr('id').split('_')[4];
    $('#tb_watchdog_use_watchdog_on_wd_heartbeat_' + node_num).remove();
}