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