Welcome to mirror list, hosted at ThFree Co, Russian Federation.

server_variables.js « js - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f2ba9620e83b56fa742068640cc8b5e0d7d7e83 (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
/* vim: set expandtab sw=4 ts=4 sts=4: */

/**
 * Unbind all event handlers before tearing down a page
 */
AJAX.registerTeardown('server_variables.js', function() {
    $('table.data tbody tr td:nth-child(2).editable').unbind('hover');
    $('#filterText').unbind('keyup');
});

AJAX.registerOnload('server_variables.js', function() {
    var textFilter = null, odd_row = false;
    var testString = 'abcdefghijklmnopqrstuvwxyz0123456789,ABCEFGHIJKLMOPQRSTUVWXYZ';
    var $tmpDiv, charWidth;

    // Global vars
    $editLink = $('a.editLink');
    $saveLink = $('a.saveLink');
    $cancelLink = $('a.cancelLink');

    /* Variable editing */
    $('table.data tbody tr td:nth-child(2).editable').hover(
        function() {
            // Only add edit element if it is the global value, not session value and not when the element is being edited
            if ($(this).parent().children('th').length > 0 && ! $(this).hasClass('edit')) {
                $(this).prepend($editLink.clone().show());
            }
        },
        function() {
            $(this).find('a.editLink').remove();
        }
    );

    // Filter options are invisible for disabled js users
    $('fieldset#tableFilter').css('display','');

    $('#filterText').keyup(function(e) {
        if ($(this).val().length == 0) {
            textFilter=null;
        } else {
            textFilter = new RegExp("(^| )"+$(this).val().replace(/_/g,' '),'i');
        }
        filterVariables();
    });

    if (location.hash.substr(1).split('=')[0] == 'filter') {
        var name = location.hash.substr(1).split('=')[1];
        // Only allow variable names
        if (! name.match(/[^0-9a-zA-Z_]+/)) {
            $('#filterText').val(name).trigger('keyup');
        }
    }

    /* Table width limiting */
    $('table.data').after($tmpDiv=$('<span>'+testString+'</span>'));
    charWidth = $tmpDiv.width() / testString.length;
    $tmpDiv.remove();

    $(window).resize(limitTableWidth); // FIXME: this doesn't work that well and binding anything to the window resize event is a bad idea
    limitTableWidth();

    /* This function chops of long variable values to keep the table from overflowing horizontally
     * It does so by taking a test string and calculating an average font width and removing 'excess width / average font width'
     * chars, so it is not very accurate.
     */
    function limitTableWidth() {
        var fulltext;
        var charDiff;
        var maxTableWidth;
        var $tmpTable;

        $('table.data').after($tmpTable = $('<table id="testTable" style="width:100%;"><tr><td>' + testString + '</td></tr></table>'));
        maxTableWidth = $('#testTable').width();
        $tmpTable.remove();
        charDiff =  ($('table.data').width() - maxTableWidth) / charWidth;

        if ($('body').innerWidth() < $('table.data').width() + 10 || $('body').innerWidth() > $('table.data').width() + 20) {
            var maxChars = 0;

            $('table.data tbody tr td:nth-child(2)').each(function() {
                maxChars = Math.max($(this).text().length, maxChars);
            });

            // Do not resize smaller if there's only 50 chars displayed already
            if (charDiff > 0 && maxChars < 50) { return; }

            $('table.data tbody tr td:nth-child(2)').each(function() {
                if ((charDiff > 0 && $(this).text().length > maxChars - charDiff) || (charDiff < 0 && $(this).find('abbr.cutoff').length > 0)) {
                    if ($(this).find('abbr.cutoff').length > 0) {
                        fulltext = $(this).find('abbr.cutoff').attr('title');
                    } else {
                        fulltext = $(this).text();
                        // Do not cut off elements with html in it and hope they are not too long
                        if (fulltext.length != $(this).html().length) { return 0; }
                    }

                    if (fulltext.length < maxChars - charDiff) {
                        $(this).html(fulltext);
                    } else {
                        $(this).html('<abbr class="cutoff" title="' + fulltext + '">' + fulltext.substr(0, maxChars - charDiff - 3) + '...</abbr>');
                    }
                }
            });
        }
    }

    /* Filters the rows by the user given regexp */
    function filterVariables() {
        var mark_next = false, firstCell;
        odd_row = false;

        $('table.filteredData tbody tr').each(function() {
            firstCell = $(this).children(':first');

            if (mark_next || textFilter == null || textFilter.exec(firstCell.text())) {
                // If current global value is different from session value (=has class diffSession), then display that one too
                mark_next = $(this).hasClass('diffSession') && ! mark_next;

                odd_row = ! odd_row;
                $(this).css('display','');
                if (odd_row) {
                    $(this).addClass('odd');
                    $(this).removeClass('even');
                } else {
                    $(this).addClass('even');
                    $(this).removeClass('odd');
                }
            } else {
                $(this).css('display','none');
            }
        });
    }
});

/* Called by inline js. Allows the user to edit a server variable */
function editVariable(link)
{
    var varName = $(link).parent().parent().find('th:first').first().text().replace(/ /g,'_');
    var $mySaveLink = $saveLink.clone().show();
    var $myCancelLink = $cancelLink.clone().show();
    var $cell = $(link).parent();
    var $msgbox = PMA_ajaxShowMessage();

    $cell.addClass('edit');
    // remove edit link
    $cell.find('a.editLink').remove();

    $mySaveLink.click(function() {
        var $msgbox = PMA_ajaxShowMessage(PMA_messages.strProcessingRequest);
        $.get($(this).attr('href'), {
                ajax_request: true,
                type: 'setval',
                varName: varName,
                varValue: $cell.find('input').val()
            }, function(data) {
                if (data.success) {
                    $cell.html(data.variable);
                    PMA_ajaxRemoveMessage($msgbox);
                } else {
                    PMA_ajaxShowMessage(data.error, false);
                    $cell.html($cell.find('span.oldContent').html());
                }
                $cell.removeClass('edit');
            }, 'json');

        return false;
    });

    $myCancelLink.click(function() {
        $cell.html($cell.find('span.oldContent').html());
        $cell.removeClass('edit');
        return false;
    });

    $.get($mySaveLink.attr('href'), {
            ajax_request: true,
            type: 'getval',
            varName: varName
        }, function(data) {
            if (data.success == true) {
                // hide original content
                $cell.html('<span class="oldContent" style="display:none;">' + $cell.html() + '</span>');
                // put edit field and save/cancel link
                $cell.prepend('<table class="serverVariableEditTable" border="0"><tr><td></td><td style="width:100%;">' +
                              '<input type="text" id="variableEditArea" value="' + data.message + '" /></td></tr></table>');
                $cell.find('table td:first').append($mySaveLink);
                $cell.find('table td:first').append(' ');
                $cell.find('table td:first').append($myCancelLink);

                // Keyboard shortcuts to the rescue
                $('input#variableEditArea').focus();
                $('input#variableEditArea').keydown(function(event) {
                    // Enter key
                    if (event.keyCode == 13) {
                        $mySaveLink.trigger('click');
                    }
                    // Escape key
                    if (event.keyCode == 27) {
                        $myCancelLink.trigger('click');
                    }
                });
                PMA_ajaxRemoveMessage($msgbox);
            } else {
                $cell.removeClass('edit');
                PMA_ajaxShowMessage(data.error);
            }
        });

    return false;
}