Translated using Weblate.
[phpmyadmin.git] / js / server_variables.js
blobef453f02d4df1ff90ac68ac7b9b554f9154857ad
1 $(function() {
2     var textFilter = null, odd_row = false;
3     var testString = 'abcdefghijklmnopqrstuvwxyz0123456789,ABCEFGHIJKLMOPQRSTUVWXYZ';
4     var $tmpDiv, charWidth;
6     // Global vars
7     editLink = '<a href="#" class="editLink" onclick="return editVariable(this);">' + PMA_getImage('b_edit.png') + ' ' + PMA_messages['strEdit'] + '</a>';
8     saveLink = '<a href="#" class="saveLink">' + PMA_getImage('b_save.png') + ' ' + PMA_messages['strSave'] + '</a> ';
9     cancelLink = '<a href="#" class="cancelLink">' + PMA_getImage('b_close.png') + ' ' + PMA_messages['strCancel'] + '</a> ';
11     /* Variable editing */
12     if (is_superuser) {
13         $('table.data tbody tr td:nth-child(2)').hover(
14             function() {
15                 // Only add edit element if it is the global value, not session value and not when the element is being edited
16                 if ($(this).parent().children('th').length > 0 && ! $(this).hasClass('edit')) {
17                     $(this).prepend(editLink);
18                 }
19             },
20             function() {
21                 $(this).find('a.editLink').remove();
22             }
23         );
24     }
26     // Filter options are invisible for disabled js users
27     $('fieldset#tableFilter').css('display','');
28      
29     $('#filterText').keyup(function(e) {
30         if ($(this).val().length == 0) {
31             textFilter=null;
32         } else {
33             textFilter = new RegExp("(^| )"+$(this).val().replace(/_/g,' '),'i');
34         }
35         filterVariables();
36     });
38     if (location.hash.substr(1).split('=')[0] == 'filter') {
39         var name = location.hash.substr(1).split('=')[1];
40         // Only allow variable names
41         if (! name.match(/[^0-9a-zA-Z_]+/)) {
42             $('#filterText').attr('value',name).trigger('keyup');
43         }
44     }
45     
46     /* Table width limiting */
47     $('table.data').after($tmpDiv=$('<span>'+testString+'</span>'));
48     charWidth = $tmpDiv.width() / testString.length;
49     $tmpDiv.remove();
51     $(window).resize(limitTableWidth);
52     limitTableWidth();
53     
54     /* This function chops of long variable values to keep the table from overflowing horizontally 
55      * It does so by taking a test string and calculating an average font width and removing 'excess width / average font width' 
56      * chars, so it is not very accurate.
57      */
58     function limitTableWidth() {
59         var fulltext;
60         var charDiff;
61         var maxTableWidth;
62         var $tmpTable;
64         $('table.data').after($tmpTable = $('<table id="testTable" style="width:100%;"><tr><td>' + testString + '</td></tr></table>'));
65         maxTableWidth = $('#testTable').width();
66         $tmpTable.remove();
67         charDiff =  ($('table.data').width() - maxTableWidth) / charWidth;
69         if ($('body').innerWidth() < $('table.data').width() + 10 || $('body').innerWidth() > $('table.data').width() + 20) {
70             var maxChars = 0;
72             $('table.data tbody tr td:nth-child(2)').each(function() {
73                 maxChars = Math.max($(this).text().length, maxChars);
74             });
76             // Do not resize smaller if there's only 50 chars displayed already
77             if (charDiff > 0 && maxChars < 50) { return; }
79             $('table.data tbody tr td:nth-child(2)').each(function() {
80                 if ((charDiff > 0 && $(this).text().length > maxChars - charDiff) || (charDiff < 0 && $(this).find('abbr.cutoff').length > 0)) {
81                     if ($(this).find('abbr.cutoff').length > 0) {
82                         fulltext = $(this).find('abbr.cutoff').attr('title');
83                     } else {
84                         fulltext = $(this).text();
85                         // Do not cut off elements with html in it and hope they are not too long
86                         if (fulltext.length != $(this).html().length) { return 0; }
87                     }
89                     if (fulltext.length < maxChars - charDiff) {
90                         $(this).html(fulltext);
91                     } else {
92                         $(this).html('<abbr class="cutoff" title="' + fulltext + '">' + fulltext.substr(0, maxChars - charDiff - 3) + '...</abbr>');
93                     }
94                 }
95             });
96         }
97     }
98     
99     /* Filters the rows by the user given regexp */
100     function filterVariables() {
101         var mark_next = false, firstCell;
102         odd_row = false;
103         
104         $('table.filteredData tbody tr').each(function() {
105             firstCell = $(this).children(':first');
107             if (mark_next || textFilter == null || textFilter.exec(firstCell.text())) {
108                 // If current global value is different from session value (=has class diffSession), then display that one too
109                 mark_next = $(this).hasClass('diffSession') && ! mark_next;
111                 odd_row = ! odd_row;
112                 $(this).css('display','');
113                 if (odd_row) {
114                     $(this).addClass('odd');
115                     $(this).removeClass('even');
116                 } else {
117                     $(this).addClass('even');
118                     $(this).removeClass('odd');
119                 }
120             } else {
121                 $(this).css('display','none');
122             }
123         });
124     }
127 /* Called by inline js. Allows the user to edit a server variable */
128 function editVariable(link)
130     var varName = $(link).parent().parent().find('th:first').first().text().replace(/ /g,'_');
131     var mySaveLink = $(saveLink);
132     var myCancelLink = $(cancelLink);
133     var $cell = $(link).parent();
135     $cell.addClass('edit');
136     // remove edit link
137     $cell.find('a.editLink').remove();
139     mySaveLink.click(function() {
140         $.get('server_variables.php?' + url_query, {
141                 ajax_request: true,
142                 type: 'setval',
143                 varName: varName,
144                 varValue: $cell.find('input').attr('value')
145             }, function(data) {
146                 if (data.success) {
147                     $cell.html(data.variable);
148                 } else {
149                     PMA_ajaxShowMessage(data.error, false);
150                     $cell.html($cell.find('span.oldContent').html());
151                 }
152                 $cell.removeClass('edit');
153             }, 'json');
155         return false;
156     });
158     myCancelLink.click(function() {
159         $cell.html($cell.find('span.oldContent').html());
160         $cell.removeClass('edit');
161         return false;
162     });
164     $.get('server_variables.php?' + url_query, {
165             ajax_request: true,
166             type: 'getval',
167             varName: varName
168         }, function(data) {
169             // hide original content
170             $cell.html('<span class="oldContent" style="display:none;">' + $cell.html() + '</span>');
171             // put edit field and save/cancel link
172             $cell.prepend('<table class="serverVariableEditTable" border="0"><tr><td></td><td style="width:100%;">' +
173                           '<input type="text" id="variableEditArea" value="' + data + '" /></td></tr</table>');
174             $cell.find('table td:first').append(mySaveLink);
175             $cell.find('table td:first').append(' ');
176             $cell.find('table td:first').append(myCancelLink);
178             // Keyboard shortcuts to the rescue
179             $('input#variableEditArea').focus();
180             $('input#variableEditArea').keydown(function(event) {
181                 // Enter key
182                 if(event.keyCode == 13) mySaveLink.trigger('click');
183                 // Escape key
184                 if(event.keyCode == 27) myCancelLink.trigger('click');
185             });
186         });
188     return false;